/*************************************************
Modal Window
version: 1.0
modified: 2008.10.13
project: PokerNews
author: Audrius N.
*************************************************/

function modalWindow(width,heigh)
{
	this.url=null;
    this.fg=this.bg=null;
	this.width=width;
	this.height=heigh;
	this.isClosed=true;
	this.isFrame=false;
	this.ie6=false;
	this.imgPath= document.location.host.indexOf('my.pokernews')==0 ? '/i/' : '/img/';

    var _self=this;
	jQuery(document).ready(  function (){_self.init();} );
	jQuery(window).bind( 'resize', function (){_self.setPosition();});
}


/*
* Atidaro frame langa
* width ir height - optional
*************************************************/
modalWindow.prototype.openURL=function (url,width,height)
{
	if (url) {
		if (width && width>0) this.width=width;
		if (height && height>0) this.height=height;
		if (this.fg && this.frame) {
    		this.loading();
			//Pakraunam frame
			this.isClosed=false;
			jQuery(this.frame).bind('load',function (){top.modalActive.showFrame()});
			this.frame.src=url;
		} else this.url=url;
	}
}


/*
* Atidaro ir padaro visible div langa
*
*************************************************/
modalWindow.prototype.open=function (html)
{
    this.isFrame=false;
	if (html) jQuery(this.fg).html('<div class="div-modal-padding"><a href="./" onclick="if (top.modalActive) top.modalActive.close();return false;" id="a-modal-close"><img src="'+this.imgPath+'modal.close.gif" border="0" alt="Close" /></a>'+html+'</div>');
	top.modalActive=this;
    this.setPosition();
	var ifr=this.ie6 ? this.bg.firstChild : this.bg;
	jQuery([ifr,this.bg,this.fg]).show();
	jQuery(this.frame).hide();
}


/*
* Uzdaro atidaryta modal langa (nesvarbu ar div ar frame)
*
*************************************************/
modalWindow.prototype.close=function ()
{
	var ifr=this.ie6 ? this.bg.firstChild : this.bg;
	jQuery([ifr,this.bg,this.fg,this.frame]).hide();
 	this.isClosed=true;
	this.isFrame=false;
}


/*
* Pakeicia lnago matmenis
*
*************************************************/
modalWindow.prototype.resizeTo=function (width,height)
{
    if (width && width>0) this.width=width;
	if (height && height>0) this.height=height;
	if (!this.isClosed) this.setPosition();
	return this;
}


/*
* Parodo modalini langa "Loading..."
*
*************************************************/
modalWindow.prototype.loading = function ()
{
	this.open('<div id="window-loading"><img src="'+this.imgPath+'modal.loading.gif" alt="Loading..." /></div>');
}


/*
* Privati. Inicializuoja. Pasikrovus puslapiui paleidzia konstruktorius.
*
*************************************************/
modalWindow.prototype.init=function ()
{
    var topDocument=top.document;
	if (!topDocument.getElementById('modalWindowForeground')) {

		//sukuriam objektus
        var objBody = topDocument.getElementsByTagName("body").item(0);

        //background
		jQuery(objBody).append('<div id="modalWindowBackground" style="display:none;background-color:#000;position:absolute;z-index:100;"></div>');
        //ie6 reikia ifframe backgrounde papildomai
		var agt=navigator.userAgent.toLowerCase();
		if ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)) { //IE
			var index = navigator.appVersion.indexOf('MSIE');
			if (index != -1 && parseInt(navigator.appVersion.substring(index+5))<7) {
            	jQuery('#modalWindowBackground',objBody).append('<iframe frameborder="0" scrolling="no" style="display:none;background-color:#000;position:absolute;"></iframe');
				this.ie6=true;
			}
		}

		//modal - frame (kai kontentas frame)
		jQuery(objBody).append('<iframe id="modalWindowFrame123" name="modalFrame123" frameborder="0" scrolling="auto" style="display:none;background-color:#fff;position:absolute;z-index:105;width:0px;height:0px"></iframe>');

		//modal - div (kai kontentas dive)
		jQuery(objBody).append('<div id="modalWindowForeground" style="display:none;background-color:#fff;position:absolute;z-index:102;overflow:auto;"></div>');
	}

	this.bg=topDocument.getElementById('modalWindowBackground');
    this.fg=topDocument.getElementById('modalWindowForeground');
	this.frame=topDocument.getElementById('modalWindowFrame123');
	var _self=this;
	jQuery(this.bg).bind('click', function(){_self.close();});
	if (this.bg) this.setOpacity(this.bg,75);
	if (this.url) this.openURL(this.url);
}


/*
* Privati. Frame langa padaro visible
*
*************************************************/
modalWindow.prototype.showFrame=function ()
{
	if (this.isClosed) return;
	this.isFrame=true;
	top.modalActive=this;
	this.setPosition(true);
	var ifr= this.ie6 ? this.bg.firstChild:this.bg;
	jQuery([ifr,this.bg,this.fg,this.frame]).show();
	jQuery(this.fg).hide();
}


/*
* Privati. Fonui nustato dalini permatomuma
*
*************************************************/
modalWindow.prototype.setOpacity=function (object, opacity)
{
    object.style.opacity = (opacity / 100);
	object.style.MozOpacity = (opacity / 100);
	object.style.KhtmlOpacity = (opacity / 100);
	object.style.filter = "alpha(opacity=" + opacity + ")";
}


/*
* Privati. Centruoja modalini langa (nustato jo pozicija)
*
*************************************************/
modalWindow.prototype.setPosition=function () {
	var isFrame = top.modalActive && top.modalActive.isFrame ? true : false;
    if(this.fg) {
    	var jDoc = jQuery(document);
		var x = jDoc.width();
		var y = jDoc.height();

		//Opera 9.5 neuzdengia visko, jei dokumentas trumpesnis
		if(jQuery.browser.opera) {
			if (window.innerHeight>y) y=window.innerHeight;
		}

		var ifr=this.ie6 ? this.bg.firstChild:this.bg;
		jQuery([ifr,this.bg]).css({
            width : x + 'px',
        	height : y + 'px',
        	top : '0px',
        	left : '0px'
		});

        var jWin=jQuery(window);

		var wH=jWin.height();
		//opera neteisingai pasako
        if ( jQuery.browser.opera || (jQuery.browser.safari && parseInt(jQuery.browser.version) > 520) ) {
			wH = window.innerHeight - ((jQuery(document).height() > window.innerHeight) ? 20 : 0);
		}
		var viewPortCenter = {
			x: jWin.width()/2 + jWin.scrollLeft() ,
			y: wH/2 + jWin.scrollTop()
		}
		var fg = isFrame ? this.frame : this.fg;
		var winModal = {
			width: this.width ? this.width : jQuery(fg).width(),
			height: this.height ? this.height : jQuery(fg).height()
		}
		//if (frame) winModal.height=jQuery(this.frame.contentDocument).height();
		jQuery(fg).css( {
			width : winModal.width+'px',
        	height : winModal.height+'px',
        	left : (viewPortCenter.x - (winModal.width / 2)) + 'px',
        	top : (viewPortCenter.y - (winModal.height / 2)) + 'px'
		} );
    }
}