jQuery(document).ready( function() {

	bodywidth	= jQuery('body').width();
	popupleft	= bodywidth / 2 - 550 + 'px';
	popupleft2	= bodywidth / 2 - 200 + 'px';

	supportbar	= jQuery('#support-bar');
	popups		= jQuery('.popup');
	products	= jQuery('div.product');
	products_large	= jQuery('div.product_large');
	closes		= jQuery('div.close');
	flinks		= jQuery('.footerlink');
	arrows_left	= jQuery('.img_arrow_left');
	arrows_right	= jQuery('.img_arrow_right');
	cur_item	= -1;

	flinks.each( function(e) { jQuery(this).click( function() { popupBox(jQuery(this).attr('rel')); }); });

	popups.each( function(f) {
		jQuery(this).hide();
		jQuery(this).css({
			position	: 'absolute',
			top		: '20px',
			left		: popupleft2,
			'z-index'	: 10
		});
		jQuery(this).click( function() {
			jQuery(this).hide('slow');
			jQuery("#wrapper").fadeTo('fast', 1);
		});
	});

	arrows_left.each( function(g) { jQuery(this).click( function() { moveLeft(); }); });
	arrows_right.each( function(h) { jQuery(this).click( function() { moveRight(); }); });
	closes.each( function(i) { jQuery(this).click( function() { closeModal(); }); });

	products_large.each( function(j) {

		jQuery(this).hide();
		jQuery(this).css({
			position	: 'absolute',
			top		: '50px',
			left		: popupleft,
			'z-index'	: 10
		});
	});

	products.each( function(k) {

		jQuery(this).css('cursor', 'hand');
		if (jQuery(this).attr("rel") != '') {
			jQuery(this).click( function() {
				showModal(jQuery(this).attr("rel"));
			});
		}

	});

	supportbar.css('cursor', 'hand');
	supportbar.click( function() {
		showModal('7');
	});

	function popupBox(el_id) {
		el = jQuery('#'+el_id);
		jQuery("#wrapper").fadeTo('normal', .2);
		jQuery("div.popup:not(#"+el_id+")").hide("fast");
		jQuery("div.popup#"+el_id).show("slow");
	}

	function closeModal() {
		cur_item = -1;
		products_large.each( function(j) {
			jQuery(this).hide('slow');
		});
//		jQuery("#wrapper").fadeTo('fast', 1);
	}

	function showModal(item) {
		cur_item = item;
//		jQuery("#wrapper").fadeTo('normal', .2);
		jQuery("div.product_large:not(#product_"+item+")").hide("fast");
		jQuery("div.product_large#product_"+item).show("slow");
	}

	function switchModal(item) {
		if (cur_item == -1) {
			showModal(item);
			return;
		}
		cur_item = item;
//		jQuery("#wrapper").fadeTo('fast', .2);
		jQuery("div.product_large:not(#product_"+item+")").hide();
		jQuery("div.product_large#product_"+item).show();
	}

	function moveLeft() {
		if (cur_item == 1) cur_item = products.length+1;
		else cur_item--;
		if (cur_item == 6) cur_item = 5;
		switchModal(cur_item);
	}

	function moveRight() {
		if (cur_item > products.length) cur_item = 1;
		else cur_item++;
		if (cur_item == 6) cur_item = 7;
		switchModal(cur_item);
	}

	function handleEscape(e) {
		if (e.keyCode == 27) closeModal();
		if (cur_item > -1) {
			if (e.keyCode == 37) moveLeft();
			if (e.keyCode == 39) moveRight();
		}
	}

	jQuery(document).keydown(handleEscape);		

});
