var images_arr = new Array();
var previous_img = null;



$(document).ready(function(){
	var images_count = $('div.top-image img').length;
	for(var i = 0;i < images_count; i++){
		images_arr[i] = $('div.top-image img')[i];
		if(i!=0)
			$(images_arr[i]).css('opacity',0);
	}
	initTopImages(0);	
	initPopup();
	initTabs();
	
});

/**
 *	show next image, and hide previous
 */ 

function initTopImages(numb){
	var images_count = $('div.top-image img').length;	
	var this_image = $('div.top-image img')[numb];
	
	if(previous_img!=null){
		//$(previous_img).hide();
		$(previous_img).animate({opacity: 0}, 800);
		//$(previous_img).css('display', 'none');
	}	
	//$(this_image).show();	
	$(this_image).css('display','block');
	$(this_image).animate({opacity: 1}, 800);	
	numb++;
	if(numb >= images_count)
		numb = 0;
	previous_img = this_image;
	setTimeout('initTopImages(' + numb + ')', 8000);	
}

// small popup in the bottom of page
function initPopup(){
	var popup = $('div#page');
	var open_link = $('div.share div.right');
	open_link.click(function(){
		var pop_h = popup.height();
		var pop_w = popup.width();
		var top_pos = $('div.right').offset().top - pop_h - 5;
		var left_pos = $('div.right').offset().left + $('div.right').width() + 5 - pop_w;
		popup.css('top',top_pos);
		popup.css('left',left_pos);
		popup.toggle(200);
		return false;
	});
}

//toggle Photos and Videos tabs
var a_ind = 0;
function initTabs(){
	$('#tabs li a').click(function(){
		$(this).attr('class', 'active');		
		if ($(this).attr('rel') == 'videos') {			
			$('#tabs li a[@rel=photos]').attr('class', '');
			$('#photos-list').hide();
			$('#videos-list').show();			
		} else {			
			$('#tabs li a[@rel=videos]').attr('class', '');
			$('#videos-list').hide();			
			$('#photos-list').show();
		}
		return false;
	});
	
	//each article
	$('#photos-list .image a').each(function(){
		this.rel = a_ind;
		a_ind++;
	});
	$('#photos-list div.image a').click(function (){		
		var image_container = $('#img_container a')[0];
		$(image_container).empty().append(photos[this.rel].html);		
		return false;		
	});
	a_ind = 0;
	$('#videos-list .image a').each(function(){
		this.rel = a_ind;
		a_ind++;
	});
	$('#videos-list div.image a').click(function (){		
		var image_container = $('#img_container a')[0];		
		$(image_container).empty().append(videos[this.rel].html);		
		return false;		
	});
}

