/**
 * CMSlider.js
 *
 * DOM object management class for CoalSlider
 *
 * @copyright Coalmarch Productions LLC 2007 All Rights Reserved
 * @authors Ken Winters <kwinters@coalmarch.com>
 * @version 0.3
 * @license MIT
 *
 **/

var global_CMSliderController = null;
CMSlider = Class.create();
Object.extend(Object.extend(CMSlider.prototype, CMBase.prototype), {

	initialize: function()
	  {
	  	/**
		 * @var value of the rel field to look for
		 **/
		this.typeValue = "text/sliderHandle";

		/**
		 * @var prepended to the link tag ID to find slider dom object
		 **/
		this.domObjectPrefix = "sliderFor_";

		/**
		  * @var When you want the slider to open on a click, we use click.
		  * If you want the slider to open on something else, that's your
		  * business.
		  * For now only click is supported, not really seeing a big need for
		  * mouseover or anything but someone else could extend this once
		  * the safari event assignment on link text instead of link element thing
		  * gets fixed.
		  **/
		this.showEvent = "click";

		 /**
		  * @var Speed of fades / appears. Affected by method type inline.
		  **/
		this.duration = 0.4;

	  	if( !document.getElementsByTagName ){ return; }
		CMBase.prototype.initialize.call(this);

		for(var i=0;i<this.carousel.length;i++)
		{
			var thisSlider = this.carousel[i];
			var sliderID = thisSlider.domAnchor.id;

			/**
			 * check autoscroll and title display options
			 **/
			if( thisSlider.domObject.className.indexOf("autoScroll") > -1 )
			{
				//check for scroll interval (autoScroll2000, etc.)
				thisSlider.autoScroll = true;
				var interval = 7000;
				try {
					var autoScrollEnd = thisSlider.domObject.className.indexOf("autoScroll")+10;
					var valueEnd = thisSlider.domObject.className.indexOf(" ",autoScrollEnd);
					if( valueEnd == -1 )
					{
						valueEnd = thisSlider.domObject.className.length;
					}
					interval = parseInt(
						thisSlider.domObject.className.substring(
							autoScrollEnd,valueEnd
						)
					);
				} catch (e) {interval = 7000;}
				setInterval("autoScrollSliderController('"+sliderID+"')", interval);
			}
			else
			{
				thisSlider.autoScroll = false;
			}
			if( thisSlider.domObject.className.indexOf("showTitles") > -1 )
			{
				thisSlider.showTitles = true; //show title under the generated image
			}
			else
			{
				thisSlider.showTitles = false;
			}

			thisSlider.mainView = document.getElementById("sliderFor_"+sliderID);
			thisSlider.mainViewWrapper = document.getElementById("wrapperFor_"+sliderID);
			thisSlider.mainViewWrapper.style.position = 'relative';
			thisSlider.imageSize = thisSlider.mainViewWrapper.clientWidth;
			thisSlider.dotNav = document.getElementById('dotNav_'+sliderID);
			thisSlider.dotButtons = new Array();
			thisSlider.leftNav = document.getElementById('leftNav_'+sliderID);
			thisSlider.rightNav = document.getElementById('rightNav_'+sliderID);
			thisSlider.thumbImages = document.getElementsByClassName("thumb","sliderData_"+sliderID);
			thisSlider.currentThumbNum = 0;
			thisSlider.duration = this.duration;

			this.setup(thisSlider);
		}
	},
	setup: function(thisSlider)
	{
		if (thisSlider.thumbImages.length < 1)
		{
			return;//no images, try to be minimally broken
		}

		if (thisSlider.dotNav != null)
		{
			thisSlider.dotNav.innerHTML="";
		}


		var newMainViewHTML = '';
		for (j=0;j<thisSlider.thumbImages.length;j++)
		{
			newMainViewHTML += this.getImageHTML(thisSlider.thumbImages[j],thisSlider.showTitles);
		}
		newMainViewHTML += this.getImageHTML(thisSlider.thumbImages[0],thisSlider.showTitles);
		newMainViewHTML += "";

		thisSlider.mainView.innerHTML = newMainViewHTML;
		thisSlider.mainView.style.width = (thisSlider.thumbImages.length+1) + '00%';

		if (thisSlider.thumbImages.length < 2)
		{
			if (thisSlider.dotNav != null)
			{
				thisSlider.dotNav.style.display="none";
				if (thisSlider.dotNavWrapper)
				{
					thisSlider.dotNavWrapper.style.display="none";
				}
			}
			thisSlider.leftNav.onclick = function(evt) { }
			thisSlider.rightNav.onclick = function(evt) { }
			thisSlider.leftNav.style.cursor="";
			thisSlider.rightNav.style.cursor="";
		}
		else
		{
			thisSlider.leftNav.style.cursor="pointer";
			thisSlider.rightNav.style.cursor="pointer";
			if (thisSlider.dotNav != null)
			{
				thisSlider.dotNav.style.display="block";
				if (thisSlider.dotNavWrapper)
				{
					thisSlider.dotNavWrapper.style.display="block";
				}
				for (i=0;i<thisSlider.thumbImages.length;i++)
				{
					var domImage = thisSlider.thumbImages[i];
					if (domImage != null)
					{
						var tempElement = document.createElement('div');
						if (i == thisSlider.currentThumbNum)
						{
							tempElement.className = "lfloat currentSliderDot";
						}
						else
						{
							tempElement.className = "lfloat otherSliderDot";
						}
						tempElement.title = domImage.title;
						tempElement.alt = "navThumb"+i;
						thisSlider.dotNav.appendChild(tempElement);
						thisSlider.dotButtons[thisSlider.dotButtons.length] = tempElement;
						tempElement.style.cursor = "pointer";
						tempElement.onclick = function(evt)
						{
							if (! evt) evt = window.event;
							var domEventTarget = evt.target || evt.srcElement;
							var imageNum = domEventTarget.alt.substring(8);
							thisSlider.autoScroll = false; //if it was auto-scrolling, and we got user intervention, stop autoscrolling
							global_CMSliderController.slide(thisSlider,imageNum);
						}
					}
				}
			}

			thisSlider.leftNav.onclick = function(evt)
			{
				thisSlider.autoScroll = false;
				global_CMSliderController.slide(thisSlider,"left");
			}
			thisSlider.rightNav.onclick = function(evt)
			{
				thisSlider.autoScroll = false;
				global_CMSliderController.slide(thisSlider,"right");
			}
		}

		if (thisSlider.thumbImages.length < 2 && thisSlider.leftNav.className.substring(0,2) == "on")
		{
			thisSlider.leftNav.className = "off"+thisSlider.leftNav.className.substring(2);
			thisSlider.rightNav.className = "off"+thisSlider.rightNav.className.substring(2);
		}
		if(thisSlider.thumbImages.length >= 2 && thisSlider.leftNav.className.substring(0,3) == "off")
		{
			thisSlider.leftNav.className = "on"+thisSlider.leftNav.className.substring(3);
			thisSlider.rightNav.className = "on"+thisSlider.rightNav.className.substring(3);
		}
	},
	slide: function(thisSlider,imageNumRequest)
	{
		var direction = -1;
		if (imageNumRequest == "left")
		{
			imageNum = thisSlider.currentThumbNum - 1;
			direction = 1;
		}
		else if (imageNumRequest == "right")
		{
			imageNum = thisSlider.currentThumbNum + 1;
		}
		else
		{
			imageNum = imageNumRequest;
		}
		var slideLength = Math.abs(thisSlider.currentThumbNum-imageNum);
		if (imageNum >= thisSlider.thumbImages.length)
		{
			imageNum = 0;
			slideLength = 1;
		}
		if (imageNum < 0)
		{
			imageNum = thisSlider.thumbImages.length - 1;
			thisSlider.mainView.style.left = "-" + (thisSlider.imageSize*thisSlider.thumbImages.length) + "px";
			thisSlider.mainView.style.top = "0px";
			slideLength = 1;
		}
		if (imageNumRequest < thisSlider.currentThumbNum && imageNumRequest >= 0)
		{
			direction = 1;
		}

		if (thisSlider.currentThumbNum != imageNum)
		{
			//going from fake first to right toward anywhere needs to reset to start
			if (direction == -1 && thisSlider.currentThumbNum == 0)
			{
				thisSlider.mainView.style.left = "0px";
				thisSlider.mainView.style.top = "0px";
			}
			new Effect.Move(thisSlider.mainView, {
				x: thisSlider.imageSize*direction*slideLength,
				y: 0,
				duration: thisSlider.duration,
				queue: { position:'end', scope:'CMSlider', limit:1 }
			});
		}

		thisSlider.currentThumbNum = imageNum;

		for (k=0;k<thisSlider.dotButtons.length;k++)
		{
			if (k == thisSlider.currentThumbNum)
			{
				thisSlider.dotButtons[k].className = "lfloat currentSliderDot";
			}
			else
			{
				thisSlider.dotButtons[k].className = "lfloat otherSliderDot";
			}
		}
	},
	getImageHTML: function(imageData, showTitles)
	{
		try {
			if( imageData.onclick )
			{
				var imageOnClick = (imageData.onclick + '');
				imageOnClick = imageOnClick.substr(imageOnClick.indexOf('{') + 1,imageOnClick.length - imageOnClick.indexOf('{') - 2).replace(/"/g, "'");
			}
		} catch (e) {}
		try {
			if (imageData == null) {return '';}
			if (showTitles == true)
			{
				return '<div class="lfloat sliderGeneratedImage"><a href="' + imageData.href + (imageOnClick ? ( '" onclick="' + imageOnClick ) : '' ) + '" title="' + imageData.title + '">' +
				'<img border="" alt="' + imageData.title + '" src="' + imagePath + imageData.rel + '" title="' + imageData.title + '" />' +
				'</a><br /><a href="' + imageData.href + '" >' + imageData.title + '</a></div>';
			}
			return '<div class="lfloat sliderGeneratedImage"><a href="' + imageData.href + (imageOnClick ? ( '" onclick="' + imageOnClick ) : '' ) + '" title="' + imageData.title + '">' +
				'<img border="" alt="' + imageData.title + '" src="' + imagePath + imageData.rel + '" title="' + imageData.title + '" />' +
				'</a></div>';
		} catch (e) {
			return '';
		}
	}
});

function autoScrollSliderController(anchor_id)
{
	for(var e=0;e<global_CMSliderController.carousel.length;e++)
	{
		if( anchor_id == global_CMSliderController.carousel[e].domAnchor.id )
		{
			if (global_CMSliderController.carousel[e].autoScroll)
			{
				try {
					if( document.getElementById('lightbox').style.display != 'none' )
					{
						return;
					}
				} catch (error) {}
				global_CMSliderController.slide(global_CMSliderController.carousel[e],"right");
			}
			return;
		}
	}
}

function initCMSliderController() { global_CMSliderController = new CMSlider(); }
Event.observe(window, 'load', initCMSliderController, false);
