﻿function SmartSlideShow() {
	var myBase = this;

	var imgList = [];

	var rotators = [];

	var masterDiv = null;
	var innerDiv = null;

	var currentElem = null;

	var playing = false;

	var lastTimeOut = null;
	var active = false;

	this.CurrentIndex = function() {
		if (!rotators || rotators.length == 0)
			return -1;

		var elem;
		var currentIndex = -1;

		for (var num = 0; num < rotators.length; num++) {
			elem = rotators[num];
			if (elem != currentElem)
				continue;

			currentIndex = num;
			break;
		}

		return currentIndex;
	}

	this.RotateNext = function(offset) {

		myBase.ClearTimeout();

		if (!rotators || rotators.length == 0)
			return;

		var elem;

		var currentIndex = this.CurrentIndex();

		if (currentIndex == -1)
			return;

		currentIndex += offset;

		while (currentIndex < 0) {
			currentIndex += rotators.length;
		}

		while (currentIndex >= rotators.length) {
			currentIndex -= rotators.length;
		}

		elem = rotators[currentIndex];

		myBase.Rotate(elem);
	}

	var innertableexists = false;
	this.Rotate = function(elem) {

		if (innertableexists) {
			var currentindexdiv = document.getElementById("SSShowCurrentIndexDiv");
			var playpausediv = document.getElementById("SSShowPlayIcon");
			var imgBox = document.getElementById("SShowImageBox");

			if (!currentindexdiv || !playpausediv || !imgBox) {
				//alert("currentindexdiv, imgBox or playpausediv not found");

			} else {
				currentindexdiv.innerHTML = 'Showing image ' + (this.CurrentIndex() + 1) + ' of ' + rotators.length;
				var playpauseClass = 'SSShow' + (playing ? 'Pause' : 'Play') + 'Img'

				if (SSShowPlaplaypausedivyIcon.className != playpauseClass)
					SSShowPlaplaypausedivyIcon.className = playpauseClass;

				var im = this.ImageFullImage(elem);



				var alt = '';

				if (elem.getAttribute("alt")) {
					alt = elem.getAttribute("alt");
				}

				var image = '<img src="' + im + '" alt="' + alt + '" />';

				if (imgBox.innerHTML != image)
					imgBox.innerHTML = image;

				return;
			}


		}

		currentElem = elem;
		var innerTable = '<table class="SSShowInnerTable"><tr>';


		innerTable += '<tr><td><div class="SSShowNavControls"><table class="SSShowNavControls"><tr>';
		//start curve
		innerTable += '<td class="SSShowNavLeftCurve" ></td>';

		innerTable += '<td title="Previous image" class="SSShowPrevImg" onmousedown="javascript:SSShow.RotateNext(-1);"></td>'; //prev
		innerTable += '<td class="SSShowSpacer" ></td>';
		innerTable += '<td title="Play/Pause slideshow" class="SSShow' + (playing ? 'Pause' : 'Play') + 'Img" id="SSShowPlayIcon" onmousedown="javascript:SSShow.ChangePlayState();"></td>'; //pause/continue
		innerTable += '<td class="SSShowSpacer" ></td>';
		innerTable += '<td title="Next image" class="SSShowNextImg" onmousedown="javascript:SSShow.RotateNext(1);"></td>'; //next
		innerTable += '<td class="SSShowSpacer" ></td>';
		innerTable += '<td title="Close slideshow" class="SSShowCloseImg" onmousedown="javascript:SSShow.closeRotation();"></td>'; //close
		//end curve
		innerTable += '<td class="SSShowNavRightCurve" ></td>';
		
		innerTable += '</tr></table></div></td></tr>'

		innerTable += '<tr>';

		innerTable += '<td><div id="SSShowCurrentIndexDiv" class="SSShowCurrentIndexDiv">Showing image ' + (this.CurrentIndex() + 1) + ' of ' + rotators.length + '</div></td>';

		innerTable += '</tr>'


		innerTable += '<th id="SShowImageBox">';

		var img = this.ImageFullImage(elem);



		var alt = '';

		if (elem.getAttribute("alt")) {
			alt = elem.getAttribute("alt");
		}

		innerTable += '<img src="' + img + '" alt="' + alt + '" />';

		innerTable += '</th></tr>';





		innerTable += '</table>';

		innerDiv.innerHTML = innerTable;

		var x = ((screenWidth() / 2) - (innerDiv.offsetWidth / 2));
		/*var y = ((screenHeight() / 3) - (innerDiv.offsetHeight / 2));

		if (y < 0)
		y = 0;*/

		var y = 0; //override to always stay on top - keeps play/next/prev controls in same place

		innerDiv.style.left = x + 'px';
		innerDiv.style.top = y + 'px';

		myBase.ClearTimeout();
		if (playing) {
			lastTimeOut = setTimeout(function() { myBase.RotateNext(1); }, 3000);
		}
	}

	this.ChangePlayState = function() {
		playing = !playing;

		myBase.ClearTimeout();


		this.RotateNext(0);
	}

	this.ClearTimeout = function() {
		if (lastTimeOut) {
			clearTimeout(lastTimeOut);
			lastTimeOut = null;
		}
	}

	this.InitRotation = function(startElem) {

		masterDiv = document.createElement('div');

		innerDiv = document.createElement('div');

		masterDiv.className = "SSShowMasterDiv";

		innerDiv.className = "SSShowInnerDiv";

		masterDiv.onclick = function() { SSShow.closeRotation(); };

		document.body.appendChild(masterDiv);

		document.body.appendChild(innerDiv);

		this.Rotate(startElem);
	}

	this.closeRotation = function() {
		currentElem = null;

		document.body.removeChild(masterDiv);
		document.body.removeChild(innerDiv);
		masterDiv = null;
		innerDiv = null;
		active = false;
		myBase.showFlash();
	}

	this.hideFlash = function() {
		showHideFlash(false);
	}

    this.showFlash = function() {
        showHideFlash(true);
    }

    this.StartRotationURL = function(anchor) {

    	var rurl = anchor.href;

    	var rel = '';

    	if (anchor.getAttribute("rel") && anchor.getAttribute("rel").substr(0, 7) == "sslide[") {
    		rel = anchor.getAttribute("rel");
    	}

    	//alert("StartRotationURL called - '" + rurl + "'!");

    	if (!imgList || imgList.length < 1) {
    		//alert("nothing in imglist!");
    		return;
    	}

    	var elem;

    	//find the first element with the src

    	for (var num = 0; num < imgList.length; num++) {
    		elem = imgList[num];
    		//alert(this.ImageFullImage(elem).toLowerCase() + '\n' + rurl.toLowerCase());



    		if (rel == '' || rel == elem.getAttribute("rel")) {
    			if (this.ImageFullImage(elem).toLowerCase() == rurl.toLowerCase()) {
    				//alert("rotation found!!");
    				this.StartRotation(elem);
    				return;
    			}
    		}
    	}


    }

    this.StartRotation = function(startElem) {
	
		if (active)
			return;
		active = true;
		myBase.hideFlash();

		rotators = [];

		if (!imgList || imgList.length < 1)
			return;

		var elem;

		//TODO: don't add duplicates, and with the src for startelem remember to check that it ISN'T startelem. Snooch.

		for (var num = 0; num < imgList.length; num++) {
			elem = imgList[num];

			if (elem.getAttribute("rel") != startElem.getAttribute("rel"))
				continue;
			//alert(this.ImageFullImage(elem).toLowerCase());

			//if the item IS startElem then it's fine
			if (elem == startElem) {
				rotators.push(elem);
				//alert("added:start element");
			} else if (this.ImageFullImage(elem).toLowerCase() == this.ImageFullImage(startElem).toLowerCase()) {
				continue;
				//alert("not added:same path as startElem");
			} else {
				//iterate through rotators to find if a duplicate exists

				if (!rotators || rotators.length < 1) {
					rotators.push(elem);
					//alert("added:first item, thus no duplicates");
					continue;
				}

				var fullImage = this.ImageFullImage(elem);
				//check for duplicates
				for (var rotNum = 0; rotNum < rotators.length; rotNum++) {
					if (fullImage.toLowerCase() == this.ImageFullImage(rotators[rotNum]).toLowerCase()) {
						continue;

					}
				}

				rotators.push(elem);
				//alert("added: no duplicates");
			}







		}
		playing = true;

		myBase.InitRotation(startElem);
	}

	this.ImageFullImage = function(elem) {

		var fullImg;

		if (elem.getAttribute("ssshowfull")) {
			fullImg = elem.getAttribute("ssshowfull");
		} else {
			fullImg = elem.getAttribute("src");
		}

		return fullImg;
	}


	this.init = function() {
		//runs through all appropriately named tags and adds the onclick event to run the image rotation

		var elems = document.getElementsByTagName("img");


		if (!elems || elems.length < 1)
			return;


		var elem;

		for (var num = 0; num < elems.length; num++) {
			elem = elems[num];

			//alert(elem.getAttribute("rel") + '\n' + elem.src);

			if (!elem.getAttribute("rel"))
				continue;


			if (elem.getAttribute("rel") && elem.getAttribute("rel").substr(0, 7) == "sslide[") {
				imgList.push(elem);

				elem.onclick = function() { SSShow.StartRotation(this); };

				elem.style.cursor = 'pointer';
			}
		}



	}
	
	
}



var SSShow = new SmartSlideShow();
