// JavaScript Document

var xmlReq;
var xmlRes;
var timer;

var timeout=5000;
var longtimeout=8000;
 
$(function(){
		   
		
		
		//hover effect on thumbs
		$('.proj img').fadeTo('fast',0.4);			
		function fadeIN(){
			$(this).find('img').fadeTo( 0, 1);	
			}
		function fadeOUT(){
			$(this).find('img').fadeTo( 0, 0.4);	
			}	
		
		$('.proj').mouseover(fadeIN);
		$('.proj').mouseout(fadeOUT);  
		/////
		
		
		
		   
		$('.proj').click(function(e){	
								
					e.preventDefault(); 		  
					if ($(this).hasClass('selected'))return;
					  
					  
					//////////add hover
					$('.proj.selected').mouseover(fadeIN);
					$('.proj.selected').mouseout(fadeOUT);
					$('.proj.selected').find('img').fadeTo( 0, 0.4);	
					 /////////
					 
					$('.proj.selected').removeClass('selected');
					$(this).addClass('selected');
					
					///remove hover
					$('.proj.selected').find('img').fadeTo( 0, 1);	
					$('.proj.selected').unbind('mouseover',fadeIN);
					$('.proj.selected').unbind('mouseout',fadeOUT);   
				///////

	
					var count= $('.proj').length;
					var index= $('.proj').index($(this));
										  
					if (count>5){		  
					  if (index>1&&index<(count-2)) $('#projbarcont').animate({ left: (5-(index-2)*166)+'px' },'fast','swing');					  
					  if (index==1) $('#projbarcont').animate({ left: (5-(index-1)*166)+'px' },'fast','swing');					  
					  if (index==(count-2)) $('#projbarcont').animate({ left: (5-(index-3)*166)+'px' },'fast','swing');						  
					}
					 
					var thref='/projs'+$(this).attr('href').slice(8)+'/info.xml' ;
					
				   location.hash=$(this).attr('href').slice(9);
					
					$("#imagecontin").prepend("<div class='state'>Loading...</div>");
					$("#diskcont-pad").prepend("<div class='state'>Loading...</div>");
					xmlReq=  $.ajax({
								   url: thref,
								   processData: false,
								   dataType: 'xml',
								   success: function(xml){
									   			$("#imagecontin").fadeOut('fast');
												$("#diskcont-pad").fadeOut('fast',updateView);
												
												
												xmlRes=xml;
																												  
																	  
												
									   }
								 });						
						});
		
			
		 
		 function updateView(){
			 	clearTimeout(timer);
				$("#imagecontin").html( '' );
				$("#diskcont-pad").html( '' );
				
				disc_cont='<a target="_blank" href="'
						  +$(xmlRes).find('url').text()
						  +'"><p>'
						  +$(xmlRes).find('stitle').text()
						  +'</a><br/>'
						  +$(xmlRes).find('disc_'+$('#seledlang').attr('alt')).text()+'</p>';
						  
				$("#diskcont-pad").append(disc_cont);
				$("#diskcont-pad").fadeIn();
				
				
				$("#imagecontin").append('<div id="images-bar"></div>');
				$(xmlRes).find('imgs img').each(function(){
						$('#images-bar').append('<a href="/projs/'+$(xmlRes).find('foldername').text()+'/'+$(this).find('fname').text()+'" alt="'+$(this).find('title').text()+'"></a>');								 				});
				
				$('#images-bar a:first').addClass('selected');
				
				$('#images-bar a').click(function(e){
										e.preventDefault(); 
										if ($(this).hasClass('selected')) return;
										$('#images-bar a.selected').removeClass('selected');
										$(this).addClass('selected');
										swapimgs();
										clearTimeout(timer);
										timer=setTimeout("selectnext()",longtimeout);
										
												  });
				
				
				$("#imagecontin").fadeIn();
				
				
				  var img = new Image();
				 $(img).load(function () {
								 $(this).hide();
								  $('#imagecontin').removeClass('loading').append(this);
   								  $(this).fadeIn();
    						}).attr('src', $('#images-bar a.selected').attr('href')).addClass('theimg');
				 
				 
				 timer=setTimeout("selectnext()",timeout);

			 }
		
			 
	
		
		function leftArClick (e){
					  e.preventDefault(); 
					  var x=parseInt($('#projbarcont').css('left'));
					  if (x <5) {	
							$('#arrow_left').unbind('click', leftArClick);
							$('#projbarcont').animate({ left: (x+166)+'px' },'fast','swing',function(){ $('#arrow_left').click(leftArClick); });
					   }		
					
					   
							}
							
							
		function rightArClick(e){
					  e.preventDefault(); 
					   var count= $('.proj').length;
					  var x=parseInt($('#projbarcont').css('left'));
					if (x > 5-((count-5)*166) ){
						$('#arrow_right').unbind('click', rightArClick);
						$('#projbarcont').animate({ left: (x-166)+'px' },'fast','swing',function(){ $('#arrow_right').click(rightArClick); });						
					}
					
								}					
		
		
		
		$('#arrow_left').click(leftArClick);
		$('#arrow_right').click(rightArClick);
		   
		   
		 $('.proj').bstip({color:'tooltip' ,opacity:1, speed:0});
		 
		 
		 
		 $('#home-choose').mouseover(function(e){											 
							$('#home-choose img').animate({top:-10}, {duration: 100, easing: 'easeOutExpo'}).animate({top:0}, {duration: 1000, easing: 'easeOutBounce'});
 											 });
		 
		 
		 
		 	///load from hash
		if (location.hash){
				
			$('.proj').each(function(){									 
										if ($(this).attr('href').slice(9) == location.hash.slice(1))  $(this).click();										
										});			
			}
		////	
  
		   });//end of on ready



function selectnext(){
	
			var nextim =$('#images-bar a.selected + a');
			var torem =$('#images-bar a.selected');
			
			if (nextim.length==1){
			nextim.addClass('selected');
			torem.removeClass('selected');				
			}else{
				torem.removeClass('selected');	
				$('#images-bar a:first').addClass('selected');
				}
			
			swapimgs();	
			timer=setTimeout("selectnext()",timeout);
			}	 
			
	function swapimgs(){
				
				$('.theimg').fadeOut('fast',function(){$(this).remove();});
				
				var img = new Image();
				 $(img).load(function () {
								  $(this).hide();
								  $('#imagecontin').removeClass('loading').append(this);
   								  $(this).fadeIn();
    						}).attr('src', $('#images-bar a.selected').attr('href'))
				 			  .addClass('theimg')
				 			  .attr('alt',$('#images-bar a.selected').attr('alt'));
				
			}			