// JavaScript Document

function jiggle ()
{
	$('#main').css('minHeight',($(window).height() - 198) + 'px');
	if ($(document).width() < 800)
	{
		$(document).width(800);
	}
	var content_width = $('#content').width();
	var padding = 32;
	var menu_width = 0;
	var right_menu_width = 0;
	var hide_last = false;
	var menu_space = 32;
	if ($('#right_menu'))
	{
		if (!$('#right_menu li'))
		{
			$('#right_menu').addClass('hide');
		}
		else
		{
			right_menu_width = $('#right_menu').width();
		}
	}
	if ($('#menu'))
	{
		menu_width = $('#menu').width();
	}
	menu_space = menu_width+padding+right_menu_width;
	if ($('#content').width() != ($(window).width()-menu_space))
	{
		$('#content').width($(window).width()-menu_space);
		if ($('#product_right').length)
		{
			$('#product_right').width($('#content').width());
			if ($('#content .product_image_section').length)
			{
				var product_right_width = $('#content').width()/2;
				if ($('#content').width()-parseInt($('#content .product_image_section').width())-parseInt($('#content .product_image_section').css("margin-right")) > 100)
				{
					product_right_width = $('#content').width()-parseInt($('#content .product_image_section').width())-parseInt($('#content .product_image_section').css("margin-right"));
					//alert('1: ' + $('#content').width() + ' 2: ' + parseInt($('#content .product_image_section').width()) + ' 3: ' + parseInt($('#content .product_image_section').css("margin-right")) );
				}
				$('#product_right').width(product_right_width);
				if ($('#right_menu').position().top > 180)
				{
					var i = 1;
					while ($('#right_menu').position().top > 180)
					{
						if (i > 100)
						{
							break;
						}
						$('#content').width($(window).width()-menu_space-i);
						i++;
					}
					i = 1;
					while ($('#product_right').position().top > 200)
					{
						if (i > 100)
						{
							break;
						}
						$('#product_right').width(product_right_width - i);
						i++;
					}
					while ($('#product_right').position().top <= 200)
					{
						if (i == 0)
						{
							break;
						}
						$('#product_right').width(product_right_width - i);
						i--;
					}
					i++;
					$('#product_right').width(product_right_width - i);
				}
			}
		}
		if ($('.product_listings li').length || $('.category_listings li').length)
		{
			var product_listing_optimal = 265;
			var product_listing_width = product_listing_optimal;
			var product_listing_min = 260;
			var product_listing_max = 295;
			var product_listing_row = Math.floor($('#content').width()/(product_listing_optimal+36));

			if (((product_listing_max+36)*product_listing_row) >= ($('#content').width())) // First check to see if making it the MAX width will stretch it all the way...
			{
				product_listing_width = Math.floor($('#content').width()/product_listing_row)-36;
				product_listing_row = Math.floor($('#content').width()/(product_listing_width+36));
			}
			else if (((product_listing_min+36)*(product_listing_row+1)) < $('#content').width()) // If not, then check to see if reducing it to the MIN will make another product fit in the row
			{
				product_listing_width = Math.floor($('#content').width()/(product_listing_row+1))-36;
				product_listing_row = Math.floor($('#content').width()/(product_listing_width+36));
			}
			else if (((product_listing_max+36)*product_listing_row) < $('#content').width())
			{
				product_listing_width = product_listing_max;
				product_listing_row = Math.floor($('#content').width()/(product_listing_max+36));
			}
			if (product_listing_row == 5)
			{
				hide_last = true;
			}
			if ($('.product_listings li').length)
			{
				$('.product_listings li').each(function() {
					$(this).width(product_listing_width);
				});
				if (hide_last)
				{
					var i = 1;
					$('.product_listings li').each(function() {
						if (i == 6)
						{
							$(this).hide();
							i = 0;
						}
						i++;
					});
				}
			}
			if ($('.category_listings li').length)
			{
				$('.category_listings li').each(function() {
					$(this).width(product_listing_width);
				});
			}
		}
	}
}

function popup(URL,width,height)
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=0,menubar=0,resizable=1,width=" + width + ",height=" + height + ",left = " + (width/2) + ",top = 100');");
}

function search_focus (e,event_type)
{
	if (event_type == 'focus')
	{
		if ($('#search_text').val() == 'search!')
		{
			$('#search_text').val(null);
			$('#search_text').removeClass('search_text_default');
			$('#search_text').addClass('search_text');
		}
	}
	else if (event_type == 'blur' && !$('#search_text').val())
	{
		$('#search_text').val('search!');
		$('#search_text').removeClass('search_text');
		$('#search_text').addClass('search_text_default');
	}
}
function submit_search (e)
{
	if ($('#search_text').val() != 'search!' && (($(e).attr('id') == 'search_text' && $('#search_text').val().length > 3) || ($(e).attr('id') != 'search_text' && $('#search_text').val().length > 1)))
	{
		$(e).closest('form').submit();		
	}
	else
	{
		search_focus(e,'blur');
	}
}

function preload_images()
{
	if (typeof masthead_image == 'undefined')
	{
		$('#loader').hide();
	}
	else
	{
		var images = masthead_image;
		for (var i = 0; i < images.length; i++)
		{
			$('<img />').attr('src', images[i]);
		}
	}
}

function masthead ()
{
	if (typeof masthead_image == 'undefined')
	{
		$('#loader').hide();
	}
	else
	{
		var image = masthead_image[0];
		var image_align = '0 0';
		if (image.match(/desert/i))
		{
			var image_align = 'center 0';
		}
		$('#header').css('background','url("' + image + '") no-repeat scroll ' + image_align + ' #565656');
		$('#loader').fadeOut();
	}
}

function payment_continue()
{
	if ($('#payment_continue').length)
	{
		$('#payment_continue').click();
	}
}

function email_show()
{
	if ($('#email_show').length)
	{
		var reveal_text = '(click to reveal&hellip;)';
		var email = 'marc@'+window.location.href.replace(/([^\/]+)([\/]{2})([w]{3}\.)?([^\/]+)\/.*/i,'$4');
		var subject = ($('#contact_subject').length) ? $('#contact_subject').val().replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A'): '';
		$('#email_show').html(reveal_text);
		$('#email_show').bind('click',function () {
			if ($(this).html().substr(0,6) == reveal_text.substr(0,6))
			{
				$(this).html('').hide().html('<a href="mailto:Cycle%20Centre%20&lt;'+email+'&gt;?subject='+subject+'" title="Email us!">'+email+'</a>').fadeIn();
			}
		} );
	}
}

$(document).ready(function() {
	jiggle ();
	masthead();
	payment_continue();
	email_show();
	$('#cart tbody tr:even td').addClass('alt');
	$('.alternate tbody tr:even td').addClass('alt');
	setTimeout("jiggle()",100);
	setTimeout("jiggle()",300);
	setTimeout("jiggle()",800);
});

$(window).resize(function() {
	jiggle ();	
	setTimeout("jiggle()",100);
});

preload_images();

