var Site = {
	
	domready: function()
	{
		if ( $.browser.msie )
			$('body').addClass('ie');
		
		try
		{
			Cufon.replace('h1, h2, label', {
				fontFamily: 'Helvetica LT CondensedBlack'
			});
		}
		catch(error)
		{
			//alert(error)
		}
		
		// Prevent user's right click
		var $body = $('body');
		$body
			.bind("contextmenu", function(event) {
				event.preventDefault();
			});
		
			
		var $links = $('#content ul.links');
		if ( $links.length )
		{
			var $elements = $('#content .element');
			$elements.filter(':not(:first)').hide();
			
			$links.find('li a').each(function(e){
				var link = $(this);
				var list = link.parent();
				
				link.click(function(l){
					
					if ($(this).is(':not(.hidden)'))
					{
						var length = $elements.length,
							currentIndex = $elements.index($elements.filter(':visible')),
							calculatedIndex = 0;
						
						if ( list.hasClass('prev') )
						{
							var $prev = $elements.filter(':visible').prev(),
								calculatedIndex = $elements.index($prev);
							
							if (currentIndex == length)
							{
								$prev = $elements.filter(':last');
								calculatedIndex = length;
							}
						}
						else if ( list.hasClass('next') )
						{
							var $next = $elements.filter(':visible').next(),
								calculatedIndex = $elements.index($next);
							
							if (currentIndex == (length - 1))
							{
								$next = $elements.filter(':first');
								calculatedIndex = 0;
							}
						}
						
						if ( calculatedIndex == (length - 1) )
						{
							$links.find('li.prev a').removeClass('hidden');
							$links.find('li.next a').addClass('hidden');
						}
						else if ( calculatedIndex > 0 )
						{
							$links.find('li.prev a').removeClass('hidden');
							$links.find('li.next a').removeClass('hidden');
						}
						else
						{
							$links.find('li.prev a').addClass('hidden');
							$links.find('li.next a').removeClass('hidden');
						}
						
						//Cufon.refresh();
						
						$elements
							.eq(calculatedIndex)
							.show()
							.siblings('.element')
							.hide();	
					}
					
					return false;
				});
			});
			
			$links.find('li.prev a').addClass('hidden');
		}
		
		// Set width for overflow list items
		if ( $('.overflow').length )
		{
			var pagingElements = $('.overflow a'),
				totalWidth = 0;
			
			pagingElements.each(function(){
				totalWidth += $(this).outerWidth() + 1;
			});
			
			if ( totalWidth > 0 )
				$('.overflow ul.paging').width(totalWidth);
			
			$(window).resize(function()
			{
				checkPosition( $('.overflow a.selected').parent() );
			});
		}
		
		var $subnavs = $('#nav .navigation ul ul'),
			hideTimer = 0;
		if ( $subnavs.length )
		{
			$subnavs.each(function()
			{
				var ul = $(this);
				
				ul.prev().hover(
					function()
					{
						clearTimeout(hideTimer);
						$subnavs.hide();
						ul.show();
					},
					function()
					{
						hideTimer = setTimeout(function()
						{
							$subnavs.hide();
						}, 300);
					});
				
				ul.hover(
					function()
					{
						clearTimeout(hideTimer);
						$subnavs.hide();
						ul.show();
					},
					function()
					{
						hideTimer = setTimeout(function()
						{
							$subnavs.hide();
						}, 300);
					});
			});
		}
		
		
		
		var $paging = $('#paging');
		if ( $paging.length )
		{
			var $nav = $('#nav'),
				timer = 0;
			
			if ( $nav.length )
			{
				timer = setTimeout(function()
				{
					$nav
						.stop()
						.animate({
							top: '-59px'
						}, 700);
				}, 1500);
				
				$nav.hover(
					function ()
					{
						clearTimeout(timer);
						
						$nav
							.stop()
							.animate({
								top: '0'
							}, 400);
					}, 
					function ()
					{
						timer = setTimeout(function()
						{
							$nav
								.stop()
								.animate({
									top: '-59px'
								}, 700);
						}, 1500);
					}
				);
	
			}
		}
	},
	
	RemoveDefaultInputValue:
	{
		init: function($inputitem)
		{
			$inputitem.each(function()
			{
				Site.RemoveDefaultInputValue.setRemoveAction($(this));
			})
		},
		setRemoveAction: function($inputitem)
		{
			$("form").submit(function()
			{
				if ($inputitem.val() == $inputitem.attr("title"))
				{
					$inputitem.val("");
				}
				return true;
			});
			
			$inputitem.bind("focus", function(event)
			{
				if ($inputitem.val() == $inputitem.attr("title"))
				{
					$inputitem.val("");
				}
			}).bind("blur", function(event)
			{
				if ($inputitem.val() == "")
				{
					$inputitem.val($inputitem.attr("title"));
				}
			});
		}
	},
	
	randomImages: function(images, element)
	{
		var x = Math.floor(Math.random() * images.length);
		var randomImage = images[x];
		
		if ( $(element).length )
			$(element)
				.attr('src', randomImage)
				.css('display','block');
	}
};

$(document).ready( Site.domready );
$(window).load( Site.load );
