/*
<style>
	#overlay {
		position:absolute; 
		top:0; 
		left:0; 
		z-index:300; 
		width: 100%;
		background-color:#000;			
	}
</style>
*/
// -----------------------------------------------------------------------------------

//
//  Configuration
//
OverlayOptions = Object.extend({
     'opacity'	: 0.6   // controls transparency of shadow overlay    
	,'color'	: '#000'
	,'zIndex'	: 300	
}, window.OverlayOptions || {});

// -----------------------------------------------------------------------------------

var Overlay = Class.create();

Overlay.prototype = {

    showCallback		: '', //funkcja wywoywana po wywietleniu warstwy
    closeCallback		: '', //funkcja wywoywana po ukryciu warstwy
    visibleContainer	: '', //id elementu ktory ma znajdowac sie nad warstwa
	
	containers			: [],	
    
    //
    // initialize()
    // Constructor
    //
    initialize: function() {
		var self=this;            
		this.updateTagList();
		            
        // Code inserts html at the bottom of the page :
        //
        //  <div id="overlay"></div>        
		if(!$('overlay'))
		{
			var pageContainer= $('pageContainer') ?  $('pageContainer') : $$('body')[0];
				
			pageContainer.appendChild(Builder.node('div',{id:'overlay'}));
	        //var objBody = $$('body')[0];		
			//objBody.appendChild(Builder.node('div',{id:'overlay'}));
	    }    
		$('overlay').hide().observe('click', (function() { this.hide(); }).bind(this));
		
		/*$$('.overlayClose').each(function(elem, index) {
		  	elem.observe('click', function() { self.hide(); });
		});*/
		Event.observe(document, 'keydown',function(e){
			var key = (typeof e.which != "undefined") ? e.which : e.keyCode;			
			if(key==27 && $('overlay').visible())
			{
				self.hide();	
			}		
		});
		
		$('overlay').hide();
		$('overlay').setStyle({
			 'position'			: 'absolute' 
			,'top'				: '0'
			,'left'				: '0' 
			,'width'			: '100%'
			,'height'			: '100%'
			,'border'			: 'none'
			,'margin'			: '0'
			,'padding'			: '0'
			,'zIndex'			: OverlayOptions.zIndex 			
			,'backgroundColor'	: OverlayOptions.color	
			,'opacity'			: OverlayOptions.opacity	
		});
		//$('overlay').setStyle({ opacity: OverlayOptions.opacity });		
		//$('overlay').setOpacity(OverlayOptions.opacity);
    },
      
	addContainer: function(name,params)
	{
		var self=this;
		if($(name))
		{
			$(name).down('dt').observe('click',function(){
				self.hide();
			});
			
			/*$(name).select('overlayClose').each(function(elem){
				self.hide();
			});*/
			
			this.containers[name]= params;
			
			/*if(typeof this.containers[name].onShow == 'function')
			{
				this.containers[name].onShow();
			}*/
			
			
			if($(params.trigger))
			{
				$(params.trigger).observe('click',function(event){
					self.show(name);					
					this.blur();
					event.stop();
					return false;
				});
				
				$(params.trigger).show();
			}
		}
		//this.containers[name]=[];
		
		//this.containers[name]['name']=name;			
	},      
    //
    //  show()
    //  Display overlay
    //
    show: function(visibleContainer) { 
		   
    	var self=this;
    	var obj={};
		
    	
    	
    	if($(visibleContainer))
    	{
			this.visibleContainer=visibleContainer;
			$(visibleContainer).show();
		}
    	
		if(this.visibleContainer)
		{    	
			$$('select', 'object', 'embed').each(function(node){
				
				if(!node.descendantOf(self.visibleContainer))
				{ 
					node.style.visibility = 'hidden';
				}
			});
		}
		else
		{
			$$('select', 'object', 'embed').each(function(node){
				
				node.style.visibility = 'hidden';
			});	
		}

        // stretch overlay to fill page and fade in
        var arrayPageSize = this.getPageSize();
        //$('overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
        $('overlay').setStyle({ height: arrayPageSize[1] + 'px' });
		$('overlay').show();
		
		if(eval('typeof ' + visibleContainer) != 'undefined')
		{
			eval('obj = '+visibleContainer);
			//alert(typeof obj.onShow);		
			if(typeof obj.onShow == 'function')
			{
				obj.onShow();
			}
		}
		this.onShow(visibleContainer);

    },
    
    onShow : function(name) {
    	    			
		if(typeof this.showCallback == 'function')
		{
			this.showCallback();
		}
		
		if(typeof this.containers[name] != 'undefined')
		{
			if(typeof this.containers[name].onShow == 'function')
			{
				this.containers[name].onShow();
			}
		}
		
	},


    //
    //  hide()
    //
    hide: function() {
		if($(this.visibleContainer))
		{
			$(this.visibleContainer).hide();	
		}
		$('overlay').hide();
        //new Effect.Fade(this.overlay, { duration: this.overlayDuration });
        $$('select', 'object', 'embed').each(function(node){ 
			node.style.visibility = 'visible';
		});
        
        this.onHide(this.visibleContainer);
    },
    
    onHide : function(name) {
		if(typeof this.hideCallback == 'function')
		{
			this.hideCallback();
		}
		if(typeof this.containers[name] != 'undefined')
		{
			if(typeof this.containers[name].onHide == 'function')
			{
				this.containers[name].onHide();
			}
		}
	},
	
	//
    // updateTagList()
    //
    /*updateTagList: function() {   
        this.updateTagList = Prototype.emptyFunction;

        document.observe('click', (function(event){
            //var target = event.findElement('[class^=showOverlay]') || event.findElement('[class^=showOverlay]');
            var target = event.findElement();
            //if (target) {
            if(target.hasClassName('showOverlay')) {
            	target.overlayObj=this;
                event.stop();
                this.show(target);
            }
        }).bind(this));
    },*/
    updateTagList: function() {   
        this.updateTagList = Prototype.emptyFunction;
		var self=this;
		/*$$('.addOverlay').each(function(elem) {
			elem.overlayObj=self;	
		  //alert(s);
		});*/

		
        document.observe('click', (function(event){
            //var target = event.findElement('[class^=showOverlay]') || event.findElement('[class^=showOverlay]');
            var target = event.findElement();
            //if (target) {
           /*if(target.hasClassName('showOverlay')) {
            	target.overlayObj=this;
                event.stop();
                this.show(target);
            }*/
            
            if(target.hasClassName('overlayClose')) {
            	event.stop();
                this.hide();
            }
            
        }).bind(this));
    },
	
    //
    //  getPageSize()
    //
    getPageSize: 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];
	}
}
//var overlayObj=new Overlay();;
/*Event.observe(window, 'load',
	function() { new Overlay(); }
);*/

Event.observe(window, 'load', function () { 
	
	var overlay=new Overlay();
	
/*	overlay.addContainer('advancedSearch',{
						
		 'trigger'	: 'advancedSearchTrigger'
		,'onShow'	: function() {
			this.onShow=null;
			new Ajax.Updater($('advancedSearch').select('dd')[0], 'load.php?module=offer&file=search_advanced', {
				method : 'get',
				parameters: {}
			})
		}
		,'onHide'	: function() { }
	});	
	
	overlay.addContainer('logInForm',{			
		 'trigger'	: 'logInFormTrigger'
		,'onShow'	: function() {}
		,'onHide'	: function() {}
	});
	
	overlay.addContainer('wishList',{			
		 'trigger'	: 'wishListTrigger'
		,'onShow'	: function() {}
		,'onHide'	: function() {}
	});
	
	overlay.addContainer('cart',{			
		 'trigger'	: 'cartTrigger'
		,'onShow'	: function() {}
		,'onHide'	: function() {}
	});*/
		 
			 
});
 
