//  jsWin 1.2.0
//  by Nicolas Janik - http://www.k3media.com
//  2007-10-10
//
//  Licensed under the Creative Commons Attribution 3.0 License
//  http://creativecommons.org/licenses/by/3.0/
//

var jsWin = Class.create();

jsWin.objects = [];

jsWin.visibles = [];

jsWin.instance=function(ident){
	
	if(typeof(ident)!='undefined'){
		return jsWin.fromIdent(ident);	
	}
	
	if(typeof(this.jswin)=='undefined'){
		this.jswin=new jsWin();
	}
	return(this.jswin);
}

jsWin.autoClose=function(){
	if(jsWin.autoCloseWin && jsWin.autoCloseWin.isVisible()){
		jsWin.autoCloseWin.close();
	}
}

jsWin.AutoCloseCountdown=function(timer){
	var w=jsWin.autoCloseWin;
	if(typeof(timer)=='undefined'){
		timer=w._getOption('autoClose');
	}
	if(w._getOption('displayAutoCloseCountdown')==true){
		w.setFooter("Close in " + timer + " seconds");
	}
	if(timer - 1 > 0 && w.isVisible()){
		w.timeoutId=setTimeout("jsWin.AutoCloseCountdown("+ (timer - 1) +")",1000);
	}
},

jsWin.fromIdent = function(ident){
	jsw = null;
	jsWin.objects.each(function(jswin){
		if(jswin.ident==ident){
			jsw = jswin;	
		}	
	})
	
	return jsw;
	
},




jsWin.prototype = {

	ie6:false,
	element:null,
	_body:null,
	content:null,
	options:null,
	optionsDefault:null,
	closeType:'hide',
	autoCloseTimer:0,
	modalDiv:null,
	ie7:false,
	ident:null,
	
	initialize: function(options, ident) {
		
		if(typeof(ident)!='undefined'){
			this.ident = ident;
		}
		jsWin.objects.push(this);
		this.jswinId = jsWin.objects.length - 1;
		
		if(navigator.appVersion.indexOf("MSIE 6.0")>=0){ this.ie6=true; }
		if(navigator.appVersion.indexOf("MSIE 7.0")>=0){ this.ie7=true; }

		this.element=Builder.node('div',{style:'display:none;',className:'jsWinDefaultSkin'},[
			Builder.node('div',{className:'titleBar'},[
				Builder.node('div',{className:'title'},''),
				Builder.node('div',{className:'close', style:'cursor:hand;cursor:pointer;'},'close')
			]),
			Builder.node('div',{className:'content'},''),
			Builder.node('div',{className:'footerBar', align:'right'},'')
		]);
		
		
		
		this._body=$$('body')[0];
		this._body.appendChild(this.element);
		
		if(this.ie6){
			Element.setStyle(this.element,{position:'absolute'});
		}
		
		this.content = Element.down(this.element,('div.content'));
		

		this.element.down('div.close').onclick=function(e){ 
				this.close();
				//Event.stop(e);
				
		}.bind(this);	
		
		Event.observe(window,'resize',function(e){
			if(this.isVisible()){
				if(this._getOption('modal')){
					this.resizeModal();	
				}
				this.center();
			}
		}.bind(this));
		
		this.setOptions(options);
		new Draggable(this.element,{starteffect:null, endeffect:null, handle:'titleBar'});
	
	
	},
	
	
	contentDomElement: function(){
		return this.element.down('div.content');	
	},
	
	
	setOptions: function(options){
		
		this.options=$H(options);
		
		this.optionsDefault = $H({
			htmlId:null,
			modal:false,
			width:500,
			height:0,
			onShowCallback:null,
			onCloseCallback:null,
			autoClose:-1,
			displayAutocloseCountdown:true,
			clearContentOnClose:false,
			
			useDefaultEffects:false,
			
			effectOnShow:null,
			effectOnShowOptions:{},
			
			effectOnClose:null,
			effectOnCloseOptions:{},
			
			zIndex: 999,
			
			hideTagObjects:false,  //if true, hide all objects tags (flash) when jswin is visible
			viewportPadding:80,
			cssSkin:'jsWinDefaultSkin'
		});
		
		
		this.setWidth(this._getOption('width'));
		this.setHeight(this._getOption('height'));		
		this.element.className = this._getOption('cssSkin');
		
		if(this._getOption('useDefaultEffects') && this._getOption('effectOnShow')==null){
			this.addOptions({
				effectOnShow:'appear',
				effectOnShowOptions:{duration:0.4},
				effectOnClose:'fade',
				effectOnCloseOptions:{duration:0.4}
			});
		}
		
		if(this._getOption('htmlId')){
				this.element.id = this._getOption('htmlId');
		}
		
	},
	
	
	addOptions: function(options){
		this.setOptions(this.options.merge(options));
	},
	
	
	reset: function(){
		this.setTitle("");
		this.setContent("");
		this.setFooter("");
		this.setOptions({});
	},
	
	
	
	_getOption: function(name){
		if(typeof(this.options.get(name))!='undefined'){
			return(this.options.get(name));
		}
		return(this.optionsDefault.get(name));
	},
	
	
	center: function(){
		var elSize			= this.element.getDimensions();
		var viewportSize	= document.viewport.getDimensions();
		var scrollValue		= document.viewport.getScrollOffsets();
		
		var t = Math.round((viewportSize["height"] - elSize["height"])/2);
		if(this.ie6){
			t=t+scrollValue[1];
		}
		var l = Math.round((viewportSize["width"] - elSize["width"])/2) + scrollValue[0];
		if(t<0) t=0;
		Element.setStyle(this.element,{ top:t+"px", left:l+"px" });
		
	},	
	
	
	resizeModal: function(){
		var viewport = document.viewport.getDimensions();
		Element.setStyle(jsWin.modalDiv,{ width:viewport["width"]+"px", height:viewport["height"]+"px" });
		Element.show(jsWin.modalDiv);
	},
	
	
	
	_displaymodal: function(state){
		
		if(this.ie6){
			return false;	
		}
		
		var otherModal = false;
		
		if(state){
			
			if(jsWin.modalDiv==null){
				var zindex = this._getOption('zIndex')-1;
				if(this.ie6 || this.ie7){
					zindex>999 ? zindex=999:'';
				}
				jsWin.modalDiv = new Element("div",{style:'z-index:' + zindex, className: this._getOption('cssSkin') + '_modal'});
				this._body.appendChild(jsWin.modalDiv);
				this.resizeModal();
			}
			
			jsWin.modalDiv.show();
			
		}else if(jsWin.modalDiv){
			jsWin.modalDiv.hide();
		}
	},	
	
	
	setCloseType: function(type){
		this.closeType=type;
	},
	

	onClose: function(){
		return(true);
	},
	
	
	close: function(){
		
		if(this._getOption('clearContentOnClose')==true){
			this.contentDomElement().update("");
		}
		
		
		if(this._getOption('hideTagObjects')==true){
			$$("object").each(function(obj){Element.setStyle(obj,{visibility:"visible"})});
		}

		if(this.ie6){
			$$('select').each(function(el){Element.setStyle(el,{visibility:'visible'})});
		}	
		
		if(typeof(this._getOption('onCloseCallback'))=="function"){
			this._getOption('onCloseCallback')();	
		}
		
		
		if(this._getOption('autoClose')>-1 && this.timeoutId){
			window.clearTimeout(this.timeoutId);
		}
		
		
		if(this.closeType=='remove'){
				if(this.onClose()){ 
					if(this.options.get("modal") && jsWin.modalDiv){
						jsWin.modalDiv.remove();
						delete(jsWin.modalDiv);
					}
					this.remove(); 
					this.element=null;
				}
		}else{
				if(this.onClose()){ this.hide() }
		}		
	},
	
	

	show: function(){
		
		if(this.ie6){
			$$('select').each(function(el){Element.setStyle(el,{visibility:'hidden'})});
		}	
		
		this.addToVisibles();
		this.manageZindex();
		
		if(this._getOption('hideTagObjects')==true){
			$$("object").each(function(obj){Element.setStyle(obj,{visibility:"hidden"})});
		}
		
		if(typeof(this._getOption('onShowCallback'))=="function"){
			this._getOption('onShowCallback')();	
		}		
		
		
		if(this._getOption('modal')){
			this._displaymodal(true);
		}
		
		this.center();

	
		if(this._getOption('effectOnShow')!=null){
			var effect = eval("Element." + this._getOption('effectOnShow'));
			if(typeof(effect)=='function'){
				effect(this.element ,this._getOption('effectOnShowOptions'));
			}else{
				Element.show(this.element);	
			}
		}else{
			Element.show(this.element);	
		}		
		
		
		if(this._getOption('autoClose')>-1){
			jsWin.autoCloseWin=this;
			jsWin.AutoCloseCountdown();
			setTimeout('jsWin.autoClose()',this._getOption('autoClose')*1000);
		}
	},
	
	
	manageZindex: function(){
		Element.setStyle(this.element,{zIndex:this._getOption("zIndex")});
		jsWin.visibles.each(function(jswin,index){
			if(index == jsWin.visibles.length-1){
				var z=this._getOption("zIndex");
			}else{
				var z=this._getOption("zIndex")-(1+jsWin.visibles.length-index);
			}
			Element.setStyle(jswin.element,{zIndex:z});
		}.bind(this));	
	},
	

	
	isVisible: function(){
		y=this.element;
		return(this.element && Element.visible(this.element));
	},
	
	
	
	hide: function(){
		this.removeFromVisibles();
		this._displaymodal(false);
		
		if(this._getOption('effectOnClose')!=null){
			var effect = eval("Element." + this._getOption('effectOnClose'));
			if(typeof(effect)=='function'){
				effect(this.element ,this._getOption('effectOnCloseOptions'));
			}else{
				Element.hide(this.element);	
			}
		}else{
			Element.hide(this.element);	
		}
		
		if(jsWin.visibles.length){
			jsWin.visibles.last().show();	
		}
		
		
		
	},
	
	remove: function(){
		this.removeFromVisibles();
		this.element.remove();
		jsWin.objects.each(function(jswin){
			if(jswin==this){
				jswin = null;
			}
		}.bind(this));
		this.deleted = true;
		if(!this.ie6 && !this.ie7){
			delete(this)
		}
		if(jsWin.visibles.length){
			jsWin.visibles.last().show();	
		}		
		
	},
	
	setWidth: function(w){
		w = Math.min(document.viewport.getWidth()-this._getOption('viewportPadding'), w);
		Element.setStyle(this.element,{width:parseInt(w)+10+"px"});
	},
	
	setHeight: function(h){
		h = Math.min(document.viewport.getHeight()-this._getOption('viewportPadding'), h);
		if(parseInt(h)!= 0){
			Element.setStyle(this.content,{height:parseInt(h)+"px"});
		}else{
			Element.setStyle(this.content,{height:'auto'});
		}
	},
	
	setSize: function(w, h){
		this.setWidth(w);
		this.setHeight(h);
	},
	
	maximize: function(){
		var marginWidth=90;
		var marginHeight=200;
		var viewportSize	= document.viewport.getDimensions();
		this.setWidth(viewportSize['width']-marginWidth);
		this.setHeight(viewportSize['height']-marginHeight);
		this.center();
	},
	
	setTitle: function(txt){
		this.element.down('div.title').update(txt);
	},
	
	setContent: function(data){
		this.content.update(data);
	},

	setFooter: function(txt){
		this.element.down('div.footerBar').update(txt);
	},
	
	
	
	setContentFromUrl: function(url, title, options, doNotShow){
	
		var callBack = null;
		options = !options ? $H() : $H(options);
		if(typeof(options.get("callBack"))=="function"){
			var callBack = 	options.get("callBack");
			options.unset("callBack");
		}
	
		this.addOptions(options);
		this.setTitle(!title ? '' : title);
		this.setContent("Loading...");	
		if(!doNotShow){
			this.show();
		}
		var ar = new Ajax.Request(url,{evalScripts:true});		
			
		ar.options.onSuccess = function(origReq){
			this.setContent(origReq.responseText);
			//if(this._getOption('height')==0){
				this.center();
				if(callBack){
					callBack();	
				}
			//}
		}.bind(this);
		
	},
	
	addToVisibles: function(){
		this.removeFromVisibles();
		jsWin.visibles.push(this);
	},
	
	removeFromVisibles: function(){
		jsWin.visibles.each(function(jswin,index){
			if(jswin==this){ 
				delete(jsWin.visibles[index]);
				jsWin.visibles = jsWin.visibles.compact();
			 }
		}.bind(this));
	}
	
	
}

