$(document).ready(function()
{
	var shadowbox_options = ''; // Will be appended to rel attribute for images and media
	
	//---------------------------------------------START CUSTOM JS//
	
	if($('div#wrapper').hasClass('static') && $('div#header span.checkout').length > 0)
	{
		$.ajax({
				async			: false,
				url				: _root,
				type			: "POST",
				data			: ({
										custom			: 'true',
										action			: 'check_header',
										ajax			: 'true'
									}),
				success			: function(data)
				{
					data = data.split('~');
					
					if(data[0] != undefined && data[0] != '')
					{
						$('div#header div.basket_container').replaceWith(data[0]);
					}
					
					if(data[1] != undefined && data[1] != '')
					{
						$('div#header span.basket').replaceWith(data[1]);
					}
					
					if(data[2] != undefined && data[2] != '')
					{
						$('div.break a.signin').replaceWith(data[2]);
					}
					
					if(data[3] != undefined && data[3] != '')
					{
						$('div.break a.account').replaceWith(data[3]);
					}
					
					if(data[4] != undefined && data[4] != '')
					{
						$('form.keyword_search input#keywords').val(data[4]);
					}
				}
		});
	}
	
	if($('div.product_filter').length > 0)
	{
		$.ajax({
				async			: false,
				url				: _root,
				type			: "POST",
				data			: ({
										custom			: 'true',
										action			: 'product_filters',
										category_id		: $('div.product_filter').attr('id').replace('category_filter_',''),
										ajax			: 'true'
									}),
				success			: function(data)
				{
					$('div.product_filter').replaceWith(data);
				}
		});
	}
	
	$('ul#nav li ul li').bind('mouseenter',function()
	{
		var subnav = $(this).children().next('ul');
		
		if(subnav != undefined && subnav.height() != null)
		{
			$(this).addClass('active');
			
			if(subnav.height() > $(this).parent().height())
			{
				$(this).parent().css('height',subnav.height()+'px');
			}
			else
			{
				subnav.css('height',$(this).parent().height()+'px');
			}
		}
		
	}).bind('mouseleave',function()
	{
		if($(this).parent().parent().hasClass('subnav'))
		{
			$(this).removeClass('active');
			$(this).parent().css('height','auto');
		}
	});
	
	if($('textarea').length > 0)
	{
		$('textarea').each(function()
		{
			$(this).css('resize','none');
		});
	}
	
	if($('input#keywords').length > 0)
	{
		$('input#keywords').bind('focus',function()
		{
			if($(this).val() == 'SEARCH')
			{
				$(this).val('');
			}
		}).bind('blur',function()
		{
			if($(this).val() == '')
			{
				$(this).val('SEARCH');
			}
		});
	}
	
	if($('ul.info').length > 0)
	{
		$('ul.info li').mouseover(function()
		{
			if(!$(this).hasClass('active'))
			{
				$('div.info').hide()
				$('div.content'+$(this).attr('class').replace('info','')).show();
				$('ul.info li').removeClass('active');
				$(this).addClass('active');
			}
		});
	}
	
	if($('body').attr('id') == 'checkout')
	{
		function checkCountry(value,state_field,focus)
		{
			if(value == 'United States')
			{
				if($('label[for="'+state_field+'"] small').length == 0)
				{
					$('label[for="'+state_field+'"]').html('State <small>Please ensure that you enter the two letter USPS state code</small>');
					
					if(focus == 'true')
					{
						$('input#'+state_field).focus();
					}
				}
			}
			else
			{
				$('label[for="'+state_field+'"]').html('County');
			}
		}
		
		if($('select#country').length > 0)
		{
			checkCountry($('select#country').val(),'county','false');
			
			$('select#country').unbind().bind('change',function()
			{
				checkCountry($(this).val(),'county','true');
			});
		}
		
		if($('select#delivery_country').length > 0)
		{
			checkCountry($('select#delivery_country').val(),'delivery_county','false');
			
			$('select#delivery_country').unbind().bind('change',function()
			{
				checkCountry($(this).val(),'delivery_county','true');
			});
		}
	}
	
	$.getScript(_root+'media/js/jquery-corner.js', function()
	{
		$('ul#nav li ul').corner('6px');
		$('div#content div.column h6').corner('top 3px');
		$('div#content div.column a.last').corner('bottom 3px');
		$('div#content div.product_filter h6').corner('top 3px');
		$('div#content div.product_filter a:last-child').corner('bottom 3px');
		$('div#content div.full_page').corner('6px');
		$('body#login div#content').corner('6px');
		$('body.product div#content div.item').corner('6px');
		
		if($('div.page.account').length > 0)
		{
			$('div#content').corner('6px');
		}
		
		if($('body').attr('id') != 'home')
		{
			$('div#content div.page').corner('6px');
		}
		
		if($('div.alternative').length > 0)
		{
			$('div.alternative').corner('6px');
		}
		
		if($('div.secure').length > 0)
		{
			$('div.secure').corner('6px');
		}
	});
	
	$('div.product_images ul li a img').each(function()
	{
		$.preloadImages.add($(this).attr('src').replace('.thumb.', '.full.'));
	});
	
	$('div.product_images ul li a').mouseover(function()
	{
		$('img#full').attr('src', ($(this).attr('href')).replace('thumb', 'full'));
		$('img#full').attr('alt', ($(this).attr('alt')));
		$('a#enlarge').attr('href', ($(this).attr('href')).replace('full', 'thumb'));
		$('a#enlarge').attr('title', ($(this).attr('title')));
	});
	
	$('div.product_images ul li:first a').click(function()
	{
		$('a#enlarge').click();
	});
	
	$('div.product_images ul li a').click(function()
	{
		$('img#full').attr('src', ($(this).attr('href')).replace('thumb', 'full'));
		$('img#full').attr('alt', ($(this).attr('alt')));
		$('a#enlarge').attr('href', ($(this).attr('href')).replace('full', 'thumb'));
		$('a#enlarge').attr('title', ($(this).attr('title')));
		return false;
	});
	
	//---------------------------------------------END CUSTOM JS//
	
	$("li.subnav").hover(function()
	{
		$(this).addClass("active");
		if($.browser.msie && $.browser.version < 7)
		{
			if($('ul#nav li.subnav.roller_skates').hasClass('active') || $('ul#nav li.subnav.inline_skates').hasClass('active'))
			{
				$('form.quick_find select').each(function() {
					$(this).css('visibility','hidden');
				});		
			}
			else
			{
				$('form.quick_find select').each(function() {
					$(this).css('visibility','visible');
				});		
			}
		}
		if($("a.nav_search").hasClass("active"))
		{
			$("a.nav_search").removeClass("active");
			$("form.search").animate({ opacity: 0 }, 250);
			
			search_height_timeout = setTimeout(function()
			{
				$("form.search").css("height","0");
			}, 1000);
		}
	},
	function()
	{
		$(this).removeClass("active");
		$('form.quick_find select').each(function() {
			$(this).css('visibility','visible');
		});
	});
	
	$('a[rel*="external"]').click(function()
	{
		open($(this).attr('href'));
		return false;
	});
	
	$('a[rel*="shadowbox"]').each(function()
	{
		if(!$(this).hasClass('page_media'))
		{
			var enlarge_img = $(this).attr('rel').split(' ');
			
			if(enlarge_img != 'undefined' && enlarge_img != '')
			{
				$(this).removeAttr('rel');
				
				if(enlarge_img[1] != undefined)
				{
					$(this).attr('rel', enlarge_img[1]+shadowbox_options);
				}
				else
				{
					$(this).attr('rel', enlarge_img[0]+shadowbox_options);
				}
			}
		}
		else
		{
			var media_rel = $(this).attr('rel');
			
			$(this).removeAttr('rel');
			
			$(this).attr('rel', media_rel+shadowbox_options);
		}
	});
	
	$("ul.thumbs li a img").each(function()
	{
		$.preloadImages.add($(this).attr("src").replace("thumb", "mid"));
	});
	
	$("ul.thumbs li a").mouseover(function()
	{
		$("img#full").attr("src", ($(this).attr('href')).replace("full", "mid"));
		$("img#full").attr("alt", ($(this).attr('alt')));
		$("a#enlarge").attr("href", ($(this).attr('href')).replace("thumb", "full"));
		$("a#enlarge").attr("title", ($(this).attr('title')));
	});
	
	$("ul.thumbs li:first a").click(function()
	{
		$("a#enlarge").click();
	});
	
	$("ul.thumbs li a").click(function()
	{
		$("img#full").attr("src", ($(this).attr('href')).replace("full", "mid"));
		$("img#full").attr("alt", ($(this).attr('alt')));
		$("a#enlarge").attr("href", ($(this).attr('href')).replace("thumb", "full"));
		$("a#enlarge").attr("title", ($(this).attr('title')));
		return false;
	});
	
	if($('a.page_media').length > 0)
	{
		var page_media = $('a.page_media').attr('rel').split(' ');
		
		if(page_media != 'undefined' && page_media != '')
		{
			$('a.page_media').removeAttr('rel');
			$('a.page_media').attr('rel', page_media[1]);
		}
	}
	
	if($("li.help a").length > 0)
	{
		$("li.help a").click(function()
		{
			return false;
		});
		
		var help_timeout = 'undefined';
		
		$("li.help a").bind("mouseenter",function()
		{
			if(help_timeout != 'undefined')
			{
				clearTimeout(help_timeout);
			}
			$("li.help div").fadeOut();
			$(this).next().addClass('active').fadeIn();
		}).bind("mouseleave",function()
		{
			help_timeout = setTimeout(function()
			{
				$('li.help div.active').removeClass('active').fadeOut();
			},2000);
		});
		
		$('input').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
		
		$('select').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
		
		$('textarea').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
	}
	
	if($('a[rel*="shadowbox"]').length > 0)
	{
		if($('a.page_audio').length > 0 || $('a.page_video').length > 0)
		{
			Shadowbox.init({
								continuous		:	true,
								ext				: 	{
										                img		:	['png', 'jpg', 'jpeg', 'gif', 'bmp'],
										                swf		:	['swf'],
										                flv		:	['flv', 'm4v', 'mp4', 'mp3'],
										                qt		:	['dv', 'mov', 'moov', 'movie'],
										                wmp		:	['asf', 'wm', 'wmv'],
										                qtwmp	:	['avi', 'mpg', 'mpeg']
										            },
								players			: 	['img', 'flv', 'iframe'],
								useSizzle		: 	false
							});
		}
		else
		{
			Shadowbox.init({
								continuous		: 	true,
								players			: 	['img'],
								useSizzle		: 	false
							});
		}
	}
	
	if($('.postcode_anywhere').length > 0)
	{
		$.getScript(_root+'media/js/postcode_anywhere.js');
	}
});
