var admin=false;
$(document).ready(function()
{
	function transition(base, id, time, prev)
	{
		if(!admin)
		{
			if($(base+'-'+id).length)
				$(base+'-'+id).fadeIn(time, function()
				{
					$(base+'-'+prev).hide(0, function()
					{
						transition(base, parseInt(id)+1, time, id);
					});
				});
			else
			{
				$(base+'-'+1).show(0, function()
				{
					$(base+'-'+prev).fadeOut(time, function()
					{
						transition(base, 2, time, 1);
					});
				});
			}
		}
	}
	
	function update_cookie(method, id)
	{
		if(method=='add')
		{
			$.ajax(
				{
					'url':'index.php?r=site/refreshCookie&method=add&id='+id,'cache':false,'success':function(html)
					{
						//alert(html);				
					}
				});
		}
			
		if(method=='remove')
		{
			$.ajax(
				{
					'url':'index.php?r=site/refreshCookie&method=remove&id='+id,'cache':false,'success':function(html)
					{
						//alert(html);					
					}
				});
		}
	}
	
	function open_menu()
	{
		$.ajax(
		{
			'url':'index.php?r=site/getCookie','cache':false,'success':function(html)
			{
				var id_array=html.split('|');
		
				for(var i in id_array)
				{
					$('#drop-'+id_array[i]).click();
				}			
			}
		});
		
		
	}
	
	$('.slideshow').each(function()
	{
		var base=$(this).attr('id').split('-')[1];
		var time=$(this).attr('id').split('-')[2];
		var seed=$('[id^=slideshow-'+base+'-]').size();
		var random=(Math.floor(Math.random()*(parseInt(seed)-1))+1);
		$('#slideshow-'+base+'-'+random).show();
		transition('#slideshow-'+base, (parseInt(random)+1), parseInt(time), random);
	});
	
	$( ".mainmenu-1" ).mouseenter(function()
	{
		$(this).animate({backgroundColor: "#ee0"}, 200 );
	});
	$( ".mainmenu-1" ).mouseleave(function()
	{
		$(this).animate({backgroundColor: "#ff0"}, 200 );
	});
	
	$( ".mainmenu-2" ).mouseenter(function()
	{
		$(this).animate({backgroundColor: "#ed0"}, 200 );
	});
	$( ".mainmenu-2" ).mouseleave(function()
	{
		$(this).animate({backgroundColor: "#fe0"}, 200 );
	});
	
	$( ".mainmenu-3" ).mouseenter(function()
	{
		$(this).animate({backgroundColor: "#ec0"}, 200 );
	});
	$( ".mainmenu-3" ).mouseleave(function()
	{
		$(this).animate({backgroundColor: "#fd0"}, 200 );
	});
	
	$('input[type=text]').keypress(function(event)
	{
		if (event.keyCode == '13')
			event.preventDefault();
	});
	
	$('.drop_handle').live('click', function()
	{
		var id=$(this).attr('id').split('-')[1];
		$('#submenu-'+id).toggle();
		if($('#submenu-'+id).is(':visible'))
		{
			$('#menu-'+id).css('font-size', '90%').css('font-weight', 'bold');
			update_cookie('add',id);
		}
		else
		{
			$('#menu-'+id).css('font-size', '100%').css('font-weight', 'normal');
			update_cookie('remove', id);
		}
	});
		
	open_menu();

	$('#search-button').live('click', function()
	{
		$('#site-search').submit();
	});
});
