// JScript File
//竖向滚动
var isMoving = false;
var curIndex = 0;

function moveElement(elementID, final_x, final_y, interval) {
    if (!document.getElementById) return false;
    if (!document.getElementById(elementID)) return false;
    isMoving = true;
    var elem = document.getElementById(elementID);
    if (elem.movement) {
        clearTimeout(elem.movement);
    }
    if (!elem.style.left) {
        elem.style.left = "0px";
    }
    if (!elem.style.top) {
        elem.style.top = "0px";
    }
    var xpos = parseInt(elem.style.left);
    var ypos = parseInt(elem.style.top);
    if (xpos == final_x && ypos == final_y) {
        isMoving = false;
        return true;
    }
    if (xpos < final_x) {
        var dist = Math.ceil((final_x - xpos) / 10);
        xpos = xpos + dist;
    }
    if (xpos > final_x) {
        var dist = Math.ceil((xpos - final_x) / 10);
        xpos = xpos - dist;
    }
    if (ypos < final_y) {
        var dist = Math.ceil((final_y - ypos) / 10);
        ypos = ypos + dist;
    }
    if (ypos > final_y) {
        var dist = Math.ceil((ypos - final_y) / 10);
        ypos = ypos - dist;
    }
    elem.style.left = xpos + "px";
    elem.style.top = ypos + "px";
    var repeat = "moveElement('" + elementID + "'," + final_x + "," + final_y + "," + interval + ")";
    elem.movement = setTimeout(repeat, interval);
}

function verticalScroll(scrollBody, scrollList, scrollUp, scrollDown, imgId, speed) {

    var scrollBody=document.getElementById(scrollBody),scrollList=document.getElementById(scrollList),scrollUp=document.getElementById(scrollUp),scrollDown=document.getElementById(scrollDown),bigImg=document.getElementById(imgId),height = scrollBody.clientHeight,imgs = scrollList.getElementsByTagName('img'),lis = scrollList.getElementsByTagName('li'),liLength=lis.length;
   if (liLength == 0) return;
   if(parseInt(liLength%4)===0)
   {
        myLength=parseInt(liLength/4);
   }
   else{
        myLength=parseInt(liLength/4)+1;
   }
   var ulHeight = height*myLength;
  
   
    for (var i = 0; i < imgs.length; i++) {
        imgs[i].onclick = function (i) {
        	return function(){
        		var src = this.id;
	            removeClassName();
	            this.className = 'on';
	            bigImg.src = src;
	            curIndex = i; //新增 点击前进和后退浏览图片
	            InitLinkVisible(curIndex,imgs.length); //新增
	            document.getElementById("carousel_photo_intro").innerHTML = this.title;
	            var title = this.alt;
	            var sType = "0"
	            if(title.indexOf('_')  > -1 )
	            {
	            	sType = title.substring(0,1);
	            	title = title.substr(2,title.length - 2);
	            }
	            if (sType != "0") {
	            	
	                var sURL = Gurl + title + "/" + ResId + "/PhotoDetail.aspx";
	                var PostData = "";
	                new AjaxReq({
	                    url: sURL,
	                    method: 'post',
	                    callType: 'html',
	                    data: PostData,
	                    onSuccess: function () {
	                        var res = this.ajax.response.html;
	                        if (res) {
	                            var obj = eval(res);
	                            $("useralbum").href = obj.useralbum;
	                            $("userpic").src = obj.userpic;
	                            $("nickname").href = obj.userURL;
	                            $("nickname").innerHTML = obj.nickname;
	                            $("uploadcount").innerHTML = obj.uploadcount;
	                        }
	                    }

	                }).send();
        		};
	       }
        }(i);
    };
     var removeClassName = function(){
	 for(var i =0;i<imgs.length;i++){
		if(imgs[i].className == 'on'){
			imgs[i].className ='';
		}
	 };
 }
 scrollUp.onclick = function(){
    if(isMoving)return;
	var top = parseInt(scrollList.style.top) || 0;
	if(top < 0){ 
		moveElement("samples_list",0,top + height,speed);
	}
    if(top>=-height){
       scrollUp.className="scrollUpOff";
    }
	if(-top < ulHeight){
	   scrollDown.className="scrollDownOn";
	}
 }
 scrollDown.onclick = function(){
    if(isMoving)return;
	var top = parseInt(scrollList.style.top) || 0;
	if(-top < ulHeight - height){
		moveElement("samples_list",0,top - height,speed);
	}
    if(top >= 0){
       scrollUp.className="scrollUpOn";
    }
	if(-top >= ulHeight - (height*2)){
	   scrollDown.className="scrollDownOff";
	}
 }

}

  clickScrollDownNew = function(newindex){
  	var scrollBody = document.getElementById("carousel_container");
    var scrollList = document.getElementById("samples_list");
    var scrollUp = document.getElementById("scrollUp");
    var scrollDown = document.getElementById("scrollDown")
    var lis = scrollList.getElementsByTagName('li');
    var newLength = lis.length;
	var height = scrollBody.clientHeight;	 
    if (lis.length == 0) return;
    var newHeight = height* newindex;
    
	var ulHeight = height * newLength;
    if(isMoving)return;
	var top = parseInt(scrollList.style.top) || 0;
	if(-top < ulHeight - height && newindex>0){
		moveElement("samples_list",0,- newHeight,4);
	}
  
	var curpage = (newLength%4 == 0 && newLength > 0)? (parseInt(newLength/4) - 1) : (parseInt(newLength/4));
	
	if(newLength <= 4)
	{
		scrollUp.className="scrollUpOff";
		scrollDown.className="scrollDownOff";
	}
	if(newindex > 0)
	{
		scrollUp.className="scrollUpOn";
	}
	if(newindex > 0 && curpage > 0 && newindex == curpage)
	{
		scrollDown.className="scrollDownOff";
	}
 }


/*---------------新增前进后退浏览图片 begin ------------  */
var cache = {};
var urlArr = new Array();
var urlArrlen = 0;

var ID = function(id){
	return document.getElementById(id);
};

var InitImgArray = function(curId)
{
	if(urlArrlen > 0) return;
	if(ID("samples_list") == null ) return;
	
	var imgs = ID("samples_list").getElementsByTagName('img');
	if(imgs == null || imgs.length == 0) return ;
	
	urlArrlen = imgs.length;
	for(var i=0; i< imgs.length; i++)
	{
		var smallsrc = imgs[i].src;
		if(CurAlbumId == 4) //菜单
		{
			smallsrc = smallsrc.replace("\/small\/","\/big\/");
		}
		urlArr[i] = smallsrc;
		var title = imgs[i].alt;
        if(title.indexOf('_')  > -1 && title.substr(2,title.length - 2) == curId )
        {
        	 curIndex = i;
        	 imgs[i].className = "on";
        }
	}
	
	//每一页的最后一个图片,否则 + 4(1页4张,编号从0开始)
	var newpageindex = (curIndex >0)? (curIndex + 1) : 0;
	if(newpageindex%4 == 0 && newpageindex > 0) 
	{
		newpageindex = parseInt(newpageindex/4) - 1;
	}
	else
	{
		newpageindex = parseInt(newpageindex/4); //parseInt(newpageindex/4)*4 + 4 ;
	}
	clickScrollDownNew(newpageindex);
};

var ClickBrowseImg = function(type)
{
	if(urlArr == null || urlArr.length == 0) return;
	if(type == "pre" && curIndex >0)
	{
		curIndex--;
	}
	else if(type == "next" && (curIndex + 1) <= urlArr.length )
	{
		curIndex++;
	}
	var imgs = ID("samples_list").getElementsByTagName('img');
	if(imgs == null || imgs.length ==0 || (curIndex +1) > imgs.length) return ;
	
	InitLinkVisible(curIndex,imgs.length);
	//alert(imgs[curIndex].id);
	imgs[curIndex].onclick();
	
	CallScollChange(type);
}

var InitLinkVisible = function(newindex, imglen)
{
	ID("linkPre").style.display = "";
	ID("linkNext").style.display = "";
	if(newindex == 0 )
	{
		ID("linkPre").style.display = "none";
	}
	else if((newindex +1) == imglen )
	{
		ID("linkNext").style.display = "none";
	}
}

InitImgArray(curPhotoId);//curPhotoId为页面定义参数，当前请求图片的id
InitLinkVisible(curIndex,urlArrlen);

var fnPicShow = function(urlArr,curId){

	if(urlArr == null || urlArr.length == 0 ) return;
	
	var id = curId, l = urlArr.length;
	var prev, next;
	if(id === 0){
		prev = "";
	}else{
		prev = id-1;
	}
	if(id === l-1){
		next = "";	
	}else{
		next = id+1;	
	}
	//滚动高度
	var sTop = function() {   
		var scrollPos = 0;   
		var d = document.documentElement, b = document.body, w = window;
		if (typeof w.pageYOffset !== "undefined") {   
		 scrollPos = w.pageYOffset;   
		}   
		else if (typeof document.compatMode !== "undefined" && document.compatMode !== "BackCompat") {   
		 scrollPos = d.scrollTop;   
		}   
		else if (typeof b !== "undefined") {   
		 scrollPos = b.scrollTop;   
		}   
		return scrollPos; 
	}();
	//显示区域的高度和宽度
	var cHeight = function(){
		if(document.all){
			return document.compatMode == "CSS1Compat"? document.documentElement.clientHeight :
					 document.body.clientHeight;
		}else{
			return self.innerHeight;
		}
	}();
	var cWidth = function(){
		if(document.all){
			return document.compatMode == "CSS1Compat"? document.documentElement.clientWidth :
					 document.body.clientWidth;
		}else{
			return self.innerWidth;
		}
	}();
//	alert(sTop);
	//创建空div
	var boxdiv = document.createElement("div");
	boxdiv.id = "appendBox";
	if(!ID("appendBox")){
		document.getElementsByTagName("body")[0].appendChild(boxdiv);
	}else{
		ID("appendBox").style.display = "block";
	}
	//图片预加载
	ID("appendBox").innerHTML = '<div id="blank" style="width:100%; height:'+(cHeight+sTop)+'px; background:black; position:absolute; left:0; top:0; opacity:0.4; filter:alpha(opacity=40); z-index:999;"></div><div style="position:absolute; z-index:1000; padding:18px; background:white; left:'+((cWidth-100)/2 - 18)+'px; top:'+(sTop + (cHeight-100)/2 - 18)+'px;" id="appendPicBox"><img id="theShowPic" src="'+waitUrl+'" /></div>';
	
	var preloader = new Image();
	preloader.src = urlArr[id];
	//图片的高宽
	var w = preloader.width, h = preloader.height;
	
	if(!w){
		preloader.onload = function() {
			//获取图片的宽度
			w = preloader.width;
			h = preloader.height;
			cache["cache_"+id] = true;
			callback();
		};
	}
	var callback = function(){
		var bodyHeight = document.body.scrollHeight;
		if( h > bodyHeight - 50){
			bodyHeight = h + 200;	
		}
		ID("appendBox").innerHTML = '<div id="blank" style="width:100%; height:'+(bodyHeight)+'px; background:black; position:absolute; left:0; top:0; opacity:0.4; filter:alpha(opacity=40); z-index:999;"></div><div style="position:absolute; z-index:1000; padding:18px; background:white; overflow:hidden;" id="appendPicBox"><img id="theShowPic" src="'+urlArr[id]+'" /><img id="closePicBtn" src="'+closeUrl+'" style="position:absolute; right:-3px; top:-2px; z-index:1000; cursor:pointer;" /><a title="查看上一张图片" id="picPrev" hidefocus="true" href="javascript:void(0);" rel="'+prev+'" style="width:50%; height:100%; _height:600px; background:url('+leftUrl+') no-repeat -1000px -1000px; left:0; top:0; position:absolute; outline:0;"></a><a title="查看下一张图片" id="picNext" href="javascript:void(0);" rel="'+next+'" hidefocus="true" style="width:50%; height:100%; _height:600px; background:url('+rightUrl+') no-repeat -1000px; right:0; top:0; position:absolute; outline:0;"></a></div>';	
		
		//给左右切换区域定宽
		ID("picPrev").style.width = ID("picNext").style.width =  w/2 + 18 + "px";
		//居中定位
		var t = (bodyHeight-h)/2 - 18, l = (cWidth-w)/2 - 18;
		ID("appendPicBox").style.left = l + "px";
		ID("appendPicBox").style.top = t + "px";
		//显示左右箭头
		ID("picPrev").onmouseover = function(){
			if(this.rel){
				this.style.cursor = "url(http://s1.57575777.com/adentity/images/point_left.cur),pointer";	
				this.title = "查看上一张图片";
			}else{
				this.title = "这是第一张图片";
			}
		};
		ID("picNext").onmouseover = function(){
			if(this.rel){
				this.style.cursor = "url(http://s1.57575777.com/adentity/images/point_right.cur),pointer";	
				this.title = "查看下一张图片";	
			}else{
				this.title = "这是最后一张图片";	
			}
		};
		ID("picPrev").onmouseout = function(){
			this.style.cursor = "default";	
		};
		ID("picNext").onmouseout = function(){
			this.style.cursor = "default";	
		};
		ID("picPrev").onclick = function(){
			var rel = this.rel;
			if(rel !== ""){
				rel = parseInt(rel);
				fnPicShow(urlArr,rel);	
			}
			return false;
		};
		ID("picNext").onclick = function(){
			var rel = parseInt(this.rel);
			if(rel){
				fnPicShow(urlArr,rel);	
			
			}
			return false;
		};
		//关闭
		ID("blank").onclick = function(){
			ID("appendBox").style.display = "none";
		};
		ID("closePicBtn").onclick = function(){
			ID("appendBox").style.display = "none";
		};
	}
	if(cache["cache_"+id] || w){
		callback();
	}
};


var CallScollChange = function(typename)
{
	if(curIndex <= 0 ) return;
	//var scrollUp = document.getElementById("scrollUp");
    //var scrollDown = document.getElementById("scrollDown");
	var curImgNum = curIndex;// + 1; //图片编号
	var preImgNum = 0;
	if(typename == "pre")
	{
	 	preImgNum = curImgNum + 1;
	 	if(parseInt(curImgNum/4) != parseInt(preImgNum/4) && ID("scrollUp"))
	 	{
	 		ID("scrollUp").onclick();
	 	}
	}
	else if(typename == "next")
	{
	 	preImgNum = curImgNum - 1;
	 	if(parseInt(curImgNum/4) != parseInt(preImgNum/4) && ID("scrollDown"))
	 	{
	 		ID("scrollDown").onclick();
	 	}
	}
};

document.getElementById("showBigPic").onclick = function(){
	fnPicShow(urlArr,curIndex);
	return false;
};