$(function(){
		
	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);																
	});	
	
	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 button
			$(".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());
	}
	
	//add showstep() to all buttons
	$(".step a").each(function(){
		$(this).click(function(){
			showstep($(this).attr('rel'));					   
		});						   
	});
	
	$(".next-step").each(function(){
		$(this).click(function(){
			showstep($(this).attr('rel'));					   
		});						   
	});
	
	//add selectitem to all item links
	$(".item a").each(function(){
		$(this).click(function(){
			selectitem($(this).attr('itemid'),$(this).attr('step'),$(this).attr('itype'));					   
		});						   
	});
	
	$(".step a:first").trigger('click'); //activate first step
	
	//validate form
	$("#kit_form").validate({
		errorClass: "dwyer",
		errorElement: "span"
	});
	
	//open and close popup
	$(".btn_customize_kit, .choices").click(function(){
		$("#customize_kit").fadeIn();
		$overlay = $('<div id="overlay">&nbsp;</div>').css({'background':'#f90',height:'100%',width:'100%',position:'fixed',left:0,top:0,opacity:50/100}).insertBefore($("#customize_kit"));

	});
	$(".popup-close").click(function(){
		$("#customize_kit").fadeOut();
		$overlay.remove();
	});
	
	//make add to cart buttons submit form
	$(".bnt_add_to_cart").click(function(){
		$("#kit_form").submit();
		return false;
	});
	
	//append popup to bottom of documents
	$("#customize_kit").center(true).appendTo("body");	
	
});
