/* Catalogue JavaScript */ 


$(function(){
	

	/* Collect all product data (from non JavaScript table)
	************************************************************/
	var products = new Array();
	$("#non-script-colour-size table tbody tr").each(function (i) {
		products[i] = {
			colour : jQuery.trim( $(this).find(".colour").text() ),
			colourID : $(this).find(".colour input").val(),
			size : jQuery.trim( $(this).find(".size").text() ),
			sizeID : $(this).find(".size input").val(),
			sequence : $(this).find(".size input").attr("tabindex"),
			m : $(this).attr("m"),
			l : $(this).attr("l"),
			h : $(this).attr("h"),
			soldOut : $(this).find(".soldout").length > 0
		};
	});
	// DEBUG - show all product data
	/*$("body").prepend("<div id='DEBUG' style='background:yellow; padding:1em; color:red; margin-top:2em;'></div>");
	for( i in products ) {
		$("#DEBUG").prepend("("+i+") - colour["+products[i].colour+"]["+products[i].colourID+"] size["+products[i].size+"]["+products[i].sizeID+"] sequence["+products[i].sequence+"] m["+products[i].m+"] l["+products[i].l+"] h["+products[i].h+"]  soldOut["+products[i].soldOut+"]<br/>");
	}*/
	
	
	
	/* Generate Colour/Size Dropdown lists
	************************************************************/
	$("#js-colour-size").show();
	for( i in products ) {
		
		// Generate colour options (only for unique colours)
		var unique = true;
		$("#ColourId option").each(function () {
			if( products[i].colour == products[$(this).attr('id')].colour ) unique = false;
		});
		if( unique ) {
			var preSelected = "";
			if( START_COLOUR != null && START_COLOUR == products[i].colour ) preSelected = "selected='selected'";
			$("#ColourId").append("<option value='"+products[i].colourID+"' id='"+i+"' "+preSelected+" >"+products[i].colour+"</option>");
		}
		
		// Generate size options (only for unique sizes, in sequence order)
		var unique = true;
		var before = null;
		$("#SizeId option").each(function () {
			if( products[i].size == products[$(this).attr('id')].size ) unique = false;
			if( Number(products[$(this).attr('id')].sequence) <= Number(products[i].sequence) ) before = $(this);
		});
		if( unique ) {
			var optionHTML = "<option value='"+products[i].sizeID+"' id='"+i+"'>"+products[i].size+"</option>";
			if( $("#SizeId option").length <= 0 ) {
				$("#SizeId").append(optionHTML);
			} else if( before == null ) {
				$("#SizeId option:first").before(optionHTML);
			} else {
				before.after(optionHTML);
			}
		}
		
	}
	// Mark sold out colours
	$(".colour option").each(function () {
		var soldOutColour = true;
		for( i in products ) {
			if( products[$(this).attr('id')].colour == products[i].colour && !products[i].soldOut ) {
				soldOutColour = false;
				break;
			}
		}
		if( soldOutColour ) {
			$(this).append(" - Sold Out");
		}
	});
	
	
	/* Colour dropdown change
	************************************************************/
	$(".colour select").change(function () {
		var selectedProduct = products[$(".colour option:selected").attr('id')];
		//alert( IMAGE_PATH + "/" + selectedProduct.m );
		$(".product-image img").attr( "src", IMAGE_PATH + "/" + selectedProduct.m );
		// DUMMY TEST */ $(".product-image img").attr( "src", IMAGE_PATH + "/191853_"+ selectedProduct.colour +"_m.jpg" );
		$(".product-image").attr("href", ZOOM_LINK + "&Colour=" + selectedProduct.colour + "&l=" + selectedProduct.l + "&h=" + selectedProduct.h   );
		disableSizes( selectedProduct.colour )
		$(".size select").change();
	}).change();
	
	// Mark size as sold out or unavailable for a particluar colour 
	function disableSizes( colour ) {
		$(".size option").each(function () {
			$(this).attr("soldout","false").attr("unavailable","true");
			for( i in products ) {
				if( products[i].colour == colour && products[i].size == products[$(this).attr("id")].size ) {
					if( products[i].soldOut ) $(this).attr("soldout","true")
					$(this).attr("unavailable","false")
					break;
				}
			}
			var text = products[$(this).attr("id")].size;
			if( $(this).attr("unavailable") == "true" ) {
				text += " - Unavailable";
			} else if( $(this).attr("soldout") == "true" ) {
				text += " - Sold Out";
			}
			$(this).text(text);
		});
	}
	
	
	/* Size dropdown change
	************************************************************/
	$(".size select").change(function () {
		if( $(".size option:selected").attr('soldout') == "true" || $(".size option:selected").attr('unavailable') == "true" ) {
			$("#OMQuantity, #js-colour-size input:submit").attr("disabled","disabled").addClass("disabled");
		} else {
			$("#OMQuantity, #js-colour-size input:submit").attr("disabled","").removeClass("disabled");
		}
	}).change();

		   
	
	/*	 Generate alternate image view links
	***********************************************************/
	if( ALTERNATE_NAMES[0] != "" ) {
		var altString = "Alternate View: ";
		//alert( ZOOM_LINK );
		for( i in ALTERNATE_NAMES ) {
			//altString += " <a onclick='popup(this.href,600,680); return false;' target='_blank' href='"+ZOOM_LINK+"&Colour="+ALTERNATE_NAMES[i]+"'>"+ALTERNATE_NAMES[i]+"</a>"
			altString += " <a onclick='popup(this.href,600,680); return false;' target='_blank' href='"+ZOOM_LINK+"&Colour="+ALTERNATE_NAMES[i]+"&l="+LINE+"_"+ALTERNATE_NAMES[i]+"_l.jpg&h="+LINE+"_"+ALTERNATE_NAMES[i]+"_h.jpg'>"+ALTERNATE_NAMES[i]+"</a>"
			if( ALTERNATE_NAMES.length > Number(i+1) ) altString += ",";
		}
		$(".alternate-view").append( altString );
	}
	
	
	

});