var max_index=0;
var current_index=0;
var current_pos_left=0;
var action_pos_left=0;
var scroll_speed=10;
var current_opacity=0;
var action_direct=0;
var action_timer=null;
var action_speed=8000;
var min_scroll_speed=10;
var current_text_opacity=60;

var image_w=280;
var image_h=210;
var thumb_w=64;
var thumb_h=48;
var thumb_sw=48;
var thumb_sh=36;
var thumb_stop=false;

var scroll_min_pixel=0;





function initScrollBar(){ 
	for (var i = 0; i < imgArray.length; i++) {
		var obj_link = document.createElement("a");
		obj_link.href = linkArray[i];
		obj_link.target = "_blank";
		var obj_img = document.createElement("img");
		obj_img.src = imgArray[i];
		obj_img.alt = tipArray[i];
		obj_link.appendChild(obj_img);
		document.getElementById("scroll_content").appendChild(obj_link);

		var obj_more_link = document.createElement("a");
		obj_more_link.href = linkArray[i];
		obj_more_link.title = tipArray[i];
		obj_more_link.target = "_blank";
		var obj_more_img = document.createElement("img");
		obj_more_img.src = imgArray[i];
		obj_more_img.alt = tipArray[i];
		obj_more_img.setAttribute("rel", i);
		obj_more_img.setAttribute("id", "obj_more_img_" + i);
		obj_more_img.onmouseover = changeIndex;
		obj_more_img.onmouseout = unchangeIndex;
		obj_more_img.style.width=thumb_sw+"px";
		obj_more_img.style.height=thumb_sh+"px";
		obj_more_link.appendChild(obj_more_img);
		document.getElementById("scroll_more_thumbs").appendChild(obj_more_link);


		document.getElementById("scroll_text").innerHTML=tipArray[0];// + '<a href="">[详细]</a>';
	}

	
	action_timer=window.setTimeout(autoScroll,action_speed);

}


function autoScroll(){
	if(action_direct==0) {
		next();
		if(current_index==max_index)
			action_direct=-1;
	}
	else {
		prev();
		if(current_index==0)
			action_direct=0;
	}
	action_timer=window.setTimeout(autoScroll,action_speed);
}

function prev(){
	current_index--;
	if(current_index<0){
		current_index=max_index;
		action_direct=0;
	}
	action_pos_left=-(image_w*current_index);
	autoScrollPosition();
}
function next(){
	current_index++;
	if(current_index>max_index){
		current_index=0;
		action_direct=-1;
	}
	action_pos_left=-(image_w*current_index);
	autoScrollPosition();
}
function changeIndex(){	

	var o=this;
	window.setTimeout(function() { zoomin_thumbnails(o) }, 10);
	if(this.getAttribute("rel")>current_index) 
		action_direct=0;
	else if(this.getAttribute("rel")>current_index)
		action_direct=-1;
	current_index=this.getAttribute("rel");
	if(current_index==max_index){
		action_direct=-1;
	} else if(current_index==0){
		action_direct=0;
	}
	action_pos_left=-(image_w*current_index);
	autoScrollPosition();
}

function unchangeIndex(){
	ch=parseInt(this.style.height);
	cw=parseInt(this.style.width);
	var o=this;
	window.setTimeout(function() { zoomout_thumbnails(o); }, 10);
}

function zoomin_thumbnails(o){
	ch=parseInt(o.style.height);
	cw=parseInt(o.style.width);
	ch+=3;
	cw+=4;
	if(ch>thumb_h)ch=thumb_h;
	if(cw>thumb_w)cw=thumb_w;

	o.style.width=cw + "px";
	o.style.height=ch + "px";
	if(ch<thumb_h||cw<thumb_w) {
		window.setTimeout(function() { zoomin_thumbnails(o) }, 1);
	}
}
function zoomout_thumbnails(o){
	ch=parseInt(o.style.height);
	cw=parseInt(o.style.width);
	ch-=3;
	cw-=4;
	if(ch<thumb_sh)ch=thumb_sh;
	if(cw<thumb_sw)cw=thumb_sw;

	o.style.width=cw + "px";
	o.style.height=ch + "px";
	if(ch>thumb_sh||cw>thumb_sw) {
		window.setTimeout(function() { zoomout_thumbnails(o) }, 1);
	}
}


function autoScrollPosition(){
	if(current_pos_left>action_pos_left){
		scroll_speed = Math.ceil((current_pos_left-action_pos_left)/10);
		current_pos_left-=Math.max(scroll_speed,min_scroll_speed);
		if(current_pos_left-action_pos_left<scroll_min_pixel){
			current_pos_left=action_pos_left;
		}
	}
	if(current_pos_left<action_pos_left){
		scroll_speed = Math.ceil((action_pos_left-current_pos_left)/10);
		current_pos_left+=Math.max(scroll_speed,min_scroll_speed);
		if(action_pos_left-current_pos_left<scroll_min_pixel){
			current_pos_left=action_pos_left;
		}
	}
	document.getElementById("scroll_content").style.left = current_pos_left + "px";	
	if(current_pos_left!=action_pos_left){
		window.setTimeout(autoScrollPosition, 10);
	}else{
		document.getElementById("scroll_text").innerHTML=tipArray[current_index];// + '<a href="">[详细]</a>';
	}
}

var more_timeout=null;
function show_more(){
	window.clearTimeout(action_timer);
	window.clearTimeout(more_timeout);
	darken_text(current_text_opacity);
	brighten_more(current_opacity);
}
function hide_more(){
	darken_more(current_opacity);
	brighten_text(current_text_opacity);
	action_timer=window.setTimeout(autoScroll,action_speed);
}
function check_hide_more(){
	more_timeout=window.setTimeout(hide_more, 1000);
}
function brighten_more(b){
	if(b<100){
		b+=5;
	}
	if(b>100)b=100;
	document.getElementById("scroll_more").style.filter = "alpha(opacity=" + b + ")";
	document.getElementById("scroll_more").style.opacity = b / 100;
	current_opacity=b;
	if(b<100){
		window.setTimeout(function () {brighten_more(b)}, 10);
	} else {
		
	}
}
function darken_more(b){
	if(b>0){
		b-=5;
	}
	if(b<0)b=0;
	document.getElementById("scroll_more").style.filter = "alpha(opacity=" + b + ")";
	document.getElementById("scroll_more").style.opacity = b / 100;
	current_opacity=b;
	if(b>0){
		window.setTimeout(function () {darken_more(b)}, 10);
	} else {
	}
}

function brighten_text(b){
	if(b<60){
		b+=5;
	}
	if(b>60)b=60;
	document.getElementById("scroll_text").style.filter = "alpha(opacity=" + b + ")";
	document.getElementById("scroll_text").style.opacity = b / 100;

	current_text_opacity=b;
	if(b<60){
		window.setTimeout(function () {brighten_text(b)}, 20);
	} else {
		
	}
}
function darken_text(b){
	if(b>0){
		b-=5;
	}
	if(b<0)b=0;
	document.getElementById("scroll_text").style.filter = "alpha(opacity=" + b + ")";
	document.getElementById("scroll_text").style.opacity = b / 100;
	current_text_opacity=b;
	if(b>0){
		window.setTimeout(function () {darken_text(b)}, 10);
	} else {

	}
}

$(document).ready(function(){
	//$("head").append('<link type="text/css" rel="stylesheet" href="http://css.exueo.com/home/focus.scroll.css" />');
	max_index=imgArray.length-1;
	$("#scroll_window").empty();
	$("#scroll_window").append('<div id="scroll_content"></div><div id="scroll_text"></div><div id="scroll_more"><div id="scroll_more_thumbs"></div></div>');
	initScrollBar();

	$("#scroll_window").bind("mouseover",show_more);
	$("#scroll_window").bind("mouseout",check_hide_more);
});