var count=1;
jQuery(function($) {
	isout=0;
	start=$(".active").position().left + 35;
	$("#pointer").css({backgroundPosition: start + 'px 0'});
	function resetpointer() {
		if(isout==1){
			$("#pointer").stop().animate({backgroundPosition: start + 'px 0'});
		}
	}
	$("ul.nav li a").mouseenter(function() {
		isout=0;
		newleft=$(this).position().left + 35;
		$("#pointer").stop().animate({backgroundPosition: newleft + 'px 0'});
	});
	$("ul.nav li a").mouseout(function() {
		isout=1;
		resetpointer();
	});
	
	
	resetpointer();
	
	if($("#order-form").length>0){
		$("#order-form").validate();
	}
});

function addDiv() {
	var ship1=$("input[name='ship_1']:checked").val();
	var quan1=$("input[name='quan_1']:checked").val();
	count++;
	 var newDiv=$("#ship-temp").clone().attr('id','ship-'+count).css("display","none").insertBefore("div.submit").slideDown("slow");
	 newDiv.children("h2").after("<a href=\"javascript:delDiv("+count+")\" class=\"delrecpt\" id=\"del_"+count+"\">Delete This Recipient</a>");
	 newDiv.html(newDiv.html().replace(/_1/g,"_" + count));
	 renameNew(newDiv,count);
	 $("#quan_1-"+quan1).attr("checked", "checked");
	 $("#ship_1-"+ship1).attr("checked", "checked");
}

function delDiv(num) {
	if($("#del_"+num).html()=="Delete This Recipient"){
		$("#del_"+num).slideUp("fast",function(){
			$(this).html("Confirm Delete?").slideDown("fast");
			var t=setTimeout("$(\"#del_"+num+"\").slideUp(\"slow\",function(){	$(this).html('Delete This Recipient').slideDown(\"slow\");});",5000);
			});
	}else{
		var count=$(".ship").size();
		 var newDiv=$("#ship-" + num).slideUp("slow",function(){ $(this).remove(); });
		 /*for(i=num+1;i<count;i++){
			var newDiv=$("#ship-"+i);
	 		newDiv.html(newDiv.html().replace(/_1/g,"_" + count));
			rename(newDiv,i-1,i);
			$("#ship-"+i).attr('id','ship-'+(i-1));
		}*/
	}
}

function renameNew(newDiv,count){
	newDiv.children().each(function(){
		/*var cid=$(this).attr("id");
		var cname=$(this).attr("name");
		
		var cfor=$(this).attr("for");
		
		if(cid!=undefined){
			cid=cid.replace("_1","_" + count);
			$(this).attr("id",cid);
		}
		if(cname!=undefined){
			cname=cname.replace("_1","_" + count);
			$(this).attr("name",cname);
		}
		if(cfor!=undefined){
			cfor=cfor.replace("_1","_" + count);
			$(this).attr("for",cfor);
		}*/
		var cval=$(this).val();
		var ctype=$(this).attr("type");
		if(cval!=undefined){
			if(ctype!="radio"){
				$(this).val("");
			}
		}
		
		renameNew($(this),count);
		
	});
}

function rename(newDiv,count,orig){
	newDiv.children().each(function(){
		var cid=$(this).attr("id");
		var cname=$(this).attr("name");
		var cfor=$(this).attr("for");
		var cval=$(this).val();
		var chref=$(this).attr("href");
		if(cid!=undefined){
			cid=cid.replace("_"+orig,"_" + count);
			$(this).attr("id",cid);
		}
		if(cname!=undefined){
			cname=cname.replace("_"+orig,"_" + count);
			$(this).attr("name",cname);
		}
		if(cfor!=undefined){
			cfor=cfor.replace("_"+orig,"_" + count);
			$(this).attr("for",cfor);
		}
		if(chref!=undefined){
			chref=chref.replace("delDiv("+orig+")","delDiv(" + count + ")");
			$(this).attr("href",chref);
		}
		rename($(this),count,orig);
	});
}