
function gallery_move(num){
	
	$('.gallery-nav .btn.current').removeClass('current');
	
	var current_item = $('#btn_' + num);
	
	mousemove = setTimeout(function(){
		
		var article_holder 	= $('#article_content_' + num);
		var gallery 		= $('.gallery-holder');
		var new_title 		= article_holder.find('.title').html();
		var new_content 	= article_holder.find('.content').html();
		var new_src 		= article_holder.find('.image').attr('src');
		var channelName 	= article_holder.attr('channel_name');
		var title 		= gallery.find('.pic-content H3');
		var content 		= gallery.find('.pic-content p');
		var img 		= gallery.find('.display-pic img');
		
		$('#article-channel').html(channelName);
		if(article_holder.attr('cat_name')) $('.gallery-holder .now-at').html('Now at <b>'+article_holder.attr('cat_name').toUpperCase()+'</b>');
		
		title.fadeOut('slow',function(){
			$(this).html(new_title).fadeIn('slow');
		});
		
		content.fadeOut('slow',function(){
			$(this).html(new_content).fadeIn('slow');
		});
		
		$('.pic-clicker').click(function(){
			document.location.href = article_holder.find('.title A').attr('href');
		});
		
		img.fadeOut('slow',function(){
			current_item.addClass('current');
//			$(this).attr({ 'src':new_src, 'title':new_title, 'alt':new_title }).load(function(){ $(this).fadeIn('slow'); });
			$(this).attr({ 'src':new_src, 'title':new_title, 'alt':new_title }).fadeIn('slow');
		});
		
		// remove all classes, add class for the current article's channel
		if(_cur_channel_id==0 && article_holder.attr('channel_id')!='') $('.display-pic').next().removeClass().addClass('gallery-bg-'+article_holder.attr('channel_id'));
		
	}, btn_delay);
	
}


function gallery_inc(){
	var current_item = $('.btns').find('.current');
	current_item = parseInt(current_item.attr('id').split('_').slice(-1));
	var next_item = (current_item < gallery_count-1 ? current_item+1 : 0);
	//console.log('next_item = '+next_item);
	gallery_move(next_item);
}

$(document).ready(function(){
	//switch pics automatically
	interval = setInterval(gallery_inc,timeout);
	
	//check if the current picture& text is the right text that should be displayed according to the selected pic
//		setInterval(get_current_pic,1000);
	
	// preload bg image
	$("<img>").attr('src','/_media/images/newhome/top_flash_bg.png');
	$("<img>").attr('src','/_media/images/newhome/top_flash_bg_1.png');
	$("<img>").attr('src','/_media/images/newhome/top_flash_bg_2.png');
	$("<img>").attr('src','/_media/images/newhome/top_flash_bg_3.png');
	$("<img>").attr('src','/_media/images/newhome/top_flash_bg_4.png');
	$("<img>").attr('src','/_media/images/newhome/top_flash_bg_5.png');
	$("<img>").attr('src','/_media/images/newhome/top_flash_bg_6.png');
	
	$('.gallery-nav .btns .btn').hover(function(){
		clearInterval(interval);
		clearTimeout(mousemove);
		var num = $(this).attr('id').split('_').slice(-1);
		gallery_move(num);
	});
	
	$('.gallery-nav .next,.gallery-nav .prev').click(function(){
		clearInterval(interval);
		clearTimeout(mousemove);
		var current_item = $('.btns').find('.current');
		current_item = parseInt(current_item.attr('id').split('_').slice(-1));
		var next_item;
		
		if($(this).is('.next')){
			if(current_item < gallery_count-1){
				next_item = current_item +1;
			}else{
				next_item = 0;
			}
		}else if($(this).is('.prev')){
			if(current_item > 0){
				next_item = current_item-1;
			}else{
				next_item = gallery_count-1;
			}
		}
		
		gallery_move(next_item);
	});
});
