var wdPosition = 0;

var wdProductCounter = 0;

var wdMaxPosition = 0;

var wardrobeOpen = false;

	

$(document).ready(function(){

	$("p.copyright").html("&copy; Viyella 2010.");

	$("#headerStores").text("Stores");

	$("#searchForm fieldset label").text("Search");   

	var maxHeight = $("div#contentCont").height();

	var startY = $('div#mainContentCont').offset({relativeTo: 'body'}).top - 16;

	

	floatDiv("wardrobeTab", startY, maxHeight).float();

	

	$("div#wardrobeTab").hover(function(){

		$(this).addClass('hoverActive').find("img#wardrobeTabImage").attr('src', '/pws/images/tab_Wardrobe_1.gif');

	}, function(){

		if(!wardrobeOpen){

			$(this).removeClass('hoverActive').find("img#wardrobeTabImage").attr('src', '/pws/images/tab_Wardrobe_0.gif');

		}

	});



	$("input.clearValue").focus(function() {

		if( this.value == this.defaultValue ) {

			this.value = "";

		}

	}).blur(function() {

			if( !this.value.length ) {

				this.value = this.defaultValue;

			}

	});



	loadMiniBasket();

	loadWardrobePopout();

	

	if(typeof sIFR == "function"){

		sIFR.replaceElement(named({sSelector: "body h1.sifr", sFlashSrc: "/pws/flash/sifr.swf", sWmode: "transparent", sColor: "#082151"}));

		sIFR.replaceElement(named({sSelector: "body h1.sifr-light", sFlashSrc: "/pws/flash/sifr.swf", sWmode: "transparent", sColor: "#082151"}));

	}



	$("div#miniBasket").hoverIntent(function(){

		$("div#miniBasketDetail").slideDown("fast");

	}, function() {

		$("div#miniBasketDetail").slideUp("fast");

	});

	

	$("img.draggableImg").draggable({

		helper:'clone', 

		opacity:0.5,

		delay:10,

		zIndex:999

	}); 

	

	$("div#wardrobeTab,div#wardrobeContent").droppable({ 

		accept: "img.draggableImg",

		tolerance: 'touch',

		activeClass: 'hoverActive',

		hoverClass: 'hoverOver',

		activate:  function(ev, ui){

			$("img#wardrobeTabImage").attr('src', '/pws/images/tab_Wardrobe_1.gif');

			wardrobeOpen = true;

		},

		deactivate: function(ev, ui){

			$("img#wardrobeTabImage").attr('src', '/pws/images/tab_Wardrobe_0.gif');

			wardrobeOpen = false;

		},

		over: function(ev, ui){

			$("div#wdProductsWindow").css("borderLeft","1px solid #082151").css("borderRight","1px solid #082151");

		},

		out: function(ev, ui){

			$("div#wdProductsWindow").css("borderLeft","1px solid #B5D1E8").css("borderRight","1px solid #B5D1E8");

		},

		drop: function(ev, ui){

			var droppedProductId = $(ui.draggable).attr("id");

			var currentCookie = $.cookie('wardrobe');

			$("div#wdProductsWindow").css("borderLeft","1px solid #B5D1E8").css("borderRight","1px solid #B5D1E8");

			if(currentCookie === null || currentCookie === ''){

				$.cookie('wardrobe', droppedProductId , {expires: 31, path: '/'});

				loadWardrobePopout();																		

			}

			else{

				if(!currentCookie.match(droppedProductId)){

					$.cookie('wardrobe', currentCookie + ',' + droppedProductId , {expires: 31, path: '/'});

					loadWardrobePopout();																		

				}

			}

			wardrobeOpen = false;

		}

	});	

});







var setFloatInterval = false;

var setFloatTimeOut = false;



function stopFloat(){

	clearInterval(setFloatInterval);

}



function floatDiv(id, startY, maxHeight) {

	var element = $("#" + id);

	window[id + "_object"] = element;

	element.currentY = element.startY = startY;

	element.setPosition = function(newY){

		this.css('top', newY);

	}



	/* If you're not at the top of the page then move the wardrobe tab to viewable window */

	if ($(document).scrollTop() != 0) {

		element.currentY += ($(document).scrollTop() + element.startY - element.currentY);			

		element.setPosition(element.currentY);

	}



	element.float = function(){

		var pageY = $(document).scrollTop();

		this.currentY += (pageY + this.startY - this.currentY)/8;		

		

		if(this.currentY > maxHeight){

			this.currentY = maxHeight;

		}

		

		this.setPosition(this.currentY);

		

		$(window).scroll(function(){

			if(setFloatInterval != false){

				clearInterval(setFloatInterval);

				clearTimeout(setFloatTimeOut);

			}

			setFloatInterval = setInterval(id + "_object.float()", 40);

			setFloatTimeOut = setTimeout("stopFloat()", 4000);

		});		

	}

	return element;

}



function loadMiniBasket(){

	$.ajax({

		url: "/pws/ssnMiniBasketCount.jsp",

		cache: false,

		success: function(html){

			$("div#miniBasket div#miniBasketOverview p a").html(html);

		}

	});

	$.ajax({

		url: "/pws/ssnMiniBasketProducts.jsp",

		cache: false,

		success: function(html){

			$("div#miniBasket div#miniBasketDetail").html(html);

			miniBasketSliding();

			

			//Hide select boxes with an iframe for ie6

			//var mbd = $("div#miniBasketDetail");

			//if($.browser.msie && $.browser.version === "6.0"){

				//var iframe = $('<iframe id="minibasketIframe" src=""></iframe>');

				//mbd.prepend(iframe);

			//}

		}

	});

}







function miniBasketSliding() {

	var visibleProducts = 3;

	var totalProducts = $("div.mbProduct").length;

	var productHeight = 89;

	var productsWindowHeight = visibleProducts * productHeight;

	var totalProdSets = Math.ceil(totalProducts / visibleProducts);

	var currentProdSet = 1;

	var currentProdSetMargin = 0;



	if (totalProducts > visibleProducts){

		$("div.mbProductsControl").show();

		$("div#mbProductsWindow").css("height", productsWindowHeight);

		$("img#mbProductsNext").show().bind("click", function(){

			currentProdSet = currentProdSet + 1;

			currentProdSetMargin = currentProdSetMargin - productsWindowHeight;

			$("div#mbProductSet").animate({ 

				marginTop: currentProdSetMargin

			}, 500 );

			$("img#mbProductsPrev").show();

			if (currentProdSet == totalProdSets) {

				$("img#mbProductsNext").hide();

			}

			return false;

		});

		$("img#mbProductsPrev").bind("click", function(){

			currentProdSet = currentProdSet - 1;

			currentProdSetMargin = currentProdSetMargin + productsWindowHeight;

			$("div#mbProductSet").animate({ 

				marginTop: currentProdSetMargin

			}, 500 );

			$("img#mbProductsNext").show();

			if (currentProdSet == 1) {

				$("img#mbProductsPrev").hide();

			}

			return false;

		});

	}

}



function loadWardrobePopout(){

	

	var wardrobeTab = $('div#wardrobeTab');

	

	//Hide select boxes with an iframe for ie6

	if($.browser.msie && $.browser.version === "6.0"){

		var iframe = $('<iframe id="wardrobeIframe" src=""></iframe>');

		wardrobeTab.append(iframe);

	}

	

	var currentCookie = $.cookie('wardrobe');	

	if(currentCookie !== null && currentCookie != undefined && currentCookie != ''){

		productIds = currentCookie.split(",");

		wdProductCounter  = productIds.length;

		wdMaxPosition = (wdProductCounter -2)>0 ? wdProductCounter -2 : 0;

		html = "";



		for(i=0; i<wdProductCounter; i++){

			id = productIds[i].split("_");

			html = html + "<div class='wdProduct cf'><img src='/pws/images/catalogue/products/" + id[2] + "/small/" + id[2] + "_1.jpg' width='111' height='130' class='wardrobeImg'><a href='javascript:void(0);removeFromWardrobe(" + id[1] + ");'><img src='/pws/images/btn_wardrobeRemove.gif' width='48' height='28' class='wardrobeRemove'></a></div>";

		}

	}

	else{

		html = "<p>&nbsp;</p><p>Drag and drop products you like into this area.</p>"

	}

	$("div#wdProductSet").empty().html(html);



	wardrobeSliding();

}



function wardrobeSliding() {

	var visibleProducts = 2;

	var totalProducts = $("div.wdProduct").length;

	var productHeight = 140;

	if (totalProducts > 2){

		var productsWindowHeight = visibleProducts * productHeight;

	} else {

		var productsWindowHeight = totalProducts * productHeight;

	}

	var totalProdSets = Math.ceil(totalProducts / visibleProducts);

	var currentProdSet = 1;

	var currentProdSetMargin = 0;

	

	productsWindowHeight = productsWindowHeight === 0 ? 140 : productsWindowHeight;

	

	$("div#wdProductsWindow").css("height", productsWindowHeight);

	

	if (totalProducts > visibleProducts){



		$("div.wdProductsControl").show();

		$("img#wdProductsNext").show().bind("click", function(){

			currentProdSet = currentProdSet + 1;

			currentProdSetMargin = currentProdSetMargin - productsWindowHeight;

			$("div#wdProductSet").animate({ 

				marginTop: currentProdSetMargin

			}, 500 );

			$("img#wdProductsPrev").show();

			if (currentProdSet == totalProdSets) {

				$("img#wdProductsNext").hide();

			}

			return false;

		});

		$("img#wdProductsPrev").bind("click", function(){

			currentProdSet = currentProdSet - 1;

			currentProdSetMargin = currentProdSetMargin + productsWindowHeight;

			$("div#wdProductSet").animate({ 

				marginTop: currentProdSetMargin

			}, 500 );

			$("img#wdProductsNext").show();

			if (currentProdSet == 1) {

				$("img#wdProductsPrev").hide();

			}

			return false;

		});

	}

}





function removeFromWardrobe(prod_ID){

	var newVal = '';

	var newVal = '';

	var wardrobeCookie = $.cookie('wardrobe');

	var wardrobeCrumbs = new Array();

	wardrobeCrumbs = wardrobeCookie.split(",");



	if (wardrobeCrumbs.length > 1) {

		for(i=0; i<wardrobeCrumbs.length;i++) {

			CookieProd_ID = wardrobeCrumbs[i].split("_");

			CookieProd_ID = CookieProd_ID[1];

			if(CookieProd_ID != prod_ID){

				if(newVal.length > 0) newVal += ',';

				newVal += wardrobeCrumbs[i];

			}

		}

		$.cookie('wardrobe', newVal , {expires: 31, path: '/'});

	} else {

		$.cookie('wardrobe', null, { path: '/' });

	}

	$("div#wardrobeTab").toggleClass('hoverActive').find("img#wardrobeTabImage").attr('src', '/pws/images/tab_Wardrobe_0.gif');

	wdPosition = 0;

	loadWardrobePopout();																		

}



function printpage() {

	window.print();  

}







(function($){

	$.fn.pngfix = function(){

		var that = this;

		if(!($.browser.msie && $.browser.version === "6.0")){

			return $(this);

		}

		return this.each(function(i, n) {

			var imageSrc = $(n).attr("src");

			var imageHeight = $(n).attr("height");

			var imageWidth = $(n).attr("width");

			var imageFilter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=" + imageSrc + ", sizingMethod='crop')";

			$(n).wrap('<span class=""></span>');

			$(n).wrap('<span></span>');

			$(n).parent().parent().css({

				'filter': imageFilter,

				'height': imageHeight,

				'width': imageWidth,

				'display': 'block',

				'margin': '0'

			});

			$(n).parent().css('display','none');

		});

	};

})(jQuery);

/*
 * 	Easy Slider 1.7 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
/*
 *	markup example for $("#slider").easySlider();
 *	
 * 	<div id="slider">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="" /></li>
 *			<li><img src="images/02.jpg" alt="" /></li>
 *			<li><img src="images/03.jpg" alt="" /></li>
 *			<li><img src="images/04.jpg" alt="" /></li>
 *			<li><img src="images/05.jpg" alt="" /></li>
 *		</ul>
 *	</div>
 *
 */

(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			800,
			auto:			false,
			pause:			2000,
			continuous:		false, 
			numeric: 		false,
			numericId: 		'controls'
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var s = $("li", obj).length;
			var w = $("li", obj).width(); 
			var h = $("li", obj).height(); 
			var clickable = true;
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			$("ul", obj).css('width',s*w);			
			
			if(options.continuous){
				$("ul", obj).prepend($("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"));
				$("ul", obj).append($("ul li:nth-child(2)", obj).clone());
				$("ul", obj).css('width',(s+1)*w);
			};				
			
			if(!options.vertical) $("li", obj).css('float','left');
								
			if(options.controlsShow){
				var html = options.controlsBefore;				
				if(options.numeric){
					html += '<ol id="'+ options.numericId +'"></ol>';
				} else {
					if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
					html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
					html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
					if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';				
				};
				
				html += options.controlsAfter;						
				$(obj).after(html);										
			};
			
			if(options.numeric){									
				for(var i=0;i<s;i++){						
					$(document.createElement("li"))
						.attr('id',options.numericId + (i+1))
						.html('<a rel='+ i +' href=\"javascript:void(0);\">'+ (i+1) +'</a>')
						.appendTo($("#"+ options.numericId))
						.click(function(){							
							animate($("a",$(this)).attr('rel'),true);
						}); 												
				};							
			} else {
				$("a","#"+options.nextId).click(function(){		
					animate("next",true);
				});
				$("a","#"+options.prevId).click(function(){		
					animate("prev",true);				
				});	
				$("a","#"+options.firstId).click(function(){		
					animate("first",true);
				});				
				$("a","#"+options.lastId).click(function(){		
					animate("last",true);				
				});				
			};
			
			function setCurrent(i){
				i = parseInt(i)+1;
				$("li", "#" + options.numericId).removeClass("current");
				$("li#" + options.numericId + i).addClass("current");
			};
			
			function adjust(){
				if(t>ts) t=0;		
				if(t<0) t=ts;	
				if(!options.vertical) {
					$("ul",obj).css("margin-left",(t*w*-1));
				} else {
					$("ul",obj).css("margin-left",(t*h*-1));
				}
				clickable = true;
				if(options.numeric) setCurrent(t);
			};
			
			function animate(dir,clicked){
				if (clickable){
					clickable = false;
					var ot = t;				
					switch(dir){
						case "next":
							t = (ot>=ts) ? (options.continuous ? t+1 : ts) : t+1;						
							break; 
						case "prev":
							t = (t<=0) ? (options.continuous ? t-1 : 0) : t-1;
							break; 
						case "first":
							t = 0;
							break; 
						case "last":
							t = ts;
							break; 
						default:
							t = dir;
							break; 
					};	
					var diff = Math.abs(ot-t);
					var speed = diff*options.speed;						
					if(!options.vertical) {
						p = (t*w*-1);
						$("ul",obj).animate(
							{ marginLeft: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);				
					} else {
						p = (t*h*-1);
						$("ul",obj).animate(
							{ marginTop: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);					
					};
					
					if(!options.continuous && options.controlsFade){					
						if(t==ts){
							$("a","#"+options.nextId).hide();
							$("a","#"+options.lastId).hide();
						} else {
							$("a","#"+options.nextId).show();
							$("a","#"+options.lastId).show();					
						};
						if(t==0){
							$("a","#"+options.prevId).hide();
							$("a","#"+options.firstId).hide();
						} else {
							$("a","#"+options.prevId).show();
							$("a","#"+options.firstId).show();
						};					
					};				
					
					if(clicked) clearTimeout(timeout);
					if(options.auto && dir=="next" && !clicked){;
						timeout = setTimeout(function(){
							animate("next",false);
						},diff*options.speed+options.pause);
					};
			
				};
				
			};
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
			
			if(options.numeric) setCurrent(0);
		
			if(!options.continuous && options.controlsFade){					
				$("a","#"+options.prevId).hide();
				$("a","#"+options.firstId).hide();				
			};				
			
		});
	  
	};

})(jQuery);