//////////////////////////////////////////////////////////////////
// piclist.js
//
// David Yang (prowyh@hotmail.com)
// Copyright (C) Levensoft Co., Ltd. (www.levensoft.com)
//
// 2005.11.17
//////////////////////////////////////////////////////////////////

var piclist = new Object();
piclist.rotatings = new Array();
piclist.instances = new Array();
piclist.current = null;
piclist.register = function(o)
{
    rotating = new Object();
    rotating.interval = new Array();
    rotating.id = 0;
    rotating.mode = 0;
    rotating.delay = 2500;
    rotating.width = 88;
    rotating.height = 22;
    rotating.instance = o;
    
    piclist.rotatings.push(rotating);
    
    var serviceObject = new Object();
    serviceObject.name = "piclist";
    serviceObject.id = o.id;
    
    var a = o.id.split("_");
    serviceObject.wid = a[a.length - 1];

    piclist.instances.push(serviceObject);
};

/////////////////////////////////////////////
// piclist rotating
piclist.getCurrent = function(o)
{
    var rotating = null;
    
    for (var i = 0; i < piclist.rotatings.length; i++)
    {
        if (piclist.rotatings[i].instance == o)
        {
            rotating = piclist.rotatings[i];
            break;
        }
    }
    
    return rotating;
};
piclist.initPageInRotating = function(o, mode, interval)
{
    var rotating = piclist.getCurrent(o);
    if (rotating == null) return;
    
    rotating.mode = mode;
    rotating.interval = interval;
    if (arguments.length == 5)
    {
        rotating.width = arguments[3];
        rotating.height = arguments[4];
    }
    
    var str = rotating.content[0];
	
	var rot = new StringBuilder();
	rot.append("<div id=\"rotatingBox_", o.id, "\" style=\"overfloat:hidden;height:", rotating.height, "px;\"");
	rot.append(" onmouseover=\"clearInterval(", rotating.interval[0], ")\"");
	rot.append(" onmouseout=\"", rotating.interval[0], " = setInterval('piclist.startPageInRotating(", o, ")', ", rotating.delay, ")\">");
	rot.append("<div>", str, "</div>");
	rot.append("</div>");
	document.write(rot.toString());
	
	rotating.id++;
	rotating.interval[0] = setInterval("piclist.startPageInRotating(" + o + ")", rotating.delay);
};
piclist.startPageInRotating = function(o)
{
    var rotating = piclist.getCurrent(o);
    if (rotating == null) return;
    
	var str = rotating.content[rotating.id];

	rotating.id++;
	if (rotating.id >= rotating.content.length)
	{
		rotating.id = 0;
    }
    
    var rotatingBox = document.getElementById("rotatingBox_" + o.id);
    if (rotatingBox)
    {
	    if (rotatingBox.childNodes.length == 1)
	    {
		    var nextLine = document.createElement("DIV");
		    nextLine.innerHTML = str;
		    rotatingBox.appendChild(nextLine);
	    }
	    else
	    {
		    rotatingBox.childNodes[0].innerHTML = str;
		    rotatingBox.appendChild(rotatingBox.childNodes[0]);
		    rotatingBox.scrollTop = 0;
	    }
    }
    
	clearInterval(rotating.interval[1]);
	rotating.interval[1] = setInterval("piclist.scrollPageInRotating(" + o + ")", 20);
};
piclist.scrollPageInRotating = function(o)
{
    var rotating = piclist.getCurrent(o);
    if (rotating == null) return;
    
    var rotatingBox = document.getElementById("rotatingBox_" + o.id);
    if (rotatingBox)
    {
	    rotatingBox.scrollTop++;
	    if (rotatingBox.scrollTop % rotating.height == (rotating.height - 1))
	    {
		    clearInterval(rotating.interval[1]);
	    }
    }
}

/////////////////////////////////////////////
// piclist paging
piclist.paging = function(page, wid)
{
    if (! wid) return;
    var name = "";

    for (var i = 0; i < piclist.instances.length; i++)
    {
        if (wid == piclist.instances[i].wid)
        {
            name = piclist.instances[i].name;
            piclist.current = document.getElementById(piclist.instances[i].id);
            break;
        }
    }

    util.openMessaging(gMessage.READING_DATA_MESSAGE);
    webServiceFramework.initialize("LevenSiteService");
    webServiceFramework.registerHandler(piclist.refresh);
    webServiceFramework.callService("LevenSiteService", "GetList", 3, name + ";" + wid + ";" + page);
};
piclist.refresh = function(o)
{
    webServiceFramework.unregisterHandler();
    
    if (piclist.current)
    {
        piclist.current.parentElement.innerHTML = o.firstChild.nodeValue;
    }
};

/////////////////////////////////////////////
// piclist fading
var piclist_strIFaderTransType     = "blend"; // "reveal" | "blend"
var piclist_intIFaderRevealType    = 23;       // 1~23
var piclist_intIFaderTransDuration = 3.000;    // 3.0 seconds
var piclist_intIFaderTransDelay    = 5000;     // 5 seconds

var piclist_objTimer = null;
var piclist_intCurrImg = 0;
var piclist_aryObjFadingImgs = new Array();
var piclist_ie4 = document.all ? true : false;
var piclist_nn4 = document.layers ? true : false;
var piclist_switchMode = 0;

function piclist_initPageInFading(o, mode, transDelay)
{
	if (!piclist_ie4 && !piclist_nn4) return;

    piclist_switchMode = mode;
	piclist_strIFaderTransType = mode > 1 ? "reveal" : "";
	if (transDelay > 0) piclist_intIFaderTransDelay = transDelay * 1000;

	if (piclist_ie4)
	{
		var img = document.all["idPicListFadingImage"];
		if (img == null) return;

		img.parentElement.href = "";
		img.parentElement.onclick = piclist_pageInFadingClick;

		if (piclist_strIFaderTransType != "")
		{
			img.style.filter = (piclist_strIFaderTransType == "reveal") ? "revealTrans" : "blendTrans";
			img.style.filter.duration = piclist_intIFaderTransDuration;
			img.style.filter.transition = piclist_intIFaderRevealType;
		}
	}
	piclist_objTimer = window.setInterval("piclist_pageInFading()", piclist_intIFaderTransDelay);
}
function piclist_getNext()
{
    if (piclist_switchMode == 0 || piclist_switchMode == 2)
    {
		if (piclist_intCurrImg == (piclist_aryObjFadingImgs.length - 1))
		{
			piclist_intCurrImg = -1;
		}
		piclist_intCurrImg++;
		
        return piclist_aryObjFadingImgs[piclist_intCurrImg];
    }
    else
    {
        var rnd = Math.random() * piclist_aryObjFadingImgs.length - 1;

        return piclist_aryObjFadingImgs[Math.round(rnd)];
    }
}
function piclist_pageInFading()
{
    var img = piclist_getNext();
    if (! img) return;
    
	if (piclist_ie4)
	{
		with (document.all["idPicListFadingImage"])
		{
			if (piclist_strIFaderTransType == "reveal")
			{
				filters(0).transition = piclist_intIFaderRevealType;
			}
			
			title = img.alt;
	
			if (piclist_strIFaderTransType != "") filters(0).apply();
			src = img.url;
			if (piclist_strIFaderTransType != "") filters(0).play();
		}
	}
	else if (piclist_nn4)
	{
		document.images["idPicListFadingImage"].src = img.url;
	}
}

function piclist_pageInFadingClick()
{
	var objCurrImg = piclist_aryObjFadingImgs[piclist_intCurrImg];

	switch (typeof(objCurrImg.command)) {
	case "function":
		objCurrImg.command();
		break;
	case "string":
		switch(objCurrImg.target) {
		case "_self":
			self.location.href = objCurrImg.command;
			break;
		case "_blank":
			window.open(objCurrImg.command);
			break;
		case "_parent":
			parent.location.href = objCurrImg.command;
			break;
		case "_top":
			top.location.href = objCurrImg.command;
			break;
		default:
			window.open(objCurrImg.command, objCurrImg.target);
		}
	}
}

function piclist_pageInFadingClearTimer()
{
	window.clearInterval(piclist_objTimer);
}
window.onunload = piclist_pageInFadingClearTimer;

function piclist_objFadingImg(url, command, alt, target)
{
	this.url = (url == null) ? "" : url;
	this.command = (command == null || command == "") ? document.location.href : command;
	this.alt = (alt == null) ? "" : alt;
	this.target = (target == null || target == "") ? "_self" : target;
}

