var sr = new Array();
var st = new Array();
var SlideRotator = Class.create();

SlideRotator.prototype = {
	initialize: function(moduleID, dynamic, duration, transition, pauseOnMouseFocus, customSlideLayout, direction){
		this.moduleID = moduleID;
		this.dynamic = dynamic;
		this.duration = duration;
		this.transition = transition;
		this.pauseOnMouseFocus = pauseOnMouseFocus;
		this.customSlideLayout = customSlideLayout;
		this.lastTransitionTime = (new Date()).getTime();
	
		if (parseInt($("dvFrontParent" + moduleID).style.zIndex) > parseInt($("dvBackParent" + moduleID).style.zIndex)) {
			this.currentSlide = "dvBack" + moduleID;
			this.nextSlide = "dvFront" + moduleID;
			this.currentSlideParent = "dvBackParent" + moduleID;
			this.nextSlideParent = "dvFrontParent" + moduleID;			
		} else {
			this.currentSlide = "dvFront" + moduleID;
			this.nextSlide = "dvBack" + moduleID;
			this.currentSlideParent = "dvFrontParent" + moduleID;
			this.nextSlideParent = "dvBackParent" + moduleID;			
		}

		this.paused = false;
		this.direction = direction;
		
		if (pauseOnMouseFocus)
		{
			Event.observe("dvContent"+moduleID, 'mouseover', this.onMouseOver.bind(this));
			Event.observe("dvContent"+moduleID, 'mouseout', this.onMouseOut.bind(this));
		}
		
		//Replace link for Previous and Next Slide Links
		if(this.dynamic)
		{
			if($("GoPrevSlide")!=null)
				$("GoPrevSlide").setAttribute("href","javascript:changeSlide(" + this.moduleID + ", -1);");
			if($("GoNextSlide")!=null)
				$("GoNextSlide").setAttribute("href","javascript:changeSlide(" + this.moduleID + ",  1);");
				
			$(this.currentSlideParent).style.position = "absolute";
			$(this.nextSlideParent).style.position = "absolute";
			this.ResizeModuleContent();
		}
		
		this.showSlides();	
	},
	
	onMouseOver: function (ev) {
		this.paused = true;
	},
	
	onMouseOut: function (ev) {
		this.paused = false;
	},
	
	flip: function(direction){
		
		if (this.currentSlide == "dvFront" + this.moduleID){
			this.currentSlide = "dvBack" + this.moduleID;
			this.currentSlideParent = "dvBackParent" + this.moduleID;
			
			this.nextSlide = "dvFront" + this.moduleID;
			this.nextSlideParent = "dvFrontParent" + this.moduleID;

			$(this.nextSlideParent).style.zIndex = parseInt($(this.currentSlideParent).style.zIndex) - direction;
		} else {
			this.currentSlide = "dvFront" + this.moduleID;
			this.currentSlideParent = "dvFrontParent" + this.moduleID;
			
			this.nextSlide = "dvBack" + this.moduleID;
			this.nextSlideParent = "dvBackParent" + this.moduleID;
			
			$(this.currentSlideParent).style.zIndex = parseInt($(this.nextSlideParent).style.zIndex) + direction;
		}
	},
	
	registerCallback: function() {
		var callback = function () {
			this.tick();
		}
		
		if (this.dynamic){
			st[this.moduleID.toString()] = window.setTimeout(callback.bind(this), this.duration * 1000);
		}
    },
	
	showSlides: function(){
				
		var afterFinishCallback = (function(direction) {
			this.flip(direction);
			this.registerCallback();
		}).bind(this);
		
		switch (this.transition) {
			case "SwapFade":	
				new Effect.Parallel([new Effect.Fade(this.currentSlide, {sync: true}), new Effect.Appear(this.nextSlide, {sync: true})], {duration: 1,afterFinish: afterFinishCallback(1)});
				break;
			case "CrossFade":
				//new Effect.Parallel([new Effect.BlindUp(this.currentSlide, {sync: true}), new Effect.BlindDown(this.nextSlide, {sync: false})], {duration: 3,afterFinish: afterFinishCallback(1)});
				window._timeoutID = setTimeout("Element.toggle('" + this.currentSlide + "')", 900);
				new Effect.BlindUp(this.currentSlide, {duration:1});
				new Effect.BlindDown(this.nextSlide, {duration:1, afterFinish: afterFinishCallback(1)});
				break;
			case "Puff":				
				Element.show(this.nextSlide);			
				new Effect.Puff(this.currentSlide, {duration:1, afterFinish: afterFinishCallback(-1)});
				break;
			case "BlindDown":
				setTimeout("Element.hide('" + this.currentSlide+ "')", 1000);
				new Effect.BlindDown(this.nextSlide, {duration:1, afterFinish: afterFinishCallback(1)});
				//setTimeout("Element.hide('" + this.currentSlide+ "')", 1000);
				//Spry.Effect.Slide(this.nextSlide, {duration:1000,from:'0%',to:'100%',toggle:false,finish:afterFinishCallback(1)});
				break;
			case "BlindUp":
				Element.show(this.nextSlide);
				setTimeout("Element.toggle('" + this.currentSlide + "')", 900);
				new Effect.BlindUp(this.currentSlide, {duration:1, afterFinish: afterFinishCallback(-1)});
				break;
			case "SwitchOff":
				Element.show(this.nextSlide);
				new Effect.SwitchOff(this.currentSlide, {duration:1, afterFinish: afterFinishCallback(-1)});
				break;
			case "DropOut":
				Element.show(this.nextSlide);
				new Effect.DropOut(this.currentSlide, {duration:1, afterFinish: afterFinishCallback(-1)});
				break;
			case "Squish":
				Element.show(this.nextSlide);
				new Effect.Squish(this.currentSlide, {duration:1, afterFinish: afterFinishCallback(-1)});
				break;
			case "Shrink":
				Element.show(this.nextSlide);
				setTimeout("Element.toggle('" + this.currentSlide + "')", 900);
				new Effect.Shrink(this.currentSlide, {duration:1, afterFinish: afterFinishCallback(-1)});
				break;																								
			case "None":
				Element.hide(this.currentSlide);
				Element.show(this.nextSlide);
				afterFinishCallback(1);
				break;
		}
			
		this.ResizeModuleContent();

	},
	
	ResizeModuleContent: function()
	{
		
		// MODULE RESIZE CONTENT
		// Mozilla
		if(navigator.userAgent.search("Firefox/") != -1 || navigator.userAgent.search("Opera/") != -1)
		{		
	
			var oldLeftPosition = $(this.currentSlide).style.left;	
			$(this.currentSlide).style.left = -10000;
				 
			if(this.transition == "SwapFade" || this.transition == "CrossFade" || this.transition ==  "BlindDown")
				Element.show(this.currentSlide);	
						
			$(this.currentSlide).style.width = -100;
			$(this.currentSlide).style.height = -100;
			

			if (parseInt($(this.currentSlide).offsetWidth) >= parseInt($("dvContent" + this.moduleID).parentNode.offsetWidth))
			{
				$("dvContent" + this.moduleID).style.width = parseInt($(this.currentSlide).offsetWidth) + "pt";
			}

				$("dvContent" + this.moduleID).style.height = (parseInt($(this.currentSlide).offsetHeight) + parseInt($(this.currentSlide).offsetTop)) + "pt";

			
			if(this.transition == "SwapFade" || this.transition == "CrossFade" || this.transition ==  "BlindDown")
				Element.hide(this.currentSlide);
		 		
			$(this.currentSlide).style.left = oldLeftPosition;		
			
		}
		else
		//Internet Explorer Browser and Others
		//if(navigator.userAgent.search("MSIE") != -1)
		{
			if(this.transition == "SwapFade" || this.transition == "CrossFade" || this.transition ==  "BlindDown")
				Element.show(this.currentSlide);	
				
			var oldWidth = $(this.currentSlide).style.width;
			$(this.currentSlide).style.width = 0;
			
			if (parseInt($(this.currentSlide).offsetWidth) >= parseInt($("dvContent" + this.moduleID).parentNode.offsetWidth))
			{
				$("dvContent" + this.moduleID).style.width = parseInt($(this.currentSlide).offsetWidth) + "px";
				$(this.currentSlide).style.width = parseInt($(this.currentSlide).offsetWidth) + "px";
			}
			else
			{
				$(this.currentSlide).style.width = oldWidth;
			}
			
			$("dvContent" + this.moduleID).style.height = parseInt($(this.currentSlide).offsetHeight) + "px";		 
		 
			if(this.transition == "SwapFade" || this.transition == "CrossFade" || this.transition ==  "BlindDown")
			Element.hide(this.currentSlide);
		}
	},

	
	tick: function(){	   
		if (this.paused) { this.registerCallback(); return; }
	   
		TECRRequest('GetSlide', this.moduleID, this.direction, (function(res){
			var target = $(this.nextSlide);

			try{
				var rmsli = res.value;
				target.innerHTML = parseSlideLayout(this.customSlideLayout, rmsli.SlideContent, this.moduleID);								
			} catch(ex){}
			
			if (target.innerHTML.length == 0) {
				target.innerHTML = "No content defined, or content not enabled!";
			}
			
			this.lastTransitionTime = (new Date()).getTime();
			this.showSlides();
		 }).bind(this));		
		 
		 if (this.restart > 0){
			this.restart = 0;
		 }
		 this.direction = 0;		
		 
	}

}

function parseSlideLayout(customSlideLayout, slideContent, moduleID){

	var tempContent;
	var pattern = "";	
	var content = tempContent = customSlideLayout;
	var hasNextTag = false;
	var hasPreviousTag = false;
	
	//<SlideContent /> tag
	content = content.replace(/<SlideContent\s*\/>/gi, slideContent);
	
	//<next ... /> tag
	pattern = /<next\s?(.+?)\s?\/>/gi;
	if (pattern.test(content)){
		var nextTag = content.match(pattern)[0];

			var mouseOver = "";
			var mouseOut = "";
			var text = "";
			var css = "";
			var style = "";
			
			//mouseOver attribute
			pattern = /\smouseOver\s?=\s?"\s?(.+?)\s?"/gi;		
			if (pattern.test(nextTag)){
				mouseOver = nextTag.match(pattern)[0] || '';		
				mouseOver = mouseOver.replace(pattern, "$1");
			}
			
			//mouseOut attribute
			pattern = /\smouseOut\s?=\s?"\s?(.+?)\s?"/gi;
			if (pattern.test(nextTag)){
				mouseOut = nextTag.match(pattern)[0] || '';		
				mouseOut = mouseOut.replace(pattern, "$1");
			}
			
			//text attribute
			pattern = /\stext\s?=\s?"\s?(.+?)\s?"/gi;	
			if (pattern.test(nextTag)){
				text = nextTag.match(pattern)[0] || '';		
				text = text.replace(pattern, "$1");
			}
			
			//css attribute
			pattern = /\scss\s?=\s?"\s?(.+?)\s?"/gi;
			if (pattern.test(nextTag)){
				css = nextTag.match(pattern)[0] || '';		
				css = css.replace(pattern, "$1");
			}
			
			//style attribute
			pattern = /\sstyle\s?=\s?"\s?(.+?)\s?"/gi;
			if (pattern.test(nextTag)){
				style = nextTag.match(pattern)[0] || '';		
				style = style.replace(pattern, "$1");
			}
		
		var nextLink = "<a href=\"javascript:changeSlide(" + moduleID + ", 1);\"";
		if (css.length > 0){
			nextLink += " class=\"" + css + "\"";
		}
		if (style.length > 0){
			nextLink += " style=\"" + style + "\"";
		}	
		nextLink += " >";
				
			if (mouseOut.length > 0){
				nextLink += "<img src=\"" + mouseOut + "\" border=\"0\" alt=\"Next slide\" title=\"Next slide\"";
				if (mouseOver.length > 0){
					nextLink += " onMouseOver=\"this.src='" + mouseOver + "'\" onMouseOut=\"this.src='" + mouseOut + "'\"";
				}
				nextLink += " />";
			} else {
				nextLink += text;
			}
			
		nextLink += "</a>";
		
		hasNextTag = true;
	}
	
	
	//<previous ... /> tag
	pattern = /<previous\s?(.+?)\s?\/>/gi;
	if (pattern.test(content)){
		var previousTag = content.match(pattern)[0];
			
			pattern = "";
			
			mouseOver = "";
			mouseOut = "";
			text = "";
			css = "";
			style = "";
			
			//mouseOver attribute
			pattern = /\smouseOver\s?=\s?"\s?(.+?)\s?"/gi;		
			if (pattern.test(previousTag)){
				mouseOver = previousTag.match(pattern)[0] || '';		
				mouseOver = mouseOver.replace(pattern, "$1");
			}
			
			//mouseOut attribute
			pattern = /\smouseOut\s?=\s?"\s?(.+?)\s?"/gi;
			if (pattern.test(previousTag)){
				mouseOut = previousTag.match(pattern)[0] || '';		
				mouseOut = mouseOut.replace(pattern, "$1");
			}
			
			//text attribute
			pattern = /\stext\s?=\s?"\s?(.+?)\s?"/gi;	
			if (pattern.test(previousTag)){
				text = previousTag.match(pattern)[0] || '';		
				text = text.replace(pattern, "$1");
			}
			
			//css attribute
			pattern = /\scss\s?=\s?"\s?(.+?)\s?"/gi;
			if (pattern.test(previousTag)){
				css = previousTag.match(pattern)[0] || '';		
				css = css.replace(pattern, "$1");
			}
			
			//style attribute
			pattern = /\sstyle\s?=\s?"\s?(.+?)\s?"/gi;
			if (pattern.test(previousTag)){
				style = previousTag.match(pattern)[0] || '';		
				style = style.replace(pattern, "$1");
			}
		
		var previousLink = "<a href=\"javascript:changeSlide(" + moduleID + ", -1);\"";
		if (css.length > 0){
			previousLink += " class=\"" + css + "\"";
		}
		if (style.length > 0){
			previousLink += " style=\"" + style + "\"";
		}	
		previousLink += " >";
				
			if (mouseOut.length > 0){
				previousLink += "<img src=\"" + mouseOut + "\" border=\"0\" alt=\"Previous slide\" title=\"Previous slide\"";
				if (mouseOver.length > 0){
					previousLink += " onMouseOver=\"this.src='" + mouseOver + "'\" onMouseOut=\"this.src='" + mouseOut + "'\"";
				}
				previousLink += " />";
			} else {
				previousLink += text;
			}
			
		previousLink += "</a>";
		
		hasPreviousTag = true;
	}
	
	//replace tags with actual links
	if (hasNextTag){
		content = content.replace(/<next\s?(.+?)\s?\/>/gi, nextLink);
	}
	if (hasPreviousTag){
		content = content.replace(/<previous\s?(.+?)\s?\/>/gi, previousLink);
	}
	
	return content;
}

function changeSlide(moduleID, direction){

	if(!sr[moduleID.toString()].lastTransitionTime ||
	 ((new Date()).getTime() - sr[moduleID.toString()].lastTransitionTime) > 1500) //miliseconds
	{
		clearTimeout(st[moduleID.toString()]);
			
		var oldDirection = sr[moduleID.toString()].direction;
		
		sr[moduleID.toString()].direction = direction;	
		var oldPaused = sr[moduleID.toString()].paused; 
		sr[moduleID.toString()].paused = false;
		sr[moduleID.toString()].tick();
		sr[moduleID.toString()].paused = oldPaused;
		
		//restore direction
		sr[moduleID.toString()].direction = oldDirection;
		sr[moduleID.toString()].lastTransitionTime = (new Date()).getTime();
	}	
}
