﻿var isIE = (document.all && window.ActiveXObject && !window.opera) ? true : false;

var in_opacity = 0;
var out_opacity = 100;
var last_on = "pic0";

var wait = 1;
var onetime = 0;
var onetime1 = 0;
var auto;

function standby() {
	preloadimg("images/poster0.jpg", "pic0");
	preloadimg("images/poster1.jpg", "pic1");
	preloadimg("images/poster2.jpg", "pic2");
	preloadimg("images/poster3.jpg", "pic3");
	preloadimg("images/poster4.jpg", "pic4");

	var pic0 = document.getElementById("pic0");
	pic0.style.filter = "alpha(opacity=0)";
	pic0.style.opacity = 0;

	var pic2 = document.getElementById("pic2");
	pic2.style.filter = "alpha(opacity=100)";
	pic2.style.opacity = 1;
	changeBtnPic("pic2");
	last_on = "pic2";

	auto = setInterval("autoPlay()", 2000);
}

function autoPlay() {
	var no = Number(last_on.substr(last_on.length - 1, last_on.length));

	if (no == 4) {
		no = 0;
	}
	else {
		no = 1 + no;
	}

	no = "pic" + no;
	changePic(no);
}

function preloadimg(url, objName) {
	var img = new Image();

	var obj = document.getElementById(objName);

	img.onload = function() {
		obj.style.backgroundImage = "url('" + url + "')";
	};

	img.onerror = function() {
		obj.innerHTML = "<p>图片加载失败！</p>";
	};

	img.src = url;
}

function fadeIn(id) {	
	var a = document.getElementById(id);
	in_opacity = in_opacity + 25;
	a.style.filter = "alpha(opacity=" + in_opacity + ")";
	a.style.opacity = in_opacity / 100;

	if (in_opacity < 100) {
		setTimeout(function() { fadeIn(id); }, 50);
	} else {
		in_opacity = 0;
	}
}

function fadeOut(id) {
	var a = document.getElementById(id);
	out_opacity = out_opacity - 25;
	a.style.filter = "alpha(opacity=" + out_opacity + ")";
	a.style.opacity = out_opacity / 100;
	if (out_opacity > 0) {
		setTimeout(function() { fadeOut(id); }, 50);
	} else {
		out_opacity = 100;
		onetime1 = 0;
	}
}

function changeBtnPic(id) {
	var no1 = Number(id.substr(id.length - 1, id.length));
	var no2 = Number(last_on.substr(last_on.length - 1, last_on.length));

	var a = document.getElementById("nav_btn" + no1);
	var b = document.getElementById("nav_btn" + no2);

	a.className = "nav_item_hover";
	b.className = "nav_item";
}

function changePic(id) {
	if (onetime == 0) {
		onetime = 1;
	}
	if (wait == 1) {
		if (id != last_on) {
			fadeIn(id);
			fadeOut(last_on);
			changeBtnPic(id);
			last_on = id;
			wait = 0;
			setTimeout("wait=1;", 300);
		}
	}
}

function btnOut(id) {
	auto = setInterval("autoPlay()", 2000);
}

function clickpic(id) {
	clearInterval(auto);
	changePic("pic" + id);

}
