﻿var timer;
var sums;
var Ttimer;
var li_width
//图片移动过程中点击无效
var move = false;

function $$$I(id){
	return"string"==typeof id?document.getElementById(id):id
}

function mycarousel_getItemHTML(item)
{
	if(sums<=scroll){
		return '<li view_image_index="'+item.title+'"><a href="'+item.link+'"><img title="" src="'+item.url+'"/></a></li>';
	}else{
		return '<li onmouseover="stopAuto()" onmouseout="setAuto()" view_image_index="'+item.title+'"><a href="'+item.link+'"><img title="" src="'+item.url+'"/></a></li>';
	}
};

$(document).ready(function(){
	sums=mycarousel_itemList.length;//图片总数
	if(sums > 0){
		for(i=(sums-1);i>(sums-(scroll+1));i--){
			if(i<0){
				break;
			}
			$("#Imgs").append(mycarousel_getItemHTML(mycarousel_itemList[i]));
		}
		
		//设置ul的width
		li_width = parseInt($("#Imgs > li").css('width')) + parseInt($("#Imgs > li").css('padding-left')) + parseInt($("#Imgs > li").css('padding-right'));
		var total_width = li_width * (scroll+2);
		$("#Imgs").css("width",total_width);
	
		if(sums>scroll){
			setAuto();
			$("#img-right").css("cursor","pointer");
			$("#img-left").css("cursor","pointer");
	
			$("#img-left").click(function(){
				stopAuto()
				//判断图片是否正在移动
				if(!move){
					//移动开始
					move = true;
					var firstindex=$("#Imgs li").eq(0).attr("view_image_index");
					if((parseInt(firstindex)-scroll)>0){
						var appendindex=parseInt(firstindex)-scroll;
					}else{
						var appendindex=parseInt(firstindex)-scroll+sums;
					}
					$("#Imgs").append(mycarousel_getItemHTML(mycarousel_itemList[appendindex-1]));
					$("#Imgs").animate({
						left:-li_width
							}, s_time, function(){
						$("#Imgs").css("left",'0px');
						$("#Imgs li").eq(0).remove();
						//移动结束
						move=false;
					});
				}
			});
			$("#img-right").click(function(){
				stopAuto()
				//判断图片是否正在移动
				if(!move){
					//移动开始
					move = true;
					var lastindex=$("#Imgs li").eq(scroll-1).attr("view_image_index");
					if((parseInt(lastindex)+scroll)>sums){
						var prependindex=parseInt(lastindex)+scroll-sums;
					}else{
						var prependindex=parseInt(lastindex)+scroll;
					}
					$("#Imgs").prepend(mycarousel_getItemHTML(mycarousel_itemList[prependindex-1]));
					$("#Imgs").css("left",'-'+li_width+'px');
					$("#Imgs").animate({
						left:0
							}, s_time, function(){
						$("#Imgs").css("left",'0px');
						$("#Imgs li").eq(scroll).remove();
						//移动结束
						move=false;
					});
				}
			});
		}
	}	
});

function setAuto() {
	if(sums>scroll){
	    timer=setInterval(function(){
	    	//移动开始
	 	   	move = true;
	 	   	var index=0;
			var firstindex=$("#Imgs li").eq(0).attr("view_image_index");
				if((parseInt(firstindex)-scroll)>0){
					var appendindex=parseInt(firstindex)-scroll;
				}else{
					var appendindex=parseInt(firstindex)-scroll+sums;
				}
				$("#Imgs").append(mycarousel_getItemHTML(mycarousel_itemList[appendindex-1]));
				$("#Imgs").animate({
					left:-li_width
						}, s_time, function(){
					$("#Imgs").css("left",'0px');
					$("#Imgs li").eq(0).remove();
					//移动结束
					move=false;
				});
		},(s_time+1000))
	}
}

function stopAuto() {
    clearInterval(timer);
}


