$(document).ready(function(){
		$("#step2 select,#step2 input,#step3 select,#step3 input").attr("disabled","disabled")
		$("#step2,#step3").css("color","#C5C5C5")
		$("#quantity").css("border","1px solid #C5C5C5")
		
		$("#prod").change(function(){ prodChange() })
		
		function prodChange(){
			if($("#prod").val() != "default"){
				$("#step2").find(".step2-head-inactive").attr("class","step2-head-active")
				$("#step2").find(".step2-mid-inactive").attr("class","step2-mid-active")
				$("#step2").find(".step2-bottom-inactive").attr("class","step2-bottom-active")
				
				$("#step3").find(".step3-head-inactive").attr("class","step3-head-active")
				$("#step3").find(".step2-mid-inactive").attr("class","step3-mid-active")
				$("#step3").find(".step2-bottom-inactive").attr("class","step3-bottom-active")
				
				$("#step2 select,#step2 input,#step3 select,#step3 input").attr("disabled","")
				$("#step2,#step3,#quantity").removeAttr("style")
				
				$("#recalculate").attr("src","assets/images/recalculate.gif")
				
				$("#attributes").html("<li class='loading'><span>Loading...</span><img src='assets/images/ajax-loader.gif' alt='loading'></li>")
				
				var html = $.ajax({
					url: "ajax.asp?action=getprodattrs&product=" + $("#prod").val(),
					async: false,
					complete: function(){
						setTimeout(function(){
							$(".loading").fadeOut("fast",function(){
								$("#attributes").html(html)
								$("#attributes").hide()
								
								$("#attr_2 option[value='29']").attr("selected","selected")
								$("#attr_3 option[value='5']").attr("selected","selected")
								$("#attr_10 option[value='5']").attr("selected","selected")
								
								
								$("#attributes li").each(function(){
									$(this).find("select").change(function(){ attrChange($(this).attr("name"),$(this).val()) })
								})
								
								
								
								
								//IF NCR PADS
								if($("#prod").val() == "14"){
									$("#attr_35, #attr_39, #attr_43").parent().hide()
									$("#attr_36, #attr_40, #attr_44").parent().hide()
									$("#attr_32").change(function(){
										switch($(this).val()){
											case "1": // 2 parts
												$("#attr_35, #attr_39, #attr_43").parent().slideUp(200) //part 3
												$("#attr_36, #attr_40, #attr_44").parent().slideUp(200) //part 4
											break;
											case "2": // 3 parts
												$("#attr_35, #attr_39, #attr_43").parent().slideDown(200) //part 3
												$("#attr_36, #attr_40, #attr_44").parent().slideUp(200) //part 4
											break;
											case "3": // 4 parts
												$("#attr_35, #attr_39, #attr_43").parent().slideDown(200) //part 3
												$("#attr_36, #attr_40, #attr_44").parent().slideDown(200) //part 4
											break;
										}
									})
								}
								
								//IF WIRO BOUND BROCHURES
								if($("#prod").val() == "13"){
									$("#attr_26, #attr_28, #attr_29").parent().hide() //FRONT COVER
									$("#attr_46, #attr_47, #attr_48").parent().hide() //BACK COVER
									$("#attr_25").change(function(){
										if($(this).val() == "1"){
											$("#attr_26, #attr_28, #attr_29").parent().slideUp(200)
										}
										else {
											$("#attr_26, #attr_28, #attr_29").parent().slideDown(200)
										}
									})
									$("#attr_45").change(function(){
										if($(this).val() == "1" || $(this).val() == "4"){
											$("#attr_46, #attr_47, #attr_48").parent().slideUp(200)
										}
										else {
											$("#attr_46, #attr_47, #attr_48").parent().slideDown(200)
										}
									})
								}
								
								
								
								
								
								//IF BROCHURES (STITCHED)
								if($("#prod").val() == "6"){
									$("#attr_3, #attr_6, #attr_4").parent().hide()
									var attr12_opt1 = $("#attr_12 option[value='1']").clone()
									$("#attr_12 option[value='1']").remove()
									$("#attr_12 option[value='2']").attr("selected","selected")
									
									$("#attr_5").parent().after("<li id='wf_calc'>For a 4 page leaflet, please change the product type in step 1 to <strong><a href='index.php?p=5'>'leaflet'</a></strong></li>")
								}
								
								//IF BROCHURES (PERFECT BOUND)
								if($("#prod").val() == "11"){
									$("#attr_3, #attr_6, #attr_4").parent().hide()
								}
								
								
								//IF POSTERS
								if($("#prod").val() == "3"){
									$("#attr_9").parent().after("<li id='wf_calc'>Printed poster prices can also be found on the <br /><strong><a href='../products/posters'>Wide Format Calculator.</a></strong></li>")
								}
								
								$("#attr_5").change(function(){ bookletCoverChange( $("#attr_5").val() ); })
								
								$("#attributes").slideDown("slow")
								
								//INIT
								//IF LEAFLETS
								if($("#prod").val() == "5"){
									attrChange("attr_15","6")
								}
								else {
									attrChange("attr_15","1")
								}
								//END INIT
								
								
								$("#status-message").hide()
								
								function bookletCoverChange(value){
									if(value == 2){
										$("#attr_3, #attr_6, #attr_4").parent().slideDown("fast")
										$("#attr_12").prepend(attr12_opt1)
										$("#attr_12 option[value='1']").attr("selected","selected")
									}
									else {
										$("#attr_3, #attr_6, #attr_4").parent().slideUp("fast")
										$("#attr_12 option[value='1']").remove()
										$("#attr_12 option[value='2']").attr("selected","selected")
									}
								}
								
								$(".more-info").hover(function(){
									var moreinfo = $(this).parent().find(".attr-desc").text()	
									$(this).parent().append("<div class='tool-tip'><div class='tt-top'></div><div class='tt-mid'>"+moreinfo+"</div><div class='tt-bottom'></div></div>")
									$(this).parent().find(".tool-tip").hide().fadeIn(200)
	   							},function(){
		   							$(this).parent().find(".tool-tip").fadeOut(200,function(){
										$(this).remove()											   
									})
	    						})
								
							})
						},600)
					}
				}).responseText;
			}
			else {
				$("#step2").find(".step2-head-active").attr("class","step2-head-inactive")
				$("#step2").find(".step2-mid-active").attr("class","step2-mid-inactive")
				$("#step2").find(".step2-bottom-active").attr("class","step2-bottom-inactive")
				
				$("#step3").find(".step3-head-active").attr("class","step3-head-inactive")
				$("#step3").find(".step3-mid-active").attr("class","step2-mid-inactive")
				$("#step3").find(".step3-bottom-active").attr("class","step2-bottom-inactive")
			
				$("#step2 select,#step2 input,#step3 select,#step3 input").attr("disabled","disabled")
				$("#step2,#step3,#price").css("color","#C5C5C5")
				$("#recalculate").attr("src","assets/images/recalculate-bw.gif")
				$("#status-message").hide()
				$("#quantity").css("border","1px solid #C5C5C5")
				$("#attributes").html("<li><p class='no-attr-text'>Select A Product From Step 1.</p></li>")
				$("#prices").html("<tr><td colspan='2'><p class='no-attr-text'>Customise Your Product Using The Attributes In Step 2.</p></td></tr>")
				$("#price-options").remove()
				
			}
		}
		
		function attrChange(caller,callerVal){
			if(callerVal != "default"){
				var response = $.ajax({
					url: "ajax.asp?action=prodattrchange&product="+$("#prod").val()+"&attr="+caller+"&attrval="+callerVal,
					async: false
				}).responseText;
				response = eval(response);
				if(parseInt(response.attrstochange) > 0 ){
					for(i=1; i<=parseInt(response.attrstochange); i++){
						var html = eval("response.html" + String(i));
						var target = eval("response.target" + String(i));
						$("#"+target).html(html);
					}
					$("#status-message").html("<img src='assets/images/change-attribute.gif' alt='Attributes Have Changed' />").fadeIn("fast")
				}
				else {
					$("#status-message").fadeOut("fast")	
				}
				
				//IF POSTERS
				/*
				if($("#prod").val() == "3"){
					if( $("#attr_9").val() == "45"){
						if($("#bespoke-dimensions").length < 1){
							$("#attr_9").parent().append("<div id='bespoke-dimensions'><label for='bd_h'>Height:</label><input type='text' name='bd_h' id='bd_h' value='100'><label for='bd_w'>Width:</label><input type='text' name='bd_w' id='bd_w' value='100'></div>")
							$("#bespoke-dimensions").hide().slideDown(100)
						}
					}
					else {
						$("#bespoke-dimensions").slideUp(100,function(){ $(this).remove() })
					}
			}
			*/
				//DISABLE INPUT FOR AUTO CREASING
				var papervalues = ["22","21","15","16","17","18","23","24","25","26","27","28","29","30","31","32","33","34"];
				var pagevalues = ["3","4","5","8","9","10","11","12","13","14","15","16"];
				
				var autocreasing1;
				var autocreasing2;
		
				
				for(var i=0; i<=pagevalues.length-1; i++){
					if( $("#attr_15").val() == pagevalues[i] ){
						autocreasing1 = 1;
					}
				}
				
				for(var i=0; i<=papervalues.length-1; i++){
					if( $("#attr_16").val() == papervalues[i] ){
						autocreasing2 = 1;	
					}
				}
				
				if(autocreasing1 == 1 && autocreasing2 == 1){
					$("#attr_7").find("option[text='No Crease']").remove()
					autocreasing1 = 0;
					autocreasing2 = 0;
				}
				else {
					var nocreaseopt = $("#attr_7").find("option[text='No Crease']")
					var prod = $("#prod").val()
					if(nocreaseopt.length == 0 && prod !== "14" && prod !== "7"){ //prod 14 and 7 do not want to have the no crease option
						$("#attr_7").append("<option value='3' selected='selected'>No Crease</option>");
						//calculatePrice();
					}
				}
				//END OF DISABLE INPUT CODES
				
	
				calculatePrice()
			}
		}
		 
		function calculatePrice(){
			
			$.post("formula.asp", $("#printwizardform").serialize(), function(data){ 
				$("#price-options").fadeOut(200,function(){
					$(this).remove();
				})
				$("#prices").fadeOut(200,function(){
					
					//DEBUG CODE
					//$("#debug-info").remove()
					
					
					$("#prices").html(data)
					$("#price_0").attr("checked","checked")
					$("#prices").fadeIn(200)
					
					if( $("#prod").val() == "3" && $("#f_used").val() == "wideformat"){
						$("#attr_16").html("<option value='33'>220gsm Silk</option>")
						$("#status-message").fadeIn("slow")
					}
					//DEBUG CODE
				   	// $("body").append( $("#debug-info") )
					//$("#prices").find("#debug-info").remove()
					
					$.post("pricecalculatorprocess.asp", $("#printwizardform").serialize(), function(data){
						$("#price-options").html(data);
						$(".step3-mid-active").append( $("#price-options") );
						$("#prices").find("#price-options").remove();
						$("#price-options").fadeIn(200);
						
						
						$("#prices input[type='radio']").click(function(){									
							$("#price-options").fadeOut(200,function(){
								$.post("pricecalculatorprocess.asp", $("#printwizardform").serialize(), function(data){ 
									$("#price-options").html(data)
								})
								$("#price-options").fadeIn(200)
							})
						})
					})
				})
			});
		}
		
		$("#recalculate").click(function(){
			calculatePrice()
		})
		
		prodChange();
		
		//SLIDEMENU CODE
		$("#slidemenu a").click(function(){
		$(this).parent().parent().find(".active").removeClass("active");
		$(this).addClass("active");
		
		var i = $("#slidemenu a").index(this);
		
		switch(i){
			case 0:
				itemIndex = 5;
			break;
			case 1:
				itemIndex = 10;
			break;
			case 2:
				itemIndex = 12;
			break;
			case 3:
				itemIndex = 6;
			break;
			case 4:
				itemIndex = 11;
			break;
			case 5:
				itemIndex = 13;
			break;
			case 6:
				itemIndex = 3;
			break;
			case 7:
				itemIndex = 4;
			break;
			case 8:
				itemIndex = 2;
			break; 
			case 9:
				itemIndex = 1;
			break;
			case 10:
				itemIndex = 9;
			break;
			case 11:
				itemIndex = 7;
			break;
			case 12:
				itemIndex = 8;
			break;
			case 13:
				itemIndex = 14;
			break;
		}
		
				
		
		$("#prod").val(itemIndex);
		prodChange();

	})
		
})
