$(function(){
						   
	//button actions 
	$(".btn_add_to_cart").click(function(){
		$("#item_form").submit();	
		return false
	});
	$(".btn_customize_kit").click(function(){
		return false								 
	});
		 
	$(".quantity1").change(function(){
		$(".quantity2").val($(this).val());
	});
	
	$("#loading_kit").ajaxStart(function(){
		$(this).show();									  
	});
	$("#loading_kit").ajaxStop(function(){
		$(this).hide();									  
	});
	$(".quantity1").ajaxStart(function(){
		$(this).attr('disabled',true);									  
	});
	$(".quantity1").ajaxStop(function(){
		$(this).removeAttr('disabled');									  
	});
	
	var enlargeImage = function(productid,src){
		window.open('/shop/gallery.cfm?pid='+productid+'&src='+src,'Window1',
'menubar=no,width=400,height=400,toolbar=no');	
	}
	
	$(".item_image").click(function(){	
		var uid = $(this).attr('uid');
		var src = $(this).attr('src');
		enlargeImage(uid,src);																
	});	
	$(".xother_images img").click(function(){
		$(".item_image").attr('src',$(this).attr('src'));
	});					
		 
	// customizable kit functions
	
	$overlay = $('<div id="overlay">&nbsp;</div>').css({'background':'#f90',height:'100%',width:'100%',position:'fixed',left:0,top:0,opacity:50/100}).insertBefore($("#kit_content")).hide();
	
	var showstep = function(step){
		
		//alert("showing step " + step);
		
		var tab = $("#tab_"+step);
		var button = $("#btn_"+step);
		
		$(".tab").hide(); //hide other tabs
		$(".step a").removeClass('active'); //remove active class from other buttons
		
		$("#active_step").html(step); //update active step
		tab.fadeIn('fast'); //show tab
		button.addClass('active'); //make button active
		
		return false
	}
	
	var selectitem = function(itemid,step,itype){
		var input = $("input#item_" + step);
		var nextstep = parseInt(step)+parseInt(1);
		var button = $("#step_"+step+"_item_"+itemid);
		input.val(itype+"|"+itemid);
		$(".items_step_"+step).removeClass('active');
		button.addClass('active');
		if(!$("#btn_"+step).hasClass('prev-step')) {
			$("#btn_"+step).addClass('prev-step');
			$("#btn_"+nextstep).trigger('click');
		}
		if($("#kit_form").valid() && !$(".add-to-cart a").hasClass('active')) {
			//activate add to cart button0
			$(".add-to-cart a").removeAttr('disabled').addClass('active').click(function(){$("#kit_form").submit()});
			//add add to cart button
			$(".top-add-to-cart").show().click(function(){$("#kit_form").submit()});
			$(".top-next-step").hide();
		}
		return false
		//alert("step "+step+": "+input.val());
	}
	
	//open and close popup
	var openKitOptions = function(){
		$("#customize_kit").center(true).fadeIn();
		$overlay.show();

	}
	
	var closeKitOptions = function(){
		$("#customize_kit").fadeOut();
		$overlay.hide();
	}
		 
	// when syle menu changes
	$("#xstyle").change(function(){
		var id = $(this).val();
		var quantity = $("input[name='txt_quantity']").val();
		var itemBtn = $('#item_'+id);
		if(!$('#item_'+id).attr('id')) {
			
			kitBtn = $('#kit_'+id);// kit
			$.ajax({
				type: "post",
				url: "/shop/ajax/kitOptions.cfm",
				data: "kitid="+id,
				success: function(kitOptions){
					$.ajax({
						type: "post",
						url: "/shop/ajax/kitHTML-v2.cfm",
						data: "kitid="+id+"&quantity="+quantity,
						dataType: "html",
						success: function(kitHTML){
							//alert("success!");
							// add kit html to page
							$("#kit_content").html(kitHTML);
							
							if(kitOptions>0) {
								//alert("customizable kit");
								
								// show appropriate button
								$(".btn_customize_kit").ajaxStop(function(){
									$(this).show();
								});
								$(".btn_add_to_cart").hide();
								
								// add kit option button actions
								$(".btn_customize_kit").click(function(){
									openKitOptions();
								});
								$(".popup-close").click(function(){
									closeKitOptions();
								});
								$(".step a").each(function(){
									$(this).click(function(){
										showstep($(this).attr('rel'));					   
									});						   
								});
								$(".next-step").each(function(){
									$(this).click(function(){
										showstep($(this).attr('rel'));					   
									});						   
								});
								$(".item a").each(function(){
									$(this).click(function(){
										selectitem($(this).attr('itemid'),$(this).attr('step'),$(this).attr('itype'));					   
									});						   
								});
								//other actions
								$("#kit_form").validate({
									errorClass: "dwyer",
									errorElement: "span"
								});
								$(".step a:first").trigger('click'); //activate first step
							
							} else {
								
								//alert("simple kit");
								$(".btn_add_to_cart").unbind(); //unbind previous click events
								$(".btn_add_to_cart").show().click(function(){
									$("#kit_form").submit();
									return false
								});
								/*$(".btn_add_to_cart").ajaxStop(function(){
									$(this).show().click(function(){
										$("#kit_form").submit();
										return false
									});
								});*/
							}
						}
					});	
				}
			});
			if(!kitBtn.hasClass('active')){
				kitBtn.trigger('click');
			}
		} else {
			if(!itemBtn.hasClass('active')){
				itemBtn.trigger('click');	
			}
			$(".btn_add_to_cart").unbind(); //unbind previous events
			$(".btn_add_to_cart").click(function(){
				$("#item_form").submit();	
				return false
			});
		}
	});
		   
	$("a.xitem").each(function(){
		
		$(this).click(function(){
							   
			//grab variables from link attributes
			var type = $(this).attr('itype');
			var iTitle = $(this).attr('title');
			var uid = $(this).attr('uid');
			var iOn_sale = $(this).attr('on_sale');
			var iRetail_price = $(this).attr('retail_price');
			var iPrice = $(this).attr('price');
			var iImage = $(this).find('img').attr('src');
			
			if(type=="item") {
				
				//$(".quantity1").removeAttr('disabled');//enable quantity
				
				//show appropriate button
				$(".btn_customize_kit").hide();
				$(".btn_add_to_cart").show().click(function(){
					$("#item_form").submit();
					return false
				});
				
				
				var pTitle = $(this).attr('product');
				var productid = $(this).attr('productid');
			
				//build item detail html
				var detailHtml = '<div class="xfeatured_image"><img class="item_image" src="' + iImage + '" alt="'+ iTitle +'" uid="'+ productid +'"';
				detailHtml += 'width="135"></div><br />';										  
				detailHtml += '<span class="xproduct_name"><strong>'+ pTitle +'</strong></span>';
				detailHtml += '<span class="xitem_name">' + iTitle + '</span>';
				if(iOn_sale!='false') {
					detailHtml += '<span class="xitem_price"><em>Was: $' + iRetail_price + '</em><br />NOW: $' + iPrice + '</span>';
				} else {
					detailHtml += '<span class="xitem_price">$' + iPrice + '</span>';
				}
			} else if(type=="kit") {
				
				/*$loading = $('<div id="loading_kit" style="font-weight: normal; display: inline; background: url(/elements/images/icons/loading-16x16.gif) 4px 50% no-repeat;">&nbsp; &nbsp; &nbsp;<em>loading kit...</em></div>').css({'margin':0,'padding':0}).insertAfter(".quantity1"); //add progress indicator*/
				
				//show appropriate button
				$(".btn_add_to_cart").hide();
				$(".btn_customize_kit").hide();
				
				//build kit detail html
				var detailHtml = '<div class="xfeatured_image"><img class="item_image" src="' + iImage + '" alt="'+ iTitle +'" uid="'+ uid +'"';
				detailHtml += 'width="135"></div><br />';
				detailHtml += '<span class="xproduct_name"><strong>'+ iTitle +'</strong></span>';
				if(iOn_sale!='false') {
					detailHtml += '<span class="xitem_price"><em>Was: $' + iRetail_price + '</em><br />NOW: $' + iPrice + '</span>';
				} else {
					detailHtml += '<span class="xitem_price">$' + iPrice + '</span>';
				}
				
				
			}
							   
			// set active class
			$('a.xitem').removeClass('active');
			$(this).addClass('active');
			
			//update active item details
			$(".xitem_info").html(detailHtml);
			
			// and enlarge click event
			$(".item_image").click(function(){
				var uid = $(this).attr('uid');
				var src = $(this).attr('src');
				enlargeImage(uid,src);																
			});
			
			//change select menu
			$("#xstyle").val(uid).trigger('change');
			
			return false;
		});	
	
	});
	
	
});
