// JavaScript Document
$(document).ready(function() {
	
	var winWidth = document.body.clientWidth;
	
	if(winWidth < 1100) {
		$('#leftFlourish').hide();
		$('#rightFlourish').hide();
	}
	
	$('.buy').click(function() {
	    if($('#ctl00_ContentPlaceHolder1_ddlSize').val() != "0")
        {
		    addToCart();
		    $('img#cartImg').remove();
		    $('#cartImgWrap').prepend('<img src="gfx/cartAnim.gif?q=' + new Date().getMilliseconds() + '" id="cartImg" />');
		}
		else
		{
		    $('#error').dialog();		    
		}
		return false;
	});
	
	$('.cartItem a.remove').click(function(){
		var cartItem = $(this).parent().addClass('remove');	
		$(this).parent().fadeOut();
		return false;
	});
	
	$('ul.prodList li').hover(function() {
		$('a.hoverBorder', this).show();
		$('a.hoverBuy', this).show();
	},function(){
		$('a.hoverBorder').hide();
		$('a.hoverBuy').hide();	
	});
	$('ul.prodShowList li').hover(function() {
		$('a.hoverBorder').hide();
		$('a.hoverBorder', this).show();								   
	},function(){
		
	});
	
	$('ul.prodShowList a').hover(function() {
	    var relvalue = $(this).attr("rel");
		var aclass = $(this).attr("class");
		if(aclass == 'iframe' || relvalue.indexOf('shadowbox') > -1) {
			return false;
		} else {
			var img = $(this).attr("href");
			var imgStr = '<img src="'+img+'" alt="rollover" id="currentImg" />';
			var currentImg = $('#prodShow img').attr("src");
			if(img != currentImg) {
				$('#prodShowWrap').empty();
				$('#prodShowWrap').append(imgStr);
				$('img#currentImg').hide().fadeIn(500);
			}
		}
	});
	$('ul.prodShowList a').click(function() {
		return false;									  
	});
	
	$('#navWrap').corner();
	$('.addWhite').corner();
	$('#sideBar').corner();
	$('#frontInfo').corner();
	$('p.msg').corner();
	$('p.sent').corner();
	$('p.sentfail').corner();
	
	/*$('a.iframe').fancybox({'hideOnContentClick': true, 'frameWidth' : 400, 'frameHeight' : 490});
	$('a.spinme').fancybox({'hideOnContentClick': true, 'frameWidth' : 400, 'frameHeight' : 490});*/
	
});



function addToCart()
{    
    $.post('CartHandler.aspx', {action: 'add', pId: $('#hdnProdId').val(), qty: $('#amount').val(), size: $('#ctl00_ContentPlaceHolder1_ddlSize').val()},
	    function(data){
	        $('p#cartStatus').empty().append(data);    
	    });
}

function updateCities(country)
{
    $.post('ResellerHandler.aspx', {action: 'updateCities', country: country},
	    function(data){
	        $('#selectCity').empty().append(data);    
	        $('#selectCity').focus();
	    });
}

function displayResults()
{
    var cityValue = $('#selectCity').val();
    var countryValue = $('#selectCountry').val();
    var dam = $('#dam:checked').val() == undefined ? 0 : 1;
    var herr = $('#herr:checked').val() == undefined ? 0 : 1;
    var ugg = $('#ugg:checked').val() == undefined ? 0 : 1;
    $.post('ResellerHandler.aspx', {action: 'displayResults', country: countryValue, city: cityValue, dam: dam, herr: herr, ugg: ugg},
    function(data){
        $('#retailResults').empty().append(data);            
    });
}

function showSelected(retailerId)
{
    $.post('ResellerHandler.aspx', {action: 'showSelected', retailer: retailerId},
    function(data){
        $('#retailSelected').empty().append(data);            
    });
}

function getCartQuantity()
{
    $.post('CartHandler.aspx', {action: 'getQuantity'},
	    function(data){
	        $('p#cartStatus').empty().append(data);    
	    });
}

function removeFromCart(cartId)
{
    $.post('CartHandler.aspx', {action: 'remove', cartId: cartId},
    function(data){
        $('#cartSum').empty().append(data);
        getCartQuantity();
    });
}

function updateCartAmount(cartId, amount)
{    
    $.post('CartHandler.aspx', {action: 'updateAmount', qty: amount, cartId: cartId},
        function(data){
            $('#cartSum').empty().append(data);
            getCartQuantity();    
        }
    );
}

function updateSize(cartId, attributeId)
{    
    $.post('CartHandler.aspx', {action: 'updateSize', size: attributeId, cartId: cartId});
}