$(function(){
    
    /*
     * Pretty forms
     */
    $('form.prettyform input[@type*=radio]').css('border','none');
    $('form.prettyform input[@type*=checkbox]').css('border','none');
    if($.browser.mozilla) { prettyform(); }
    
    /*
     * LI hover fix for IE6
     */
    $('#navigation ul li').each( function(){
        $(this).mouseover( function(){ $(this).addClass('hover'); } ).mouseout( function(){ $(this).removeClass('hover'); } );
    });
    
    /*
     * Blur on focus
     */
    $('a').focus(function() { this.blur(); });
    $('input[@type*=submit]').focus(function() { this.blur(); });
    $('input[@type=text], input[@type=password]').addClass("input");

    $("#stockists h2").click(function(){
    	$(this).next("ul").slideToggle("slow");
    	$(this).toggleClass("active");
    });    
    
	// alert( parseInt( $("#cartbox span.number").text() ) );
	if( parseInt( $("#cartbox span.number").text() ) == 0 ){
		$('#cartbox').hide();
	} else {
		$('#cartbox').show();
	} 
   
	
	
	$("div.news-item").click(function(){
		  window.location=$(this).find("a").attr("href"); return false;
	});
	$("div.news-item").hover(
			  function () {
			    $(this).addClass("hover");
			  },
			  function () {
			    $(this).removeClass("hover");
			  }
			);
	
	
	getCart();
});


function getCart(data) {
    if (data == null) {
        var data = new Object();
    }
    $.ajax({
        url: "/order/get-cart",
        type: "POST",
        data: data,
        success: function(msg) {
        	// Update cart with cart contents
        	$('#cart').html(msg);

        	// Update cart box with cart total
        	$.ajax({
        		url : "/order/get-cart/total",
        		type : "POST",
        		success : function(msg) {
        			$('#cartbox .total').html(msg);
        			showHideCart();

        		}
        	});

        	// Update cart box with cart item count
        	$.ajax({
        		url : "/order/get-cart/itemcount",
        		type : "POST",
        		success : function(msg) {
        			$('#cartbox .number').html(msg);
        			showHideCart();
        		}
        	});
        }
    });
}


var t;

function addToCartSuccess(msg) {
    if (msg == "success") {
        clearTimeout(t);
        getCart();
        $.blockUI({ 
            message: $('#cart-reminder'),
            css: { 
               border: '1px solid #BC1334',
               width:  '250px',
               margin: '0 auto',
               '-webkit-border-radius': '10px', 
               '-moz-border-radius':    '10px' 
            }
        });
        t = setTimeout($.unblockUI, 2500);
    } else {
        alert("An error occurred while trying to purchase item");
    }
}


function unblockUI() {
    clearTimeout(t);
    $.unblockUI();
}

function showHideCart(){
	if( parseInt( $("#cartbox span.number").text() ) == 0 ){
		$('#cartbox').hide();
	} else {
		$('#cartbox').show();
		if( $("#wine-prices").length && $("#bigCheckOut").length == 0 ) {
			$("#wine-prices").append( '<a href="/Checkout" id="bigCheckOut" title="Checkout"></a>' );
		}
	} 
}
showHideCart();
/*
 * Pretty forms
 */
function prettyform(){
  /*
   * Hide forms
   */
  $( 'form.prettyform' ).hide().end();
  /*
   * Label & list formatting
   */
  $( 'form.prettyform' ).find( 'label' ).not( '.noprettyform' ).each( function( i ){
    var labelContent = this.innerHTML;
    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = '';
    this.appendChild( labelSpan );
  } ).end();
  
  /*
   * Show forms
   */
  $( 'form.prettyform' ).show().end();
}
