
		$(document).ready(function(){
		
			// Show step one onload
				advance_step(1);
				
				// Cookies
				var x_ctype = $.cookie('x_ctype');
				var x_cstate = $.cookie('x_cstate');
				var	cat_trail  = $.cookie('cat_trail');
				if(x_ctype)	{
					$('input[value="'+x_ctype+'"]').attr('checked', 'checked');
				}
		
				if(cat_trail)	{
					$('input[value="'+cat_trail+'"]').attr('checked', 'checked');
				}
				
				if(x_cstate != undefined)	{
						$('input[value="'+x_cstate+'"]').attr('checked', 'checked');
				}
				
					
				update_product_name();
				
				if(cat_trail != undefined) {
					p =  $("input[name='cat_trail']:checked").parent('li').parent('ul').toggle();
				}
				
				

			// Handles checkboxes in IE
			$(function () {
			    if ($.browser.msie) {
			        $('input:checkbox').click(function () {
			            this.blur();
			            this.focus();
			        });
			        $('input:radio').click(function () {
			            this.blur();
			            this.focus();
			        });
			    }
			});
		});	
		
		
		function validate_step1() {
			x_ctype = get_ctype();
			
			if(x_ctype == undefined) {
				$('#step1_page .product_error').html('<div class="error">Please select one to get started.</div>');
				// history.back(-1);
				window.location="#step1";
		  	return false;
		  }
			
			$.cookie('x_ctype', get_ctype(), { expires: 1 });
	
			advance_step(2);
		}
		
		function validate_step2() {
			x_cat_trail = get_cat_trail();
			
			if(x_cat_trail == undefined) {
				$('#step2_page .product_error').html('<div class="error">Please select a Product.</div>');
				window.location="#step2";
		  	return false;
		  }
			
			$('#step4_page .category_name').html('');
			
			$.cookie('cat_trail', get_cat_trail(), { expires: 1 });
									
			advance_step(3);
		}
		
		function validate_step3() {
			x_ctype = get_ctype();

			
			//if(x_ctype == "wholesale") {
			//		var x_cstate = ($("#select2").val() || []).join(', '); 
			//} else {
				var x_cstate = $("#cstate_r :selected").val();
			//}
					
			
			if(x_cstate == undefined) {
				$('#step3_page .product_error').html('<div class="error">Please select a State.</div>');
				// history.back(-1);
				window.location="#step2";
		  	return false;
		  }
		  
			$.cookie('x_cstate', x_cstate, { expires: 1 });		
			advance_step(4);
		}
		
		
		$(function () {		
				$(window).bind('hashchange', function () {
					
					  // do some magic
					  var url = $.bbq.getState( "hash" );
					  var hash = window.location.hash || '#step1';
					  
					  if(hash == "#")
					  	hash = '#step1';
					  switch(hash) {
					  	case "#step1":
								  advance_step(1);
							break;  	
					  	
					  	case "#step2":
								  validate_step1();
							break;
							
					  	case "#step3":
								  validate_step2();
							break;
							
							case "#step4":
								  validate_step3();
							break;
							
							case "#step5":
					  			//$.cookie('cat_trail', get_cat_trail(), { expires: 1 });
									// advance_step(5);
							break;
							
							case "#learn_more":
							$.scrollTo($('.bottom_info'), 800);
							return false;
							break;
					  }
				});
				
			  $(window).trigger("hashchange");
		});
		
		
		
			function update_product_name() {	
				if($("input[name='cat_trail']:checked").val() == undefined)
					return false; 
					
					p =  $("input[name='cat_trail']:checked").next().attr('id');
					
				 	p = p.split(" - ");
					if(p.length == 2)
						product_name = $("input[name='cat_trail']:checked").next().html();
					else 
						product_name = p[1] + " - " + p[2];
						
					$('#lumber_product').html('for '+product_name);
			}
				
		
		function get_ctype() {
			return $("input[name='ctype']:checked").val();
		}
			
		function get_cat_trail() {
			return $("input[name='cat_trail']:checked").val();
		}
		function get_product_name() {
			 if (!$("input[name='cat_trail']:checked").length)
						window.location = get_page_name();
			 
			return product = $("input[name='cat_trail']:checked").next().attr("id");	
		}
		
		
		function get_page_name() {
      var file_name = document.location.href;
      var end = (file_name.indexOf("?") == -1) ? file_name.length : file_name.indexOf("?");
     	file_name = file_name.substring(file_name.lastIndexOf("/")+1, end);
     	end = (file_name.indexOf("#") == -1) ? file_name.length : file_name.indexOf("#");
     	return file_name.substring(file_name.lastIndexOf("/")+1, end);
    }

			
			
		function set_breadcrumb(step) {
			
			if(step > 1) {
				var x_ctype = get_ctype();
				var ctype = (x_ctype == "wholesale") ? "Wholesaler" : "Retailer";
				
				$("#breadcrumb #step1 .link a").html("I am a " + ctype);	
			}
			
			if(step > 2) {
				var product = get_product_name();
				$("#breadcrumb #step2 .link a").html(product);	
			}
			
					$("#breadcrumb").children().removeClass("active");
				 	
					$("#breadcrumb #step"+step+"_start").addClass("active");
					$("#breadcrumb #step"+step).addClass("active");
		}
		
		
		function advance_step(step) { 
			
					var page_name = get_page_name();
					var bulk = $("#bulk").val() == 1 ? 1 : 0;

					var url = $.bbq.getState( "url" );
					var x_ctype = get_ctype();
					var x_cat_trail = get_cat_trail();
					
				
					if(x_ctype == undefined && step > 1) 
						window.location = page_name;
						
					total = 5;//$("#breadcrumb .step").size();
					
					set_breadcrumb(step);
					
					
					for(i=1;i<=total;i++) {
						// Make previous links active
						if(i == step) {
							switch(i) {
								case 2:
									// $(".ctype_location").hide();
									$("#ctype_"+x_ctype).show();
									
									break;
									
								case 3:
								
									$(".ctype_location").hide();
									// $("#ctype_"+x_ctype).show();
									$("#ctype_retailer").show();
									
								
									break;
									
								case 4:
									
									var error = false;
									
									
									//if(x_ctype == "wholesale") {
									//	var x_cstate =  ($("#cstate_w #select2").val() || []).join(', '); 
									//} else {
									  	var x_cstate = $("#cstate_r :selected").val();
									//}
								
									if(!x_cstate) {
										$('.location_error').html('<div class="error">Please select a State.</div>');
										error = true;
									} else {
										$('.location_error').html('');
									}
									
									
							  	var href = "";
									if(error == true) {
									 		//advance_step(2);
									 		window.location="#step3";
									}
										
							  	loadProducts();
							  			
									break;
								
								
								case 4:
									//loadGraph();
									
								break;
								
								case 6:
								
									var table = $('table.cat_products');
									
								  var x_pids = $("input[name=product_id]",table).map(function() {
					   				val = $(this).val();
					   				return val.replace(",","-");
									 }).get().join();
									 
									var x_trail = table.attr("id").substr(6);
									var cat_name = $("#step4_page .bt_next_step a").attr("id");
					   		
					   	
					   		$.ajax({
	                type:'GET',
	                url: 'graph-estimates-inline.php',
	                data:'pid='+x_pids+'&name='+cat_name+'&cats='+x_trail+"&est="+data,
	                success: function(data){
	                     $("#step5_page .loaded_content").html(data);
	                }
	             });     
	             
									break;
							}
							
						} else {
							
							$("#breadcrumb #step"+i+"_start").removeClass("active");
							$("#step"+i+"_page").removeClass("active");
							// Hide previous step
					  	$("#step"+i+"_page .page_content").hide();
					  	$("#step"+i+"_bottom").hide();
					  	
							if(i < step) {
								$("#step"+i+"_page").addClass("active");
							} else {
							// Make upcoming  links inactive
								$("#step"+i+"_page").removeClass("active");
							}
						}
						
						
					}
					
					$("#main").removeClass();
					$("#main").addClass("step"+step+"_page");
					  	
			  	$(".step_number").html(step);
			  	
					$("#steps #step"+step+'_page').addClass('active');
					$("#step"+step+'_page .page_content').show();
					//$('#step'+step+'_bottom').show();
					
				}
				
				
				function loadContent(elementSelector, sourceUrl) {
					$(""+elementSelector+"").load(sourceUrl);
				}
				
				
				
			  
			  function loadProducts() {
			  		
			  	$("#step3_page .loaded_content").html("<div class='loading'>Loading...</div>");
			  	
			  		
					var catid = get_cat_trail();
					
					cats = catid.split("|");
					$("#trail1").html(cats[0]);
					$("#trail2").html(cats[1]);
					$("#trail3").html(cats[2]);
					
					//if(cats[0] == 1)
					//	$("#trail2").html("");
					
			    	var checkElement = $(this).next();
			    	if(checkElement.is('ul')) {
			    		if(curmenu) {
			    			curmenu.slideToggle('fast');	
			    		}
			    		
			    		curmenu = checkElement;

			    		//$("#catNav ul").hide();

        			return;
			      } 
			      
			  //		advance_step(3);
			 			
			 			
				  	
						// Get the cat_ids from trail
						var x_trail1 = $("#trail1").html();
						var x_trail2 = $("#trail2").html();
						var x_trail3 = $("#trail3").html();
							
						// Show products
						$.post("ajax/get-products.php", {trail1 : x_trail1, trail2 : x_trail2, trail3 : x_trail3}, function(data){
							   if (data.length>0){
							  // 	advance_step(3);
			  					
							   	
									//	var x_width = $(window).width() - 340;	
										
										//$("#products").width(x_width);
										
							     	$("#step4_page .loaded_content").html(data);  
							   	} 
						});
			  }
			  
			  
			  
			// Launch colorbox 
			  function PopupLink(curr_href) {
        $.fn.colorbox({ href: curr_href,
                        width: "95%", height: "95%", iframe: true, 	scrolling: false,
                        overlayClose: false, open: true });
   				}


