/* JavaScript document */

/**
 * 
 */




Object.extend(Element, {
		 getWidth: function(element) {
		 element = $(element);
		 return element.offsetWidth;
		 },
		 setWidth: function(element,w) {
		 element = $(element);
		 element.style.width = w +"px";
		 },
		 setHeight: function(element,h) {
		 element = $(element);
		 element.style.height = h +"px";
		 },
		 setTop: function(element,t) {
		 element = $(element);
		 element.style.top = t +"px";
		 },
		 setLeft: function(element,l) {
		 element = $(element);
		 element.style.left = l +"px";
		 },
		 setSrc: function(element,src) {
		 element = $(element);
		 element.src = src;
		 },
		 setHref: function(element,href) {
		 element = $(element);
		 element.href = href;
		 },
		 setInnerHTML: function(element,content) {
		 element = $(element);
		 element.innerHTML = content;
		 }
});

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
Position.getPageSize = getPageSize;


var DA = Object(); 

var hPrompts = new Array();
hPrompts.topzIndex=50;
hPrompts.defaultOverlayOpacity=0.5;
DA.Prompt = Class.create();
DA.Prompt.prototype = {
	
	initialize : function (promptText, options){
		if (typeof promptText == 'undefined'){	
			alert("Prompt needs at least one parameter - prompt text!");
			return false;
		}
		this.promptText=promptText;
		this.renderedOnce=false;
		this.isOpen = false;
		//this.title=(title) ? title : '';
		this.form = document.createElement('form');
		this.form.setAttribute('method','post');
		this.form.setAttribute('action',window.location);
		this.form.onsubmit=function(){ return false; }
		this.container = document.createElement('div');
		this.container.style.zIndex='20';
		this.titleContainer = document.createElement('div');
		this.ctrlContainer = document.createElement('div');
		this.ctrlContainer.align="center";
		
		this.form.Prompt=this;
		
		
		this.form.triggerHandler = this.triggerHandler.bind(this);
		this.options = {
			body:'<br /><input type="text" name="userInput" value="" />',
			container: { className:'promptContainer' },
			ctrlContainer: { className : 'promptCtrlContainer' },
			titleContainer: { className : 'promptTitleContainer'},
			title : document.title,
			cover : 1,
			onConfirm: function(form,obj){ 
							obj.result = true;
							obj._hide(); 
						},
			onCancel: function(form,obj){ 
							obj.result = false; 
							obj._hide();
						},
			on_Create: function(form,obj){
						
			},
			onBeforeAsk: function(form, obj){
				
			},
			onAfterAsk: function(form, obj){
				//$(form).enable();
				Form.focusFirstElement(form);
				if (typeof Draggable!='undefined'){
					new Draggable(obj.container,{ 
						handle: obj.titleContainer
					});
				}
			},
			onAfterActivate: function(form){
				//$(form).enable();
			},
			onAfterDeactivate: function(form){
				//$(form).disable();
			}
		}
		Object.extend(this.options, options || { simple: true});

		 
		 var Ctrls={};
		 if (typeof this.options.controls == 'object') {
			if (typeof this.options.controls.defaults == 'boolean' && this.options.controls.defaults==false){
				/// NO DEFAULT CONTROLS
				Ctrls = this.options.controls
			}
			else {
				// merge with default controls
				Ctrls.Confirm = "OK";
				Object.extend(Ctrls, this.options.controls);
				Ctrls.Cancel = "Cancel";
				Object.extend(Ctrls, this.options.controls);
			}
		 }
		 else {
			 Ctrls = {"Confirm":"OK", "Cancel":"Cancel"};
		 }
		this.options.controls = Ctrls;
		this.build();
		this.triggerHandler('_Create');
	},
	ask : function (){
		this.triggerHandler('BeforeAsk');
		this.isOpen = true;
		if (this._asked){
					document.body.appendChild(this.container);
					this.triggerHandler('_afterAsk');
					this._activate();
					return this;
		}	
		
		document.body.appendChild(this.container);
		$(this.titleContainer).down('.closeButton').observe('click',this._hide.bind(this));
		$(this.titleContainer).observe('mousedown',this._activate.bind(this));
		$(this.titleContainer).down('.resetButton').observe('click',this._reset.bind(this));
		$(this.titleContainer).down('.foldButton').observe('click',this._fold.bind(this,$(this.titleContainer).down('.foldButton')));
		this._asked=true;
		this.triggerHandler('AfterAsk');
		this._activate();
		return this;
	},
	build: function(){
				this.container.innerHTML='';
				hPrompts[hPrompts.length]=this;
		Object.extend(this.container, this.options.container || {});
		Object.extend(this.titleContainer, this.options.titleContainer || {});
		Object.extend(this.ctrlContainer, this.options.ctrlContainer || {});
		
		this.titleContainer.innerHTML =
		'<span class="closeButton" style="float:right; background:#AAA; color:#000; padding:0px 3px; font-size:9px; border: outset 1px #AAA" title="Zapri">close</span>'+
		'<span class="resetButton" style="float:right; background:#AAA; color:#000; padding:0px 3px; font-size:9px; border: outset 1px #AAA" title="Reset">reset</span>'+ 
		'<span class="foldButton" style="float:right; background:#AAA; color:#000; padding:0px 3px; font-size:9px; border: outset 1px #AAA" title="Fold">fold</span>'+ 
		this.options.title ;
			
		this.ctrlContainer.innerHTML = '';
		for (var action in this.options.controls){
			if (action != 'defaults'){ 
			var label = this.options.controls[action];
			var button = document.createElement('input');
			button.setAttribute('type','submit');
			button.setAttribute('name',action);
			button.setAttribute('value',label);
			Event.observe(button, 'click', this._submit.bindAsEventListener(this,button.name));
			this.ctrlContainer.appendChild(button);
			}
		}
		this.container.appendChild(this.titleContainer);
		Event.observe(this.titleContainer, 'click', this._activate.bindAsEventListener(this));
		
		this.form.innerHTML='<div class="promptContainerBody">' + this.promptText + this.options.body + '</div>';
		this.form.appendChild(this.ctrlContainer);
		Event.observe($(this.form).childElements()[0], 'click', this._activate.bindAsEventListener(this));
		this.container.appendChild(this.form);
		
		this.container.style.left=hPrompts.length*40 + 'px';
		this.container.style.top=hPrompts.length*20 + 'px';
	},
	_submit: function(event, action){
		this.lastCommand = action;
		this.triggerHandler(action);
		this.triggerHandler('AfterSubmit');
	},
	_hide : function(){
		if (this.isOpen==false) { return false; }
		this.isOpen = false;
		document.body.removeChild(this.container);
		var dontClear = false;
		hPrompts.each(function(obj){
			if (obj.isOpen && obj.options.cover>0){
				dontClear = true;
				obj._activate();
				throw $break;
			}
		});
		
		if (dontClear == false) { Overlay.clear(); }
		
		this.triggerHandler('_Finish');
	},
	_reset: function(){
		if(this.triggerHandler('Reset'));
		this.form.reset();
		this._asked=false;
	},
	_fold: function(button){
		this.container.down('.promptContainerBody').toggle();
		this.ctrlContainer.toggle();
		if (button.innerHTML=='fold'){
			button.innerHTML='unfold';
		}
		else {
			button.innerHTML='fold';
		}
	},
	_destroy : function(){
		this.ask = function(){}
		if (this.isOpen){
		this._hide();
		}
		delete this.container;
		this.container=document.createElement('div');
		this._hide = function(){}
	},
	_activate : function(e){
		//console.log(this.lastCommand);
		if (!this.isOpen) { this.ask(); }
		this.triggerHandler('beforeActivate');
		hPrompts.each(function(obj){ obj._deactivate(); });
		this.container.style.zIndex = hPrompts.topzIndex+2;
		if (this.options.cover==1){
			Overlay.set(hPrompts.defaultOverlayOpacity,hPrompts.topzIndex-1);
		}
		if (this.options.cover>1){
			Overlay.set(hPrompts.defaultOverlayOpacity,hPrompts.topzIndex+1);
		}
		this.titleContainer.removeClassName('passive');
		this.triggerHandler('AfterActivate');
	},
	_deactivate : function(){
		this.triggerHandler('BeforeDeactivate');
		this.titleContainer.addClassName('passive');
		this.container.style.zIndex=hPrompts.topzIndex;
		this.triggerHandler('AfterDeactivate');
	},
	triggerHandler: function(event){
		if (!typeof event == 'string') { return false; }
		if(this.options['on' + event]){
		try {
			result = (this.options['on' + event])(this.form,this);
		}
		catch(e){
			//throw e;
			return false;
		}
		}
		return result;
	}
};


DA.Overlay = Class.create();
//MF.Overlay.prototype = document.createElement('div');
Object.extend( DA.Overlay.prototype, {
	initialize: function(overElement){
		
		this.screen = document.createElement('DIV');
		
		if (typeof overElement == 'undefined' || !overElement){
		this.objBody = $(document.getElementsByTagName('body')[0]);
			 dims = getPageSize();
		 	Element.setWidth(this.screen,dims[0]);
			Element.setHeight(this.screen,dims[1]);
		}
		else { this.objBody =$(overElement); 
			Element.setWidth(this.screen,Element.getWidth(this.objBody));
			Element.setHeight(this.screen,Element.getHeight(this.objBody));
		}

		
		
		this.screen.style.display='none';
		this.screen.className = 'Overlay';
		this.objBody.appendChild(this.screen);
		this.screen.isSet=false;
	},
	
	set: function(opacity, zIndex){
		if(zIndex) { this.screen.style.zIndex=zIndex; }
		if (this.isSet) { return true; }
		this.opacity = opacity;
			if (this.objBody.tagName=='BODY'){
				odims = Position.getPageSize();
				dims=new Array (odims[0],odims[1]);
			}
			else {
				dims = new Array($(this.objBody).getWidth(),this.objBody.getHeight());
			}
				//console.log(objBody);
				
				Element.setWidth(this.screen,dims[0]);
				//console.log(Element.getWidth(objBody));
				Element.setHeight(this.screen, dims[1]);
		if (opacity){
			this.screen.setOpacity(0);
		}else {
			this.screen.setOpacity(1);
		}
		this.screen.show();
		
		if (opacity && typeof Effect!= 'undefined'){
			new Effect.Opacity(this.screen, {duration: 0.6, from:0, to:opacity});
		}
		else {
			this.screen.setOpacity(1);
		}
		this.isSet = true;
	},
	
	clear: function(){
		if(!this.isSet) return false;
		this.isSet=false;
		if (this.opacity && typeof Effect!= 'undefined'){
			new Effect.Opacity(this.screen, { from:this.opacity, to:0, afterFinish: function(obj){ 
				obj.element.hide();
				}
			});
		}
		else {
			this.screen.hide();
		}
		
	}

	
});

Event.observe(window, 'load', function(){ window.Overlay = new DA.Overlay(); });


