//////////////////////////////////////////////////////////////////////////////
// main.js
// 
//		Copyright 2010 Sony Corporation
//
//////////////////////////////////////////////////////////////////////////////

var pmbahm = {

	imgList : null,
	currentIdx : 0,
	loadCount : 0,

	onLoadModule : function()
	{
		if(rsrc == undefined)
			return;

		if(rsrc.title != undefined && rsrc.title != null)
			document.title = rsrc.title;

		if(rsrc.hd == undefined || rsrc.hd == null || rsrc.hd.length == 0)
		{
			header.style.visibility = "hidden";
			mvslide.style.border = "none";
		}
		else
		{
			if(header_icon != null &&
			   rsrc.ic != undefined && rsrc.ic != null)
			{
				header_icon.onload = function(){ this.style.visibility = "visible"; };
				if(navigator.userAgent.match(/MSIE [56]/) != null &&
				   window.ActiveXObject)
				{
					header_icon_t.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + rsrc.ic + "', sizingMethod='scale')";
					header_icon.src = rsrc.ic;
					header_icon_t.style.width = header_icon.width + "px";
					header_icon_t.style.height = header_icon.height + "px";
					header_icon.style.visibility = "hidden";
				}
				else
					header_icon.src = rsrc.ic;
			}

			header_msg.innerHTML = rsrc.hd;
		}

		if(rsrc.desc == undefined || rsrc.desc == null || rsrc.desc.length == 0)
			imgdesc.parentNode.removeChild(imgdesc);
		else
			imgdesc.innerHTML = rsrc.desc;

		var imgNum = this.loadImages();
		if(imgNum <= 0)
			return;

		if(headerdate != undefined && rsrc.time != null)
		   headerdate.innerHTML = rsrc.time;

		if(footer != undefined)
		{
			if(rsrc.ft == undefined || rsrc.ft == null || rsrc.ft.length == 0)
			{
				footer.parentNode.removeChild(footer);
			}
			else
			{
				footer.innerHTML = rsrc.ft;
			}
		}

		this.currentIdx = 0;
	},

	imageLoaded : function()
	{
		this.loadCount++;
		if(this.loadCount != this.imgList.length)
			return;

		var width = 0, height = 0, i;
		for(i=0; i<this.imgList.length; i++)
		{
			if(this.imgList[i].alt.length == 0)
			{
				width = this.imgList[i].width;
				height = this.imgList[i].height;
				break;
			}
		}

		var style, left;
		for(i=0; i<this.imgList.length; i++)
		{
			if(this.imgList[i].width != width)
			{
				this.imgList[i].width = width;
				this.imgList[i].height = height;
			}
			style = this.imgList[i].style;
			style.top = "0px";
			if(i == 0)
			{
				style.position = "relative";
				style["margin-left"] = "auto";
				style["margin-right"] = "auto";
			}
			else
			{
				style.position = "absolute";
				if(isIE())
					style.left = "0px";
				else
					style.left = left;
				style.visibility = "hidden";
			}

			snmvimgs.appendChild(this.imgList[i]);
			this.setMouseMoveHandler(this.imgList[i], MouseMoveHandler);

			if(i == 0)
				left = this.imgList[i].offsetLeft;
		}

		if(rsrc.demo != undefined)
		{
			if(rsrc.demo == "rt")
				this.doAutoScroll(true);
			else if(rsrc.demo == "ow")
				this.doAutoScroll(false);
		}
	},

	setMouseMoveHandler : function(elm, handler)
	{
		if(isIE())
		{
			elm.attachEvent("onmousemove", handler);
		}
		else
		{
			elm.addEventListener("mousemove", handler, false);
		}
	},

	onMouseMove : function(e)
	{
		var pos;
		if(isIE() || isOpera())
			pos = e.offsetX;
		else
			pos = e.layerX;
		if(pos == undefined)
			return;

		var idx = Math.floor(pos * this.imgList.length / this.imgList[0].offsetWidth);
		if(this.nextIndex != null)
			this.cancelScroll();

		this.show(idx);
	},

	show : function(idx)
	{
		if(idx >= this.imgList.length)
			idx = this.imgList.length - 1;
		else if(idx < 0)
			idx = 0;
		if(idx == this.currentIdx)
			return;

		for(var i=0; i<this.imgList.length; i++)
		{
			if(i == idx)
			{
				this.imgList[i].style.visibility = "visible";
				if(i != 0 && !isIE())
				{
					this.imgList[i].style.left = this.imgList[0].offsetLeft;
				}
			}
			else 
				this.imgList[i].style.visibility = "hidden";
		}
		this.currentIdx = idx;
	},

	loadImages : function()
	{
		var len = rsrc.num;
		if(len <= 0)
			return 0;

		this.imgList = new Array();
		for(var i=0; i<len; i++)
		{
			this.imgList[i] = document.createElement("img");
			this.imgList[i].onload = ImageLoadHandler;
			this.imgList[i].onerror = ImageLoadError;
		}

		var path;
		var is_desc
		if(rsrc.order != undefined && rsrc.order == "desc")
			is_desc = true;
		else
			is_desc = false;
		var t = (new Date()).toString();
		for(var i=0; i<len; i++)
		{
			if(is_desc)
				path = "resource/" + (len - i) + ".jpg?" + t;
			else
				path = "resource/" + (i + 1) + ".jpg?" + t;
			this.imgList[i].alt = path;
			this.imgList[i].src = path;
		}
		return len;
	},

	nextIndex : null,
	SC_INTVL : 50,
	scRoundTrip : false,
	scDir : 1,
	doAutoScroll : function(rt)
	{
		if(rt == true)
			this.scRoundTrip = true;
		else
			this.scRoundTrip = false;
		this.nextIndex = this.currentIdx;
		this.scDir = 1;
		window.setInterval("pmbahm.scroll()", this.SC_INTVL);
	},

	cancelScroll : function()
	{
		if(this.nextIndex != null)
		{
			window.clearTimeout();
			this.nextIndex = null;
		}
	},

	scroll : function()
	{
		if(this.nextIndex == null)
			return;
		this.show(this.nextIndex);
		if(this.nextIndex == this.imgList.length - 1)
		{
			if(this.scRoundTrip == true)
				this.scDir = -1;
			else
			{
				this.cancelScroll();
				return;
			}
		}
		else if(this.nextIndex == 0 && this.scDir < 0)
			this.cancelScroll();

		this.nextIndex += this.scDir;
	}
};

window.onload = function(){ pmbahm.onLoadModule(); };

function MouseMoveHandler(e)
{
	pmbahm.onMouseMove(e);
};

function ImageLoadHandler()
{
	this.alt = "";
	this.onload = null;
	this.onerror = null;
	pmbahm.imageLoaded();
};

function ImageLoadError()
{
	this.onload = null;
	this.onerror = null;
	pmbahm.imageLoaded();
};

var bIE = null;
function isIE()
{
	if(bIE == null)
	{
		if(navigator.appName.indexOf("Internet Explorer") != -1)
			bIE = true;
		else
			bIE = false;
	}

	return bIE;
};

var bOpera = null;
function isOpera()
{
	if(bOpera == null)
	{
		if(navigator.appName.indexOf("Opera") != -1)
			bOpera = true;
		else
			bOpera = false;
	}

	return bOpera;
}

