$(document).ready(function(){
						   
	
	var tabContentClose = '<div id="tabContentClose"><img src="gfx/tabContentClose.gif" alt="Close" title="Close" /></div>';
	$(tabContentClose).prependTo('div.sideContentContainer');
	$('div.tabs').css( 'display','block');	
	var tabWidth = 270;
	var tabsOpen = false;
	
	//initial state
	$('div#registerContent, div#searchContent, div#tabContentClose').hide();
	$("div.tabContent").css( 'width',0 );
	$(".registerTab, .searchTab").css( 'overflow', 'hidden').find('a').removeAttr("href");

	$(window).scroll( updateLayout ).resize( updateLayout );
	
	
	function checkPage( )
	{
		var startPage =  $("div.main").attr("id");
		
		switch (startPage) { 
			case 'storesPage': 
			case 'articlePage':
				openSearch();
            break;
			
			// has been removed from template
			/*case 'registerPage': 	
				hideTab( $(".registerTab") );
				hideTab( $(".searchTab") );
            break;*/
		}
	};
	
	
	function hideTab( $tab )
	{
		$tab.hide();
	};

	function showTabs( )
	{
		if(tabsOpen == false)
		{
			$("div.tabContent").animate({width: tabWidth }, function() {
				$('div#tabContentClose').animate ({opacity: 'show' });
			});	
			tabsOpen = true;
		}
	};
	
	function openSearch( )
	{
		showTabs();
		$('div#registerContent').stop().animate({opacity: 'hide' }, function() {
			$('div#searchContent').animate({opacity: 'show' });	
		});
		
		$(".registerTab").css( 'width', 30 );
		$(".searchTab").css( 'width', 32 );
	};
	
	function openRegister( )
	{
		showTabs();
		$('div#searchContent').stop().animate({opacity: 'hide' }, function() {
			$('div#registerContent').animate({opacity: 'show' });
		});
		
		$(".searchTab").css( 'width', 30 );
		$(".registerTab").css( 'width', 32 );
	};
	

	$('div#tabContentClose').click(function () 
	{
		$('div#tabContentClose').animate({opacity: 'hide'} , function() {
			$("div.tabContent").animate({ width: 0 });
			$('div#searchContent, div#registerContent').stop().animate({opacity: 'hide' });
			tabsOpen = false;
			
			$(".registerTab , .searchTab").css( 'width', 32 );
		});
	});

	$(".searchTab").click(function () 
	{
		openSearch();
	});
	
	$(".registerTab").click(function () 
	{
		openRegister();
	});
	
	
	function updateLayout( )
	{
	
		var scrollTop = $(window).scrollTop(); 
	    var tabPos =   	scrollTop + ($("div#tabContent").height()/2);	
		
		if( scrollTop < 105 )
		{
			tabPos = tabPos + 105 - scrollTop;
		}
		else if( $("div.tabContent").height() + 105 >= $(window).height() )
		{
			tabPos = 105;
		}
		else
		{
			tabPos = tabPos + 25;	
		}
			
		$("div.sideContent").stop().animate({marginTop: tabPos }, 500 );
		$("div#tabContentClose").stop().animate({marginTop: tabPos-15 }, 500 );	
			
	};
	
	checkPage();

  });
