function InfoData() {
	this.info = {};
	this.observeres = [];
}
InfoData.prototype.register = function(observer) {
	this.observeres.push(observer);
};
var browserCanBlend = (true);
var transitions = "progid:DXImageTransform.Microsoft.Fade(duration=1)";
var arrPreload = new Array();
var imageSrcArray = new Array();
var imageLittleSrcArray = new Array();
var imageNameArray =new Array();
var imageContentArray =new Array();
var SHOWINGSTRING='第<FONT color=#ff6600>%slideNum</FONT>/%slideTotal张';
var _PRELOADRANGE = 8;		//缩略图数量
var _PREVIEW_WIDTH = 52;	//缩略图的宽度
var curImg = 0;				//当前图片的index
var numImgs = 0;			//总照片数
var begImg  = 0;			//初始化的图片的index
var firstframeindex  = -1;	//缩略图左边第一张的index
var inittimer  = null;		//用于首次打开时显示图片


function scalePhoto(imgId, width, height) {
    var img = document.getElementById(imgId);

    if (img != null) {
        var imgWidth = img.width;
        var imgHeight = img.height;
        if (imgWidth == 0)
        {
            img.width = width;
        }
        if (imgHeight == 0) {

            img.height = height;
        }
        if (imgWidth > width && imgHeight > height) {
            //both image's width and height are larger than required one
            var widthRate = imgWidth / width;
            var heightRate = imgHeight / height;
            if (widthRate > heightRate) {
                //use width to retrieve the image
                img.width = width;
                img.height = imgHeight * (width / imgWidth);
            } else {
                img.height = height;
                img.width = imgWidth * (height / imgHeight);
            }
        }
        else if (imgWidth > width) {
            //the image width lg than the required width
            img.width = width;
            img.height = imgHeight * (width / imgWidth);
        }
        else if (imgHeight > height) {
            //the image height lg than the required
            img.height = height;
            img.width = imgWidth * (height / imgHeight);
        }
    }
}

function replaceNum(myinput, token, newstr)
{
    var input = myinput;
    var output = input;
    var idx = output.indexOf(token);
    if (idx > -1)
	{
		output = input.substring(0, idx);
		output += newstr;
		output += input.substr(idx+token.length);
    }
    return output;
}

function preloadRange(intPic,intRange) {
	var divStr = "";

	for (var i=intPic; i<intPic+intRange&&i<numImgs; i++) {
		arrPreload[i] = new Image();
		arrPreload[i].src = imageSrcArray[i];
	}

	var strIn = "";
	var k = 0;
	for (i=intPic; i<intPic+intRange; i++) {
		j = i;
		var strA = "";
		var nextImg = curImg;
		if(j<numImgs && j>=0){
			strA = "<td width=\"80\"><table border=\"0\" cellspacing=\"2\" "+GetBorder(j,curImg)+"><tr><td align=center>"+
			"<a href='javascript:changeSlide("+j+")'><img id="+j+" width="+_PREVIEW_WIDTH+" height="+_PREVIEW_WIDTH+" border=0 src='"+
			imageLittleSrcArray[j]+"'></a></td></tr></table></td>";
		}
 		divStr += strA;
	}
	document.getElementById("div1").innerHTML = "<table><tr>"+divStr+"</tr></table>";
	return true;
}

function GetBorder(i,j)
{
	if(i==j)  return " bgcolor=\"#FFCC00\" ";
	return " bgcolor=\"#FFFFFF\" onmouseover=\"this.style.backgroundColor='#ffcc00'\" onmouseout=\"this.style.backgroundColor=''\"";

}

function changeSlide(n)
{
	if(inittimer!=null)  {window.clearInterval(inittimer);inittimer=null;}
    if (document.all)
	{
		/* transistion effects */
		if (browserCanBlend)
		{
			document.all.imgp.style.filter=transitions;
			document.all.imgp.filters[0].Apply();
		}else{
    		document.all.imgp.style.filter="blendTrans(duration=1)";
    		document.all.imgp.filters.blendTrans.Apply();
    	}
    }
	curImg = n;

    //点击查看图片详细信息的地方（需要修改）
    var htmlCont = "" ;
    if (imageSrcArray.length>0) {
		htmlCont ="<center><a href=\""+imageSrcArray[curImg]+"\" target=\"_blank\">" +
			"<img id='js_image' onload='scalePhoto(this.id,550,400);' onerror=\" alert('图片出错');\"  src=\"" + imageSrcArray[curImg] + "\" alt=\"" + imageNameArray[curImg] + "\"";
		htmlCont += " border=0 class=uimg ></a></center>";
  	}

    //pnumLine：显示几多张图片及现在是第几张图片
    var pnumLine = "<span >" + replaceNum(replaceNum(SHOWINGSTRING, "%slideNum", eval(curImg+1)),"%slideTotal", numImgs) + "</span >";
    document.getElementById("pnumDiv").innerHTML = pnumLine;
    //pnumLine：显示几多张图片及现在是第几张图片
    document.getElementById("pnameDiv").innerHTML = imageNameArray[curImg];
	//pnumLine：显示几多张图片及现在是第几张图片
    document.getElementById("pcontentDiv").innerHTML = imageContentArray[curImg];
    //htmlCont: 图片显示内容及点击连接
    document.getElementById("imgp").innerHTML = htmlCont;

    if (document.all){
    // 用当前图片filter方式运行
		document.all.imgp.filters[0].Play();
    }

    //一开始预计图片位置
    if(curImg<=firstframeindex)  firstframeindex--;
    if(curImg<firstframeindex)  firstframeindex=curImg;
    if(curImg>=firstframeindex+_PRELOADRANGE-1)  firstframeindex++;
    if(curImg>firstframeindex+_PRELOADRANGE-1)  firstframeindex=numImgs-_PRELOADRANGE;
    if(firstframeindex>=numImgs-_PRELOADRANGE+1)  firstframeindex = numImgs-_PRELOADRANGE;
    if(firstframeindex<0)  firstframeindex=0;
    preloadRange(firstframeindex,_PRELOADRANGE);
}

function forward()
{
	var nextFrame = curImg+1;
	if (nextFrame >= numImgs){
		nextFrame = 0;
	}
	changeSlide(nextFrame);
}

function rewind()
{
	var nextFrame = curImg-1;
	if (nextFrame < 0){
		nextFrame = numImgs-1;
	}
	changeSlide(nextFrame);
}

function navforward()
{
    firstframeindex++;
    if(firstframeindex>numImgs-_PRELOADRANGE)  firstframeindex = numImgs-_PRELOADRANGE;
    if(firstframeindex<0)  firstframeindex=0;
    preloadRange(firstframeindex,_PRELOADRANGE);
}

function navrewind()
{
    firstframeindex--;
    if(firstframeindex<0)  firstframeindex=0;
    preloadRange(firstframeindex,_PRELOADRANGE);
}

function initalbum(photo)
{
    imageSrcArray = new Array();
    imageLittleSrcArray = new Array();
    imageNameArray =new Array();
	imageContentArray =new Array();
	//begImg = 0;
    numImgs=0
	if(photo==null)  return;
    numImgs = photo.length;
	for(i=0;i<photo.length;i++){
        imageSrcArray[i] = photo[i].url
        imageLittleSrcArray[i] = photo[i].littleUrl
        imageNameArray[i] = photo[i].name
		imageContentArray[i] = photo[i].content
	}

	curImg = begImg;
	firstframeindex = curImg-Math.floor((_PRELOADRANGE-1)/2);
    if (curImg < 0 || curImg > numImgs - 1)
    	  curImg = numImgs - 1;
	if (imageSrcArray.length>0);{
		inittimer=window.setInterval('changeSlide(curImg)',30);
    }
}

/********************ajax operations******************************/
function UFolderesView(){
	this.onrefresh = function(infoes){
		global["albuminfolist"] = infoes;
		procPoint('albumslideshow', 'mainpoint', global);
	}
	this.onchange = function(item){}
	this.ondelete = function(item){}
}
var uFolderes = new InfoData();
uFolderes.register(new UFolderesView());
function cAlbumBackParam(stask){
	global.album_currentid = parseInt(stask.pop());
	begImg = parseInt(stask.pop());
}
function procAlbumShowFolder(id,beginid,folderid){
		var ajax = new AjaxWrap(null);
		ajax.openText("getalbumfolder.do?uid=" + id+"&beginid=" + beginid+"&folderid=" + folderid, true);
}
