// Begin JSGallery Class
// Based on http://www.dynamicdrive.com/dynamicindex14/leftrightslide_dev.htm
// Made OO and multipurpose by Chris Barnes

// ********* Usage *************
// var objJSGallery=new JSGalleryClass("objJSGallery");
// objJSGallery.sliderwidth="150px"
// objJSGallery.sliderheight="110px"
//Specify the slider's images
// objJSGallery.arrSlides[0]='<a href="http://"><img src="images/thumb.gif" border=0 width=150 height=110></a>'
// objJSGallery.arrSlides[1]='<a href="http://"><img src="images/horsham.gif" border=0 width=150 height=110></a>'
// objJSGallery.arrSlides[2]='<a href="http://"><img src="images/stove.gif" border=0 width=150 height=110></a>'
// 
// //Pre cache images:
//eg: objJSGallery.arrPreload.push("images/thumb.gif");
// objJSGallery.show();

// HTML CONTROL: <a onMouseout="objJSGallery.conveyorDirection='static';" onMouseover="objJSGallery.conveyorDirection='left';">left</a>


// ******************** Begin Class *****************
function JSGalleryClass(strVariableName,autoConveyorDirection) {

	
	// ************** Constructor ***********
	//Note that strVariableName is the name of the variable instance eg:
	//objGall=new JSGalleryClass("objGall")
	//THIS IS EESENTIAL !

	// autoConveyorDirection can be set to static, left or right.
	// default is static which will create a gallery navigable via commands
	// setting to left or right creates an auto slide show

	//boolRefillCalled is necessary to force a reload to force IE to render content properly after all tables have been rendered/
	// this is due to issues with IE where the object is instantiated WITHIN a table or div 
	boolRefillCalled=0;


	if (typeof(autoConveyorDirection)=="undefined"){
		autoConveyorDirection="static"
	}


	this.conveyorDirection=autoConveyorDirection;

	//Set Defaults
	strDivID=randomPassword(20);
	strSection2=randomPassword(20);
	strSection3=randomPassword(20);

	//Specify the slider's width (in pixels)
	this.sliderwidth="300px"
	//Specify the slider's height
	this.sliderheight="300px"
	

	//Specify the default slider's slide speed (larger is faster 1-10)
	this.slidespeed=3

	//configure background color:
	this.slidebgcolor="#EAEAEA"
	

	//Specify the slider's images
	var leftrightslide=new Array()
	this.arrSlides=new Array()
		
	// Use this to force a preload cache
	this.arrPreload=new Array()
	
	

	//Specify gap between each image (use HTML):
	this.imagegap=""

	//Specify pixels gap between each slideshow rotation (use integer):
	this.slideshowgap=0
	
	//Fuck knows why this has to be there to keep IE happy....
	slideshowgap=this.slideshowgap	

	var iedom=document.all||document.getElementById
	var actualwidth=''
	var cross_slide, ns_slide

	// *************** Methods ********************

	this.show=function(){
		
		this.WriteHTML();		
		this.preload();
		
	}


	this.preload=function(){
	
		if (this.arrPreload.length==0){
			return;
		}
	
	
	
		
		var arrGroup = new Array();
		for (var i=0;i<this.arrPreload.length;i++){
			arrGroup[i]=new Image;
			
			if (i==this.arrPreload.length-1){
				// Last image has loaded, pause a second then fillup with images 
				var strCallFunction=strVariableName+".fillup()";
				arrGroup[i].onLoad=setTimeout(strCallFunction,1000);
				
			}
			
			arrGroup[i].src=this.arrPreload[i];	
			
		}
					
	
	}
	
	
	
	this.WriteHTML=function(){


		//Correct Display discrepancy between IE and Firefox
		if (navigator.appName=="Microsoft Internet Explorer"){
			this.sliderheight=(parseInt(this.sliderheight)-3)+'px';
		}


		this.copyspeed=this.slidespeed
		leftrightslide=this.arrSlides;
		var chr39="'";

		if (iedom||document.layers){
			with (document){
			//document.write('<table border="0" cellspacing="0" cellpadding="0"><td>')
			if (iedom){
				write('<div style="position:relative;width:'+this.sliderwidth+';height:'+this.sliderheight+';overflow:hidden">')
				var $line='<div style="position:absolute;width:'+this.sliderwidth+';height:'+this.sliderheight+';"'

				if (autoConveyorDirection !="static"){
					$line=$line +'onMouseover="'+strVariableName+'.conveyorDirection='+chr39+'static'+chr39+'" onMouseout="'+strVariableName+'.conveyorDirection='+chr39+'left'+chr39+'"'
				}
				
				$line=$line +'>'
				
				
				write($line)
				write('<div id="' + strSection2 + '" style="position:absolute;left:0px;top:0px"></div>')
				write('<div id="' + strSection3 + '" style="position:absolute;left:-1000px;top:0px"></div>')
				write('</div></div>')
			}
			else if (document.layers){
				write('<ilayer width='+this.sliderwidth+' height='+this.sliderheight+' name="ns_slidemenu">')
				write('<layer name="ns_slidemenu2" left=0 top=0 onMouseover="copyspeed=0" onMouseout="copyspeed='+this.slidespeed+'"></layer>')
				write('<layer name="ns_slidemenu3" left=0 top=0 onMouseover="copyspeed=0" onMouseout="copyspeed='+this.slidespeed+'"></layer>')
				write('</ilayer>')
			}
			//document.write('</td></table>')
			}
		}
		

		this.arrSlides='<nobr>'+this.arrSlides.join(this.imagegap)+'</nobr>'
		
		if (iedom) document.write('<span id="' + strDivID + '" style="visibility:hidden;position:absolute;top:-100px;left:-9000px">'+this.arrSlides+'</span>')

		//window.onload=this.fillup()
		
		
	}


	this.fillup=function(){


		if (iedom){
			cross_slide=document.getElementById? document.getElementById(strSection2) : document.all.strSection2
			cross_slide2=document.getElementById? document.getElementById(strSection3) : document.all.strSection3
			cross_slide.innerHTML=cross_slide2.innerHTML=this.arrSlides
			actualwidth=document.all? cross_slide.offsetWidth : document.getElementById(strDivID).offsetWidth
			cross_slide2.style.left=actualwidth+this.slideshowgap+"px"
		}
		else if (document.layers){
			ns_slide=document.ns_slidemenu.document.ns_slidemenu2
			ns_slide2=document.ns_slidemenu.document.ns_slidemenu3
			ns_slide.document.write(this.arrSlides)
			ns_slide.document.close()
			actualwidth=ns_slide.document.width
			ns_slide2.left=actualwidth+this.slideshowgap
			ns_slide2.document.write(this.arrSlides)
			ns_slide2.document.close()
		}
		
		
		lefttime=setInterval(strVariableName+".slide()",30)
				

	}
	

	
	this.slide=function(){
	
		
		if (boolRefillCalled==0){
			this.fillup()
			boolRefillCalled=1;
		}

		if (this.conveyorDirection=="static"){
			this.copyspeed=0
		} 
	
	
		if (this.conveyorDirection=="right"){			
			this.copyspeed=this.slidespeed
			this.slideright();
			
		} 
		
		if (this.conveyorDirection=="left"){
			this.copyspeed=this.slidespeed
			this.slideleft()
		} 



		
	
	}


	this.slideleft=function(){
		
		//This is necessary becuase this function gets called independantly from the system after init
		copyspeed=this.copyspeed

		if (iedom){


			if (parseInt(cross_slide.style.left)>(actualwidth*(-1)+8)){
				cross_slide.style.left=parseInt(cross_slide.style.left)-copyspeed+"px"
			} else {
				cross_slide.style.left=parseInt(cross_slide2.style.left)+actualwidth+this.slideshowgap+"px"
			}

			
			if (parseInt(cross_slide2.style.left)>(actualwidth*(-1)+8)){
				cross_slide2.style.left=parseInt(cross_slide2.style.left)-copyspeed+"px"
			} else {	
				cross_slide2.style.left=parseInt(cross_slide.style.left)+actualwidth+this.slideshowgap+"px"
			}

		}
			else if (document.layers){
			if (ns_slide.left>(actualwidth*(-1)+8))
			ns_slide.left-=copyspeed
			else
			ns_slide.left=ns_slide2.left+actualwidth+this.slideshowgap

			if (ns_slide2.left>(actualwidth*(-1)+8))
			ns_slide2.left-=copyspeed
			else
			ns_slide2.left=ns_slide.left+actualwidth+this.slideshowgap
		}
	}


	this.slideright=function(){
		
		//This is necessary becuase this function gets called independantly from the system after init
		copyspeed=this.copyspeed


		if (iedom){
			
			
			//for debugging: document.fred.john.value=cross_slide2.style.left;

			if (parseInt(cross_slide.style.left)<(actualwidth*(1)+8)){
				cross_slide.style.left=parseInt(cross_slide.style.left)+copyspeed+"px"
			} else {
				cross_slide.style.left=parseInt(cross_slide2.style.left)-actualwidth-this.slideshowgap+"px"
			}

			if (parseInt(cross_slide2.style.left)<(actualwidth*(1)+8)){
				
				cross_slide2.style.left=parseInt(cross_slide2.style.left)+copyspeed+"px"
			} else {	
				cross_slide2.style.left=parseInt(cross_slide.style.left)-actualwidth-this.slideshowgap+"px"
			}

		} else if (document.layers){
			if (ns_slide.left<(actualwidth*(1)+8))
				ns_slide.left+=copyspeed
			else
				ns_slide.left=ns_slide2.left-actualwidth-this.slideshowgap

			if (ns_slide2.left<(actualwidth*(-1)+8))
				ns_slide2.left+=copyspeed
			else
				ns_slide2.left=ns_slide.left-actualwidth-this.slideshowgap
		}
	}


	function randomPassword(length)
	{
	  chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	  pass = "";
	  for(x=0;x<length;x++)
	  {
	    i = Math.floor(Math.random() * 62);
	    pass += chars.charAt(i);
	  }
	  return pass;
}


} // End JSGalleryClass Class

