/*
* layer popper
* (c) insign
*
*	REQUIREMENTS: - absCorrect
*				  - jscripts/lpop/*
*				  - aculo: effects,builder
*
* @autor 	stpa
* @version	0.3
*/


// trigger to check if already opened the popper
var lpopOpened = false;

// popper object
var lpopObj    = false;

var borderWidth = false;
var buttonsHeight = false;

var bootLoader = new Image();
	bootLoader.src = absCorrect+'jscripts/lpop/lpop_loader.gif';
	bootLoader.id  = 'bootLoaderImg';

var extractNode = null;

var lpopShow = function (target,params) {

	var pop_type = params.poptype ? params.poptype : 'img';
	var height = params.height;
	var width  = params.width;

	var title  = params.title ? params.title : '';

 	// check if already displayed
	// yes - clean content, show preloader
	// no  - append stuff da - add preloader - display it!
	if (lpopOpened == true) {
		// if previous popper content was extracted, move content back to origin position
		if(extractNode)
		{
			extractNode.appendChild($('dialog_body').childNodes[0]);
			extractNode = null;
		}

		lpopCleanContent();
		lpopShowBootscreen(width,height);
		// set size & position
		lpopResize(width,height);
		lpopPosition(width,height);
	} else {
		lpopAppendHtmlToBody();
		lpopShowBootscreen(width,height);
		// set size & position
		lpopResize(width,height);
		lpopPosition(width,height);
	}

	$('pop_title').innerHTML = title;

	lpopDisplayIt(true);

	// switch type
	if (pop_type == 'img') {
		var preLoad = new Image();
		preLoad.src = target;

		lpopCleanContent();

		$('dialog_body').appendChild(preLoad);
		$('dialog_body').style.textAlign = 'center';
		preLoad.style.margin = 'auto';
		$('dialog_body').style.paddingTop = '10px';

		lpopOpened = true;

	} else if (pop_type == 'ajax') {
		// ajax -> return put to content
		lpopAjaxContent(target);
		lpopOpened = true;

	} else if (pop_type == 'div') {
		if ($(target)) {
			$('dialog_body').innerHTML = $(target).innerHTML;
		}
		lpopOpened = true;

	} else if (pop_type == 'inline') {
		if ($(target)) {
			extractNode = $(target).parentNode;
			lpopCleanContent();
			$('dialog_body').appendChild($(target));

			$('dialog_body').style.border = '0px';
		}
		lpopOpened = true;
	}
}

/*
* html construct that we need to display the popper
*/
var lpopAppendHtmlToBody = function() {
	if (typeof(lpopObj) != 'object') {
		var objBody = $$('body')[0];

		lpopObj = Builder.node('div',{id:'lpopContainer','class':'generic_dialog classifieds_pop_view_dialog pop_dialog'}, [
            Builder.node('div',{id:'lpopInnerContainer','class':'generic_dialog_popup'},
                Builder.node('table',{id:'pop_dialog_table','class':'popper_dialog_table'},
                	Builder.node('tbody',{id:'pop_dialog_tbody'},[
                		Builder.node('tr',{id:'pop_dialog_toprow'}, [
                			Builder.node('td',{'class':'lpopper_topleft'}),
                			Builder.node('td',{'class':'lpopper_border'}),
                			Builder.node('td',{'class':'lpopper_topright'})
                		]),
                		Builder.node('tr',{id:'pop_dialog_midrow'},[
                			Builder.node('td',{id:'lpopper_border','class':'lpopper_border'}),
                			Builder.node('td',{id:'pop_content','class':'lpopper_content'},
                				Builder.node('div',{id:'dialog_content','class':'dialog_content'},[
                					Builder.node('div',{id:'dialog_buttons','class':'dialog_buttons'}, [
                						Builder.node('span',{id:'pop_title','class':'dialog_title'}),
                						Builder.node('a',{id:'closeBtn',href:'#',onclick:'lpopDisplayIt(false);toggleSelectors(\'visible\');return false;'},
                							Builder.node('img',{id:'closeBtnImg',src:absCorrect+'jscripts/lpop/close_s.gif'})
                						)
                					]),
                					Builder.node('div',{id:'dialog_body','class':'dialog_body'})
                				])
                			),
                			Builder.node('td',{'class':'lpopper_border'})
                		]),
                		Builder.node('tr',{id:'pop_dialog_botrow'},[
                			Builder.node('td',{'class':'lpopper_bottomleft'}),
                			Builder.node('td',{'class':'lpopper_border'}),
                			Builder.node('td',{'class':'lpopper_bottomright'})
                		])

                	])
                )
			)
		]);

        objBody.appendChild(lpopObj);
	}
}


/*
* resize popper window
*/
var lpopResize = function(width,height) {
	$('dialog_content').style.width = '100%';
	// border width
	if (borderWidth == false) borderWidth = Element.getWidth($('lpopper_border'));
	if (buttonsHeight == false) buttonsHeight = Element.getHeight($('dialog_buttons'))
	width = parseInt(borderWidth)*2 + parseInt(width);
	$('pop_dialog_table').style.width = width+'px';

	var newheight = parseInt(buttonsHeight) + parseInt(height) + 2;
	//console.debug(Element.getHeight($('dialog_buttons')));
	$('dialog_content').style.height = newheight+'px';
}

/*
* always position the popper in the middle of the screen!
*/
var lpopPosition = function(width,height) {
	var aPS = getPageSize();
	var aPSc = getPageScroll();

	var newtop = (aPS[3]/2) - (height/2) + aPSc[1];
	if (newtop < 0) newtop = 0;
	$('lpopInnerContainer').style.top = newtop+'px';
}

/*
* only set style display of master popper div
*/
var lpopDisplayIt = function(showIt) {
	if (showIt == true) {
		new Effect.Appear(lpopObj,{duration:'0.2'});
		window.onkeypress = function(keycode) {
			if (keycode.keyCode == 27) {
				lpopDisplayIt(false);
				window.onkeypress = 0;
			}
		}
	} else {
		if(lpopObj)
		{
			new Effect.Fade(lpopObj,{duration:'0.4'});

			// if popper content was extracted at startup, move content back to origin position
			if(extractNode)
			{
				extractNode.appendChild($('dialog_body').childNodes[0]);
				extractNode = null;
			}
		}
	}
}

/*
* show sexy ajax loader as standard bootscreen of super-popper :-)
*/
var lpopShowBootscreen = function(width,height) {
	if ($('dialog_body')) {
		$('dialog_body').appendChild(bootLoader);
		var newleft = width/2 - bootLoader.width/2;
		var newtop  = height/2 - bootLoader.height/2;
		$('bootLoaderImg').style.top = newtop+'px';
		$('bootLoaderImg').style.left = newleft+'px';
	}
}

/*
* empty innerHTML of content
*/
var lpopCleanContent = function() {
	if ($('dialog_body')) {
		$('dialog_body').innerHTML = '';
	}
}


var lpopAjaxContent = function(target) {	
	var textCode = (document.characterSet)?document.characterSet:document.charset; //get the text code(like iso-8859-1)	
	var lpopAjax = new Ajax.Request(target,{
		method: 'post',
		asynchronous: 'true',
		evalScripts: 'true',
		evalJS: 'true',
		parameters: 'shop.textCode='+document.characterSet,
		onComplete: function(transport) {
    		if (200 == transport.status) {
    			$('dialog_body').innerHTML = transport.responseText;
    			//console.debug('yada yada yada');
    		} else if (404 == transport.status) {
    			$('dialog_body').innerHTML = '<h1>Error 404</h1><hr>The requested URL ' + target + ' was not found on this server.';
    			//console.debug('yada yada 404');
    		} else if (403 == transport.status) {
    			$('dialog_body').innerHTML = '<h1>Error 403</h1><hr>You have no permission to access ' + target + ' on this server.';
    			//console.debug('yada yada no permission');
    		} else {
    			$('dialog_body').innerHTML = '<h1>Error</h1><hr>An error occured while processing your request.';
    		}
  		}
	});
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}


//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
//
function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		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
		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 = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}