/*
Copyright(c) by pioneer (http://www.quangich.com)
Special thanks to http://www.hypergurl.com
*/
var imgPath='';
var borderImgPath='';
var imgArr=new Array();
var imgWidth=new Array();
var imgUrls=new Array();
var imgTitle=new Array();
var scrollerwidth=0;
var scrollerheight=0;
var scrollerspeed=1;
var imgAlbum;

var pauseTime=1;
var pauseAccumulate=0;
var widthAccumulate=0;
var firstImgWidth=0;
var loadedIdx=-1, showingIdx=-1;
var imgLoaded=0;
var imgAlbumID="";
var pieceTimer=100;
var spaceImg="";
var commentColor="#000000";

function addAd(img, w, url, tit){
	imgArr.push(img);
	imgWidth.push(w);
	imgUrls.push(url);
	imgTitle.push(tit);
}

function loadAlbumImages(){
	if((imgLoaded==1 || (loadedIdx==-1 && imgLoaded==0))&& loadedIdx<imgArr.length-1 ){
		loadedIdx++;
		imgAlbum = new Image();
		imgAlbum.src=imgPath+imgArr[loadedIdx];
		imgLoaded=0;
		waitImageLoaded();
	}
	
	if(loadedIdx==imgArr.length-1) {
		populate(imgAlbumID);
	}
}

function waitImageLoaded(id){
	if(imgAlbum.complete){		
		imgLoaded=1;
		imgWidth[loadedIdx]=imgAlbum.width;
		loadAlbumImages();		
	}else{
		setTimeout("waitImageLoaded();",10);
	}
}

function setVars(path, bpath, space, sw, sh, ptime, ccolor){
	imgPath=path;
	scrollerwidth=sw;
	scrollerheight=sh;
	pauseTime=ptime;
	borderImgPath=bpath;
	spaceImg=space;
	commentColor=ccolor;
}

function shiftIt(idx){	
	var n='', w=0, t='',u='';
	
	for(var i=0;i<idx;i++){
		/*remove first element in array*/
		n=imgArr.shift();
		w=imgWidth.shift();
		t=imgTitle.shift();
		u=imgUrls.shift();
		
		/*insert it to end of array*/
		imgArr.push(n);
		imgWidth.push(w);		
		imgTitle.push(t);
		imgUrls.push(u);
	}
}

function canScroll(){
	var totalwidth=0;
	firstImgWidth=0;
	for(var i=0;i<imgWidth.length;i++){
		totalwidth+=imgWidth[i];
		if(i<1) firstImgWidth+=imgWidth[i];
	}
	return (totalwidth>scrollerwidth);
}
	
var scrollercontent='';

var pauseit=0;
var stop=0;
	
var copyspeed=scrollerspeed;
var iedom=document.all||document.getElementById;
var cross_scroller;
var pausespeed=(pauseit==0)? copyspeed: 0;

var actualwidth='';	
function populate(id){
	if (iedom){
		cross_scroller=document.getElementById(id);
		if(cross_scroller){
			cross_scroller.style.top=0;
			cross_scroller.style.left=0;
			cross_scroller.innerHTML=rebuildHtml();				
			actualwidth=scrollerwidth;
			showingIdx=0;
			if (canScroll()) lefttime=setInterval("scrollscroller()",pieceTimer);
		}
	}		
}

function rebuildHtml(){
	var s = new String("<table border='0' cellpadding='0' cellspacing='0'><tr><td height='2px'></td></tr><tr>");
	for(var i=0;i<imgArr.length;i++){		
		/*"+(i==0?"class='imgBorderSA'":"")+" */
		s+="<td valign='top'><a "+(imgUrls[i]!=""?"href='"+imgUrls[i]+"'":"")+" onmouseover='stop=1;' onmouseout='stop=0;'><img class='imgAlbumBorderBlack' border='0' src='"+imgPath+imgArr[i]+"'></a></td>";		
		s+="<td width='2px'>"+spaceImg+"</td>";
	}
	s+="</tr></table>";	
	return s;
}

function check4Shift(){
	var w=0;
	for(var i=showingIdx+1;i<imgWidth.length;i++) w+=imgWidth[i]+3+2;
	
	return (w>scrollerwidth?0:1);
}

function scrollscroller(){		
	if(stop==1) return;
	var l = parseInt(cross_scroller.style.left);
		
	if(Math.abs(l)>=widthAccumulate){ /*enter pause mode*/
		if(check4Shift()==1){
			shiftIt(showingIdx);
			cross_scroller.innerHTML=rebuildHtml();
			showingIdx=0;
			widthAccumulate=0;
			cross_scroller.style.left="0px";
		}
		pauseAccumulate=0;
		copyspeed=0;
		pauseit=1;
		widthAccumulate+=imgWidth[showingIdx]+3+2;
		
		var el = document.getElementById("id_title_"+imgAlbumID);
		if(el) el.innerHTML=imgTitle[showingIdx];
		
		showingIdx++;
	}else{
		if(pauseit==1){			
			pauseAccumulate+=pieceTimer;
			if(pauseAccumulate>=pauseTime){ /*unpause it*/
				pauseAccumulate=0;
				pauseit=0;
				copyspeed=scrollerspeed;
			}
		}
	}
	if(pauseit==0){
		cross_scroller.style.left=parseInt(cross_scroller.style.left)-copyspeed+"px";
	}
}//function

function init(id){
	imgAlbumID=id;
	if (iedom && imgArr.length>0){
		document.write('<table style=\"z-index:-2;\" width="'+scrollerwidth+'" cellspacing="0" cellpadding="1" border="0">');
		document.write('<tr><td>');
		document.write('<div style="z-index:0;position:relative;width:'+(scrollerwidth)+'px;height:'+(scrollerheight+4)+'px;overflow:hidden;" onMouseover="copyspeed=pausespeed" onMouseout="copyspeed=scrollerspeed">')
		document.write('<div id="'+id+'" style="z-index:0;position:absolute;left:0px;top:0px;width:100%;">')
		document.write('</div></div>')
		document.write('</td></tr>');
		document.write('<tr><td valign=\"top\" class=comment style=\"color:'+commentColor+';\" id=\"id_title_'+id+'\"></td></tr>');
		document.write('</table>');
	}//if
}
