Gasoline prices have already skyrocketed past $4 a gallon in San Francisco.(AP Photo/Jeff Chiu) By Jonathan Fahey AP Energy WriterWhen Jay Ricker, owner of the BP gas station off Interstate 70 in Plainfield, Ind., set the price of unleaded gasoline at $3.44 per gallon on Monday of last week, it was 4 cents higher than the Friday before.That alone might have been irritating to drivers paying the highest gas prices in more than two years. It was even more so because it happened on a day when the price of crude oil, which is used to make gasoline, fell almost $1 a barrel."It's up 20 cents one day, down 10 cents the next day," says Oscar Elmore, a courier who was filling up his Ford Taurus at a RaceTrac service station in Dallas recently. "It sounds kinda fishy to me."[Related: GasBuddy, discounts help you save on gas]Gas prices rise when oil prices rise, and fall when oil prices fall - except when they don't. What you pay at your gas station depends on an array of factors, from what happens on an exchange in New York to what the competition is charging.This can rankle drivers, especially these days. Gas reached a national average of $3.51 a gallon on Monday. That's up 14 cents, or 4 percent, over the past week. The week before, the average rose 20 cents, the steepest increase since September 2008.A year ago, the price was $2.75. The average is the highest it's ever been this time of year, and analysts expect it to climb higher in the coming weeks.Unlike an iPhone or a pair of jeans or a Big Mac, oil and gas are commodities, and their prices can change every second at the New York Mercantile Exchange and other trading hubs. Those far-off changes affect the cost of the next day's commute.Sellers of commodities, like gas station owners and refineries, price their product based not on what it costs to produce it, but on what it costs to replace it. Stations like the Plainfield BP, which gets shipments of gas several times a week, must constantly adjust their prices to keep up with the changing costs of their shipments.Oil is the biggest factor in gas prices. It accounts for 50 to 70 percent of the cost. Recent upheaval in the Middle East and strong demand for oil around the world have pushed oil prices over $100 a barrel for only the second time in history. But the price of a gallon of gas at the pump rises â€" and, yes, falls â€" for a number of other reasons.Oil prices can be moved by geopolitics, the value of the dollar, extreme weather or Chinese demand. Gas prices can be moved by oil prices, refinery problems or even weather that might keep drivers at home.In the next few weeks, gas prices are expected to rise as refiners switch to a more expensive blend of gasoline designed to help protect against evaporation during the warmer summer months."We have to pay whatever the market says we do. It's an instantaneous world," says Joe Petrowski, CEO of Gulf Oil, a big gasoline wholesaler.Whether the gas at the Plainfield BP was made from a barrel of oil pumped a month ago 1,000 miles away in Williston, N.D., or three months ago and 7,000 miles away in Kuwait, its price is set by buyers and sellers in New York hours before Ricker buys it.There's no way to know exactly where the oil used to make the gasoline sold at the Plainfield BP came from, or even where the gas was refined. Oils from many sources are mixed together on their way to a refinery, and gasolines from many refineries are mixed together on their way to a fuel terminal, where gas is stored before trucks take it to gas stations.But here's a plausible route: Oil is pumped by a company with wells in Texas or Louisiana and piped to a major oil hub in Cushing, Okla. From there, it is sold to an energy trader who may store it or trade it a few times.Then BP buys it to feed its Whiting, Ind., refinery. After a two-week pipeline trip to Whiting, the oil is cooked into gasoline and piped to BP's fuel terminal in Indianapolis.There, BP blends it with ethanol and a few special BP-branded additives and sets a final wholesale price, known as the rack price. It's this rack price that leads to the final pump price for most station owners.A wholesaler like BP or Gulf each has its own formula for setting the rack price. In an attempt to smooth out the spikes and dips of the market, a wholesaler usually buys some of his fuel through long-term contracts. The rest is bought on the so-called spot market, priced at a given moment by a benchmark like the New York Harbor gasoline price.Every day at 5 p.m., BP tells Ricker what the rack price will be starting at 6 p.m. That price is good for 24 hours.Ricker hires a trucker to go to the terminal a short drive away in Indianapolis, fill 'er up with 10,000 gallons and bring it to his station. Then Ricker decides what price to charge customers based on his ultimate concerns: the Speedway and Circle K stations that share an intersection with him.There are only two or three pennies per gallon in profit selling gas for most station owners. What Ricker really wants is to attract customers to sell the truly precious liquids: Not the gasoline and diesel outside, but the water and soft drinks inside.Three times a day, his station manager, Debbie Sennett, records his competitors' prices. When the competition lowered prices on Tuesday, so did Ricker, to $3.24 per gallon."Gasoline is the only product in this country that if you're a penny different people will go out of their way to go somewhere else," Ricker says.Wholesale gasoline prices have risen 38 cents per gallon, or 15 percent, since the first uprising in Libya on Feb. 15. When wholesale gas prices rise fast, filling station owners get squeezed or even lose money because competition prevents them from raising retail prices as fast as costs are rising.So if it seems that station owners take their time lowering prices when oil and wholesale gas get cheaper, it's because that's exactly what they do."If gasoline prices drop a dime, a station will only pass along one or two pennies a day," says Patrick DeHaan, an analyst at GasBuddy.com, a website that collects and publishes retail gas prices. "They are slower to pass along the discount because they need to make up for money they lost when prices went up."Through the first eight weeks of 2011, average gross profit for gas stations was 4.9 percent, according to the Oil Price Information Service. In 2010, it was 6 percent.That doesn't draw much sympathy from those who have to pay more at the pump, though. "To me it seems like a money game," says Steve Armonett of Indianapolis, who pulled into Ricker's BP to fill up his Buick LeSabre recently. "They're just worried about how much money they can make."___AP Business Writers Sandy Shore in Denver, Tom Murphy in Plainfield, Ind., and David Koenig in Dallas contributed to this story.