  $(document).ready(function(){
    var active;
	var currentpage = 1;
	var adheight = "70px";
	pgstxt = currentpage + " of " + totalpages;
	$(".pgnum").html(pgstxt);
		
    $(".nextbtn").click(function () { 
		nextPage();
    });
	$(".prevbtn").click(function () { 
		prevPage();
    });
	$(".f_addtocart").click(function () { 
		pid = $(this).attr("id");
		$("#featured-product-code").val(pid);
		if ($("#featured-product-code").val() != pid){
			$("#featured-product-code").val(pid);
		}
		/*
		$.post("http://www.winebuys.com/mm5/merchant.mvc?winebuys=1", $("#featured-wine-form").serialize(),
			function(data){
				$("#" + pid).html('<img src="http://www.winebuys.com/images/btn_added.png" alt="Add to Cart" title="Add to Cart"/>');
				setTimeout(function () {
					$("#" + pid).html('<img src="http://www.winebuys.com/images/btn_add_to_cart.png" alt="Add to Cart" title="Add to Cart"/>');
				}, 3000);
			}
		);
		
		*/
		setTimeout(function () {
			$("#featured-wine-form").submit();
			$("#" + pid).html('<img src="http://www.winebuys.com/images/btn_added.png" alt="Add to Cart" title="Add to Cart"/>');
			setTimeout(function () {
				$("#" + pid).html('<img src="http://www.winebuys.com/images/btn_add_to_cart.png" alt="Add to Cart" title="Add to Cart"/>');
			}, 3000);
		}, 500);
		
		
		
    });
	
	function nextPage(){
	
		if(currentpage < totalpages){
			currentpage = currentpage + 1;	
		}
		else if(currentpage >= totalpages){
			currentpage = 1;
		}
		else if(currentpage <= 0){
			currentpage = 1;
		}
		togglePages();
	
	}
	
	function prevPage(){
		if(currentpage <= totalpages && currentpage != 1){
			currentpage = currentpage - 1;	
		}
		else if(currentpage <= 1){
			currentpage = totalpages;
		}
		togglePages();
	}
	
	function togglePages(){
		for (i=1; i<=totalpages; i++){
			tmpfwine = "#featured-wine-" + i;
			$(tmpfwine).hide();
		}
		tmpfwine = "#featured-wine-" + currentpage;
		$(tmpfwine).fadeIn();
		pgstxt = currentpage + " of " + totalpages;
		$(".pgnum").html(pgstxt);
	}
	
	// call the cookie class and set a cookie called viewState
	$.cookie("adState");
	
	// create a variable and set it to the value of the cookie
	var theState = $.cookie("adState");
	
	$("#ad-expand").click(function () { 
		//$("#winebuys-ad").slideDown();
		$("#winebuys-ad").animate({height: "400px"},1000);
		$("#ad-expand").hide();
		$("#ad-collapse").show();
		$.cookie("adState", "expanded");
		theState = "expanded";

    });
	$("#ad-collapse").click(function () { 
		//$("#winebuys-ad").slideUp();
		$("#winebuys-ad").animate({height: adheight},1000);
		$("#ad-expand").show();
		$("#ad-collapse").hide();
		$.cookie("adState", "collapsed");
		theState = "collapsed";
    });
	
	// update the hidden panels css to none, or block depending on the toggle event
	
	if(theState == "collapsed"){
		$("#winebuys-ad").animate({height: adheight},10);
		$("#ad-expand").show();
		$("#ad-collapse").hide();
	}

  });