﻿//albumRoll:[0]id [1]ok_obj,[2]ok,[3]ele,[4]w,[5]k 
//
var ok_obj=E("content").getElementsByTagName("LI")
var ok=Math.ceil(ok_obj.length)-1;
//var intDelay=30; //设置菜单显示速度，越大越慢
//var intInterval=5; 
E("content0").style.width=(ok+1)*160;//594
var ele=E("description");
var w=80;//ele.clientWidth;
var speed1=40,speed2=30;
var timers=new Array(speed1);
ele.scrollLeft=0;
var page=0;//当前页码 从0开始
//doSlide(0);
var nearPicsIndex=getNearPicsIndex(pid);
showFrist(nearPicsIndex);

function doSlide(s){
  //alert("page:"+page+" ok:"+ok+" s:"+s);
  //if (page>=ok &&s>0|| page<=0 &&s<0){ alert("抱歉,已经到头了!");
  //MenuClick();
 if (page>=(ok-1) &&s>0){
 	//alert("已经最后一张!");
 	//alert("nearPics.length:"+[nearPics.length-1])
 	getNextPics(nearPics[nearPics.length-1][0]);
 }else if( page<=0 &&s<0){  
  	//alert("这是第一张!");
  	getPrevPics(nearPics[0][0]); 	
  	//clickPrev();	
 }else{
  page+=s;
    var x=ele.scrollLeft;
    var d=page*w-x;
    //alert("n="+n);
    for(var i=0;i<speed1;i++)(
    	function(){
    		if(timers[i]) clearTimeout(timers[i]);
    		var j=i;
//    		alert(x)
    		timers[i]=setTimeout(function(){ele.scrollLeft=x+Math.round(d*Math.sin(Math.PI*(j+1)/(2*speed1)));},(i+1)*speed2);
    	}
    )();
    //showPrevNext();
    if(E("nextLink").style.display=="none"){
    	E("nextLink").style.display="";
  	 	E("nextFont").style.display="";
    }
    if(E("prevLink").style.display=="none"){
    	E("prevLink").style.display="";
  	 	E("prevFont").style.display="";
    }
}}

function showPrevNext(){
	if(page==0){
    	E("prevLink").style.display="none";
  	 	E("prevFont").style.display="none";
    }else{
    	E("prevLink").style.display="";
  	 	E("prevFont").style.display="";
    }
    if(page==ok){
     	E("nextLink").style.display="none";
  	 	E("nextFont").style.display="none";
    }else{
    	E("nextLink").style.display="";
  	 	E("nextFont").style.display="";
    }
}

//后20张图片
function getNextPics(pid){
	var loading="<img src='"+webPath+"/common/images/loading_circle.gif'>";
	$('nearPicDiv').innerHTML= loading;
	page=0;
	picDetailAjax.getNextPics(pmid,pid,LOGINER_ID,LOGIN_TIME,function(reply){
		result = reply;
		if(typeof(result)== "object" && result!= null){
			//nearPics=new Array();
			//nearPics=result;
			if(result.length==0){
				showErrMsg("这是最后一张！");				
			}else{
				//在nearPics加20张
				addNextPics(result);
			}	
			setNearPicDivIH();//加载图片			
 			ok=Math.ceil(nearPics.length)-1;
			E("content0").style.width=(ok+1)*160;//594
			ele=E("description");
			//ele.scrollLeft=0;
			var nearPicsIndex=getNearPicsIndex(pid);//图片的位置
			showFrist(nearPicsIndex);
		}else{
			alert("找不到相关图片数据，请联系管理员！");
		}			
	});
}

//前20张图片
function getPrevPics(pid){
	var loading="<img src='"+webPath+"/common/images/loading_circle.gif'>";
	$('nearPicDiv').innerHTML= loading;
	page=0;
	picDetailAjax.getPrevPics(pmid,pid,LOGINER_ID,LOGIN_TIME,function(reply){
		result = reply;
		if(typeof(result)== "object" && result!= null){
			//nearPics=new Array();
			//nearPics=result;
			if(result.length==0){
				showErrMsg("这是第一张！");
			}else{
				//在nearPics加20张
				addPrevPics(result);
			}
			setNearPicDivIH();//加载图片			
 			ok=Math.ceil(nearPics.length)-1;
			E("content0").style.width=(ok+1)*160;//594
			ele=E("description");
			//ele.scrollLeft=0;
			var nearPicsIndex=getNearPicsIndex(pid);//图片的位置
			if(result.length>0)
				nearPicsIndex=nearPicsIndex-1;		
			showFrist(nearPicsIndex);
		}else{
			alert("找不到相关图片数据，请联系管理员！");
		}			
	});
}

function addNextPics(pics){
	for(var i=0;i<pics.length;i++){
		nearPics[nearPics.length]=pics[i];
	}
}

function addPrevPics(pics){
	var temp=pics;
	for(var i=0;i<nearPics.length;i++){
		temp[temp.length]=nearPics[i];
	}
	nearPics=temp;
}

//显示第一张图片
function showFrist(nearPicsIndex){	
	var initLeft=w*nearPicsIndex;//初始位置,80为一张图片的宽度，包括间距75+5
	//alert("initLeft:"+initLeft);
  	ele.scrollLeft=initLeft;
  	//page=ele.scrollLeft==0?0:nearPicsIndex;
  	page=nearPicsIndex<0?0:nearPicsIndex;
  	//alert("page:"+page);
}


/*
function MenuClick(){
LayerMenu.filters.alpha.opacity=0; 
LayerMenu.style.display=""; 
GradientShow();
} 

function GradientShow(){ 
	LayerMenu.filters.alpha.opacity+=intInterval; 
	if (LayerMenu.filters.alpha.opacity<100) 
		setTimeout("GradientShow()",intDelay); 
	else 
		setTimeout("GradientClose()",1500)
} 

function GradientClose() { 
	LayerMenu.filters.alpha.opacity -= intInterval; 
	if (LayerMenu.filters.alpha.opacity>0) { 
		setTimeout("GradientClose()",intDelay); 
	}else { 
		LayerMenu.style.display="none";
	} 
}
GradientClose(); */


function doAlbumSlide(s,aid){
  var albumPicRollIndex=getAlbumPicRollIndex(aid); 
  var theAlbumRoll=albumPicRoll[albumPicRollIndex];
  var album_k=theAlbumRoll[3];
  var ok=theAlbumRoll[1];
  var ele=theAlbumRoll[2];
  var w=ele.clientWidth;
  if (album_k>=ok &&s>0|| album_k<=0 &&s<0)alert("抱歉,已经到头了!");
 else{
  album_k+=s;
    var x=ele.scrollLeft;
    var d=album_k*w-x;
    for(var i=0;i<speed1;i++)(
    	function(){
    		if(timers[i]) clearTimeout(timers[i]);
    		var j=i;
//    		alert(x)
    		timers[i]=setTimeout(function(){ele.scrollLeft=x+Math.round(d*Math.sin(Math.PI*(j+1)/(2*speed1)));},(i+1)*speed2);
    	}
    )();
    albumPicRoll[albumPicRollIndex][3]=album_k;
    if(album_k==0){
  	 	E("prevFont_album_"+aid).style.display="none";
    }else{
  	 	E("prevFont_album_"+aid).style.display="";
    }
    if(album_k==ok){
  	 	E("nextFont_album_"+aid).style.display="none";
    }else{
  	 	E("nextFont_album_"+aid).style.display="";
    }
}}
function doGroupSlide(s,gid){
  var groupPicRollIndex=getGroupPicRollIndex(gid); 
  var theGroupRoll=groupPicRoll[groupPicRollIndex];
  var group_k=theGroupRoll[3];
  var ok=theGroupRoll[1];
  var ele=theGroupRoll[2];
  var w=ele.clientWidth;
  if (group_k>=ok &&s>0|| group_k<=0 &&s<0)alert("抱歉,已经到头了!");
 else{
  group_k+=s;
    var x=ele.scrollLeft;
    var d=group_k*w-x;
    for(var i=0;i<speed1;i++)(
    	function(){
    		if(timers[i]) clearTimeout(timers[i]);
    		var j=i;
//    		alert(x)
    		timers[i]=setTimeout(function(){ele.scrollLeft=x+Math.round(d*Math.sin(Math.PI*(j+1)/(2*speed1)));},(i+1)*speed2);
    	}
    )();
    groupPicRoll[groupPicRollIndex][3]=group_k;
    if(group_k==0){
  	 	E("prevFont_group_"+gid).style.display="none";
    }else{
  	 	E("prevFont_group_"+gid).style.display="";
    }
    if(group_k==ok){
  	 	E("nextFont_group_"+gid).style.display="none";
    }else{
  	 	E("nextFont_group_"+gid).style.display="";
    }
}}
function showErrMsg(msg){
	showMsgDiv(msg,1,webPath+'/common/img/error16.png');
}

function showMsgDiv(msg, showSec,icon) {
	if(typeof(MY_MSG_DIV)== 'undefined' || MY_MSG_DIV== null)
		MY_MSG_DIV = new MsgDiv();
	MY_MSG_DIV.Show(msg, showSec,icon);
}
//自动消失的消息显示框
function MsgDiv() {
	this.msgDiv = null;
	var left= document.body.clientWidth- 340;//var left= document.body.clientWidth- 380;
	//this.msgStyle = "FONT-SIZE:12px;LEFT:expression(this.offsetParent.scrollLeft+100);COLOR:#ffffff;POSITION:absolute;TOP:expression(this.offsetParent.scrollTop+100);BACKGROUND-COLOR:#FF9900;";
	this.ffMsgStyle = "FONT-SIZE:12px;left:"+left+"px;COLOR:#ffffff;POSITION:absolute;TOP:220px;BACKGROUND-COLOR:#FF9900;z-index: 9999;";//
	this.msgStyle= "FONT-SIZE:12px;	COLOR:#ffffff;BACKGROUND-COLOR:#FF9900;top:210px;right:260px;position:fixed;z-index: 9999;";//right:300px;
		
		this.createDiv = function () {
		this.msgDiv = document.createElement("div");
		//window.XMLHttpRequest
		if (window.navigator.userAgent.indexOf("MSIE")>= 1) {
			this.msgDiv.style.cssText = this.ffMsgStyle;
		} else {
			this.msgDiv.style.cssText = this.msgStyle;
		}
		document.body.insertBefore(this.msgDiv, document.body.childNodes[0]);
	};
	this.Show = function (msgstr) {
		this.msgDiv.innerHTML += "<div>" + msg + "</div>";
	};
	this.createDiv();  //放置此处是为了防止以下的md值为null 
	var closeTimer;//,btmTimer
	var md = this.msgDiv;	
	this.Show = function (msg, showtime,icon) {
		if(md.innerHTML.length >100)
			md.innerHTML = "";
		if(icon==null || icon.length==0)
			md.innerHTML = "<div>" + msg + "</div>"+ md.innerHTML;
		else
			md.innerHTML = "<div><img src='"+icon+"'></img>" + msg + "</div>"+ md.innerHTML;		
		md.style.visibility = "visible";
		if (closeTimer) {
			window.clearTimeout(closeTimer);
		}
		closeTimer = setTimeout(this.Hidden, showtime * 1000);
	};
	this.Hidden = function () {
		md.innerHTML = "";
		md.style.visibility = "hidden";
	};
}
