$(document).ready(function()
{
	if($("input#store_url").length > 0)
	{
		$("input#store_url").focus(function()
		{
			if($("input#store_url").val() == 'http://www.your-store.com')
			{
				$("input#store_url").val('http://www.');
			}
		});
		
		$("input#store_url").blur(function()
		{
			if($("input#store_url").val() == '')
			{
				$("input#store_url").val('http://www.your-store.com');
			}
		});
	}
	else
	{
		var qs 	= window.location.href;
		var ar 	= new Array();
		ar		= qs.split("/");
		
		if(ar[(ar.length-1)] == "#listing")
		{
			$("input#email").focus();
		}
	}
	
	$("a[class='banner']").click(function()
	{
		var root = deriveRoot();
		
		$.ajax({
			type		: "GET",
			url			: root+"count/"+($(this).attr("id"))+"/",
			success		: function() { return true; }
		});
		return false;
	});
	
	$("a[class='store']").click(function()
	{
		var root = deriveRoot();
		
		$.ajax({
			type		: "GET",
			url			: root+"count/"+($(this).attr("id"))+"/",
			success		: function() { return true; }
		});
		return false;
	});
	
	if($.browser.safari)
	{
		$("ul#nav li a.nav_search").css("height","33px");
		$("form.search").css("top","177px");
	}
	
	if($.browser.opera)
	{
		$("ul#nav li a.nav_search").css("height","34px");
	}
	
	if($("ul#subnav").length > 0)
	{
		if(!$.browser.msie || $.browser.msie.version != 5.5)
		{
			$("ul#subnav").css("display","none");
		}
	}
	
	$(function()
	{  
		if ($.browser.msie && parseInt($.browser.version)< 7)
		{
			$("ul#nav li.subnav").hover(function()
			{  
				$(this).addClass("active");  
            },
			function()
			{
				$(this).removeClass("active");  
            });
			
			$("ul#nav li.subnav ul li.subsubnav").hover(function()
			{  
				$(this).addClass("active");  
            },
			function()
			{
				$(this).removeClass("active");  
            });
		}
	});
	
	$('a[@rel="external"]').click(function()
	{
		open($(this).attr('href'));
		return false;
	});

	// Carousel
	if($('#carousel').length)
	{
		$('div#carousel').carousel();
	}

	if($("body").attr("id") == "search")
	{
		$("form.search").css("opacity","1");
	}
	else
	{
		$("form.search").css("opacity","0");
		
		if($("form.search").length > 0)
		{
			$("input[name='keywords']").focus(function()
			{
				$(this).val('');
			});
			
			$("input[name='keywords']").blur(function()
			{
				if($(this).val() == '')
				{
					$(this).val('Enter Search Keywords');
				}
				
				$("a.nav_search").removeClass("active");
				$("form.search").animate({ opacity: 0 }, 1000);
				
				search_height_timeout = setTimeout(function()
				{
					$("form.search").css("height","0");
				}, 1000);
			});
			
			$("button[name='search']").click(function()
			{
				if($("input[name='keywords']").val() == '' || $("input[name='keywords']").val() == 'Enter Search Keywords')
				{
					return false;
				}
				else
				{
					return true;
				}
			});
		}
		
		$("a.nav_search").hover(function()
		{
			$("a.nav_search").addClass("active");
			$("form.search").css("height","auto");
			$("form.search").animate({ opacity: 1 }, 250);
		},
		function()
		{
			search_timeout = setTimeout(function()
			{
				if($("input[name='keywords']").val() != '')
				{
					$("a.nav_search").removeClass("active");
					$("form.search").animate({ opacity: 0 }, 1000);
					
					search_height_timeout = setTimeout(function()
					{
						$("form.search").css("height","0");
					}, 1000);
				};
			}, 5000);
		});
		
		$("li.subnav").hover(function()
		{
			$(this).addClass("active");
			
			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");
		});
	}
	
	$("a.nav_search").click(function()
	{
		$("input[name='keywords']").focus();
		return false;
	});
	
	$("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;
	});
});

function deriveRoot()
{
  var root = window.location.protocol+'//'+window.location.hostname+'/';
  
  if ( ( pos = window.location.pathname.search('public_html') ) != -1 )
  {
    root += window.location.pathname.substring(1,pos+12)+'dinosolesdealer.co.uk/';
  }
  
  if ( window.location.pathname.search('demo') != -1 )
  {
    root += 'demo/';
  }
  
  return root;
}