$(document).ready(function(){
/* On Load */


/* Product Image Zooming */


				if($('.zoomImage').lightBox)
				$('.zoomImage').lightBox({imageLoading: 'media/img/loading.gif'});
				
				$('.thumbImage').click(
					function()
						{
							var thumbStd = $(this).attr('content');
							var thumbFull = $(this).attr('title');
							
							$('.productImage').find('img').attr('src',thumbStd);
							$('.productImage').find('a').attr('href',thumbFull);
							
							$(this).parent().find('div').removeClass('selected');
							$(this).addClass('selected');
var options = {
					zoomWidth: 298,
					zoomHeight: 375,
					xOffset: 30,
					yOffset: 0,
					position: "right",
					title: false,
					showEffect:'fadein',
					hideEffect:'fadeout',
					fadeoutSpeed: 'fast',
					zoomType:'reverse'
				};

							$('.zoomImage').unbind().removeAttr('style').jqzoom(options);

						}
				);

/* Fading News In Footer */
$('.cyclenews').cycle();


		$('.shopby div.select ul').hide().addClass('hover');
		$('.shopby div.select').hover(
			function(){$(this).find('ul').show();},
			function(){$(this).find('ul').hide();}
		);

	$('.brands_carousel .pages').cycle({
		fx:		'scrollHorz',
		speed:	3000,
		prev:	'a.prev',
		next:	'a.next',
		timeout: 5000,
		pause:	true,
		pauseOnPagerHover: true,
		fastOnEvent: 2000
	});

$('.product img').hover(
	function(){
		$(this).parents('div.product').addClass('productHover');
		$(this).addClass('hover').animate(
			{
			width:	235,
			height:	235,
			left:	-45,
			top:	-89
			},
			{
			duration:200,
			queue:	false,
			easing: 'jswing'
			});
	},
	function(){
		$(this).parents('div.product').removeClass('productHover');
		$(this).removeClass('hover').animate(
			{
			width:	146,
			height:	146,
			left:	0,
			top:	0,
			queue:	false
			},
			{
			duration:200,
			queue:	false
			});
	}
	);

$('#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)
							{
								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();

/* End On Load */	
});


function CloseViewAll()
{
	$.cookie(currentpath+'_viewall','closed');
	$("#jViewAll").slideUp('fast');
	$("#jPagination:hidden").slideDown();
	$(".liViewAll").removeClass('selected');
	
	idx = $.cookie(currentpath+'_page');
	$(".jPager").find("li").not(".liViewAll").filter("li:eq("+(idx)+")").addClass("selected");
}

function OpenViewAll()
{
	$.cookie(currentpath+'_viewall','open');
	
	$("#jPagination").slideUp();
	
	$("#jViewAll").html('');
	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");
					});
					
	$(".liViewAll").addClass('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()
{

			$('.product img').unbind('hover').hover(
			function(){
				$(this).addClass('hover').animate(
					{
					width:	235,
					height:	235,
					left:	-45,
					top:	-89
					},
					{
					duration:200,
					queue:	false,
					easing: 'jswing'
					});
			},
			function(){
				$(this).removeClass('hover').animate(
					{
					width:	146,
					height:	146,
					left:	0,
					top:	0,
					queue:	false
					},
					{
					duration:200,
					queue:	false
					});
			}
			);

			$('.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/" class="button">Checkout</a></div>';

					$(content).appendTo($(this).parent()).fadeIn().click(
						function()
							{$(this).fadeOut()}
					).fadeTo(3000,1,function(){
							$(this).fadeOut("slow");
						});
					
						var url = $(this).attr('href');
						var id = getQSKey(url,'add');
						
						if($('#ajax_sidebar_basket').length > 0)
						{
							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');
							
							//$('#didYouSee').fadeIn().scrollFollow({container: 'content'}).click(function(){$(this).fadeOut()}).fadeTo(6000,1,function(){$(this).fadeOut("slow");});;
						}
						else
						{
							url = baseurl+'ajax.php?ajax=true&panel=ajax_basket&action=basketItemAdd&id='+id;
							loadPanel('ajax_basket',url);
							$('#ajax_basket').fadeOut('fast').fadeIn('fast');
							$(this).parent().fadeOut('fast'); //We're on the basket page, so fade out the product that's just been added
							
							// And fade out the "You might also be interested in..." box if it's now empty
							
							if($(this).parent().parent().children().filter('div:visible').length <= 2)
							{
								$(this).parent().parent().fadeOut('fast');
							}
						}
				}
		);			
		
			$('.detailAddToBasket').unbind('click').click(
			function(e)
				{
					e.preventDefault();
					
					var content = '<div class="productAdded"><u>Continue Shopping</u><a href="/basket-summary/" 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 id = getQSKey(url,'add');
						var qty = $('#formQuantity').val();
						url = baseurl+'ajax.php?ajax=true&panel=ajax_sidebar_basket&action=basketItemAdd&id='+id+'&quantity='+qty;
						
						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(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(!idx)
									{idx = 0;}
									
									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;

$('#col1 form').formStyle({
			change: function(){
				CloseViewAll();
				$('#formFilter').submit();}
		});
		
$('#col1 form').attr('action','/ajax.php?panel=ajax_category_filter').ajaxForm(function(responseText){
	
	var pagecount = parseInt(responseText);
	//alert(responseText);
	
	//if($("#jPagination").children("div").length < pagecount)
	//{
		
		$("#jPagination").children('div').fadeOut().remove();
		
		$('.jPager').each(function(){
			$(this).find("li").not(".liViewAll").fadeOut().remove();
		});
		
		//alert('adding ' + (pagecount-$("#jPagination").children("div").length) + ' extra divs');
		
		
		
		
	//}
	
		for(var x = 0;x < pagecount;x++)
		{
			$('<div></div>').appendTo("#jPagination");
		}
		
			$("#jPagination").cycle('destroy');
		
		

	$("#jPagination").children("div").each(
		function()
			{
					//Add a full page worth of placeholders
					
					var idx = $(this).index();
					
					//alert(productcount-(pagesize*idx));
					$(this).html('');
					for(var x = 0;x < pagesize && x < productcount-(pagesize*idx);x++)
					{
						$(content).appendTo(this);
					}
				
					/*
					// Procedural load temporarily removed
					
					$(this).load(baseurl+'ajax.php?ajax=true&panel=preload_product&type='+pagetype+'&category='+category+'&page='+idx,
					function()
					{
						
						//  Hide Tab if page is empty
					
						var products = $(this).find('div.product').size();
						
						var idx = $(this).index();
						if(!idx)
						{idx = 0;}
						
						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();
									}
									);
							}
										
						setup_addtobasket_buttons();

						
					}).addClass('loaded');
					
					*/
			}
	);

		
		
		
		$.cookie(currentpath+'_page',0);
		
		doPagination();
		
		
		
		/*$('.jPager').each(function(){
			$(this).find("li").not(".liViewAll").filter("li:gt("+(pagecount)+")").each(function(){$(this).fadeOut()});
			$(this).find("li").not(".liViewAll").filter("li:lt("+(pagecount)+")").each(function(){$(this).fadeIn()});
			$(this).find("li").not(".liViewAll").filter("li:eq("+(pagecount)+")").fadeIn();
		});*/
	



});

$('.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();
		}
);


doPagination();


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

}


function Bookmark() {

 title = "Winsor Bishop";
 url = "http://www.winsorbishop.co.uk";

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
 }
 
 function doPagination()
 {
 
 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');
			}
		}
);



// 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');
			}
		}
);
*/



$("#jPagination").cycle({
	fx:"scrollHorz",
	timeout:0,
	speed:1000,
	startingSlide:startingSlide,
	delay:0,
	pager:".jPager",
	before:function(){$("#jPagination").removeClass('overflow');},
	after:function(){$("#jPagination").addClass('overflow');},
	pagerAnchorBuilder: function(idx, slide){
	return "<li><a href=\""+currentpath+"page-"+(idx+1)+"/"+currentqs+"\">"+(idx+1)+"</a></li>";
	},
	// On-demand load a page, not likely this will ever happen
	onPagerEvent: function(idx, slide){
		$.cookie(currentpath+'_page',idx);
		$.cookie(currentpath+'_viewall','closed');
		$("#jViewAll").slideUp('fast');
		$("#jPagination:hidden").slideDown();
		$(".liViewAll").removeClass('selected');
		$(".jPager").find("li").not(".liViewAll").filter("li:eq("+(idx)+")").addClass("selected");
		
		//$.scrollTo(".jPager:first",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');
		}
		return false;
	}
});
 
 }