// JavaScript Document

// v text state
function vtipshow(obj,mytoppos){ 
//$('#'+obj).toggle();
	var vremove = function() { 
		$('div .vshow').each(
			function(){
				$(this).removeClass("vshow").addClass("vhide");
				$(this).css('display','none');
			}
		);
	};
	
	if ($('#'+obj).hasClass('vshow')){
		$('#'+obj).css('display','none');
		$('#'+obj).removeClass("vshow").addClass("vhide");
	} else {
		vremove();
		$('#'+obj).css({'top': mytoppos + 'px' , 'display':'block'});
		$('#'+obj).removeClass("vhide").addClass("vshow");
	};

};	

$(document).ready(function() {
	


/**
				* 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 positions 	= {
					'0'		: 0,
					'1' 	: 247,
					'2' 	: 494,
					'3' 	: 741,
					'4' 	: 988
				}
				
				var $ps_albums = $('#ps_albums');
				/**
				* number of albums available
				*/
				/* var elems = $ps_albums.children().length; */
				var elems = $ps_albums.children('.ps_album').length;				
				
				var $ps_slider		= $('#ps_slider');
				
				/* let's position all the albums on the right side of the window */
				var hiddenRight 	= $(window).width() - $ps_albums.offset().left;
				var mywindow = $(window).width();
				var myoffset = $ps_albums.offset().left;
				var timeout    = 10000;
				var runtimer = true;
				
				
				$ps_albums.children('div.ps_album').css({'left': hiddenRight + 50 + 'px','display':'none'});
				
				
				/**
				* move the first 4 albums to the viewport
				*/
				$ps_albums.children('div.ps_album:lt(4)').each(
					function(i){
						var $elem = $(this);
						/* $elem.animate().css('right', 267); */
						$elem.animate().css('display','block');
						$elem.animate({'left': positions[i] + 'px'},800,function(){
							if(elems > 4)
								enableNavRight();
						});
					}
				);
				
				
				/**
				* next album
				*/
				$('.next').bind('click',function(){
					if(!$ps_albums.children('div.ps_album:nth-child('+parseInt(first+4)+')').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 = $ps_albums.offset().left + 600;
					
					var cnt = 0;
					$ps_albums.children('div.ps_album:nth-child('+first+')').animate({'left': - hiddenLeft + 'px'},500,function(){
							var $this = $(this);
							$this.fadeOut();
							$ps_albums.children('div.ps_album').slice(first,parseInt(first+3)).each(
								function(i){
									var $elem = $(this);
									$elem.animate({'left': positions[i] + 'px'},800,function(){
										++cnt;
										if(cnt == 3){
											$ps_albums.children('div.ps_album:nth-child('+parseInt(first+4)+')').css('display','block').animate({'left': positions[cnt] + 'px'},500,function(){
												
												if(parseInt(first + 4) < elems)
												enableNavRight();
												enableNavLeft();
												++first;
											});
										}		
									});
								}
							);		
					});
				}
				
				/**
				* previous album
				*/
				$('.prev').bind('click',function(){
					if(first==1  || !navL) return;
					disableNavRight();
					disableNavLeft();
					moveLeft();
					runtimer = false;
				});
				
								
				/**
				* 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() - $ps_albums.offset().left;
				
					var cnt = 0;
					var last= first+3;
					$ps_albums.children('div.ps_album:nth-child('+last+')').animate({'left': hiddenRight + 'px'},500,function(){
							var $this = $(this);
							$this.fadeOut();
							$ps_albums.children('div.ps_album').slice(parseInt(last-4),parseInt(last-1)).each(
								function(i){
									var $elem = $(this);
									$elem.animate({'left': positions[i+1] + 'px'},800,function(){
										++cnt;
										if(cnt == 3){
											$ps_albums.children('div:nth-child('+parseInt(last-4)+')').css('display','block').animate({'left': positions[0] + 'px'},500,function(){
												--first;
												enableNavRight();
												if(first > 1)
												enableNavLeft();
											});
										}										
									});
								}
							);
					});
				}
				
				
				/* disable or enable albums navigation
				*/
				function disableNavRight () {
					navR = false;
					$('.next').addClass('disabled');
				}
				function disableNavLeft () {
					navL = false;
					$('.prev').addClass('disabled');
				}
				function enableNavRight () {
					navR = true;
					$('.next').removeClass('disabled');
				}
				function enableNavLeft () {
					navL = true;
					$('.prev').removeClass('disabled');
				}		
				
				var $ps_container 	= $('#ps_container');
				var $ps_overlay 	= $('#ps_overlay');
				var $ps_close		= $('#ps_close');
				
				
				
				/* home auto animate */
				function animalbums(){
					//alert("hi");
					if (runtimer){
						if(!$ps_albums.children('div.ps_album:nth-child('+parseInt(first+4)+')').length)  return;
					disableNavRight();
					disableNavLeft();
					moveRight();
					Func1Delay();
					}
				}
				
				//set an interval
				function runtimer(){
					setInterval(animalbums, timeout);
				}
				
				function Func1Delay(){
					window.setTimeout(animalbums, timeout);
					//setInterval(animalbums, timeout);
					}
				
				Func1Delay();	
});



