$(document).ready(function() {
	$("#search").click(function() {
		if($(this).attr('value')=='Search our site') $(this).attr('value','');
	});
	$("#search").blur(function() {
		if($(this).attr('value')=='') $(this).attr('value','Search our site');
	});
/*
	
	if( $("#btn_moreinfo").length > 0 ) {
		$("#btn_moreinfo").bind( 'click', function() {
		
		
		
		});
	}
	
*/
	//checkout shipping page
	if($("#checkout_shipping").length > 0) {
		//Helium Datepickers
		$("input[name=helium_delivery]").datepicker({ dateFormat: 'dd/mm/yy' });
		$("input[name=helium_delivery]").blur(function() {
			if($(this).attr('value')=='') $(this).attr('value','dd/mm/yyyy');
		});
		$("input[name=helium_collection]").datepicker({ dateFormat: 'dd/mm/yy' });
		$("input[name=helium_collection]").blur(function() {
			if($(this).attr('value')=='') $(this).attr('value','dd/mm/yyyy');
		});
		
		//T & C dialog boxes
		Shadowbox.init();
		
		$("#heliumTCLink").click(function() {
			Shadowbox.open({
	        	content:    '<iframe src="/information_only.php?info_id=49" height="350" width="350" style="border:none;"></iframe>',
	        	player:     "html",
	        	title:      "Helium Rental Terms &amp; Conditions",
	        	height:     350,
	       	 	width:      350
	       	});

		});
		
		$("#siteTCLink").click(function() {
			Shadowbox.open({
	        	content:    '<iframe src="/information_only.php?info_id=28" height="350" width="350" style="border:none;"></iframe>',
	        	player:     "html",
	        	title:      "Talking Balloons Terms &amp; Conditions",
	        	height:     350,
	       	 	width:      350
	       	});

		});
	}
	
	//checkout confirm page
	if($("#heliumShippingFAQLink").length > 0) {
		Shadowbox.init();
		
		$("#heliumShippingFAQLink").click(function() {
			Shadowbox.open({
	        	content:    '<iframe src="/information_only.php?info_id=48" height="350" width="350" style="border:none;"></iframe>',
	        	player:     "html",
	        	title:      "Helium Shipping Charge",
	        	height:     350,
	       	 	width:      350
	       	});

		});
	}
	
	//accordion menu
	if($(".accordion").length > 0) {
		
	
		$(".accordion").each( function() {
			var selected = false;
			var accordionID = this.id;
			
			//look to see if there are any selected elements
			$("#"+accordionID).children("h3").each( function() {
				//console.log('checking:'+accordionID+'_'+this.id);
				if($("#"+this.id).attr("class").indexOf('selected')>0) {
					//console.log('found id:'+this.id.split('_')[2]);
					selected = parseInt(this.id.split('_')[2]);				
				}
			});
			
			//console.log('activating '+selected);
				
			//activate accordion
			$('#'+accordionID).accordion( { active: selected } );
			
			//prevent FBUC
			$('.cat_name').css('visibility','visible');
		});
		
		var productListTitle = $("#title_list_product").html();
		
		//initialise 3-deep menu
		$(".productsCat").each( function() {
		
			var hasSelectedProduct = false;
			var catID = this.id.split('_')[1];
		
			$("#"+this.id+" .productList .LHMenuNestedProductLink").each( function() {
				var prodCat = 'cat_'+catID;

				if($("#"+this.id).attr("class").indexOf('selected')>0  || $("#"+prodCat+" > span").html()==$.trim(productListTitle)) {
					//product in this category is selected, so expand the category
					hasSelectedProduct = true;
					
					var prodList = 'pList_'+catID;
					
					//fix for accordion menu item height - the parent box needs to be able to expand 
					$("#"+prodCat).parent().css('height','auto');
					

					//$("#"+prodCat).css('background-color','#F41D8C');
					$("#"+prodCat+" > img").attr("src","/templates/balloons/images/style_list_hover.png");
					$("#"+prodList).slideDown();	
					
					$("#cat_"+catID+" > span").css({ 'font-weight': 'bold', 'color': '#F41D8C' });
				}

			
			});
			
			$('#'+this.id).click( function() {
					$(".productsCat").each(function(){
						//close any open product lists
						collapseProductList(this.id.split('_')[1]);
					});
					if($('#pList_'+catID).css('display')!='block') {
						//expand the clicked product list, change content area
						expandProductList(catID);
						$('#column_right').load($('#'+this.id+' > a').attr('href')+' #column_right > *');
					}
			});
			
			/*if(!hasSelectedProduct) {
				
				
				$('#'+this.id).hoverIntent( {    
				     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
				     interval: 200, // number = milliseconds for onMouseOver polling interval    
				     over: expandProductList, // function = onMouseOver callback (REQUIRED)    
				     timeout: 300, // number = milliseconds delay before onMouseOut    
				     out: collapseProductList // function = onMouseOut callback (REQUIRED)    
				});
			}*/
		});
		
		//reset other accordions when one is opened
		$('.ui-accordion-header').each( function() {
			
			$('#'+this.id).click( function() {
				//console.log('id:'+this.id);
				
				//var cPathToLoad = $('#'+this.id+' > a').attr('href').split('/')[4];
				$('#column_right').load($('#'+this.id+' > a').attr('href')+' #column_right > *');
				
				var openedAccordionID = 'accordion_'+this.id.split('_')[1]; 
				$(".accordion").each( function() {
					
					if(this.id!=openedAccordionID) {
						//console.log('collapsing accordion id: '+this.id);
						//set accordion to collapsible, deactivate (which closes all items), then switch off collapsible
						$('#'+this.id).accordion( "option", "collapsible", true );
						$('#'+this.id).accordion( "activate" , false );				
						$('#'+this.id).accordion( "option", "collapsible", false );
						//$('#'+this.id).accordion( "activate" , true );	
						//$('#'+this.id).accordion( { active: false } );
					}
				});
			
			});
		
		});
		
	}
	
	

});

function expandProductList(catID) {
	//var catID = this.id.split('_')[1];
	var prodList = 'pList_'+catID;
	var prodCat = 'cat_'+catID;
	
	//fix for accordion menu item height - the parent box needs to be able to expand 
	$("#"+prodCat).parent().css('height','auto');
	
	
	
	//$("#"+prodCat).css('background-color','#F41D8C');
	$("#"+prodCat+" > img").attr("src","/templates/balloons/images/style_list_hover.png");
	$("#"+prodList).slideDown();

}

function collapseProductList(catID) {

	//var catID = this.id.split('_')[1];
	var prodList = 'pList_'+catID;
	var prodCat = 'cat_'+catID;
	
	//$("#"+prodCat).css('background-color','#FFFFFF');
	$("#"+prodCat+" > img").attr("src","/templates/balloons/images/style_list2.png");
	$("#"+prodList).slideUp();
}
