/**
* Popup layer
* 
* @version 0.2
* @author goshi
* @package javascript::share
*
* Need portal, loader
*
* Changelog:
* 	0.2	27.03.10/goshi	using internal framework
* 
*/

function oPopupLayer(params){
	this._init(params);
	// you cant create more then one object
	oPopupLayer.Instances[0] = this;
}



oPopupLayer.prototype = {

	_layer : null,
	_layer_bg : null,
	_main_layer : null,
	_el_title : null,
	_el_content : null,
	_insert_callback : null,
	
	_loader : null,		// loader
	
	_alpha : 10, 	// alpha for background
	_correctPosAfter : true,	// automatic correct layer position after show
	_noinner : false,
	
	_doch : 0,
		
	_init: function(params){
		
		this.addParams(params);
		
	},
	
	addParams : function (params){
		
		if (typeof params != 'undefined' && params != null){
		
			for (var i in params){
				
				if (typeof this['_'+i] != 'undefined'){
					
					this['_'+i] = params[i];
				}			
			}		
		}	
	},
	
	
	create : function(e, data){
		
		var ev = window.event ? window.event : e;
		var cX = getClientWidth();//ev.clientX;
		var cY = getClientHeight();//ev.clientY;
		var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;

		// make lightbox
		//if (!lightbox_showed){}
		this._layer_bg = elem('div', {'class' : 'b-lightbox'});
		// for fucking IE add empty image
		var el_img = elem('img', {'src' : '/skin/img/share/0.gif'});
		this._layer_bg.appendChild(el_img);
		
		this._layer_bg.style.opacity = this._layer_bg.style["-khtml-opacity"] = this._layer_bg.style["-moz-opacity"] = this._alpha;
		if (/MSIE/.test(navigator.userAgent)) {
			this._layer_bg.style.filter = this._alpha === '' ? '' : "this._alpha(opacity=" + (this._alpha * 100) + ")";
			if (!this._layer_bg.currentStyle || !this._layer_bg.currentStyle.hasLayout)
				this._layer_bg.style.display = 'inline-block';
		}
		
		this._main_layer = elem('div', {'id' : 'b-lightbox-outer', 'class' : 'b-lightbox-outer'}, {'left' : 0+'px', 'top' : 0+'px'});
		var el_close = elem('div', {'class': 'close', 'id' : 'b-btn-close'}, false,  ' ');
		
		if (typeof this._noinner == 'undefined' || !this._noinner)
			this._layer = elem('div', {'class' : 'b-lightbox-inner'});
		else
			this._layer = elem('div', {'class' : 'b-lightbox-inner b-no-p'});
		
		
		var athis = this;
		portal.events.attach(this._layer_bg, "mousedown", function (){athis.destroy(athis);});
		portal.events.attach(el_close, "mousedown", function (){athis.destroy(athis);});
		
		if (!this._noinner){
			this._el_title = elem('h1', {'id' : 'l-ttl'}, {'display' : 'none'});
			this._el_content = elem('div', {'class' : 'content', 'id' : 'l-content'});
		
			this._layer.appendChild(this._el_title);
			this._layer.appendChild(this._el_content);
		} else if (typeof data != 'undefined' && data){
			this._layer.appendChild(data);
		}
		
		this._main_layer.appendChild(this._layer);
		this._main_layer.appendChild(el_close);
		
		this._layer_bg.style.width = el_img.style.width = document.body.scrollWidth + 'px';
		this._layer_bg.style.height = el_img.style.height = getDocumentHeight() + 'px';

		this._main_layer.style.left = (cX - this._main_layer.offsetWidth)/2 + 'px';
		this._main_layer.style.top = (scrollTop + (cY - this._main_layer.offsetHeight)/2 - 80) + 'px';
		
		document.body.appendChild(this._layer_bg);
		document.body.appendChild(this._main_layer);
		if (this._correctPosAfter)
			this._correctPos(this);
				
		// prepare layer
		this.clear(e);
	},
	
	insert : function (e, data, callback){
		
		var athis = this;
		if (typeof athis._noinner == 'undefined')
			athis = oPopupLayer.Instances[0];
		
		if (athis._loader) portal.loader.destroy(athis._loader);
		if (!athis._noinner){
			athis._el_title.innerHTML = data.title;
			athis._el_title.style.display = '';
			athis._el_content.innerHTML = data.content;
		}
		
		// reposition window
		if (athis._correctPosAfter)
			athis._correctPos(athis);
		
		// call events attacher
		if (typeof athis._insert_callback == 'function')
			athis._insert_callback();
	},
	
	_correctPos : function(self){
	
		if (typeof self == 'undefined' || !self)
			self = this;
		
		// saving old doc height
		if (self._doch == 0)
			self._doch = getDocumentHeight();

		self._main_layer.style.height = 'auto';
		if (!self._noinner) self._el_content.style.height = 'auto';
		
		var cY = getClientHeight();//ev.clientY;
		var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
		// if modal window has bigger dimensions
		if (self._main_layer.offsetHeight > (self._doch - 60)){
			self._main_layer.style.height = (self._doch - 60) + 'px';
			if (!self._noinner) self._el_content.style.height = (self._doch - 60 - 200) + 'px';
			self._main_layer.style.top = (145+20) + 'px';
			
			// scrolling to the window
			$.scrollTo('#b-btn-close', 'fast');
		} else if (self._main_layer.offsetHeight > (getClientHeight() - 20)){
			
			var new_y  = scrollTop + (cY - self._main_layer.offsetHeight/2)/2 - 10;
			if (new_y < 0)
				new_y = (self._main_layer.offsetHeight/2+30);
			self._main_layer.style.top = new_y + 'px';
			$.scrollTo('#b-btn-close', 'fast');
		} else {
			
			self._main_layer.style.top = (scrollTop + (cY - self._main_layer.offsetHeight/2)/2 - 10) + 'px';
		}	
	
	},
	
	clear: function(e){
		if (this._el_title != null){
			this._el_title.innerHTML = '';
			this._el_title.style.display = 'none';
		}
		// append loader to the content
		if (this._el_content != null)
			this._loader = portal.loader.create(this._el_content, {"mode" : "standart", 'loader_name' : 'loader.gif'});
		
			
	},
		
	destroy : function (owner){
		if (owner._layer_bg){
			owner._layer_bg.parentNode.removeChild(owner._layer_bg);
			owner._layer_bg = null;
			owner._main_layer.parentNode.removeChild(owner._main_layer);
			owner._main_layer = null;
				
			// remove hash 
			portal.hash.clear();
		}
	}
}

oPopupLayer.Instances = new Array();
