/* Used to update the flash header */
var flashLinks = {"home":"\/","submit":"\/designs\/submit","saywhat":"\/pages\/submit_faq","prizes":"\/pages\/prizes","vote":"\/designs\/vote","talkT":"\/blogs\/talk_t","joinus": flashHeader.defaultPlaceHolderURL,"forgot":"\/register","login":"\/login","logout":"\/logout","myprofile":"\/profile\/my_profile","mycreations":"\/designs\/mine","team":"\/posts\/view\/meet-the-springleap-team","paparazzi":"\/blogs\/paparazzi","wordup":"\/blogs\/word_up","contact":"\/contact\/contact","theworks":"\/blogs\/the_works","peeps":"\/blogs\/peeps","shoutoutsaywhat":"\/posts\/view\/what-is-theworks-wordup-paparazzi-peeps","pressLink":"\/pages\/press","shop":"\/shop","latest":"\/shop\/competition:april-2009","shopLastMonth":"\/shop\/competition:last_month","shopFeatured":"\/shop\/featured:designs","cart":"\/cart","redir_to": flashHeader.thisUrl,"cartItems":"(empty)","cartValue":""};

if (flashHeader.userLoggedIn) {
	flashLinks['username'] = flashHeader.username;
	flashLinks['mypage'] = '/peeps/' + flashHeader.username;
	flashLinks['myprofile'] = '/profile/my_profile/' + flashHeader.username;
	flashLinks['mycreations'] = '/designs/mine/' + flashHeader.username;
}
else {
	flashLinks['mypage'] = '/login';
}

$(document).ready(function() {
	/**
	 * A hack for IE6 which doesnt support hover on non-link elements
 	 **/
	$('ul.filters li.size').hover(function() {
		$(this).addClass('sizeHover');
	}, function() {
		$(this).removeClass('sizeHover');
	});

	/* REQUEST REPRINT TOOLTIP */
	if (flashHeader.userLoggedIn > 0) {
		var reprintCalloutHtml  = '<div class="unavailable">';
			reprintCalloutHtml += '<blockquote id="reprintRequestCallout" class="callout">';
			reprintCalloutHtml += '<p>This size has been sold out.</p>';
			reprintCalloutHtml += '<p>You can click here to request that we reprint the t-shirt.</p>';
			reprintCalloutHtml += '<p>If we get enough requests, it will get another run.</p>';
			reprintCalloutHtml += '</blockquote>';
			reprintCalloutHtml += '</div>';
	}
	else {
		var reprintCalloutHtml  = '<div class="unavailable">';
			reprintCalloutHtml += '<blockquote id="reprintRequestCallout" class="callout">';
			reprintCalloutHtml += '<p>This size has been sold out.</p>';
			reprintCalloutHtml += '<p>If you would like to request a reprint, you will need to login to your account first.</p>';
			reprintCalloutHtml += '</blockquote>';
			reprintCalloutHtml += '</div>';
	}

	var previousCallout = null;

	$('a.requestReprint').hover(function() {
		if (previousCallout != this || $('.unavailable').length == 0 || isNaN($('.unavailable').length)) {
			previousCallout = this;

			$(this).parent().append(reprintCalloutHtml);
			$('.unavailable', $(this).parent()).fadeIn('fast');

			// ie only
			if ($.browser.msie) {
				$($(this).parent()).css('z-index', '1000');
				$('.unavailable', $(this).parent()).css('z-index', '1001');
			}

			$('.unavailable', $(this).parent()).children('#reprintRequestCallout').css('z-index', '1002');
		}
	}, function() {
		if (previousCallout == this) {
			$('.unavailable', $(this).parent()).children('#reprintRequestCallout').css('z-index', '41');
			$('.unavailable', $(this).parent()).remove();
		}
		else {
			$('.unavailable', $(this).parent()).fadeOut('fast', function() {
				$(this).remove();
			});
		}
	}).click(function() {
		if (flashHeader.userLoggedIn > 0) {
			var info = $(this).attr('rel').split('_');

			$.getJSON('/cart/request_reprint/product:' + info[0] + '/size:' + info[1]);
		}
		else {
			document.location = '/users/login/';
		}

		return false;
	});

	/* ENLARGING THE DESIGN */
	var previousDesign = null;

	$('div.designThumb a').hover(function() {
		if (previousDesign != this || $('.designLargeWrapper').length == 0 || isNaN($('.designLargeWrapper').length)) {
			previousDesign = this;

			$(this).append('<div class="designLargeWrapper"></div>');

			// Hack to show the enlarged version above everything
			$('.storefront dl.productInfo dd div.designThumb').css('z-index', 1000);
			$(this).children('.designLargeWrapper').append('<div class="loading"><img src="' + $(this).attr('rel') + '" width="280" height="280" /></div>');
			$('.designLargeWrapper', this).fadeIn('fast');
		}
	}, function() {
		if (previousDesign == this) {
			$('.storefront dl.productInfo dd div.designThumb').css('z-index', 1);
			$(this).children('.designLargeWrapper').remove();
		}
		else {
			$(this).children('.designLargeWrapper').fadeOut('fast', function() {
				$(this).remove();
			});
		}
	});

	/* MAKE ADD BUTTON ACTIVE/INACTIVE */
	//$('table.productAvailability input').change(AddButtonStatus).keyup(AddButtonStatus);

	/* ADD TO CART FUNCTIONALITY */
	$('a.addToCartPlus').click(function() {
		var t = 'Please wait...';
		var a = '#TB_inline?width=430&height=180';
		var g = false;

		tb_show(t, a, g);
		this.blur();

		var closeLink = $('#TB_closeAjaxWindow a').clone();

		$('#TB_closeAjaxWindow').text('').append(closeLink);
		$('#TB_closeAjaxWindow a').click(function() {
			document.location = document.location;
			return false;
		});
		$('#TB_ajaxContent').css('background', 'url("/img/ajax-loader2.gif") center 50px no-repeat');

		$.getJSON(this.href, function(data) {
			$('#TB_ajaxWindowTitle').text(data[0]);
			$('#TB_ajaxContent').css('background', '#FFF').html(data[1]);
			$('#TB_ajaxContent .keepShopping').click(function() {
				document.location = document.location;
				return false;
			});

			$('#TB_window').unload(function() {
				document.location = document.location;
				return false;
			});
		});

		return false;
	});
	$('a.productAddToCart').click(function() {
		if ($(this).hasClass('disabled')) {
			return false;
		}

		var t = 'Please wait...';
		var a = '#TB_inline?width=430&height=180';
		var g = false;

		tb_show(t, a, g);
		this.blur();

		var closeLink = $('#TB_closeAjaxWindow a').clone();

		$('#TB_closeAjaxWindow').text('').append(closeLink);
		$('#TB_closeAjaxWindow a').click(function() {
			document.location = document.location;
			return false;
		});
		$('#TB_ajaxContent').css('background', 'url("/img/ajax-loader2.gif") center 50px no-repeat');

		var productId = $(this).attr('rel');

		$.post('/cart/add_to_cart2/', {
				'data[Product][id]': $(this).attr('rel'),
				'data[Size][1]': $('#dataCartSize' + $(this).attr('rel') + 'sGirls').val(),
				'data[Size][2]': $('#dataCartSize' + $(this).attr('rel') + 'mGirls').val(),
				'data[Size][3]': $('#dataCartSize' + $(this).attr('rel') + 'lGirls').val(),
				'data[Size][4]': $('#dataCartSize' + $(this).attr('rel') + 'sGuys').val(),
				'data[Size][5]': $('#dataCartSize' + $(this).attr('rel') + 'mGuys').val(),
				'data[Size][6]': $('#dataCartSize' + $(this).attr('rel') + 'lGuys').val(),
				'data[Size][7]': $('#dataCartSize' + $(this).attr('rel') + 'xlGuys').val(),
				'data[Size][8]': $('#dataCartSize' + $(this).attr('rel') + 'xxlGuys').val()
			}, function(data) {
				updateProductQuantities(productId);

				if (!$.browser.msie) {
					$.getJSON('/shop/get_cart_totals/ajax:yes/', function(cartTotals) {
						flashLinks['cartItems'] = cartTotals['total_quantity'];
						flashLinks['cartValue'] = cartTotals['total_value'];

						$('#header').empty().flash({src: '/swf/' + flashHeader.flashHeaderFile + '?' + (Math.random() * Math.random()) * Math.random(), width: 950, height: 142, quality: 'high', wmode: 'transparent', name: 'flashHeader', 'flashvars': flashLinks}, {version: 8});
					});
				}
				$('#TB_ajaxWindowTitle').text(data[0]);
				$('#TB_ajaxContent').css('background', '#FFF').html(data[1]);

				// ie only
				if ($.browser.msie) {
					$('#TB_window').unload(function() {
						document.location = flashHeader.thisUrl;
					});
				}
		}, 'json');

		return false;
	});
});

function AddButtonStatus()
{
	var id = $(this).attr('name').split('][');
	id = id[2];
	var qty = 0;

	$('dl.productId' + id + ' input:text').each(function() {
		qty += $(this).val();
	});

	if (qty > 0) {
		$('a.productAddToCart[rel="' + id + '"]').removeClass('disabled');
	}
}

function updateProductQuantities(productId)
{
	$.getJSON('/shop/get_quantities/' + productId, function(data) {
		$('#dataCartSize' + productId + 'sGirls').val((!isNaN(data[1])) ? data[1] : '');
		$('#dataCartSize' + productId + 'mGirls').val((!isNaN(data[2])) ? data[2] : '');
		$('#dataCartSize' + productId + 'lGirls').val((!isNaN(data[3])) ? data[3] : '');
		$('#dataCartSize' + productId + 'sGuys').val((!isNaN(data[4])) ? data[4] : '');
		$('#dataCartSize' + productId + 'mGuys').val((!isNaN(data[5])) ? data[5] : '');
		$('#dataCartSize' + productId + 'lGuys').val((!isNaN(data[6])) ? data[6] : '');
		$('#dataCartSize' + productId + 'xlGuys').val((!isNaN(data[7])) ? data[7] : '');
		$('#dataCartSize' + productId + 'xxlGuys').val((!isNaN(data[8])) ? data[8] : '');
	});
}