/*
	Scroller
	
	(c) Copyright 2004 - Proximity/FHV
*/

function ScrollerObject(height, top){
	this.data = {};
	this.current = null;
	this.height = height;
	this.top = top;
	
	/*** API ***/
	
	this.load = function(type, filename){
		if(this.data[filename]) return this.set(filename);

		include(fileName);
	}
	
	this.add = function(type, data, noset){
		if(data == "MULTIPLEROW"){
			this.data[type] = noset;
			return this.setMultiple(type);
		}
		
		this.data[type] = data;
		if(!noset) this.set(type);
	},
	
	this.setMultiple = function(type){
		if(type == this.current) return;

		this.clear();
		this.current = type;

		if(!isMozilla) Animate.fade(this.oExt, 0);
		
		var q = this.data[type];
		var l = Math.ceil(q.length/3)
		for(var i=0;i<q.length;i++){
			this.addItem(q[i], true, i==0);
			if((i+1)%l == 0) this.oExt.appendChild(document.createElement("br"));
		}
		this.oExt.scrollLeft = 0;
		
		if(!isMozilla) this.anim.play();
	}
	
	this.set = function(type){
		if(!this.inited) return (this.queued = type);
		if(type == this.current) return;
		
		this.clear();
		this.current = type;
		if(!isMozilla) Animate.fade(this.oExt, 0);
		
		if(this.data[type].pool){
			var q = this.data[type].pool;
			for(var i=0;i<q.length;i++) this.oExt.appendChild(q[i]);
		}
		else{
			var q = this.data[type];
			for(var i=0;i<q.length;i++) this.addItem(q[i]);
		}
		this.oExt.scrollLeft = 0;
		
		if(!isMozilla) this.anim.play();
	}
	
	this.addItem = function(oData, isThumb, isFirst){
		var img = this.oExt.appendChild(document.createElement("img"));
		img.src = oData.img;
		img.style.cursor = "hand";
		
		if(isThumb){
			img.style.border = "1px solid black";
			img.style.position = "relative";
			if(!isFirst) img.style.marginTop = "-1px";
			img.style.marginLeft = "-1px";
			Animate.fade(img, 1);
			
			img.onmouseover = function(){this.host.pause();Animate.fade(this, 0.8);}
			img.onmouseout = function(){this.host.play();Animate.fade(this, 1);}
			img.onmousedown = function(){this.host.hidePopup();this.host.showPopup(this.data)}
		}
		else if(!oData.link.match(/html/)){
			img.onmousedown = function(){this.host.pause();this.host.hidePopup();this.host.showPopup(this.data)}
		}
		
		img.host = this;
		img.galleryimg = "no";
		img.title = oData.alt;
		//img.data = oData.link;
		//img.onclick = function(){this.host.onimageclick(this.data);}
	}
	
	this.clear = function(){
		if(!this.current) return;
		var q = this.data[this.current].pool = [];
		this.current = null;
		
		for(var i=this.oExt.childNodes.length-1;i>=0;i--){
			q.unshift(this.oExt.childNodes[i]);
			this.oExt.childNodes[i].parentNode.removeChild(this.oExt.childNodes[i]);
		}
	}
	
	this.showPopup = function(link){
		this.pause();
		//Webpage
		if(link.match(/^http\:/)) return window.open(link);
		
		this.showingPopup = true;
		
		//Movie
		if(link.match(/\.mpg$/)){
			this.oPopup.innerHTML = '<OBJECT ID="mediaPlayer" style="margin-top:76px" width="384" height="288" CLASSID="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" CODEBASE="http://activex.microsoft.com/activex/ controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"  STANDBY="Loading Movie..."  TYPE="application/x-oleobject">'
			+ '<PARAM NAME="animationatStart" VALUE="true">'
			+ '<PARAM NAME="transparentatStart" VALUE="false">'
			+ '<PARAM NAME="autoStart" VALUE="true">'
			+ '<PARAM NAME="showControls" VALUE="true">'
			+ '<EMBED src="' + link + '" style="margin-top:76px" width="384" height="288"></EMBED>';
			+ '</OBJECT>';

			setTimeout("if(Scroller.oPopup.firstChild) Scroller.oPopup.firstChild.filename = '" + link.replace(/'/, "\\'") + "'", 100);
			this.oPopup.style.marginLeft = "-212px";
			this.oPopupBG.style.marginLeft = "-212px";
			this.oPopup.style.width = 
			this.oPopupBG.style.width = (424 + (isIE5x?40:0)) + "px";
			
			//QUICK FIX
			this.oPopup.style.textAlign = "center";
		}
		//Img
		else{
			// if(link.match(/\.(jpg|.gif)$/))
			this.oPopup.innerHTML = '<img src="' + link + '" />';
			this.oPopup.style.display = "block";
			
			//QUICK FIX
			var w = 680;//this.oPopup.lastChild.offsetWidth;
			this.oPopup.style.textAlign = "center";
			
			this.oPopup.style.display = "none";
			
			this.oPopup.style.width = (w-2+(isIE5x?10:0)) + "px";
			this.oPopupBG.style.width = (w+(isIE5x?10:0)) + "px";
			this.oPopup.style.marginLeft = (-1*(w+10)/2) + "px";
			this.oPopupBG.style.marginLeft = (-1*(w+10)/2) + "px";
		}
		
		this.oPopup.style.display = "block";
		this.oPopupBG.style.display = "block";
		this.oPopup.appendChild(this.popupClose);
	}
	
	this.hidePopup = function(){
		this.showingPopup = false;
		this.oPopup.style.display = "none";
		this.oPopupBG.style.display = "none";
		this.oPopup.innerHTML = "";
		this.play();
	}
	
	this.start = function(){
		clearInterval(this.timer);
		
		this.oExt.scrollLeft = 0;
		this.play();
	}
	
	this.play = function(){
		if(this.showingPopup) return;
		this.timer = setInterval("Scroller.scroll()", 25);
	}
	
	this.pause = function(){
		clearInterval(this.timer);
	}
	
	this.scroll = function(){
		this.oExt.scrollLeft += isMozilla && this.oExt.getElementsByTagName("br").length ? 2 : 1;

		if(this.oExt.firstChild.offsetWidth - this.oExt.scrollLeft <= 0){
			this.oExt.style.visibility = "hidden";
			
			var nodes = this.oExt.getElementsByTagName("br");
			var w = this.oExt.firstChild.offsetWidth;
			
			//for multiple
			var q = nodes.length;
			for(var i=0;i<q;i++){
				this.oExt.insertBefore(i-1 < 0 ? this.oExt.firstChild : nodes[i-1].nextSibling, nodes[i]);
			}
			//for singular
			if(!nodes.length) this.oExt.appendChild(this.oExt.firstChild); else w--;
			
			this.oExt.scrollLeft -= w;
			this.oExt.style.visibility = "visible";
		}
	}
	
	/*** INIT ***/
	
	this.Init = function(){
		this.oExt = document.body.appendChild(document.createElement("NOBR"));
		this.oExt.style.height = height + "px";
		this.oExt.style.position = "absolute";
		this.oExt.style.top = top + "px";
		this.oExt.style.left = "0px";	
		this.oExt.style.overflow = "hidden";
		this.oExt.style.width = "100%";
		
		if(firstpage == "true")
		{
			this.oExt.style.zIndex = 2;
		}
		
		this.oExt.host = this;
		this.oExt.onmouseover = function(){this.host.pause();}
		this.oExt.onmouseout = function(){this.host.play();}
		
		this.oPopup = document.body.appendChild(document.createElement("DIV"));
		this.oPopup.className = "popup";
		if(isIE5x) this.oPopup.style.height = 470;
		this.oPopupBG = document.body.appendChild(document.createElement("DIV"));
		this.oPopupBG.className = "popupBG";
		if(isIE5x) this.oPopupBG.style.height = 470;

		var img = this.popupClose = this.oPopup.appendChild(document.createElement("IMG"));
		img.src = "";
		img.host = this;
		img.className = "popupClose"
		//img.onclick = function(){this.host.hidePopup()}
		img.onmouseover = function(){this.src = ''}
		img.onmouseout = function(){this.src = ''}
		
		//debug
		//this.oExt.style.backgroundColor = "gray";
		this.anim = new AnimationSequence(this.oExt, "fade", 0, 1, 0, 15, 10).compile();
		this.anim.host = this;
		this.anim.onfinish = function(){this.host.start();}
		this.start();
		
		this.inited = true;
		if(this.queued) this.set(this.queued);
	}
}

function include(sourceFile){
	var scr = document.createElement("script");
	scr.src = sourceFile;
	document.body.appendChild(scr);
}