$(function() {
	load();

});

var Speed = 10; // 速度(毫秒)
var Space = 10; // 每次移动(px)
var PageWidth = 305; // 翻页宽度
var fill = 0; // 整体移位
var MoveLock = false;
var MoveTimeObj;
var Comp = 0;
var AutoPlayObj = null;

function show() {

	$("#List2").html($("#List1").html());

	$('#ISL_Cont').scrollLeft(fill);
	$("#ISL_Cont").mouseover(function() {
		clearInterval(AutoPlayObj);
	});
	$("#ISL_Cont").onmouseout = function() {
		AutoPlay();
	};
	AutoPlay();
}

function AutoPlay() { // 自动滚动
	clearInterval(AutoPlayObj);
	AutoPlayObj = setInterval('ISL_GoDown();ISL_StopDown();', 5000); // 间隔时间
}
function ISL_GoUp() { // 上翻开始
	if (MoveLock)
		return;
	clearInterval(AutoPlayObj);
	MoveLock = true;
	MoveTimeObj = setInterval('ISL_ScrUp();', Speed);
}
function ISL_StopUp() { // 上翻停止
	clearInterval(MoveTimeObj);
	if ($('#ISL_Cont').scrollLeft() % PageWidth - fill != 0) {
		Comp = fill - ($('#ISL_Cont').scrollLeft() % PageWidth);
		CompScr();
	} else {
		MoveLock = false;
	}
	AutoPlay();
}
function ISL_ScrUp() { // 上翻动作
	if ($('#ISL_Cont').scrollLeft() <= 0) {
		$('#ISL_Cont').scrollLeft(
				$('#ISL_Cont').scrollLeft() + $('#List1').offset().left);
	}
	$('#ISL_Cont').scrollLeft($('#ISL_Cont').scrollLeft() - Space);
}
function ISL_GoDown() { // 下翻
	clearInterval(MoveTimeObj);
	if (MoveLock)
		return;
	clearInterval(AutoPlayObj);
	MoveLock = true;
	ISL_ScrDown();
	MoveTimeObj = setInterval('ISL_ScrDown()', Speed);
}
function ISL_StopDown() { // 下翻停止
	clearInterval(MoveTimeObj);
	if ($('#ISL_Cont').scrollLeft() % PageWidth - fill != 0) {
		Comp = PageWidth - $('#ISL_Cont').scrollLeft() % PageWidth + fill;
		CompScr();
	} else {
		MoveLock = false;
	}
	AutoPlay();
}
function ISL_ScrDown() { // 下翻动作
	if ($('#ISL_Cont').scrollLeft() >= $('#List1').width()) {
		$('#ISL_Cont').scrollLeft(
				$('#ISL_Cont').scrollLeft() - $('#List1').width());
	}
	$('#ISL_Cont').scrollLeft($('#ISL_Cont').scrollLeft() + Space);
}
function CompScr() {
	var num;
	if (Comp == 0) {
		MoveLock = false;
		return;
	}
	if (Comp < 0) { // 上翻
		if (Comp < -Space) {
			Comp += Space;
			num = Space;
		} else {
			num = -Comp;
			Comp = 0;
		}
		$('#ISL_Cont').scrollLeft($('#ISL_Cont').scrollLeft() - num);
		setTimeout('CompScr()', Speed);
	} else { // 下翻
		if (Comp > Space) {
			Comp -= Space;
			num = Space;
		} else {
			num = Comp;
			Comp = 0;
		}
		$('#ISL_Cont').scrollLeft($('#ISL_Cont').scrollLeft() + num);
		setTimeout('CompScr()', Speed);
	}
}

function load() {

	$
			.ajax({
				cache : false,
				url : '/resources/cncgcghome.xml',
				dataType : 'xml',
				success : function(data) {
					var root = $(data);
					// var bigpath = root.attr('bigIcon');
					// var smallpath = root.attr('simIcon');
					var htmlIcon = [], htmlImg = [];
					// htmlIcon.push('<div id="List1">');
					$(data)
							.find('item')
							.each(
									function() {
										var item = $(this);
										var image = item.attr("image");
										var icon = item.attr("icon");
										var url = item.attr("url");
										var id = item.attr("id");
										var title = item.attr("title");
										// 小图
										htmlIcon.push('<div class="pic">');
										htmlIcon.push('<div class="guanggao">');
										htmlIcon.push('<a href="');
										htmlIcon.push(url);
										htmlIcon.push('"><img src="');
										htmlIcon.push(icon);
										htmlIcon
												.push('" width="288" height="100" style="margin: 0; padding: 0;" /></a>');
										htmlIcon.push('</div>');
										htmlIcon.push('</div>');

										// //////////////////////////////////
										// 大图
										htmlImg.push('<a href="');
										htmlImg.push(url);
										htmlImg.push('" target="_blank" alt="');
										htmlImg.push(title);
										htmlImg.push('" rel="');
										htmlImg.push(image);
										htmlImg
												.push('" onmouseout="playit();" onmouseover="setfoc(');
										htmlImg.push(id);
										htmlImg.push(');">');
										htmlImg.push(id);
										htmlImg.push('</a>');

									});
					$('#slider_trigger').html(htmlImg.join(""));
					slider_init();
					// htmlIcon.push('</div><div id="List2"></div>');
					$('#List1').html(htmlIcon.join(""));
					show();
				}
			});

}
