// -----------------------------------------------------------------------------------
//
//	Lightdialog v1.0
// -----------------------------------------------------------------------------------

var LightDialog = Class.create(Eventer,{
	initialize: function($super,id,conf){
		$super();
		this.opt = { overlayOpacity: 0.3,width:250,height:250,marginTop:150,overlayColor:'#000',
					 border:10,dialogBackground:'#e9e9e9',contentBackground:'#fff',overlayDuration:0.5};
		this.id = id;
		Object.extend(this.opt,conf ||{});
		var objBody = $$('body')[0];
		objBody.appendChild(Builder.node('div',{id:this.id+'overlay'}));
		$(this.id+'overlay').hide();
		// create dialog
        objBody.appendChild(Builder.node('div',{id:this.id+'container',align:'center'}, [
			Builder.node('div',{id:this.id+'dialog',align:'left'},[
				Builder.node('div',{id:this.id+'viewport'},[
					Builder.node('div',{id:this.id+'content'})
				]),
				Builder.node('div',{id:this.id+'navigation'},[
					Builder.node('a',{href:'#',id:this.id+'closeBtn'},[
						Builder.node('img',{id:this.id+'closeBtnImg'})])
				])
			])
		]));
		$(this.id+'container').hide();
		this.items = {};
		var th = this;
		$A(['overlay','viewport','content','navigation','closeBtn','container','closeBtnImg','dialog']).each(
			function(e){th.items[e] = $(this.id+e)},this);
		this.items.overlay.setStyle({zIndex:100,backgroundColor:this.opt.overlayColor,position:'absolute',top:'0px',left:'0px'});
		// set default styles
		this.items.container.setStyle({position:'absolute',top:'0px',left:'0px',
			width:'100%',paddingTop:this.opt.marginTop+'px',zIndex:101});
		this.items.dialog.setStyle({width:this.opt.width+'px',height:this.opt.height+'px',
				backgroundColor:this.opt.dialogBackground,paddingTop:this.opt.border+'px'});
		this.items.viewport.setStyle({width:(this.opt.width-2*this.opt.border)+'px',
				height:(this.opt.height-35)+'px',
				marginLeft:this.opt.border+'px',
				marginRight:this.opt.border+'px'});
		this.items.content.setStyle({width:'100%',height:'100%',
				backgroundColor:this.opt.contentBackground,overflow:'auto'});
		this.items.navigation.setStyle({width:this.opt.width+'px',height:'35px'});
		this.items.closeBtnImg.src='/s/i/lightbox/closelabel.gif';
		this.items.closeBtnImg.setStyle({cssFloat:'right',marginRight:'20px',marginTop:'5px'});
		this.items.closeBtn.onclick=this.hide.bindAsEventListener(this);
		this.items.overlay.onclick=this.hide.bindAsEventListener(this);
	},
	show: function (){
        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });

        // stretch overlay to fill page and fade in
        var ps = this._pageSize();
        this.items.overlay.setStyle({ width: ps[0] + 'px', height: ps[1] + 'px' });
        new Effect.Appear(this.items.overlay, { duration: this.opt.overlayDuration, from: 0.0, to: this.opt.overlayOpacity });
		//this.items.overlay.setOpacity(this.opt.overlayOpacity).show();
       	this.items.container.show();
       	return false; 
	},
	hide: function(){
       	this.items.container.hide();
        new Effect.Fade(this.items.overlay, { duration: this.opt.overlayDuration });
        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
		return false;
	},
	content: function(){return this.items.content;},
	error: function(msg){
		this.clear();
		this.items.content.appendChild(Builder.node('div',{'class':'err'}));
		this.items.content.down().setStyle(
			{width:(this.items.content.getWidth() - 50)+'px',
			 height:(this.items.content.getHeight() - 15)+'px',
			 margin:0,padding:'15px 0 0 50px',border:'none'}).innerHTML = msg;
	},
	message: function(msg){
		this.clear();
		this.items.content.appendChild(Builder.node('div',{'class':'info'}));
		this.items.content.down().setStyle(
			{width:(this.items.content.getWidth() - 50)+'px',
             height:(this.items.content.getHeight() - 15)+'px',
			 margin:0,padding:'15px 0 0 50px',border:'none'}).innerHTML = msg;
	},
	wait:function(){
		this.clear();	 
		this.items.content.appendChild(Builder.node("div",{align:'center'}));
		this.items.content.down().setStyle({paddingTop:'40%'}).innerHTML = "<img src='/s/i/lightbox/loading.gif'>";
	},			 
	clear: function(){
		this.items.content.innerHTML = '';	   
	},
	_pageSize: function() {
	        
	     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;
		
		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;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}
});
