//for the cart communications
//if this is the live site...
if(document.domain.toString().indexOf('originalsoupman.com') >= 0)
{
	//allow the iframe to talk to you.
	document.domain = "originalsoupman.com";
	$.fn.hashchange.src = '/domain.html';
	$.fn.hashchange.domain = "originalsoupman.com";
}

$(function()
{
	$('input.qty').live('focus',
	function()
	{
		if($(this).attr('value') == 'QUANTITY')
		{
			$(this).attr('value','');
		}
	});

	$('.addToCart').live('click',
	function(e)
	{
		e.preventDefault();
		var product = $(this).data('product');
		var prefix = $(this).data('prefix');
		addToCookieCart(product,$('#'+prefix+product).attr('value'));
	}).live('mouseover',function()
	{
		$(this).find('img').attr('src','/themes/soupman/images/main/shop_soup_add_on.png');
	}).live('mouseout',function()
	{
		$(this).find('img').attr('src','/themes/soupman/images/main/shop_soup_add.png');
	});

	$('.checkout').live('click',
	function(e)
	{
		e.preventDefault();
		var product = $(this).data('product');
		var prefix = $(this).data('prefix');
		addToCookieCart(product,$('#'+prefix+product).attr('value'));
	}).live('mouseover',function()
	{
		$(this).find('img').attr('src','/themes/soupman/images/main/checkout_on.png');
	}).live('mouseout',function()
	{
		$(this).find('img').attr('src','/themes/soupman/images/main/checkout.png');
	});

	$('#headerShopLogin').click(function(e)
	{
		e.preventDefault();
		gotoLogin();
	});
	$('#headerCheckout, #headerCart').click(function(e)
	{
		e.preventDefault();
		gotoCart();
	});
	$('.checkout').live('click',function(e)
	{
		e.preventDefault();
		gotoCart();
	});

	$('.nutrition').live('click',function()
	{
		window.open('/themes/soupman/images/main/'+$(this).data('soup')+'_info.gif','Nutrition Information','width=570,toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,location=no');
	});


});


function selectSize(prod, size, e) {
	document.getElementById('merch_fit_s').style.backgroundColor = '#fff';
	document.getElementById('merch_fit_s').style.color = '#000';
	document.getElementById('merch_fit_m').style.backgroundColor = '#fff';
	document.getElementById('merch_fit_m').style.color = '#000';
	document.getElementById('merch_fit_l').style.backgroundColor = '#fff';
	document.getElementById('merch_fit_l').style.color = '#000';
	document.getElementById('merch_fit_xl').style.backgroundColor = '#fff';
	document.getElementById('merch_fit_xl').style.color = '#000';

	setCookie(prod, size, 1);
	e.style.backgroundColor = '#de6d4d';
	e.style.color = '#fff';
}



//reqires the products semantic id and an id prefix that might be use to determine the large or the small qty field
function addToCookieCart(product, qty) {
	if (isNaN(qty) || Number(qty) < 1) {
		qty = 1;
	}
	var cookies = getCookie('product');
	var total = getCookie('totalProducts');
	if (product == 'merch-tshirt1') {
		var size = getCookie('merch-tshirt1');
	}

	if (total == null) {
		total = 0;
	}

	if (cookies == null) {
		cookies = '';
	}

	for (var i=0; i<qty; i=i+1) {
		cookies = cookies + product;
		if (product == 'merch-tshirt1') {
			cookies = cookies + "~~" + size;
		}
		cookies = cookies + "||";
	}
	total = Number(total) + Number(qty);
	setCookie('product',cookies, 1);
	setCookie('totalProducts', total, 1);

	$('#qtyCart').html(total);
	if($.browser.msie){
		$('#headerCart').stop(true,true).addClass('hasItems').animate({'backgroundColor': '#d2232a'},250);
	}
	else
	{
		$('#headerCart').stop(true,true).addClass('hasItems').animate({'backgroundColor': '#d2232a'},250).animate({'backgroundColor': '#FDB813'},250).animate({'backgroundColor': '#d2232a'},250).animate({'backgroundColor': '#FDB813'},250)
		.animate({'backgroundColor': '#d2232a'},250).animate({'backgroundColor': '#FDB813'},250).animate({'backgroundColor': '#d2232a'},250);
	}
}

function gotoCart() {
	var cookies = getCookie('product');
	var ar = cookies.split('||');
	var products = '?';
	for (var i=0; i<ar.length; i++) {
		if (ar[i] != "") {
			if (ar[i].search('tshirt1') != -1) {
				var x = ar[i].split('~~');
				switch (x[1]) {
					case 's':
						sizecode = 88;
						break;
					case 'm':
						sizecode = 89;
						break;
					case 'l':
						sizecode = 90;
						break;
					case 'xl':
						sizecode = 91;
						break;
					default:
						sizecode = 90;
						break;
				}
				products = products + "ProductCode=" + x[0] + "&select.tshirt1.14="+sizecode+"&";
			} else {
				products = products + "ProductCode="+ ar[i] + "&";
			}
		}
	}
	//clear the cookie now that we've added them all to the cart.
	setCookie('product', '', 1);
	var url = 'http://store.originalsoupman.com/ShoppingCart.asp'+products;

	clearNavs();
	$('#content').scrollTo({top:0,left:0},100);
	window.location.hash = '/cart';
	$('#content').html('<iframe id="cartFrame" frameborder=0 height="1400" scrolling="no" width="830" ></iframe>');
	$('#cartFrame').attr('src',url);

	//window.location = url;
	//parent.frames['mainFrame'].document.location = url;

}

function gotoLogin()
{
	var url = 'http://store.originalsoupman.com/login.asp';
	clearNavs();
	$('#content').scrollTo({top:0,left:0},100);
	window.location.hash = '/cart';
	$('#content').html('<iframe id="cartFrame" frameborder=0 height="1250" width="830" ></iframe>');
	$('#cartFrame').attr('src',url);
}



function getCookie(c_name) {
	if (document.cookie.length>0) {
  		c_start=document.cookie.indexOf(c_name + "=");
  		if (c_start!=-1) {
    		c_start=c_start + c_name.length+1;
    		c_end=document.cookie.indexOf(";",c_start);
    		if (c_end==-1) c_end=document.cookie.length;
    		return unescape(document.cookie.substring(c_start,c_end));
    	}
  	}
	return "";
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function updateCartTotal(qty)
{
	$('#qtyCart').html(qty);
	setCookie('totalProducts', qty, 1);
}

function initCartTotal(qty)
{
	if(qty)
	{
		$('#headerCart').stop(true,true).addClass('hasItems').animate({'backgroundColor': '#d2232a'},250);
		updateCartTotal(qty);
	}
}

$(window).load(function()
{
	$('body').append('<iframe src="http://store.originalsoupman.com/ShoppingCart.asp" height="1" width="1" frameborder="0" style="position:fixed; bottom:0; left:0;"></iframe>');
});
