
instructions = "<p id=\"instructions\">Hover over an option to see more detailed information <br><br> Click on an option to add it your custom Halcyon<\/p>";
var currentColor = "green";
var basePrice = 2100;
var currentCost = 2100;
var optionCost = 0;
var optionInfo = {
	"computer": {"cost": 197, "name": "Travel Computer", "thumb": "gps.jpg", "moreinfo": "<p>This handy computer snaps directly into your steering wheel. It keeps track of all of your bike&#39;s statistics including distance travelled, battery life, average speed, and time. It also has a built in GPS to keep you going in the right direction.<\/p>"},
	
	"speakers": {"cost": 45, "name": "Stereo Speakers", "thumb": "speakers.jpg", "moreinfo": "<p>These high-quality passive speakers include an auxiliary input so that you can hook up your favorite mp3 player and bring your music with you wherever you go.<\/p>"},
	
	"roof": {"cost": 149, "name": "Removable Canopy", "thumb": "roof.jpg", "moreinfo": "<p>Don&#39;t let bad weather ruin your day of biking. This lightweight, well-ventilated canopy will keep the elements off without impeding your visibility. The canopy pivots at the back for easy entry and exit.<\/p> ", "img_overlay": "roof.jpg", "zindex": 2},
	
	"trailer": {"cost": 235, "name": "Trailer", "thumb": "trailer.jpg", "moreinfo": "<p>This trailer greatly increases your storage space. Whether you are going on a longer trip and need your suitcase or you want to pick up some groceries, this trailer will give you that extra room.<\/p>"}, 
	
	"green": {"cost": 0, "name": "204 Green", "thumb": "green.jpg", "moreinfo": "<p><\/p>"},
	"blue": {"cost": 0, "name": "Azure", "thumb": "blue.jpg", "moreinfo": "<p><\/p>"},
	"black": {"cost": 0, "name": "Charcoal", "thumb": "black.jpg", "moreinfo": "<p><\/p>"},
	"orange": {"cost": 0, "name": "Tangerine", "thumb": "orange.jpg", "moreinfo": "<p><\/p>"},

	
	"trispoke": {"cost": 0, "name": "Tri-Spoke Wheels", "thumb": "trispoke.jpg", "moreinfo": "<p>These rims are made of carbon fiber to decrease weight. Each rim weighs 1.8 pounds, making your Halcyon more efficient and more agile.<\/p>"},
	
	"classic": {"cost": 0, "name": "Classic Bicycle Wheels", "thumb": "classic.jpg", "moreinfo": "<p>These classic wire-spoked rims make the bicycle heritage of your Halcyon unmistakeable. At 2.1 pounds per rim, though, they&#39;re more advanced than they look.<\/p>"},
	
	"fivespoke": {"cost": 0, "name": "Five Spoke Wheels", "thumb": "fivespoke.jpg", "moreinfo": "<p>Our five spoke rim will give your Halcyon a modern, sporty look. They aren&#39;t our lightest rims, at 2.3 pounds per wheel, but they make up for it in pure visual appeal.<\/p>"},
	
	"airpump": {"cost": 26, "name": "Tire Pump", "thumb": "pump.jpg", "moreinfo": "<p>This portable air pump snaps directly into your trunk, making it easy to carry with you wherever you go. Increase your range and battery life by keeping your tires at their recommended pressure. <\/p>"},
	
	"beltdrive": {"cost": 50, "name": "Belt Drive", "thumb": "belt_drive.jpg", "moreinfo": "<p>This belt replaces the stock drive chain on your Halcyon. This silent-running belt requires no grease, reduces friction, and has a longer life than a standard chain.<\/p>"},
	
	"cupholders": {"cost": 23, "name": "Cup Holders", "thumb": "cupholder.jpg", "moreinfo": "<p>This adds two cup holders to the interior of your Halcyon. Perfect for a water bottle or that morning cup of coffee.<\/p>"},
	
	"waterbladder": {"cost": 41, "name": "Water Bladder", "thumb": "bladder.jpg", "moreinfo": "<p>This seat back water bladder keeps a refreshing sip of water within reach at all times. The washable, detachable bladder is made of high-quality plastic that won&#39;t flavor your drink.<\/p>"},
	
	"armrest": {"cost": 42, "name": "Armrests", "thumb": "armrest.jpg", "moreinfo": "<p>Armrests give you a place to rest your elbows when you&#39;re relaxing at a stoplight or cruising down a long straight.<\/p>"},
	
	"wiper": {"cost": 50, "name": "Windshield Wiper", "thumb": "windshieldwiper.jpg", "moreinfo": "<p>Increase visibility on rainy days with this windshield wiper. (Requires roof.)<\/p>", "img_overlay": "wiper.png", "zindex": 4},
	
	"mirror": {"cost": 50, "name": "Wing Mirrors", "thumb": "mirrors.jpg", "moreinfo": "<p>Make your ride even safer with a pair of wing mirrors.<\/p>", "img_overlay": "mirrors.png", "zindex":5},
	
	"pedalclips": {"cost": 38, "name": "Pedal Clips", "thumb": "pedal_clip.jpg", "moreinfo": "<p>These easy-in, easy-out clips improve pedaling efficiency and work with all shoe types.<\/p>"},
	
	"battery": {"cost": 315, "name": "Spare Battery", "thumb": "battery.jpg",  "moreinfo": "<p>Toss it in the trunk to double your range or keep it around the house in case you forget to charge your main battery. Exactly the same as the battery that&#39;s included with the base Halcyon.<\/p>"},
	
	"charger": {"cost": 65, "name": "Battery Charger", "thumb": "battery_charger.jpg", "moreinfo": "<p>This compact charger can be tucked into a corner for a convenient and unobtrusive indoor charge. Perfect if you don&#39;t have an outlet near where you park your Halcyon.<\/p>"}	
	};

$.fn.image = function(src, f){ 
  		return this.each(function(){ 
    	var i = new Image(); 
    	this.appendChild(i);
		i.onload = f; 
    	i.src = src; 
   		}); 
 	} 
 
 $(document).ready(function() {
	$("fieldset").hide();
	$("fieldset:first").show();
	$("#prev").hide();
	$("#build_info").append(instructions);
	$("input[type='checkbox']").removeAttr("checked");							
	$("input[type='radio']").removeAttr("checked");							

	
	//next and prev—hide or gray out if on first or last option
	$("#next").click(function(){
		$("fieldset:visible").next("fieldset").show();			 
		$("fieldset:visible:first").hide();
		if( $("fieldset:visible").is(":last-child") ){
			$("#next").hide();	
		}
		$("#prev").show();
					 
	});	
	
	$("div.build_nav").click(function(){
		if($(this).children("a").text() == "exterior"){
			$("fieldset").hide();
			$("#exteriorFieldset").show();
			$("#prev").hide();
			$("#next").show();				
		}
		
		if($(this).children("a").text() == "interior"){
			$("fieldset").hide();
			$("#interiorFieldset").show();
			$("#prev").show();
			$("#next").show();
		}
		
		if($(this).children("a").text() == "summary"){
			$("fieldset").hide();
			$("#summaryFieldset").show();
			$("#prev").show();
			$("#next").hide();
		}
	
	});	
	
	$("#prev").click(function(){
		$("fieldset:visible").prev("fieldset").show();			 
		$("fieldset:visible:last").hide();	
		if( $("fieldset:visible").is(":first-child") ){
			$("#prev").hide();	
		}
		$("#next").show();
	
	});		
	
	$("input[name=color]").click(function(e){
		var color_choice = e.target.value;	
		currentColor = color_choice;	
		var re = new RegExp("\/[blue green black orange]+\/", "g");
		
  		$("#build_images img").attr("src", function(arr){
			var source = new String(this.src);
			return source.replace(re, "\/"+color_choice+"\/");
  		});
		
	});
	
	$("input[name=wheel]").click(function(e){
		var rim_choice = e.target.value;
		$("img[title=rim]").remove();
  		$("#build_images").append("<img src=\"images\/rims\/" + rim_choice + ".png\" title=\"rim\" style=\"z-index: 3\" \/>");
		
	});
	
							
	$("input[type=checkbox]").click(function(e){ 
		var price = optionInfo[e.target.name].cost;
		var itemName = optionInfo[e.target.name].name;
		var image_src = optionInfo[e.target.name].img_overlay;
		var zindex = optionInfo[e.target.name].zindex;
		if($(this).attr("checked")){  //needs work
			$("#summary").append("<li title=\"" + e.target.name + "\">" + itemName + ": $" + price + "</li>");
			currentCost += price;
			if(image_src){
				$("#build_images").append("<img src=\"images\/" + currentColor + "\/" + image_src +"\" title=\"" + itemName + "\" style=\"z-index:"+zindex +"\" \/>");
			}
			if(e.target.name == "roof"){
				$("input[name=wiper]").removeAttr("disabled");
			}
			
		}else{
			$("#summary").children("[title="+e.target.name+"]").remove();
			currentCost -= price;
			if(image_src){
				$("img[title="+itemName+"]").remove();
			}
			if(e.target.name == "roof"){
				if($("input[name=wiper]").attr("checked")){
					$("img[title=\"Windshield Wiper\"]").remove();
					currentCost -= optionInfo["wiper"].cost;
					$("input[name=wiper]").removeAttr("checked");
				}
			
				$("input[name=wiper]").attr("disabled", "disabled");
			}

			
		}
		optionCost = currentCost-basePrice;
		$(".total").text(currentCost);
		$("#acc").text(optionCost);
	});
	
	
	$("label").hover(function(e){
		$("#build_info").empty();	
		var attrToUse;
		if($(this).children("input").attr("type") == "radio"){
			attrToUse = "value";
		}else{
			attrToUse = "name";
		}
		
		$("#build_info").append( "<h2>" + optionInfo[$(this).children("input").attr(attrToUse)].name + "<\/h2>");
		
		if(optionInfo[$(this).children("input").attr(attrToUse)].thumb){
		
			$("#build_info").append("<div id=\"thumbHolder\"><\/div>");
			$("#build_info #thumbHolder").image("images/small/" + optionInfo[$(this).children("input").attr(attrToUse)].thumb, function(){$("#build_info img").show();
			});
		
		}
		
		
		if(optionInfo[$(this).children("input").attr(attrToUse)].cost != 0){
			$("#build_info").append( "Price: $" + optionInfo[$(this).children("input").attr(attrToUse)].cost + "<br>");
		}
		
		$("#build_info").append(optionInfo[$(this).children("input").attr(attrToUse)].moreinfo); 		
		$("#build_info").show();
	}, function(){
		//$("#build_info").hide();
		$("#build_info").empty();
		$("#build_info").append(instructions);
	});
	
 });
