$(document).ready(function(){

			$('#loginButton').click(
				function(e){
					e.preventDefault();
					if(!$('#header .login').length > 0)
					{
					var loginBox = '<div class="login"></div>';
					$('#header').find('.login').remove();
					$(loginBox).appendTo('#header').load('ajax.php?ajax=true&panel=ajax_login','',
					
						function()
							{
								$('#header .login .close').click(
									function()
										{
											e.preventDefault();
											$(this).parent().parent().slideUp('slow').remove();
										}
								);
							}
					
					).slideDown('slow');
					}
				}
			);

			$('.buttonYes').click(function(e){
				e.preventDefault();
				var href = $(this).attr('href');
				href += '&ajax=true';
				$(this).parent().parent().find('.ajax_helpful').load(href,function(responseText){$(this).html(responseText);});
				$(this).parent().fadeOut();
			});
			
			$('.buttonNo').click(function(e){
				e.preventDefault();
				var href = $(this).attr('href');
				href += '&ajax=true';
				$(this).parent().parent().find('.ajax_helpful').load(href,function(responseText){$(this).html(responseText);});
				$(this).parent().fadeOut();
			});
			
			
setup_ajaxddls();
setup_addtobasket_buttons();
setup_buttons();
			
});


function OpenViewAll()
{
	$.cookie(currentpath+'_viewall','open');
	
	$("#jPagination").slideUp();
	
	for(var x = 0;x < productcount;x++)
	{
		$(content).appendTo("#jViewAll");
	}
	
	$("#jViewAll").slideDown('slow').load(baseurl+'ajax.php?ajax=true&panel=showall_products&type='+pagetype+'&category='+category,function()
	{
		setup_addtobasket_buttons()
	});
	
	$('.jPager').each(
				function(){
					$(this).find("li").removeClass("selected");
					});
}

function getQSKey(url,key)
{
	var params = url.replace('?','&').split('&');
		for(var x = 0;x < params.length;x++)
		{
			var param = params[x];
			if(param.substring(0,key.length) == key)
				{
					return unescape(param.substring(key.length+1,param.length));
				}
		}
}



function setup_addtobasket_buttons()
{
			$('.addRecentToBasket').unbind('click').click(
			function(e)
				{
					e.preventDefault();
				
					$(this).parent().parent().fadeOut();
					
					var trs = $(this).parent().parent().parent().find('tr:visible').size();
					//alert(trs);
					
					if(trs == 1)
					{$('#recent').fadeOut();}
					
					var url = $(this).attr('href');
					var id = getQSKey(url,'add');
					url = baseurl+'ajax.php?ajax=true&panel=ajax_sidebar_basket&action=basketItemAdd&id='+id;
						
					loadPanel('ajax_sidebar_basket',url);
					$('#ajax_sidebar_basket').fadeOut('fast').fadeIn('fast');
				}
			);
			
			$('.addToBasket').unbind('click').click(
			function(e)
				{
					e.preventDefault();
					
					var content = '<div class="productAdded"><u>Continue Shopping</u><a href="basket_summary.html" class="button">Checkout</a></div>';

					$(content).appendTo($(this).parent().parent()).fadeIn().click(
						function()
							{$(this).fadeOut()}
					).fadeTo(3000,1,function(){
							$(this).fadeOut("slow");
						});
					
						var url = $(this).attr('href');
						var id = getQSKey(url,'add');
						url = baseurl+'ajax.php?ajax=true&panel=ajax_sidebar_basket&action=basketItemAdd&id='+id;
						
						loadPanel('ajax_sidebar_basket',url);
						$('#ajax_sidebar_basket').fadeOut('fast').fadeIn('fast');
				}
		);			
		
			$('.add .quantityUp').removeClass('hidden');
			$('.add .quantityDown').removeClass('hidden');
		
			$('.detailAddToBasket').unbind('click').click(
			function(e)
				{
					e.preventDefault();
					
					var content = '<div class="productAdded"><u>Continue Shopping</u><a href="basket_summary.html" class="button">Checkout</a></div>';

					$(content).appendTo($(this).parent().parent()).fadeIn().click(
						function()
							{$(this).fadeOut()}
					).fadeTo(3000,1,function(){
							$(this).fadeOut("slow");
						});
					
						var url = $(this).parent().attr('action');
						var quantity = $(this).parent().find('#formQuantity').val();
						var id = getQSKey(url,'add');
						url = baseurl+'ajax.php?ajax=true&panel=ajax_sidebar_basket&action=basketItemAdd&id='+id+'&quantity='+quantity;
						
						loadPanel('ajax_sidebar_basket',url);
						$('#ajax_sidebar_basket').fadeOut('fast').fadeIn('fast');
				}
		);
}
	
function setup_buttons()
			{
				$('.ajax_button').unbind('click');
				$('.ajax_button').click(
					function(e){
						e.preventDefault();
						var url = $(this).attr('href');
						var target = getQSKey(url,'panel');
					url = url.replace('?','?ajax=true&');
						loadPanel(target,url);
							}
						);
										
			}

			
function loadPanel(target,url)
		{
			$('#'+target).load(url,'',function()
				{
					setup_ajaxddls();
						setup_buttons();
						setup_addtobasket_buttons();
        		}
        	);
		}
		

function setup_ajaxddls()
{

$(".ajax_categoryfilter").unbind('change').change(						
	function(e)
		{
			var paramlist = '';
			var checkboxParamlist = '';
			
			$(this).parents().filter("form").find("select option:selected").each(
					function()
						{
							var id = $(this).parent('select').attr('name');
							var val = $(this).val();
							paramlist += '&' + id + '=' + $(this).val().replace(" ","%20").replace("+","%2B");
						}
				);
				
			$(this).parents().filter("form").find("input[type=checkbox]").filter("[name=formEducational]").filter(":checked").each(
					function()
						{
							var id = $(this).attr('name');
							
							if(checkboxParamlist.length > 0)
							checkboxParamlist += ',';
							
							checkboxParamlist += $(this).val().replace(" ","%20").replace("+","%2B");
						}
			);
			
			paramlist += '&educationalbenefits='+checkboxParamlist;
			
			checkboxParamlist = '';
			
			$(this).parents().filter("form").find("input[type=checkbox]").filter("[name=formCollection]").filter(":checked").each(
					function()
						{
							var id = $(this).attr('name');
							
							if(checkboxParamlist.length > 0)
							checkboxParamlist += ',';
							
							checkboxParamlist += $(this).val().replace(" ","%20").replace("+","%2B");
						}
			);
			
			paramlist += '&collections='+checkboxParamlist;
			
			$(this).parents().filter("form").find("input[type=text]").each(
					function()
						{
							var id = $(this).attr('name');
							paramlist += '&' + id + '=' + $(this).val().replace(" ","%20").replace("+","%2B");
						}
				);
				
			//alert(baseurl+'ajax.php?ajax=true&panel=showall_products&type='+pagetype+'&category='+category+paramlist);
			
			if($("#jViewAll").is(':visible'))
			{
			$("#jViewAll").load(baseurl+'ajax.php?ajax=true&panel=showall_products&type='+pagetype+'&category='+category+paramlist,function()
			{
				setup_addtobasket_buttons()
			});
			}
			
				$("#jPagination").children("div").each(
					function()
						{
							$(this).find("div.product").html('<img src="resources/images/loading.gif" style="position:absolute;left:95px;top:150px;" />');
						
							var idx = $(this).index();
							var loadUrl = baseurl+'ajax.php?ajax=true&panel=preload_product&type='+pagetype+'&category='+category+'&page='+idx+paramlist;
							//alert(loadUrl);
							$(this).load(loadUrl,
							function()
								{
									setup_addtobasket_buttons();
									
									var products = $(this).find('div.product').size();
									
									var idx = $(this).index();
									if(products == 0)
										{
	 										$('.jPager').each(
	 											function(){
													$(this).find("li").not(".liViewAll").filter("li:eq("+(idx)+")").fadeOut();
												}
												);
											$("#jPagination").cycle(0);
										}
									else
										{
	 										$('.jPager').each(
	 											function(){
													$(this).find("li").not(".liViewAll").filter("li:eq("+(idx)+")").fadeIn();
												}
												);
										}
								}
							).addClass('loaded');
						}
				);
		}								
);//end: $(".ajax_dropdownlist").change



$(".ajax_dropdownlist").unbind('change').change(						
	function(e)
		{
			var paramlist = '';
			
			$(this).parents().filter("form").find("select option:selected").each(
					function()
						{
							var id = $(this).parent('select').attr('name');
							paramlist += '&' + id + '=' + $(this).val().replace(" ","%20");
						}
				);
				
			$(this).parents().filter("form").find("input").each(
					function()
						{
							var id = $(this).attr('name');
							paramlist += '&' + id + '=' + $(this).val().replace(" ","%20");
						}
				);
				
			
			var url = $(this).parents().filter("form").attr('action')+paramlist;
			var target = getQSKey(url,'panel');
			
			url = getQSKey(url,'url')+paramlist+'&panel='+target;
			
				//url = url.replace('?','?ajax=true&');
				
				alert(url);
				
			loadPanel(target,url);
		}								
);//end: $(".ajax_dropdownlist").change


}
		
		
		
		
/* CATEGORY PAGE */
function initCategoryPage()
{
var opened = false;

$('.advancedSearchToggle').show();
$('.boxAdvancedSearch').attr('style','');
$('.boxAdvancedSearch table').attr('style','');

$("#advancedSearchToggle").mousedown(
	function(e)
		{
			e.preventDefault();
			if(opened)
			{
			$(this).attr("class","openedDown");
			}
			else
			{
			$(this).attr("class","closedDown");
			}
		}
	);

$("#advancedSearchToggle").click(
	function(e)
		{
			e.preventDefault();
			
			if(opened)
			{
				$(".boxAdvancedSearch table").animate({"top": "-=60px"},"slow");
				$(this).attr("class","closed");
			}
			else
			{
				$(".boxAdvancedSearch table").animate({"top": "+=60px"},"slow");
				$(this).attr("class","opened");
			}
			
			$(".boxAdvancedSearch").slideToggle("slow");
			
			opened = opened ? false : true;
		}						
	);

// Insert pagination placeholders
for(x = 0;x < pagecount;x++)
{
	var divPage = '<div style="width:100%;"></div>';
	if(x<currentpage-1)
	{
		$(divPage).prependTo("#jPagination");
	}
	if(x>currentpage-1)
	{
		$(divPage).appendTo("#jPagination");
	}
}

					// redefine Cycles updateActivePagerLink function 
					$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
	 					$(pager).each(
	 						function(){
	 							$(this).find("li").removeClass("selected").not(".liViewAll").filter("li:eq("+(currSlideIndex)+")").addClass("selected");
	 									}
	 								);
					}; 
					
$(".jPager").find("li").not(".liViewAll").remove();

// Setup the view all button								
$(".liViewAll a").click(
	function(e)
		{
			e.preventDefault();
			OpenViewAll();
		}
);

// Begin loading all pages
/*
$("#jPagination").children("div").each(
	function()
		{
			if($(this).attr('class') != 'loaded')
			{
				//Add a full page worth of placeholders
				
				var idx = $(this).index();
				
				//alert(productcount-(pagesize*idx));
				for(var x = 0;x < pagesize && x < productcount-(pagesize*idx);x++)
				{
					$(content).appendTo(this);
				}
				
				$(this).load(baseurl+'ajax.php?ajax=true&panel=preload_product&type='+pagetype+'&category='+category+'&page='+idx,
				function()
				{
					setup_addtobasket_buttons()
				}).addClass('loaded');
			}
		}
);
*/
		
var startingSlide = currentpage-1;
		
if($.cookie(currentpath+'_page') && startingSlide < 1)
{
	var page = $.cookie(currentpath+'_page');
	startingSlide = eval(page);
}

$("#jPagination").children("div:eq("+ startingSlide +")").each(
	function()
		{
			if($(this).attr('class') != 'loaded')
			{
				//Add a full page worth of placeholders
				
				var idx = $(this).index();
				
				//alert(productcount-(pagesize*idx));
				for(var x = 0;x < pagesize && x < productcount-(pagesize*idx);x++)
				{
					$(content).appendTo(this);
				}
				
				$(this).load(baseurl+'ajax.php?ajax=true&panel=preload_product&type='+pagetype+'&category='+category+'&page='+idx,
				function()
				{
					setup_addtobasket_buttons()
				}).addClass('loaded');
			}
		}
);		
									
$("#jPagination").cycle({
	fx:"scrollHorz",
	cleartypeNoBg:true,
	timeout:0,
	speed:1000,
	startingSlide:startingSlide,
	delay:0,
	pager:".jPager",
	pagerAnchorBuilder: function(idx, slide){
	return "<li><a href=\""+currentpath+"/page-"+(idx+1)+".html"+currentqs+"\">"+(idx+1)+"</a></li>";
	},
	// On-demand load a page, not likely this will ever happen
	pagerClick: function(idx, slide){
		$.cookie(currentpath+'_page',idx);
		$.cookie(currentpath+'_viewall','closed');
		$("#jPagination").slideDown('fast');
		$("#jViewAll").slideUp('fast');
		
		$.scrollTo(".overSearch table",400);
		
		if($(slide).attr('class') != 'loaded')
		{
		//Add a full page worth of placeholders
		for(var x = 0;x < pagesize && x < productcount-(pagesize*idx);x++)
				{
					$(content).appendTo(slide);
				}
		$(slide).load(baseurl+'ajax.php?ajax=true&panel=preload_product&type='+pagetype+'&category='+category+'&page='+idx,
				function()
				{
					setup_addtobasket_buttons()
				}).addClass('loaded');
		}
	}
});



if($.cookie(currentpath+'_viewall') == 'open')
{
	OpenViewAll();
}



}