$(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 BOOKLETS
								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")
									
								}
								
								//IF LEAFLETS
								if($("#prod").val() == "6"){
									
								}
								
								$("#attr_5").change(function(){ bookletCoverChange( $("#attr_5").val() ); })
								
								$("#attributes").slideDown("slow")
								
								attrChange("attr_15","1")
								
								$("#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>")
				
			}
		}
		
		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() })
					}
			}
			*/
				calculatePrice()
			}
		}
		
		function calculatePrice(){
			$.post("formula.asp", $("#printwizardform").serialize(), function(data){ 
				$("#prices").fadeOut(200,function(){
					
					/*DEBUG CODE
					$("#debug").remove()
					*/
					
					$("#prices").html(data)
					$("#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( $("#prices").find("#debug") )
					$("#prices").find("#debug").remove()
					*/
				})
			});  
		}
		
		$("#recalculate").click(function(){
			calculatePrice()
		})
})