// JavaScript Document
var can = 0;

jQuery(document).ready(function(){
	
	jQuery('#hGallery img').fadeTo(1,0);
	jQuery('#hGallery img').parent('a').css('visibility','hidden');
	jQuery('#hGallery img:first').fadeTo(300,1,function(){
		
		jQuery('#hGallery img:first').parent('a').css('visibility','visible');
		
	 	can = 1;
		
		jQuery('#hGallery img').each(function(){
		
		jQuery('#squareRow').append('<div class="square"></div>');
		jQuery('#squareRow .square').fadeTo(1,0.5);
		jQuery('#squareRow .square:first').fadeTo(1,1);
		jQuery('#squareRow .square:first').addClass('active');

	    });	
		
		jQuery('.square').click(function(el){
		n = jQuery(this).index();
		if(can){
		changer(n,'yes');
		}
		
		});
		
		
		invl  = setInterval( "clicker()", 4000 );
		tmout = 0;
		
	});
	
	
});

current = 0;  

    function changer(n,p){
	
	 // can = 0;
		
	if(p=='yes'){
	clearInterval(invl);	
	if(tmout){
	  clearTimeout(tmout);	
	}
	tmout = setTimeout(function(){
	  
	  invl  = setInterval( "clicker()", 4000 );
						
	}, 10000);
	
	
	
	}
	
	current = n;
		
	if(!jQuery('#squareRow .square:eq('+current+')').hasClass('active')){	
	
          jQuery('#hGallery img.active').fadeTo(200,0);
		  jQuery('#hGallery img.active').parent('a').css('visibility','hidden');
		  jQuery('#hGallery img.active').removeClass('active');
		
		  jQuery('#hGallery img:eq('+current+')').fadeTo(200,1,function(){
			
			 jQuery('#hGallery img:eq('+current+')').addClass('active');
			
		  });			 												
		  jQuery('#hGallery img:eq('+current+')').parent('a').css('visibility','visible');
				
			    jQuery('#squareRow .square').fadeTo(1,0.5);
				jQuery('#squareRow .square').removeClass('active');
				jQuery('#squareRow .square:eq('+current+')').addClass('active');	
				jQuery('#squareRow .square:eq('+current+')').fadeTo(1,1);			
 
		 
		
	  }	
	}

	
	function clicker()
	{ 
	  
	
	  if(current==jQuery('#squareRow .square:last').index()){
	  	  current = 0;  
	  }
	  else
	  {
		 current++;  
	  }
	  if(can){
	  changer(current,'no');
	  }
	}
