/** pAjaxBox
 * 
 * 	lightweight ajax box
 * 
 * 	@author		www.kocsan.hu
 * 	@since		2009-02-18
 * 
 */
 

function class_pAjaxBox(){
	this.ajaxBox = null;
	this.boxShadow = null;
	this.ajaxBoxWidth = 0;
	this.ajaxBoxHeight = 0;
	this.loadedFile = null;
	this.visible = false;
	this.generated = false;
	
	this.init = function( width, height ){
		this.ajaxBoxWidth	= width;
		this.ajaxBoxHeight	= height;
		
		// this.generateBox();
	}

// -------------------------------------------------------------------------
	/** GenerateBox
	 * 	generates the container box
	 */
	this.generateBox = function(){
		var body = this.getBodyTag();
		
		body.innerHTML 				+=	"<div id='pAjaxBoxShadow' class='pAjaxBoxShadow'></div>";
		body.innerHTML 				+=	"<div id='pAjaxBox' class='pAjaxBox'></div>";
		this.boxShadow				=	document.getElementById('pAjaxBoxShadow');
		this.ajaxBox 				=	document.getElementById('pAjaxBox');
		this.ajaxBox.innerHTML		=	"<div id='pAjaxBoxContent' class='pAjaxBoxContent'></div>";
		this.ajaxBox.innerHTML		+=	"<div id='pAjaxBoxClose' class='pAjaxBoxClose'><a href='#' onclick='pAjaxBox.hideBox(); return false;'>Bezárás</a></div>";
		this.generated = true;
		timeoutID = window.setInterval( 'pAjaxBox.renderBox()' , 1000 );
	}

// -------------------------------------------------------------------------
	/** RenderBox
	 * 	sets the actual sizes of the box and the shadow
	 */	
	this.renderBox = function(){
		this.ajaxBox.style.width	=	this.ajaxBoxWidth+'px';
		this.ajaxBox.style.height	=	'auto';
		
		var arrayPageSize = getPageSize();

		// alert('width: '+arrayPageSize[0]+' height: '+arrayPageSize[1]);
		
		this.ajaxBox.style.left = arrayPageSize[0] / 2-this.ajaxBoxWidth /2+"px";
		this.ajaxBox.style.top = 20+"px";
		
		this.boxShadow.style.width = arrayPageSize[0]+"px";
		this.boxShadow.style.height = arrayPageSize[1]+"px";

	}
	
	this.showBox = function(){
		this.boxShadow.style.display = 'block';
		this.ajaxBox.style.display = 'block';
		
		changeOpac( 0 , this.ajaxBox.id );
		changeOpac( 0 , this.boxShadow.id );

		this.renderBox();

		opacity( this.boxShadow.id, 0 , 75 , 200);
		opacity( this.ajaxBox.id, 0 , 100 , 400);
		this.visible = true;
	}
	
	this.hideBox = function(){
		this.boxShadow.style.display = 'none';
		this.ajaxBox.style.display = 'none';
		this.visible = false;
	}
	
	this.refreshBox = function(){
		if( !this.generated ) this.generateBox();
		if( !this.visible ) this.showBox();
		this.renderBox();
		window.scroll(0,0);
	}

// -------------------------------------------------------------------------	
	/** LoadFromFile
	 * 	loads the content from a file into the container box
	 */
	this.loadFromFile = function( fileName ){
		// Prototype
		if( typeof(Ajax) != 'undefined' )
			new Ajax.Updater(
				'pAjaxBoxContent',
				fileName, 
				{ 
					onComplete: this.refreshBox(),
					method: 'post'
				}
			)
		// jQuery
		else if( typeof( $ ) != 'undefined' ){
			$.get(
				fileName, 
  				function(data){
    				pAjaxBox.refreshBox();
    				document.getElementById( 'pAjaxBoxContent' ).innerHTML = data;
  				}
  			);		
		}

		this.loadedFile = fileName;
		
	}
	
	this.refresh = function(){
		if( this.loadedFile != null ){
			this.loadFromFile( this.loadedFile );
		}
	}

	this.getBodyTag = function(){
		var elements = document.getElementsByTagName( 'body' );
		return elements.item(0);
	}

}

// --------------------------------------------------------------------

function getPageSize(){
	
	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;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	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;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// -----------------------------------------------------------------------------------
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 
