var $j = jQuery;
var baseURL = "";
var optionsTotal = 0;
var basePrice = 0;
var unitPrice = 0;
var totalPrice = 0;
var prodQty = 0;
$j(function(){
			
	$j('#option-5').before('<h3><span>1</span>Paper options &amp Quantity</h3>');	
	$j('#option-7').before('<h3><span>2</span>Colour Scheme</h3><p class="colourBlurb">Plese select your wedding feature colour by clicking the button to the left and browsing our colour swatches</p>');
	$j('#option-8').before('<h3><span>3</span>Font Selection</h3><p class="fontBlurb">Please select your wedding feature fonts that will be used on your custom cards, as you can see you can choose one font for the names and one font for the rest of the text.</p>');
	$j('#option-9').after('<h3><span>4</span>Wording<span id="viewWording">See how you should word your card &raquo;</span></h3>');	
	
	$j('#option-8 .option-header').append('<span id="viewNameFonts">View Name Fonts &raquo;</span>');
	$j('#option-9 .option-header').append('<span id="viewTextFonts">View Text Fonts &raquo;</span>');
	
	$j('#option-73').before('<h3><span>1</span>Select Sample Design</h3>');
	$j('#option-73 select').before('<strong>Select Design</strong>');
	$j('#option-73').after('<p class="colourBlurb">Choose the design of card which you wish to order a sample of from the drop down to the left.</p><div class="clrBth"></div>');
	
	$j('#option-91').before('<h3><span>2</span>Select Card Type</h3>');
	$j('#option-91 select').before('<strong>Select Card Type</strong>');
	$j('#option-91').after('<p class="colourBlurb">Choose the type of card you wish to order a sample of from the list,</p><div class="clrBth"></div>');
	$j('#option-91 option:last').attr("selected", "selected");
	$j('#option-91 select').change(function(){updateTotal();});
	
	lbScrollPane = "<div id='lbScrollPane'></div>";	
	$j("#swatchPreview").click(function(){			
		lightboxbc(lbScrollPane , "Available Swatches", loadExtHTML, {pageURL:baseURL+'/ajax/swatches.php', callback: addSwatchListners} )
	});
	$j("#viewSwatch").click(function(){			
		lightboxbc(lbScrollPane , "Available Swatches", loadExtHTML, {pageURL:baseURL+'/ajax/swatches.php', callback: addSwatchListners} )
	});
	
	
	$j("#viewNameFonts").click(function(){			
		lightboxbc(lbScrollPane , "Name Fonts", loadExtHTML, {pageURL:baseURL+'/ajax/name-fonts.php', callback: addNameFontListners} )
	});
	
	$j("#viewTextFonts").click(function(){			
		lightboxbc(lbScrollPane , "Text Fonts", loadExtHTML, {pageURL:baseURL+'/ajax/text-fonts.php', callback: addTextFontListners} )
	});
	
	$j("#viewWording").click(function(){			
		lightboxbc(lbScrollPane , "Wording Examples", loadExtHTML, {pageURL:baseURL+'/ajax/wording.html'} )
	});
	
	
	
		
	
	// If sample then change QTY to 1 instead of 25
	if($j("#po_73").size() > 0  ||$j("#option-82").size() > 0)  { $j("#prodQty").attr("value", 1); }
	if($j("#option-56").size() > 0)  { $j("#prodQty").attr("value", 5); }

	
	
	updateTotal();
	
	
	$j(".option-values input[type=radio]").each(function(){
			$j(this).click(function(){updateTotal();});
	});
	
	timeoutExists = false;
	$j("#prodQty").keypress(
						
						function(){ 
						if(timeoutExists) {	clearTimeout(updateQty); }

						updateQty = setTimeout(function(){
						
						  if(parseInt($j("#prodQty").val()) < 25 && ($j("#po_73").size() == 0 && $j("#option-82").size() == 0 && $j("#option-56").size() == 0 ))
						  {
							 $j('#prodQty').val('25'); alert("Minimum purchase Quantity of 25");
						  }
                                                    else if(parseInt($j("#prodQty").val()) < 5 && ($j("#option-56").size() > 0 ))
                                                  {
                                                        $j('#prodQty').val('5'); alert("Minimum purchase Quantity of 5");
                                                  }
                                                    else
                                                  {  updateTotal();}
						  
						  } , 1000);
						timeoutExists = true;
						});
	$j("#prodQty").change(updateTotal);
	

	$j('#loadingDiv')
		.hide()  // hide it initially
		.ajaxStart(function() {
			$(this).show();
		})
		.ajaxStop(function() {
			$(this).hide();
		})
	;
	
	$j("#option-5 input:last").attr("checked", "checked");
	updateTotal();
	
	if($j("#option-21").size()>0 && $j("#option-26").size()>0 )
		$j("#option-26").before($j("#option-21"));
	
	if($j("#option-70").size()>0 && $j("#option-23").size()>0 )
		$j("#option-70").before($j("#option-23"));
		
	if($j("#option-71").size()>0 && $j("#option-74").size()>0  && $j("#option-75").size()>0  )
	{
		$j("#option-71").before($j("#option-75"));
		$j("#option-75").before($j("#option-74"));
	}
	
	if($j("#option-28").size()>0 && $j("#option-18").size()>0  && $j("#option-30").size()>0  )
	{
		$j("#option-30").after($j("#option-28"));
		$j("#option-28").after($j("#option-18"));
	}
	
	if($j("#option-87").size()>0 && $j("#option-88").size()>0  && $j("#option-13").size()>0  )
	{
		$j("#option-13").after($j("#option-87"));
		$j("#option-87").after($j("#option-88"));
	}
	
	if($j("#option-79").size()>0 && $j("#option-37").size()>0 )
		$j("#option-37").after($j("#option-79"));
	
	if($j("#option-76").size()>0 && $j("#option-11").size()>0 )
		$j("#option-11").before($j("#option-76"));
	
	$j("#option-11").before($j("#option-59"));
	
	if($j("#option-65").size()>0 && $j("#option-66").size()>0  && $j("#option-87").size()>0  && $j("#option-11").size()>0  )
	{
		$j("#option-11").before($j("#option-87"));
		$j("#option-13").before($j("#option-89"));
	}
	
	if($j("#option-13").size()>0 && $j("#option-56").size()>0 )
		$j("#option-13").before($j("#option-56"));

	
});

function updateTotal(){
	var poundSign = $j("#prodTotalPrice").text().charAt(0);
	
	prodQty = $j("#prodQty").val();	
	optionsTotal = calculateOptionsTotal();
	
	basePrice = parseFloat($j("#prodUnitPrice").attr("price"));
	
	$j.get('/ajax_price.php', { id: $j("input[name=add]").val(), quantity: prodQty }, 
		   function(data) {
			    if (data != '') basePrice = parseFloat(data);			   
				unitPrice = basePrice + optionsTotal;
				totalPrice = unitPrice * prodQty;
				
				$j("#prodUnitPrice").text(poundSign+unitPrice.toFixed(2));
				$j("#prodTotalPrice").text(poundSign+totalPrice.toFixed(2));		
				//basePrice =  basePrice.toFixed(2);				
		   });
	
	
																	 
}

function calculateOptionsTotal(){
	optionsPriceTotal = 0;
	$j(".option-values input[type=radio]").each(function(){
		if($j(this).attr("price"))
		{
			if($j(this).attr("checked") == "checked" || $j(this).attr("checked"))
				optionsPriceTotal = optionsPriceTotal + parseFloat($j(this).attr("price"));
			
		}
	});
	
	if($j("#po_91").size() > 0)
	{
		if($j("#po_91").val() == 26124)
			optionsPriceTotal -= 2;
	}

	return optionsPriceTotal;
}

function loadExtHTML( page ) { 
	$j.ajax({
	  url: page.pageURL,
	  beforeSend: function(){
	  	$j('#lbScrollPane').css("background","url("+baseURL+"/ajax/ajax-loader.gif) 315px 150px no-repeat");
	  },
	  success: function(data) {
		$j('#lbScrollPane').html(data);
		$j('#lbScrollPane').css("background","");
		// Call callback function
		if(page.callback){ page.callback( )}		
	  }
	});
}


function addSwatchListners (){
	$j('#lbScrollPane .colourSwatch').click(function(){
			var selectedOpt = $j(this).attr("opt");
			var optionSRC = $j(this).children("img").attr("src");
			var optionName = $j(this).children("span").text();
			// Loop throught the options to find the matching one.
			$j('#po_7 option').each(function(){ 
				if(jQuery.trim($j(this).text()).toLowerCase() == optionName.toLowerCase() ) {$j(this).attr("selected","selected") } 
			 });
			closelightboxbc();
			$j('#swatchPreview').attr("src",optionSRC);
			$j('#viewSwatch').text(optionName+" (click to change)");
			
		});
}
function addNameFontListners (){
	$j('#lbScrollPane .nameFont').click(function(){
			var selectedOpt = $j(this).attr("opt");
			$j("#poption-8 input").attr("checked",false);
			$j("#po_8_"+selectedOpt).attr("checked","checked");
			closelightboxbc();			
		});
}
function addTextFontListners (){
	$j('#lbScrollPane .textFont').click(function(){
			var selectedOpt = $j(this).attr("opt");
			$j("#poption-9 input").attr("checked",false);
			$j("#po_9_"+selectedOpt).attr("checked","checked");
			closelightboxbc();			
		});
}


function lightboxbc(content, title, callback, params){
	if($j("#lightboxBC").size() == 0){  var newLightbox = '<div id="lightboxBCWrapper"></div><div id="lightboxBC"><span id="lbTitle"></span><span id="lbClose"></span><div id="lbContents"></div></div>';  $j("body").append(newLightbox);	 }	
	$j("#lightboxBC div").html(content);	
	$j("#lbTitle").text(title);
	var scrollPos = [ $j(window).scrollLeft(), $j(window).scrollTop()];
	var windowSize = [$j(window).width() , $j(window).height()];

	
	$j("#lightboxBCWrapper").css({"width":windowSize[0]+"px","height":$j(document).height()+"px","top":"0px","left":scrollPos[0]+"px"});
	$j("#lightboxBC").css("top",(((windowSize[1] - $j("#lightboxBC").height())/2)+scrollPos[1])+"px");
	$j("#lightboxBC").css("left",(((windowSize[0] - $j("#lightboxBC").width())/2)+scrollPos[0])+"px");
	$j("#lightboxBC").css("width",$j("#lbContents div").css("width"));
	//$j("#lightboxBCWrapper").fadeIn();
	$j("#lightboxBCWrapper").show();
	$j("#lightboxBC").show("slow", function(){if(callback){callback( params )}});
	$j("#lightboxBCWrapper").click(function(){closelightboxbc();});
	$j("#lbClose").click(function(){closelightboxbc();});
	
	

}
function closelightboxbc(){
	$j("#lightboxBC").hide();
	$j("#lightboxBCWrapper").hide();
}

