// JavaScript Document
	$(document).ready(function()
	{
/*		var zIndexNumber = 0;
		$('div').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber += 1;
		});
	*/	
	
		$("#fabric_selection").draggable(
		{
			appendTo: 'body',
			handle: '#largeFabric_topbar'
		});

		//Move popup fabric divs to body to fix IE6 z-index / stacking issues
		$("body").append($("#largefabric") );
		$("body").append($("#fabric_selection") );

		$('.gallery a').lightBox({fixedNavigation:true});


   });

	var previousSelection = null;
	var fabricForWhat = "curtain";




	function orderSample(name, thumb_src)
	{
		document.form1.doWhat.value = "add_sample";
		document.form1.thingName.value = name;
		document.form1.action='kidscurtaincompany_shop.php';
		document.form1.thumbnail_src.value = thumb_src;
		document.form1.submit();
	}


	function ajax_loadFabrics(searchText, NO_LONGER_USED, pageNum, mode, NO_LONGER_USED, material_type)
	{

		$("#loading").css("display", "block");

		selectedColour = "";
		selectedTopic = "";
		selectedCollection = "";

		if (typeof(material_type) == "undefined")
			material_type = "fabric";

		if (typeof(mode) == "undefined" ||  mode == "" ||  mode == "sample")
		{

			selectedColour = $("#cmbColour").val(); //document.form1.cmbColour.value;
			selectedTopic = $("#cmbTopic").val();//document.form1.cmbTopic.value;
			selectedCollection = $("#cmbCollection").val();//document.form1.cmbCollection.value;
		}/*
		else if (mode == "collection")
		{
			selectedCollection = document.form1.collectionName.value;
		}*/

	//	alert("selectedColour is " + selectedColour);
		
		
		xmlHttp=GetXmlHttpObject();
		
		if (xmlHttp==null)
		{
			return;
		}
		
		if (pageNum == null)
			pageNum = 1;
		var results_per_page = -1;
		if (typeof(document.form1.results_per_page) != "undefined")
		{
			results_per_page = document.form1.results_per_page.value;
//			alert(document.form1.results_per_page.value);
			
		}


		var url="iwa/include/ajax/fabric.php?material_type=" + material_type + "&mode=" + mode +"&selectedCollection=" + selectedCollection + "&searchText=" + searchText + "&selectedColour=" + selectedColour + "&selectedTopic=" +  selectedTopic + "&pageNum=" + pageNum + "&user_chosen_results_per_page=" + results_per_page;
//alert(url);
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	
	}













	function displayValance(obj)
	{

		var valance_edit = MM_findObj("valance_show_if_adding");

		if (obj.checked)
		{
			copyFabric("curtain", "valance");
			$("#valance_show_if_adding").slideDown("slow");

		}
		else
		{
			$("#valance_show_if_adding").slideUp("slow");
		}
			
			
		curtain("form1");
			
	}
	function displayTiebacks(obj)
	{

		var tiebacks_edit = MM_findObj("tiebacks_show_if_adding");

		if (obj.checked)
		{
			copyFabric("curtain", "tiebacks");
			$("#tiebacks_show_if_adding").slideDown("slow");
		}
		else
		{
			$("#tiebacks_show_if_adding").slideUp("slow");
		}
			
			
		curtain("form1");
			
	}




	function displayFabricSelection(fabricFor)
	{

		$("#pagefade").css("z-index", "1000000");
		$("#fabric_selection").css("z-index", "1000001");
		$("#largefabric").css("z-index", "1000002");

		$("#pagefade").css("opacity", "0.2");
		$("#pagefade").fadeIn("fast");
		
		$("#fabric_selection").fadeIn("fast");
		
		fabricForWhat = fabricFor;

//		var fabric_selection = MM_findObj("fabric_selection");

//		fabric_selection.style.display = "block";
		
//		showHideComboBoxes("none");
		
		
			
		
	}
	
	function showHideComboBoxes(showOrHide)
	{
		//Hide any combo boxes, depending on what product we are on
		switch (fabricForWhat)
		{
			case "curtain":
				document.form1.liningc.style.display = showOrHide;
				break;
			case "romanblind":
			case "rollerblind":
				document.form1.blind_position.style.display = showOrHide;
				document.form1.lining.style.display = showOrHide;
				document.form1.chain.style.display = showOrHide;
				document.form1.chain_position.style.display = showOrHide;
				break;
			case "duvet":
				document.form1.duvet_size.style.display = showOrHide;
				break;
			
			
			
		}
		
	}

	function copyFabric(sourceFabricForWhat, destinationFabricForWhat)
	{
		var objInfoName_Source = MM_findObj("build_panel_material_info_name_" + sourceFabricForWhat);
		var objInfoPrice_Source = MM_findObj("build_panel_material_info_price_" + sourceFabricForWhat );

		var objInfoName_Dest = MM_findObj("build_panel_material_info_name_" + destinationFabricForWhat);
		var objInfoPrice_Dest = MM_findObj("build_panel_material_info_price_" + destinationFabricForWhat );

		objInfoName_Dest.innerHTML = objInfoName_Source.innerHTML;
		objInfoPrice_Dest.innerHTML = objInfoPrice_Source.innerHTML;

		eval("document.form1.fabricID_" + destinationFabricForWhat + ".value = document.form1.fabricID_" + sourceFabricForWhat + ".value;");
		eval("document.form1.fabricName_" + destinationFabricForWhat + ".value = document.form1.fabricName_" + sourceFabricForWhat + ".value;");
		eval("document.form1.fabricCostPerM_" + destinationFabricForWhat + ".value = document.form1.fabricCostPerM_" + sourceFabricForWhat + ".value;");
		eval("document.form1.fabricRepeat_" + destinationFabricForWhat + ".value = document.form1.fabricRepeat_" + sourceFabricForWhat + ".value;");

		eval("document.form1.fabricImage_" + destinationFabricForWhat + ".src = document.form1.fabricImage_" + sourceFabricForWhat + ".src;");


	}



	function hideFabricSelection(fabricForWhat)
	{
//		alert("hide");
		//Hide the fabric selection panel
//		var fabric_selection = MM_findObj("fabric_selection");
	//	fabric_selection.style.display = "none";
		
		showHideComboBoxes("block");
		
		$("#fabric_selection").fadeOut("fast");
		$("#pagefade").fadeOut("fast");
			
		
	}

	function selectHeading(obj, code, name, priceUnlined, priceLined)
	{

		//Store values for use by curtain calculator
		document.form1.headingCode.value = code;
		document.form1.headingName.value = name;
		document.form1.headingPriceUnlined.value = priceUnlined;
		document.form1.headingPriceLined.value = priceLined;

		var objInfo = MM_findObj("build_panel_heading_info");
		objInfo.innerHTML = name;		


		//Calculate
		curtain("form1");
	}



	
	
	function enlargeFabric(obj, title, src, pricePerM, specNames, specValues, itemID, specCollection, material_type, repeat)
	{

		$("#fabric_selection").css("z-index", "1000000");
		$("#pagefade").css("z-index", "1000001");
		$("#largefabric").css("z-index", "1000002");
		$("#largefabric").css("position", "absolute");
  
		$("#pagefade").css("opacity", "0.2");
		$("#pagefade").fadeIn("fast");
		$("#largefabric").fadeIn("fast");
		

		var enlargePanel = MM_findObj("largefabric");
		var enlargePanelTitle = MM_findObj("largeFabric_title");
		var enlargePanelImage = MM_findObj("largeFabric_image");
		var enlargePanelSpec = MM_findObj("largeFabric_spec");
		var enlargePanelOrderSample = MM_findObj("largeFabric_orderSample");



		//Get page top of fabric to be enlarged and set enlargement to be near it
		var objPos = findPos(obj);
	//	var verticalPos = objPos[0] - 380;
		
//		enlargePanel.style.top = (verticalPos + "px");

		//enlargePanel.style.display = "block";
		enlargePanelImage.src = src;


		
//			enlargePanelOrderSample.innerHTML += "<br/><br/><a id='btn_popup_view_collection' href='#'><img class='button_image'  src='iwa/images/buttons/view_collection.png' /></a>";



		
		specText = "<table border='0'>";
		quantity_type_text = "";
		display_name = material_type;
		
		switch (material_type)
		{
			case "fabric":
				specText += "  <tr><td>Price:</td><td>&pound;" + pricePerM + " per metre</td></tr>";
				quantity_type_text = " cm"; 
				quantity_default = 100;
				break;
			case "wallpaper":
				specText += "  <tr><td>Price:</td><td>&pound;" + pricePerM + " per roll</td></tr>";
				quantity_type_text = " rolls";
				quantity_default = 1;
				break;
			case "pvc":
				specText += "  <tr><td>Price:</td><td>&pound;" + pricePerM + " per metre</td></tr>";
				display_name = "PVC Fabric";
				quantity_type_text = " cm"; 
				quantity_default = 100;
				break;
		}


	
		hasCollection = false;
		
		if (specNames == -1)
		{
			$.ajax({
				url: 'iwa/include/ajax/retrieve_specs.php',
				data: "itemID=" + itemID,
				cache: false,
				async:   false,
			  
				success: function(response) 
				{
					specText += response;
//					alert(response);
				},
				error: function(data)
				{
					alert("Error retrieving item specs: " + data.status);
				}
			  
			});			
			
			if (specCollection != "")
			{
				collectionName = specCollection ;
				hasCollection = true;
			}
			
			
		}
		
		specText += "</table>";

		enlargePanelSpec.innerHTML = specText;

		enlargePanelOrderSample.innerHTML = "<img onclick='orderSample(\"" + title + "\", \"" + src + "\");' class='button_image'  src='iwa/images/buttons/order_sample_of_this_fabric.png' />";



		$(".largeFabric_toptext").html();

		if (hasCollection)
			$(".largeFabric_toptext").html("<span>Part of the '<strong>" + collectionName + "'</strong> collection.</span><a id='btn_popup_view_collection' href='#'><img class='button_image'  src='iwa/images/buttons/view_collection.png' /></a>");

		$("#largeFabric_title").html(title);

		if (fabricForWhat == "sample")
		{
			$(".largeFabric_choose").css("display", "none");

			$(".largeFabric_buy").css("display", "block");
			$(".largeFabric_buy_text").html("Buy this " + display_name + "");
			
			
			$(".largeFabric_buy_quantity_type").html(quantity_type_text);
			$("#largeFabric_buy_quantity").val(quantity_default);
			
			
		}
		else
		{
			$(".largeFabric_buy").css("display", "none");

			$(".largeFabric_choose").css("display", "block");
			$(".largeFabric_choose_text").html("Choose this " + display_name + " for your " + fabricForWhat + ".");
			
			
			
		}
/*
		if (select_action == "choose")
		{
			$("#largeFabric_choose").css("display", "block");
			$("#largeFabric_buy").css("display", "none");
		}
		if (select_action == "buy")
		{
			$("#largeFabric_choose").css("display", "none");
			$("#largeFabric_buy").css("display", "none");
		}*/
		


		$("#btn_popup_view_collection").click ( function ()
		{
			var comboCol = $("#cmbCollection");
			comboCol.val(collectionName);
			
			$("#largefabric").fadeOut("fast");
			
			
			if ($("#fabric_selection").css("display") != "block")
			{
				$("#pagefade").fadeOut("fast");
				
			}
//			$("#pagefade").fadeOut("fast");

			$("#pagefade").css("z-index", "1000001");
			$("#fabric_selection").css("z-index", "1000002");


			ajax_loadFabrics($("#searchText").val(), -1, 0, '');
			
			
		});
		
		$(".largeFabric_choose img").click ( function ()
		{
			//alert("choose material");
			

			$("#largefabric").fadeOut("fast");
			$("#fabric_selection").fadeOut("fast");
			$("#pagefade").fadeOut("fast");

			selectFabric(obj, itemID, title, pricePerM, repeat, src);

			
		}); 


		$(".largeFabric_buy img").click( function()
		{

			var length = parseFloat($("#largeFabric_buy_quantity").val());

			document.form1.action='kidscurtaincompany_shop.php';
			document.form1.doWhat.value='add';
			
			
			document.form1.thumbnailFile.value = src;

			$("#optionTypeID_1").val(title + ",0.00,Type,0");
			
			if (material_type == "fabric")
			{
				$("#optionTypeID_2").val($("#largeFabric_buy_quantity").val() + "cm,0.00,Length,0");
				document.form1.quantity.value = "1";
				document.form1.rrp.value = (pricePerM /100) * length;
			}
			else if(material_type == "wallpaper")
			{
				$("#optionTypeID_2").val("");
				document.form1.quantity.value = $("#largeFabric_buy_quantity").val();
				document.form1.rrp.value = pricePerM; // * length;
			}
			else if(material_type == "pvc")
			{
				$("#optionTypeID_2").val($("#largeFabric_buy_quantity").val() + "cm,0.00,Length,0");
				document.form1.quantity.value = "1";
				document.form1.rrp.value = (pricePerM /100) * length;
			}

//			document.form1.quantity.value = $("#largeFabric_buy_quantity").val();
			document.form1.submit();
			
			
		});




		$("#largefabric").draggable(
		{
			appendTo: 'body',
			handle: '.largeFabric_topbar'
		});

		
//		alert(specNames);

	}
	function hideLargeFabric()
	{
	//	var enlargePanel = MM_findObj("largefabric");
//		enlargePanel.style.display = "none";


		if ($("#fabric_selection").css("display") != "block")
		{
			$("#pagefade").fadeOut("fast");
			
		}
		$("#largefabric").fadeOut("fast");

		$("#pagefade").css("z-index", "1000001");
		$("#fabric_selection").css("z-index", "1000002");

		
	}



	function displayHelpAjax(objHelpButton, helpCode)
	{

		//Position help box
		pos = findPos(objHelpButton);
		newTop = pos[0] + 10;
		newLeft = pos[1] + 30;
		
		var help_obj = MM_findObj("help");
		help_obj.style.top = parseInt(newTop) + "px"
		help_obj.style.left = parseInt(newLeft) + "px"
		help_obj.style.display = "block";				



		xmlHttp=GetXmlHttpObject();
		
		if (xmlHttp==null)
			return;
		
		var url="iwa/include/ajax/displayHelp.php?itemID=" + document.form1.thingID.value + "&helpCode=" + helpCode;
//alert(url);
		xmlHttp.onreadystatechange=ajax_response_displayHelp;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);		
		
	}

	function ajax_response_displayHelp() 
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			var help_obj = MM_findObj("help");
			var helptext_obj = MM_findObj("buildhelp_text");
			
			helptext_obj.innerHTML = xmlHttp.responseText;

			
		} 
	}




	function findPos(obj)
	{
		var atop = 0;
		var aleft = 0
	
		do 
		{
			atop += obj.offsetTop;
			aleft += obj.offsetLeft;
			
			
		}
		while (obj = obj.offsetParent)
		
		return [atop, aleft]
		
		
	}


	function displayHelp(obj, topOffset, helpText)
	{
		var help_obj = MM_findObj("help");
		var helptext_obj = MM_findObj("buildhelp_text");
		
		
		helptext_obj.innerHTML = helpText;
		
		help_obj.style.top = parseInt(topOffset) + "px"
		
/*		help_obj.style.top = (parseInt(obj.style.top) + 20) + "px";
		help_obj.style.left = (parseInt(obj.style.left) + 20) + "px";
*/
		help_obj.style.display = "block";
		

	}
	
	function hideHelp(obj)
	{
		var help = MM_findObj("help");
		help.style.display = "none";
	}
	
