﻿// JScript File

function AlbumManager(mainContainer, containerDiv, rightArrow, leftArrow, width, location)
{
	var me = this;
	this.turnInterval = null;
	this.pixel = 20;
	this.extraPixel = 1;
	
	this.images = new Array();
	this.imageLocs = new Array();
	this.interval = 1;
	this.width = width;
	this.mainContainer = mainContainer;
	this.container = containerDiv;
	this.subContainer = crElm("table");
	this.subContainer.id = "tbl";
	this.location = location;
	this.leftArrow = leftArrow;
	this.rightArrow = rightArrow;
	this.turnPos = "Left";
	
	this.isStart = function()
	{
		//var subC = me.subContainer;
		var cDiv = me.container;
		return (cDiv.scrollLeft == 0) ? true : false; 
	}
	
	this.isEnd = function()
	{
		var cDiv = me.container;
		return (cDiv.scrollWidth == cDiv.scrollLeft + cDiv.clientWidth) ? true : false; 
	}

	this.turnL = function(manuelTurn)
	{
		var pxl = (manuelTurn) ? me.extraPixel : me.pixel;
		me.container.scrollLeft = me.container.scrollLeft - pxl;
		
		if(me.isStart() && manuelTurn)
		{
			me.stopTurn();
			me.turnRight(true);
		}
	}
	
	this.turnR = function(manuelTurn)
	{
		var pxl = (manuelTurn) ? me.extraPixel : me.pixel;
		me.container.scrollLeft = me.container.scrollLeft + pxl;

		if(me.isEnd() && manuelTurn)
		{
			me.stopTurn();
			me.turnLeft(true);
		}
	}
	
	this.turnLeft = function(manuelTurn)
	{
		me.turnInterval = setInterval(function(){me.turnL(manuelTurn)}, me.interval);
		me.turnPos = "Left";
	}
	
	this.turnRight = function(manuelTurn)
	{
		me.turnInterval = setInterval(function(){me.turnR(manuelTurn)}, me.interval);
		me.turnPos = "Right";
	}
	
	this.stopTurn = function()
	{
		clearInterval(me.turnInterval);
	}
	
	this.createImage = function(imgPath, location)
	{
		var imgDiv = crElm("div");
		imgDiv.className = "picbox";
		
		var img = crElm("img");
		img.src = imgPath;
		//img.style.cursor = "pointer";
		//img.onclick = function(){window.location = location};
		
		imgDiv.appendChild(img);
		
		return imgDiv;
	}
	
	this.initialize = function()
	{
	    var sbCon = this.subContainer;
	    sbCon.cellPadding = "0";
	    sbCon.cellSpacing = "0";
	    var sbConTbody = crElm("tbody");
	    var sbConTr = crElm("tr");
	    sbConTbody.appendChild(sbConTr);
	    sbCon.appendChild(sbConTbody);

		var imgs = this.images;
		for(var i=0; i<imgs.length; i++ )
		{
		    
		    var leftAreaTd = crElm("td");
		    leftAreaTd.style.backgroundColor='#fff';
			leftAreaTd.innerHTML='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
		    sbConTr.appendChild(leftAreaTd);
		    
		    var sbConTd = crElm("td");
			var img = this.createImage(imgs[i], this.imageLocs[i]);
			sbConTd.appendChild(img);
			sbConTr.appendChild(sbConTd);
			
			var rightAreaTd = crElm("td");
		    rightAreaTd .style.backgroundColor='#fff';
			rightAreaTd .innerHTML='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
		    sbConTr.appendChild(rightAreaTd);
			
			var freeTd = crElm("td");
			//freeTd.style.backgroundColor='#fff';
			freeTd.innerHTML='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			sbConTr.appendChild(freeTd);
		}
		
		this.leftArrow.onmousedown = this.turnLeft;
	    this.rightArrow.onmousedown = this.turnRight;
	    this.leftArrow.onmouseup = 
	    this.leftArrow.onmouseout = 
	    this.rightArrow.onmouseup = 
	    this.rightArrow.onmouseout = 
	    this.stopTurn = this.stopTurn;
	    
		
		this.mainContainer.onmouseover = this.stopTurn;
		function ctrlTurn()
		{
			if(me.turnPos == "Right")
				me.turnRight(true);
			else
				me.turnLeft(true);
		}
		this.mainContainer.onmouseout = ctrlTurn;
		
		this.turnRight(true);
	}
	
	// Settings
	
	this.container.style.overflow = "hidden";
	//this.container.style.height = "90px";
	this.container.style.width = this.width + "px";
	this.container.appendChild(this.subContainer);
		
	
	function crElm(tagName)
	{
		return document.createElement(tagName);
	}
	
	function getElm(elmId)
	{
		return document.getElementById(elmId);
	}
	
}
