(function($) {
	$.fn.content_stack = function(){
		//alert("content_stack");
		/**
	 * navR,navL are flags for controlling the albums navigation
	 * first gives us the position of the album on the left
	 * positions are the left positions for each of the 5 albums displayed at a time
	 */
		var navR, navL = false;
		var first = 1;
		var $stack_slider = $(this);
		
		var $preview = $(this).find('.preview');
		
		// number of elements available
		var elems = $preview.children().length;
		
		/*Raster and Positions*/
		if ($(this).find('div.three').length > 0) {
			var $count_view = 3;
			var $offset_element = 233;
		}
		
		if ($(this).find('div.four').length > 0) {
			var $count_view = 4;
			var $offset_element = 233;
		}
		
		if ($(this).find('div.five').length > 0) {
			var $count_view = 5;
			var $offset_element = 187;
		}
		
		
		if ($.browser.msie == true){
			//$stack_slider.find('a.disabled').css('display','none');
		}
		
		
		/*******COLORBOX FOR CONTENTS*******/
		$stack_slider.find('a.content_colorbox').each(function(){
			$(this).colorbox({
				initialWidth: '720px',
				inline: true,
				href: "#" + $(this).attr("href")
			});
			
		});
		
		
		/**
	 * let's position all the albums on the right side of the window
	 */
	if($preview.offset().left == null) return;
		var hiddenRight = 900;
		//var hiddenRight = $(window).width() - $preview.offset().left;
		//alert(hiddenRight);
		$preview.children('div').css('left', hiddenRight + 'px');
		
		
		
		/**
	 * move the first X albums to the viewport
	 */
		$preview.children('div:lt(' + $count_view + ')').each(function(i){
			var $elem = $(this);
			$elem.animate({
				'left': $offset_element * i + 'px',
				'opacity': 1
			}, 800, function(){
				if (elems > $count_view) 
					enableNavRight();
			});
		});
		
		/**
	 * next album
	 */
		$stack_slider.find('.next').bind('click', function(){
			if (!$preview.children('div:nth-child(' + parseInt(first + $count_view) + ')').length || !navR) 
				return;
			disableNavRight();
			disableNavLeft();
			moveRight();
		});
		
		/**
	 * we move the first album (the one on the left) to the left side of the window
	 * the next 4 albums slide one position, and finally the next one in the list
	 * slides in, to fill the space of the first one
	 */
		function moveRight(){
			var hiddenLeft = $preview.offset().left + $offset_element;
			
			var cnt = 0;
			$preview.children('div:nth-child(' + first + ')').animate({
				'left': -hiddenLeft + 'px',
				'opacity': 0
			}, 500, function(){
				var $this = $(this);
				$preview.children('div').slice(first, parseInt(first + $count_view - 1)).each(function(i){
					var $elem = $(this);
					$elem.animate({
						'left': $offset_element * i + 'px'
					}, 800, function(){
						++cnt;
						if (cnt == $count_view - 1) {
							$preview.children('div:nth-child(' + parseInt(first + $count_view) + ')').animate({
								'left': $offset_element * cnt + 'px',
								'opacity': 1
							}, 500, function(){
								//$this.hide();
								++first;
								if (parseInt(first + $count_view - 1) < elems) 
									enableNavRight();
								enableNavLeft();
							});
						}
					});
				});
			});
		}
		
		/**
	 * previous album
	 */
		$stack_slider.find('.prev').bind('click', function(){
			if (first == 1 || !navL) 
				return;
			disableNavRight();
			disableNavLeft();
			moveLeft();
		});
		
		/**
	 * we move the last album (the one on the right) to the right side of the window
	 * the previous 4 albums slide one position, and finally the previous one in the list
	 * slides in, to fill the space of the last one
	 */
		function moveLeft(){
			var hiddenRight = $(window).width() - $preview.offset().left;
			
			var cnt = 0;
			var last = first + $count_view - 1;
			$preview.children('div:nth-child(' + last + ')').animate({
				'left': hiddenRight + 'px',
				'opacity': 0
			}, 500, function(){
				var $this = $(this);
				$preview.children('div').slice(parseInt(last - $count_view), parseInt(last - 1)).each(function(i){
					var $elem = $(this);
					$elem.animate({
						'left': $offset_element * (i + 1) + 'px'
					}, 800, function(){
						++cnt;
						if (cnt == $count_view - 1) {
							$preview.children('div:nth-child(' + parseInt(last - $count_view) + ')').animate({
								'left': 0 + 'px',
								'opacity': 1
							}, 500, function(){
								//$this.hide();
								--first;
								enableNavRight();
								if (first > 1) 
									enableNavLeft();
							});
						}
					});
				});
			});
		}
		
		/**
	 * disable or enable albums navigation
	 */
		function disableNavRight(){
			navR = false;
			$stack_slider.find('a.next').addClass('disabled');
		}
		function disableNavLeft(){
			navL = false;
			$stack_slider.find('a.prev').addClass('disabled');
		}
		function enableNavRight(){
			navR = true;
			$stack_slider.find('a.next').removeClass('disabled');
		}
		function enableNavLeft(){
			navL = true;
			$stack_slider.find('a.prev').removeClass('disabled');
		}
		
		var $ps_container = $('.ps_container');
		var $ps_overlay = $('.ps_overlay');
		var $ps_close = $('.ps_close');
	}

 })(jQuery);



