var IE = document.all?true:false;

var windowX = 0;
var windowY = 0;
var scrollY = 0;
var php = new PHP_Serializer();

		
var Window = Class.create();

Window.prototype = {
  
  	pageSize: {},
  	windowScroll: {},
  	initialize: function() {
  	  	this.eventOnLoad   = this.windowOnLoad.bindAsEventListener(this);
  	  	this.eventOnResize = this.windowOnResize.bindAsEventListener(this);
  	  	this.eventDocumentReady = this.windowOnReady.bindAsEventListener(this);

		Event.observe(window, 'load', this.eventOnLoad);
		Event.observe(window, 'resize', this.eventOnResize);
		Event.observe(window, 'scroll', this.eventOnResize);
	},

	windowOnLoad: function(){
	  	this.windowScroll = this.getWindowScroll();
		updateLayersPosition();		
	},
	
	windowOnResize: function(){
	  	this.windowScroll = this.getWindowScroll();
		updateLayersPosition();
	},
	
	windowOnReady: function(){
		alert('Ready');  
	},

	// From script.aculo.us
    getWindowScroll: function() {
        var w = window;
            var T, L, W, H;
            with (w.document) {
                if (w.document.documentElement) {
                    T = documentElement.scrollTop;
                    L = documentElement.scrollLeft;
                } else if (w.document.body) {
                    T = body.scrollTop;
                    L = body.scrollLeft;
                }
                if (w.innerWidth) {
                    W = w.innerWidth;
                    H = w.innerHeight;
                } else if (w.document.documentElement && documentElement.clientWidth) {
                    W = documentElement.clientWidth;
                    H = documentElement.clientHeight;
                } else {
                    W = body.offsetWidth;
                    H = body.offsetHeight
                }
            }
            return { top: T, left: L, width: W, height: H };
        
    }

}

function updateLayersPosition(){
   	if($('popup_ads')){
   	  	elementSize = Element.getDimensions('popup_ads');
		$('popup_ads').style.top = Math.round(((Win.windowScroll['height']  - elementSize['height']) / 2) + Win.windowScroll['top']) + 'px';
  		$('popup_ads').style.left = Math.round(((Win.windowScroll['width'] - elementSize['width']) / 2) + Win.windowScroll['left']) + 'px';
  	}
}

var Win = new Window;


Event.onDOMReady(function() {
  	Win.eventOnLoad();
  	updateAds();
});


String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function updateAds(){
	var myAjax = new Ajax.Request(
			"home/index.php", 
			{
				method: 'post', 
				parameters: 'act=ads&do=check&url='+document.location, 
				onComplete: function(ajaxReq){
					if(ajaxReq.responseText!=''){
					  	info = php.unserialize(ajaxReq.responseText.trim());
					  	$('popup_ads_title').innerHTML = info['name'];
					  	$('popup_ads_content').innerHTML = info['content'];
					  	updateLayersPosition();
					  	$('popup_ads').style.display = 'block';
					  	if(info['duration']!='') setTimeout(closeAds, info['duration']*1000);				  	
					}
				}
			});	
}

function closeAds(){
  	$('popup_ads').style.display = 'none';
}
