// JavaScript Document

$(document).ready(function(){
	var t;
	j=1;
	$('div.eachPic:first').css({display:'block'});
	$('#gallery').append('<ul id="gaCount"><li class="nowChoosed"><span></span></li><li><span></span></li><li><span></span></li><li><span></span></li></ul>');
	
	var $count=$('#gaCount li');
	$count.eq(0).addClass('nowChoosed');
	var $pic=$('#gallery');
	$pic.hover(function(){
		clearInterval(t);
	},function(){
		clearInterval(t);
		t = setInterval("showPic()", 2000);
	})
	var aa = $count;
	$count[0];
	$count.each(function(index){
		$(this).click(function(){
			var i=$('#gallerySlider div:visible')[0].id;
			i=i.replace(/pic(\d)/,'$1');
			var newid='#pic'+index;
			var $nextPic=$(newid);
			var $nowPic=$('.eachPic:visible');
			$count.removeClass('nowChoosed');
			$(this).addClass('nowChoosed');
			clearInterval(t);
			if(index > i){
				$nextPic.css({left:'1860px',display:'block',zIndex:'5'});
				$('#gallerySlider').animate({left:'-1860px'});
				$('#gallerySlider').queue(function() {
					$nowPic.css({display:'none',zIndex:'0'});
					$nextPic.css({left:'930px'});
					$('#gallerySlider').css({left:'-930px'});
					$(this).dequeue();
				});
			}
			else if(index < i){
				$nextPic.css({left:'0px',display:'block',zIndex:'5'});
				$('#gallerySlider').animate({left:'0px'});
				$('#gallerySlider').queue(function() {
					$nowPic.css({display:'none',zIndex:'0'});
					$nextPic.css({left:'930px'});
					$('#gallerySlider').css({left:'-930px'});
					$(this).dequeue();
				});
			}
			else{};
			j=index+1;
	        t = setInterval("showPic()", 3000); 
		})
	});
	t = setInterval("showPic()", 3000);
});
function showPic(){
	//change
	var $pic=$('.eachPic');
	if(j<$pic.length&&j>=0){
        $("#gaCount li").eq(j).trigger('click');
	}
	else{
		j=0;
        $("#gaCount li").eq(j).trigger('click');
	}
};
