/* JScrollingSlides: Josh's Scrolling Slideshow Class */

JScrollingSlides = Class.create();
var JScrollingSlide_count = 0;
JScrollingSlides.prototype = 
{
	initialize: function(containerDiv,options)
	{
		this.containerDiv = $(containerDiv);
		
		this.debug = options['debug']?true:false;
		
		this.sliders = new Array();
		this.sliderWidths = new Array();
		this.sliderPositions = new Array(2);
		
		var thisObj = this;
		var func = function(e) { thisObj._initializeAfterLoaded(options); };
		Event.observe(window, 'load', func);
	},
	
	_initializeAfterLoaded: function(options)
	{
		this.id = JScrollingSlide_count++;
		this.idPre = "jscrollslides_"+this.id;
		
		var dimensions = Element.getDimensions(this.containerDiv);
		this.viewerWidth = dimensions.width;
		this.viewerHeight = dimensions.height;
		
		if(this.debug)
		{
			alert("Viewer Window: "+this.containerDiv+"\n"
				 +"   Width: "+dimensions.width+"\n"
				 +"   Height: "+dimensions.height+"\n"
				 +"   CSS Width: "+this.containerDiv.style.width+"\n"
				 +"   CSS Height: "+this.containerDiv.style.height+"\n"
				 +"   offsetWidth: "+this.containerDiv.offsetWidth+"\n"
				 +"   offsetHeight: "+this.containerDiv.offsetHeight+"\n"
				 +"   clientWidth: "+this.containerDiv.clientWidth+"\n"
				 +"   clientHeight: "+this.containerDiv.clientHeight+"\n"
				  );
		}
		
		this.containerDiv.style.position = "relative";
		this.containerDiv.style.overflow = "hidden";
		
		this.slides = new Array();
		if($(options['sourceList']))
			this._loadSlidesFromList($(options['sourceList']));
		
		this.debug = options['debug']?true:false;
		this.slideSpacing = options['slideSpacing']?options['slideSpacing']:5;
		this.delay = options['delay']?options['delay']:250;
		this.speed = options['speed']?options['speed']:10;
		this.defaultDelay = options['delay']?options['delay']:250;
		this.defaultSpeed = options['speed']?options['speed']:10;
		
		if(options['allowScroll'] == null || options['allowScroll'])
		{
			this._observeMouseEvents();
		}
		
		this._setup();
		
		this.start();
		
		pngFix();
	},
	
	_setup: function()
	{
		this.containerDiv.innerHTML='';
		this.sliders = new Array();
		this.sliderWidths = new Array();
		this.sliderPositions = new Array(2);
		var divName,divCode;
		
		for(var i=0;i<2;i++)
		{
			divName = this.idPre+"_scroll_"+i;
			divCode = "<div id=\""+divName+"\" style=\"position:absolute;visibility:hidden;\"></div>";
			new Insertion.Top(this.containerDiv,divCode);
			$(divName).style.left = "0px";
			$(divName).style.top = "0px";
			if(this.debug)
			{
				$(divName).style.border = "1px solid #333";
				$(divName).style.visibility = "visible";
			}
			this.sliders[i] = $(divName);
			this.sliderWidths[i] = 0;
			this.sliderPositions[i] = 0;
		}
		
		this._fillSliderDivs();
	},
	
	_fillSliderDivs: function()
	{
		var divCode,divStyle,divId;
		var left = 0, totalWidth = 0,slideListCount = 0;
		var finalFirstItem = -1, slideOneWidth = 0;
		var slider = this.sliders[0];
		
		while(totalWidth < 2*this.viewerWidth)
		{
			if(slideListCount > 100)
			{
				// Something is wrong. Bail out!
				var minw = 2*this.viewerWidth;
				var realw = this.masterListDivWidth;
				alert("Slideshow creation failed. We were trying to make the master list div at least "+minw+
						"px wide, it is only "+realw+"px wide, and we're bailing out after 100 tries.");
				return false;
			}
			
			// Add every slide to the master list DIV.
			for(var i=0;i<this.slides.length;i++)
			{
				divId = this.idPre+"_slide_"+i;
				divStyle = "position:absolute;top:0px;left:"+left+"px;";
				divCode = "<div id=\""+divId+"\" style=\""+divStyle+"\">"+this.slides[i]+"</div>";
				new Insertion.Top(slider,divCode);
				var dimensions = Element.getDimensions($(divId));
				left += dimensions.width + this.slideSpacing; 
				totalWidth += dimensions.width + this.slideSpacing;
				
				if(left >= this.viewerWidth && slider == this.sliders[0])	// this item has pushed us past the middle!
				{
					finalFirstItem = i + slideListCount*this.slides.length;
					this.sliderWidths[0] = left;
					left = 0;
					slider = this.sliders[1];
				}
			}
			slideListCount++;
		}
		this.sliderWidths[1] = left;
		
		this.sliderPositions[0] = this.viewerWidth;
		this.sliderPositions[0] = 0;
		this.sliderPositions[1] = this.sliderPositions[0] + this.sliderWidths[0];
		
		if(this.debug)
		{
			alert("Viewer width: " + this.viewerWidth + "\n" +
				  "Slider 1 width: " + this.sliderWidths[0] + "\n" +
				  "Slider 1 position: " + this.sliderPositions[0] + "\n" +
				  "Slider 2 width: " + this.sliderWidths[1] + "\n" +
				  "Slider 2 Position: " + this.sliderPositions[1] + "\n" +
				  "Slide List Count: " + slideListCount + "\n");
		}
		
		this._placeSliders();
		
		for(var i=0;i<2;i++)
		{
			this.sliders[i].style.width=this.sliderWidths[i]+"px";
			this.sliders[i].style.height=(this.viewerHeight-5)+"px";
			this.sliders[i].style.visibility="visible";
		}
	},
	
	_loadSlidesFromList: function(list)
	{
		var listItems = list.getElementsByTagName("LI");
		var item;
		
		for(var i = 0; i < listItems.length; i++)
		{
			item = listItems.item(i);
			if(item.nodeName == "LI")
			{
				this.slides.push(item.innerHTML);
			}
		}
	},
	
	_placeSliders: function()
	{
		this.sliders[0].style.left = this.sliderPositions[0] + 'px';
		this.sliders[1].style.left = this.sliderPositions[1] + 'px';
	},
	
	_normalizeSliders: function()
	{
		if(this.speed >= 0 && this.sliderPositions[0] <= -1*this.sliderWidths[0])
			this.sliderPositions[0] = this.sliderPositions[1] + this.sliderWidths[1];
		if(this.speed >= 0 && this.sliderPositions[1] <= -1*this.sliderWidths[1])
			this.sliderPositions[1] = this.sliderPositions[0] + this.sliderWidths[0];
		if(this.running && this.speed < 0 && this.sliderPositions[0] > this.viewerWidth && this.sliderPositions[1] > this.viewerWidth)
		{
			this.sliderPositions[1] = -1* this.sliderWidths[1] + 1;
			this.sliderPositions[0] = this.sliderPositions[1] - this.sliderWidths[0];
		}
		if(this.running && this.speed < 0 && this.sliderPositions[0] > this.viewerWidth)
			this.sliderPositions[0] = this.sliderPositions[1] - this.sliderWidths[0];
		if(this.running && this.speed < 0 && this.sliderPositions[1] > this.viewerWidth)
			this.sliderPositions[1] = this.sliderPositions[0] - this.sliderWidths[1];
	},
	
	_queueNext: function()
	{
		var thisObj = this;
		var func = function() { thisObj.run(); };
		window.setTimeout(func,this.delay);
	},
	
	_observeMouseEvents: function()
	{
		var thisObj = this;
		var func = function(e) { thisObj.mouseOver(e); };
		Event.observe(this.containerDiv, 'mouseover', func);
		var func = function(e) { thisObj.mouseOut(e); };
		Event.observe(this.containerDiv, 'mouseout', func);
		var func = function(e) { thisObj.mouseMove(e); };
		Event.observe(this.containerDiv, 'mousemove', func);
		this._updateContainerPosition();
	},
	
	_updateContainerPosition: function()
	{
		Position.prepare();
		this.containerPosition = Position.cumulativeOffset(this.containerDiv);
	},
	
	mouseOver: function(e)
	{
		this._updateContainerPosition();
		this.mouseMove(e);
		//this.speed = 0;
		
		/*var thisObj = this;
		var mouseOver = 
			function(e)
			{
				thisObj.mouseMove(e);
			};
			
		Event.observe(this.containerDiv, 'mousemove', mouseOver);*/
		
		
		//var func = function(e) { Event.stopObserving(this.containerDiv, 'mousemove', mouseOver); Event.stopObserving(this.containerDiv, 'mouseout', func); };
		//Event.observe(this.containerDiv, 'mouseout', func);
	},
	
	mouseOut: function(e)
	{
		this.speed= this.defaultSpeed;
	},
	
	mouseMove: function(e)
	{
		var perc = ((e.clientX-this.containerPosition[0]-this.viewerWidth/2)/(.5*this.viewerWidth))*100;
		//this.speed = (perc/40)^3;
		this.speed = perc/6;
		if(Math.abs(perc) < 10)
			this.speed = 0;
		//window.status = "Mouse Move: clientX="+e.clientX+", perc="+perc+"%, speed="+this.speed;
	},
	
	run: function()
	{
		if(this.running)
		{
			this.slide(this.speed);
			this._queueNext();
		}
		if (false && this.runs < 10)
		{
			alert("run called on "+this+", runs = "+this.runs+", running = "+this.running+"\n"+
				  "Slider 1 width: " + this.sliderWidths[0] + "\n" +
				  "Slider 1 position: " + this.sliderPositions[0] + "\n" +
				  "Slider 2 width: " + this.sliderWidths[1] + "\n" +
				  "Slider 2 Position: " + this.sliderPositions[1] + "\n");
			this.runs++;
		}
		//else
		//{
		//	alert("this.runs = "+this);
		//}
	},
	
	start: function()
	{
		this.running = true;
		this.runs = 0;
		this._queueNext();
	},
	
	slide: function(amt)
	{
		this.sliderPositions[0] -= amt;
		this.sliderPositions[1] -= amt;
		this._normalizeSliders();
		this._placeSliders();
	}
}

JPopinDiv = Class.create();
var JPopinDiv_count = 0;
JPopinDiv.prototype = 
{
	initialize: function(xPos,yPos,initialText,options)
	{
		this.id = JPopinDiv_count++;
		
		this.div_id= 'jpopin_'+this.id+'_div';
		this.topDiv_id= 'jpopin_'+this.id+'_top_div';
		this.midDiv_id= 'jpopin_'+this.id+'_mid_div';
		this.midLeftDiv_id= 'jpopin_'+this.id+'_midLeft_div';
		this.tabDiv_id= 'jpopin_'+this.id+'_tab_div';
		
		this.contentDiv_id= 'jpopin_'+this.id+'_left_div';
		
		this.xPos = xPos;
		this.yPos = yPos;
		
		this.zIndex = options['zIndex'] ? options['zIndex'] : 200;
		
		this.width = options['width'] ? options['width'] : 150;
		this.height = options['height'] ? options['height'] : 150;
		
		this.parentElement = options['parentElement'] ? $(options['parentElement']) : document.body;
		if(!this.parentElement) this.parentElement = document.body;
		
		this.tab = (options['tab']) ? options['tab'] : true;
		this.tabY = parseInt(options['tabY'] ? options['tabY'] : this.height/6);
		
		this.ltab = (options['ltab']) ? options['ltab'] : true;
		this.ltabY = parseInt(options['ltabY'] ? options['ltabY'] : -9999);
		
		var divCode;
		
		new Insertion.Bottom(this.parentElement,'<div id="'+this.div_id+'" style="position:absolute;left:'+this.xPos+'px;top:'+this.yPos+'px;display:none;z-index:'+this.zIndex+';"></div>');
		
		/* Top Divs */
  		new Insertion.Bottom(this.div_id,'<div class="jpopin_tl"></div>');
  		new Insertion.Bottom(this.div_id,'<div id="'+this.topDiv_id+'" class="jpopin_leftFlow" style="top: 0px;"></div>');
  		new Insertion.Bottom(this.topDiv_id,'<div class="jpopin_top" style="width:'+this.width+'px;"></div>');
  		new Insertion.Bottom(this.topDiv_id,'<div class="jpopin_tr" style="left:'+this.width+'px;"></div>');
  		
  		/* Mid Divs */
  		new Insertion.Bottom(this.div_id,'<div class="jpopin_left" style="height:'+this.height+'px;"></div>');
  		new Insertion.Bottom(this.div_id,'<div id="'+this.midDiv_id+'" class="jpopin_midFlow"></div>');
  		new Insertion.Bottom(this.midDiv_id,'<div id="'+this.midLeftDiv_id+'" class="jpopin_leftFlow"></div>');
  		
  		divCode  = '<div id='+this.contentDiv_id+' class="jpopin_content" style="width:'+this.width+'px;height:'+this.height+'px;';
  		if(options['contentDivStyle']) divCode += options['contentDivStyle'];
  		divCode += '"></div>';
  		new Insertion.Bottom(this.midLeftDiv_id,divCode);
  		new Insertion.Bottom(this.contentDiv_id,initialText);
  		
  		new Insertion.Bottom(this.midLeftDiv_id,'<div class="jpopin_right" style="left:'+this.width+'px;height:'+this.height+'px;"></div>');
  		
  		/* Bottom Divs */
  		new Insertion.Bottom(this.midDiv_id,'<div class="jpopin_bl" style="top:'+this.height+'px;"></div>');
  		new Insertion.Bottom(this.midLeftDiv_id,'<div class="jpopin_bottom" style="top:'+this.height+'px;width:'+this.width+'px;"></div>');
  		new Insertion.Bottom(this.midLeftDiv_id,'<div class="jpopin_br" style="top:'+this.height+'px;left:'+this.width+'px;"></div>');
  		
  		if(this.tab)
  		{
	  		new Insertion.Bottom(this.midLeftDiv_id,'<div id="'+this.tabDiv_id+'" class="jpopin_tab" style="left:'+this.width+'px;top:'+this.tabY+'px;"></div>');
  		}
  		if(this.ltab)
  		{
	  		new Insertion.Bottom(this.midLeftDiv_id,'<div id="'+this.tabDiv_id+'" class="jpopin_ltab" style="top:'+this.ltabY+'px;"></div>');
  		}
	},
	
	show: function()
	{
		Element.show(this.div_id);
	},
	
	hide: function()
	{
		Element.hide(this.div_id);
	}
}