/**
 * @author Stephan Köninger
 */
(function($) {
	$.hideSidebar = function(){
		
		if($('#left-sidebar').length > 0)
		{
			/* Preload necessary images */
			var images = new Array();
				images.push('/js/jquery.hideSidebar/img/hide_arrow.png');
				images.push('/js/jquery.hideSidebar/img/show_arrow.png');
				
			for(i = 0; i < images.length; i++)
			{
				var preloadImage = new Image();
					preloadImage.src = images[i];
			}
			
			var style = $('<link />');
				style.attr('type','text/css');
				style.attr('rel','stylesheet');
				style.attr('href','/js/jquery.hideSidebar/css/jquery.hideSidebar.css');
				
			$('head').append(style);
			
			var hideSideBarFunc = function(duration) {
			
				$.cookie('showSidebar', 0);
				
				if(typeof duration != 'number')
					duration = 250;
				
				$('#left-sidebar').css({'position':'absolute'});
				$('#left-sidebar').animate({'left': -$('#left-sidebar').outerWidth()}, duration);
				
				$('#page').css({'overflow':'hidden'});
				$('#page-content').css({'margin-left':250});
				$('#page-content').animate({
					'margin-left':0,
					'width':910
				},
				duration, 
				function() {
					$('#left-sidebar').hide();
					$('#page').css({'overflow':''});
					
					var showSideBarButton = $('<div>');
						showSideBarButton.addClass('showSidebarButton');
						showSideBarButton.hide();
						
					$('#page-content').append(showSideBarButton);
					
						showSideBarButton.fadeIn();
						showSideBarButton.click(showSideBarFunc);
				});
			};
			
			var showSideBarFunc = function() {
	
				$.cookie('showSidebar', 1);
				
				$('#page').css({'overflow':'hidden'});
				$(this).fadeOut(function() { $(this).remove(); });
				$('#left-sidebar').show();
				$('#left-sidebar').animate({
					'left': 0
				});
				$('#page-content').animate({
					'margin-left':250,
					'width':660
				}, function() {
					$('#page-content').css({
						'margin-left': 0
					});
					
					$('#left-sidebar').css({
						'position':''
					});
				});
			};
			
			var hideButton = $('<div>');
				hideButton.addClass('hideSidebarButton');
				hideButton.click(hideSideBarFunc);
			
			var hideButtonWrapper = $('<div>');
				hideButtonWrapper.addClass('hideSidebarWrapper');
				hideButtonWrapper.append(hideButton);
			
			$('#left-sidebar').append(hideButtonWrapper);
			
			if($.cookie('showSidebar') == 0)
				 hideSideBarFunc(0);
		}
	};
})(jQuery);

