var sizes = ['xs', 's', 'm', 'l'];
var max = 5;

$(document).bind('loading.facebox', function() {
    $("#opaque").show();
});
$(document).bind('close.facebox', function() {
    $("#opaque").hide();
});
$(document).bind('afterReveal.facebox', function() {
    // this is a fix for IE6 which resets the height to 100% of the window height
    $("#opaque").height($(document).height());
});

$("body").append("<div id='opaque' style='display: none;'></div>");

$(document).ready(function() {
	$('#header_main').show();
	
	initialize();
	bindButtons();
	
	$('#pic2').hide();
	
	$(".tweet").tweet({
	    query: "gadgetwidows",
	    join_text: "auto",
	    avatar_size: 32,
	    count: 10,
	    loading_text: "loading tweets..."
	});
});

function updateCart(html) {
	$('#cartContainer').html(html);
	bindButtons();
}

function bindButtons() {
	$('.icon').bind('click', function() {
		var quantity = 1;
		var size = $(this).attr('data-size');
		$.post('store/add_to_cart', { quantity : quantity, size : size }, updateCart );
	});
	$('#clear').bind('click', function() {
		$.get('store/clear', updateCart );
	});
	$('#icon_xs').bind('mouseover', function() {
		$(this).effect("bounce", { times:3 }, 500);
	});
	$('#icon_s').bind('mouseover', function() {
		$(this).effect("bounce", { times:3 }, 500);
	});
	$('#icon_m').bind('mouseover', function() {
		$(this).effect("bounce", { times:3 }, 500);
	});
	$('#icon_l').bind('mouseover', function() {
		$(this).effect("bounce", { times:3 }, 500);
	});
	$('#buy_now').bind('click', function() {
		$.get('store/has_item', function(data) {
			json = toJSON(data);
			if (json.has_item == false) {
				jQuery.facebox("You didn't select a size, silly!");
			}
			else {
				$('#paypal-form').submit();
			}
		});
	});
	$('#buy_now_wish').bind('click', function() {
		$('#paypal-form').submit();
	});
	$('#pics, #next').bind('click', function() {
		src = $('.pic').attr('src');
		src = src.split('?');
		src = src[0];
		src = src.split('/');
		src = src[src.length-1];
		if (src == 'multitouchtoo1.png') {
			$('.pic').attr('src', 'images/gadget_widow_web.jpg');
			$('.pic').addClass('pic_pad');
		}
		else {
			$('.pic').attr('src', 'images/multitouchtoo1.png');
			$('.pic').removeClass('pic_pad');
		}
	});
}

function added_to_cart(data) {
	json = toJSON(data);
	updateIcon(json.size, json.quantity);
}

function removed_from_cart(data) {
	json = toJSON(data);
	success = json.success
	if (success) {
		line_item_id = json.line_item_id
		$('#' + json.line_item_id).remove();
	}
}

function attachDelete() {
	$('#delete_button').bind('click', function() {
		var line_item = $(this).parent().parent().attr('id');
		$.post('store/remove_from_cart', { line_item : line_item }, removed_from_cart );
	});
}

function initialize() {
	$.get('store/initialize_cart', function(data) {
		json = toJSON(data);
		for (i in sizes) {
			updateIcon( sizes[i], json[ sizes[i] ] );
		}
	});
}

function updateIcon(size, quantity) {
	if (quantity <= max) {
		$("div[data-size = " + size + "]").empty();
		$("div[data-size = " + size + "]").append('<img src="images/size_' + size + '_' + quantity + '.png" />');
	}
}

function toJSON(data) {
	return eval( '(' + data + ')' );
}