/* Catalogue Script for Smiggle */


//jQuery
$(function(){
	
	//Alternate view rollover
	var originalM = "";
	var altM = $(".alternate-view a").attr("href");
	$(".alternate-view a").removeAttr("href")
	$(".alternate-view a").hover(
      function () {
		originalM = $(".product-image img").attr("src");
        $(".product-image img").attr("src",altM);
      }, 
      function () {
        $(".product-image img").attr("src",originalM);
      }
    );


	//wishlist
	if( $("#productform").size() > 0 ) {
		$("dl").after("<button id='addwish'>add to wishlist</button><br/><br/>");
		$("#addwish").click(function () {
			
			var wishlistData = $.JSONCookie('wishlist');
			if( wishlistData.products == undefined ) wishlistData.products = [];
			
			if( wishlistData.products.length >= 12 ) {
				alert( "Sorry you you alredy have 12 items in you wishlist. Please remove some if you want to add this item." );
			} else {
				var name = $("h1").text();
				var line = $("dl dd:eq(0)").text();
				var price = $("dl dd:eq(1)").text();
				var colour = jQuery.trim( $("#selectcolour option:selected").text() );
				var link = window.location.href;
				var colourIndex = link.indexOf("&colour=");
				if( colourIndex > -1 ) link = link.substring(0,colourIndex);
				var obj = { 'name': name,
							'line': line,
							'price': price,
							'colour': colour,
							'link': link };
				
				var alreadyInWishlist = false;
				for( i in wishlistData.products ) {
					if(  wishlistData.products[i].line == line && wishlistData.products[i].colour == colour ) {
						alreadyInWishlist = true;
					}
				}
				if( alreadyInWishlist ) {
					alert("This product is alredy in your wishlist.");
				} else {
					wishlistData.products.push(obj);
				}
	
				$.JSONCookie('wishlist', wishlistData, {path: '/'});
			}
			window.location = "/tem/wishlist.tem";
		});
	}
	
	if( $("body#wishlist").size() > 0 ) {
		
		var wishlistData = $.JSONCookie('wishlist')
		
		//alert( wishlistData.products );

		if( wishlistData.products != undefined ) {
			//$(".empty-list").hide();
			$(".full-list").show();
			
			for( i in wishlistData.products ) {
				var HTML = "";
				HTML += '<div class="thumb">';
				if( wishlistData.products[i].colour != "" ) {
					HTML += '<a href="'+wishlistData.products[i].link+'&colour='+wishlistData.products[i].colour+'">';
					HTML += '	<img src="/Images/Catalog/ProductImages/'+wishlistData.products[i].line+'_'+wishlistData.products[i].colour+'_m.jpg" alt="" />';
				} else {
					HTML += '<a href="'+wishlistData.products[i].link+'">';
					HTML += '	<img src="/Images/Catalog/ProductImages/'+wishlistData.products[i].line+'_m.jpg" alt="" />';
				}
				HTML += '	<div class="text">';
				HTML += '		<div class="name">'+wishlistData.products[i].name+'</div>';
				HTML += '		<div class="line">'+wishlistData.products[i].line+'</div>';
				HTML += '		<div class="colour">'+wishlistData.products[i].colour+'</div>';
				HTML += '		<div class="price">'+wishlistData.products[i].price+'</div>';
				HTML += '	</div>';
				HTML += '</a><br/>';
				HTML += '<button>Remove</button>';
				HTML += '</div>';
				$(".thumblist").prepend(HTML);
			}
				
			$("button").click(function () {
				line = $(this).siblings("a").find(".line").text();
				colour = $(this).siblings("a").find(".colour").text();
				//alert( line );
				$(this).parents(".thumb").remove();
				var wishlistData = $.JSONCookie('wishlist')
				for( i in wishlistData.products ) {
					if(  wishlistData.products[i].line == line && wishlistData.products[i].colour == colour ) {
						wishlistData.products.splice(i,1);
					}
				}
				$.JSONCookie('wishlist', wishlistData, {path: '/'});
			});
		
		} else {
			$(".empty-list").show();
			//$(".full-list").hide();
		}
			
	}
	
	
});