
var SlideShow = new Class({
	Implements : [Events, Options],
	Extends: Fx,
	options : {
		delay			: 1000,
		duration		: 2000,	
		onInitialize	: $empty,
		onChange		: $empty,
		onMouseOver		: $empty,
		onMouseOut		: $empty,
		onPress			: $empty,
		onStart			: $empty,
		onComplete		: $empty
		},
	initialize : function(element, options) {
		this.setOptions(options);
		this.container = element,
		this.children = this.container.getElements(this.container.getFirst().tagName);
		
		this.options.init = !options.onInitialize ? $empty : options.onInitialize.bind(this);
		this.options.start = !options.onStart ? $empty : options.onStart.bind(this);
		this.options.mouseover = !options.onMouseOver ? $empty : options.onMouseOver.bind(this);
		this.options.mouseout = !options.onMouseOut ? $empty : options.onMouseOut.bind(this);
		this.options.click = !options.onPress ? $empty : options.onPress.bind(this)
		
		
		this.children.addEvent("mouseover", function() {this.options.mouseover.run();}.bindWithEvent(this));
		this.children.addEvent("mouseout", 	function() {this.options.mouseout.run();}.bindWithEvent(this));	
		this.children.addEvent("click", 	function() {this.options.click.run();}.bindWithEvent(this));
		
		this.intervalID = null;
		this.currentIndex = 0;
		this.children.setStyles({
			"opacity": 0,
			"display":"none"
			});
		this.children[0].setStyles({
			"opacity": 1,
			"display":"block"
			});
		
		this.count = this.children.length;
		this.position = this.currentIndex+1;
		
		this.intransition = false;
		
		this.init();
		
		},
	
	init : function () {
		
		this.options.init.run();
		},
	start : function () {
		this.fireEvent("start");
		},
	mouseover : function () {
		this.options.mouseover.run()
		},
		
	click : function () {
		this.options.click.run()
		},
	complete : function () {
		this.fireEvent("complete");
		},
	play : function () {
		if(this.count > 1) {
			this.intervalID = this.next.periodical(this.options.delay, this)
			}
		},
	
	stop : function () {
		$clear(this.intervalID);
		},
	
	next : function () {
		if (this.intransition) return
		if (this.fxIn) {this.fxIn.cancel();}
		if (this.fxOut) {this.fxOut.cancel();}
		this.fadeOut();
		this.fadeIn();
		this.fireEvent("change");
		},
	goTo : function () {
		
		if (arguments[0] == this.position-1 || this.intransition) return
		if (this.fxIn) {this.fxIn.cancel();}
		if (this.fxOut) {this.fxOut.cancel();}
		this.stop();
		this.fadeOut();
		this.currentIndex = arguments[0] == 0 ? this.children.length-1 : arguments[0]-1;
		this.position = this.currentIndex + 1;
		this.fadeIn()
		this.play();
		},
	prev : function () {
		//alert(this.position)
		this.goTo(this.position-1 == 0 ? this.children.length-1 : this.position-2);
		},
	fadeIn : function () {
		//alert(this.currentIndex)
		this.currentIndex = this.currentIndex+1 == this.children.length ? 0 : ++this.currentIndex;
		this.position = this.currentIndex + 1;
		this.fxIn = new Fx.Morph(this.children[this.currentIndex], {
			duration : this.options.duration, 
			wait : false,
			onStart : function () {
				this.intransition = true;
				this.children.setStyle("z-index",0);
				this.children[this.currentIndex].setStyles({
					"z-index": 9999,
					"display":"block"
					});
				this.fireEvent("start");
				}.bind(this),
			onComplete : function () {
				this.intransition = false;
				this.fireEvent("complete")
				}.bind(this)
			}).start({"opacity": 1});
		},
			
	fadeOut : function () {
		this.fxOut = new Fx.Morph(this.children[this.currentIndex], {duration : this.options.duration, wait : false }).start({"opacity": 0});
		},
		
	getCount : function () {
		return this.count;
		},
	getPosition : function () {
		return this.position;
		},
	getItem : function () {
		return this.children[this.currentIndex];
		}
	});
	

var Mask = new Class({
	Implements : [Events, Options],
	Extends : Fx,
	options : {
		elementClass	: "",
		title 			: "",
		type 			: "",
		size			: "",
		url				: "",
		height			: null,
		buttons			: [],
		onInitialize	: $empty,
		onBeforeCreate	: $empty,
		onAfterCreate	: $empty,
		onBeforeDispose	: $empty,
		onAfterDispose	: $empty,
		onComplete		: $empty
		},
	initialize : function (options) {
		this.setOptions(options);
		this.options.init = !options.onInitialize ? $empty : options.onInitialize.bind(this);
		this.options.beforeCreate = !options.onBeforeCreate ? $empty : options.onBeforeCreate.bind(this);
		this.options.afterCreate = !options.onAfterCreate ? $empty : options.onAfterCreate.bind(this);
		this.options.beforeDispose = !options.onBeforeDispose ? $empty : options.onBeforeDispose.bind(this);
		this.options.afterDispose = !options.onAfterDispose ? $empty : options.onAfterDispose.bind(this);
		if(options.height != null) {
			this.options.height = options.height;
			}
		this.maskElement;
		this.titleElement;
		this.bodyElement;
		this.messageElement
		this.footerElement;
		this.containerHeight;
		this.resizeFx;
		this.resizeInt;
		this.urlRequest;
		this.init();
		
		window.addEvent("resize", function () {
			$clear(this.resizeInt);
			if($("site-mask")) {
				$("cover").setStyle("height" , window.getSize().y)
				$("site-mask").setStyle("height" , window.getSize().y)
				}
			  this.resizeInt = (function(){
					if ($("site-mask")) {
						this.resize();
						}
				  }).delay(400, this);
			
			}.bind(this));
		
		},
	init : function () {
		this.options.init.run()
		},
	create : function () {
		var blocks = {};
		this.options.beforeCreate.run();
		$(document.body).setStyle("overflow","hidden");
		$(document.body).addClass("moo-mask");
		blocks.mask 		= new Element("div", {"id":"site-mask", "class":this.options.elementClass});
		blocks.cover 		= new Element("div", {"id":"cover", "title" : "Click here to close this window"}).set("html", "&nbsp;").setOpacity(0.4).addEvent("click", function () {this.dispose();}.bind(this));
		blocks.container 	= new Element("div", {"id":"container"});
		blocks.title 		= new Element("h2").set("text", this.options.title);
		blocks.body 		= new Element("div", {"id":"mask-body"}).setStyles({"height":"25px"});
		blocks.message 		= new Element("div", {"id":"mask-message"}).set("html", "<p class=\"loading-indicator\">&nbsp;</p>");
		blocks.footer 		= new Element("div", {"id":"mask-footer"}).set("html", "<p>&nbsp;</p>");
		
		if (this.options.type != "") {
			blocks.mask.addClass(this.options.type);
			}
		
		this.maskElement 	= blocks.mask;
		this.titleElement	= blocks.title;
		this.bodyElement 	= blocks.body;
		this.messageElement = blocks.message;
		this.footerElement	= blocks.footer;
		
		if(this.options.size != "") {this.maskElement.addClass(this.options.size);}
		this.maskElement.setStyle("height", window.getScrollSize().y + "px");
		
		
		
		for(var b = 0; b < this.options.buttons.length; ++b) {
			if (!this.options.buttons[b].elementClass) {
				this.options.buttons[b].elementClass = "";
				}
			new Element("input", {"type":"button", "value": this.options.buttons[b].label, "id": this.options.buttons[b].id, "class":this.options.buttons[b].elementClass}).addEvent("click", function (ev, button) {
				if (button.label.toLowerCase() == "close" && !button.onClick) {
					this.dispose();
					}
				else if (button.label.toLowerCase() == "print" && !button.onClick) {
					print();
					}
				else {
					button.onClick.run(ev, this)
					}
				
				}.bindWithEvent(this, this.options.buttons[b])).inject(this.footerElement.getElement("p"));
			
			}
		this.maskElement.setStyle("top", window.getScroll().y + "px");
		
		blocks.message.inject(blocks.body);
		blocks.title.inject(blocks.container);
		blocks.message.inject(blocks.body);
		blocks.body.inject(blocks.container);
		blocks.footer.inject(blocks.container);
		blocks.cover.inject(this.maskElement);
		
		blocks.container.inject(this.maskElement);
		
		
		this.maskElement.injectInside(document.body);
		
		if (this.options.url != "") {
			this.loadURL(this.options.url)
			}
		this.options.afterCreate.run();
		return this;
		},
	dispose : function () {
		$(document.body).removeClass("moo-mask");
		if(this.urlRequest) {
			this.urlRequest.cancel()
			}
		if (this.maskElement) {
			this.options.beforeDispose.run();
			var fx = new Fx.Morph(this.maskElement, {
				duration: 400, 
				wait: true,
				onComplete : function () {
					this.maskElement.dispose();
					$(document.body).setStyle("overflow","auto");
					this.options.afterCreate.run();
					}.bind(this)
				}).start({"opacity":0});
			}
		},
	setHTML : function (html) {
		this.getBodyElement().set("html", html);
		this.resize();
		this.fireEvent("complete");
		return this;
		},
	getTitleElement : function() {
		return this.titleElement;
		},
	getBodyElement : function() {
		return this.messageElement;
		},
	getFooterElement : function() {
		return this.footerElement;
		},
	putMessage : function (html, type) {
		if (!type) type = "info";
		if(!$("mask-form-response")) {
			new Element("div", {"id":"mask-form-response", "class":type}).set("html", "<div id=\"response-body\">" + html + "</div>").inject(this.getBodyElement(), "top");
			}
		else {
			$("mask-form-response").set("class", type);
			$("response-body").set("html", html);
			}
		},
	setContainerHeight : function () {
		this.getBodyElement().setStyle("height", "auto");
		if (this.options.height) {
			this.containerHeight = this.options.height;
			}
		else {
			this.containerHeight = this.getBodyElement().getSize().y;
			}
		},
	resize : function () {
		var _fromTop = this.getBodyElement().getCoordinates().top;
		var _scrollTop = window.getScroll().y;
		var _winHeight = window.getSize().y
		var _containerHeight = this.containerHeight;
		var _availableHeight = _winHeight - (_fromTop - _scrollTop) - this.footerElement.getSize().y;
		var _targetHeight = Math.min(_containerHeight, _availableHeight);
		//console.debug(_winHeight +  "-  ( "+ _scrollTop + "-"+ _fromTop + ") -" + this.footerElement.getSize().y)
		//console.debug(_winHeight + " : " + _availableHeight)
		if ((_targetHeight != this.bodyElement.getSize().y || this.getBodyElement().getStyle("visibility") == "hidden")) {
			if (this.getBodyElement().getStyle("visibility") == "hidden") this.getBodyElement().setStyle("display","none");
			this.getBodyElement().setStyles({"overflow":"hidden"});
			this.resizeFx = new Fx.Morph(this.bodyElement, {
											wait:false, 
											duration:500,
											onComplete : function () {
												if (this.getBodyElement().getStyle("display") == "none") {
													this.getBodyElement().setStyles({
														"display":"block",
														"opacity":0,
														"visibility":"visible"
														});
													var _fx = new Fx.Morph(this.getBodyElement(), {
														wait:false, 
														duration:500,
														
														
														}).start({opacity:[0,1]});
													}
												//if (_targetHeight < _containerHeight) {
													this.getBodyElement().setStyle("height", _targetHeight);
													this.getBodyElement().setStyles({"overflow":"auto"});
												//	}
												//this.resize.delay(3000, this);
												
												//this.resizeInt = this.resize.periodical(1000, this);
												}.bind(this)
											}).start({height:_targetHeight});
				
			
			}
		//alert(this.getBodyElement().getSize().y)
		},
	loadURL : function () {
		
		this.urlRequest = new Request.HTML({
					url: this.options.url,
					update : this.getBodyElement(),
					evalResponse  : true,
					onComplete : function (responseText) {
						//this.setHTML(responseText.toString())
						
						
						//this.resizeInt = this.resize.periodical(1000, this);
						this.fireEvent("complete");
						}.bind(this),
					onSuccess : function (responseText) {
						this.bodyElement.setStyles({"overflow":"hidden"});
						this.getBodyElement().setStyle("visibility","hidden");
						this.setContainerHeight();
						this.resize();
						
						//this.setHTML(responseText.toString())
						}.bind(this),
					onFailure : function (result) {	
					//for(p in result) alert(p)
						//alert(this.url);
						var _tmp = new Element("div").set("html", result.responseText).setStyle("display","none");
						//alert(_tmp.getElement("h2"));
						this.setHTML(_tmp.get("html"));
						this.maskElement.addClass("error");
						var h = this.getBodyElement().getElement("td[id=tableProps2]").get("text");
						var d = this.getBodyElement().getElement("td[id=tablePropsWidth]").get("text");
						_tmp.dispose();
						this.getBodyElement().setStyle("visibility","hidden");
						this.setHTML("<h3>Exception Occured</h3><p><strong>" + h + "</strong></p><p>" + d + "</p><p>URL : " + this.options.url + "</p>");
						this.setContainerHeight();
						this.resize();

						//this.resize();
						}.bind(this)
					}).send();
		}
	});

	
