/**
 * Luotu: 5.5.2008 Marko Behm
 * Tarvitsee toimiakseen:
 * - prototype.js
 * - effects.js
 * - scriptaculous.js
 * - ajax-post.js
 * - ajax_popup.css
 *
 * Käyttöesimerkki:
 * popup('{$www_base}ajax/popup/language','100px',1);
 *
 * Muokattu:
 * 3.9.2008 MB	Yksikertaistettu toimintaa ja lisätty toimintavarmuutta
 * 23.9.2008 MB	Siirrytään käyttämään prototypen ajax-ominaisuutta
 * 23.9.2008 MB	Lisätty sulkemisen yhteyteen automaattinen uudelleenlataus (setReload)
 * 24.9.2008 MB	Lisätty taustan läpinäkyvyyden hallinta scriptaculous-kirjastolla
*/


var transparent     = '0.7';
var overlayDuration = 0;

var _forceReload = false;
var _backgroundColor = '#555555';


/* Avaa annetun urlin div-popupissa */
function popup(action, bgcolor, margintop){
	var main  = document.body;

	main.style.overflowX = 'hidden';								// Estetään bodyn overflowaus popupin aikana

	if (bgcolor)													// Asetetaan taustaväri
		backgroundColor = bgcolor;
	else if (bgcolor === false || bgcolor === 'none')
		backgroundColor = '';
	else
		backgroundColor = _backgroundColor;

	if (!$('ajax_black_overlay')){									// Varjoalue luodaan body-osan alkuun
		var div_popup_overlay = document.createElement('DIV');
		main.appendChild(div_popup_overlay);

		div_popup_overlay.setAttribute('id','ajax_black_overlay');
		div_popup_overlay.style.backgroundColor = backgroundColor;
		new Effect.Appear('ajax_black_overlay', { duration: overlayDuration, from: 0.0, to: transparent });
		$('ajax_black_overlay').onclick = function(){closeMe("");}	// Sulkee popupin, jos käyttäjä klikkaa varjoaluetta
	}

	if (!$('ajax_popup_holder')){									// Popup_holder asemoi popupin css:llä
		var div_popup_holder = document.createElement('DIV');
		main.appendChild(div_popup_holder);

		div_popup_holder.setAttribute('id','ajax_popup_holder');

		$('ajax_popup_holder').onclick = function(e) {
			if (!e) var e = window.event;
			var clickObj = Event.element(e).id;
			if (clickObj == 'ajax_popup_holder') {
				closeMe('');										// Sulkee popupin, jos käyttäjä klikkaa popup-holderia
			}
		}
	}

	if (!$('ajax_popup')){											// Varsinainen popup, jonka sisältö ladataan ajaxilla
		var div_popup = document.createElement('DIV');
		$('ajax_popup_holder').appendChild(div_popup);

		div_popup.setAttribute('id','ajax_popup');
		div_popup.className = 'ajax_popup_hidden';
	}

	if(arguments[3]){												// Debug näyttää ajettavan urlin
		alert(action);
		$('ajax_popup').innerHTML = '<div style="width:400px; background-color:white; color:#555555; padding:10px; border:1px solid:#555555;">Ladataan...<br><br>'+action+'</div>';
	}


	if ($('ajax_popup')){											// Ladataan sisältö ajax-kutsulla
		loadContent(action,'ajax_popup');
	}
	else
		alert('targettia ei löydy');

	var arrayPageSize = getPageSize();								// Venytetään varjo koko näytölle
	//$('ajax_black_overlay').style.width = arrayPageSize[0]+'px';
	$('ajax_black_overlay').style.height = arrayPageSize[1]+'px';
	$('ajax_black_overlay').style.width  = '100%';
	//$('ajax_black_overlay').style.height = '200%';


	var arrayPageScroll = getPageScroll();							// Lasketaan popupin asema
	var top = arrayPageScroll[1] + (arrayPageSize[3] / 10);

	if (margintop)
		$('ajax_popup_holder').style.top = margintop+'px';
	else
		$('ajax_popup_holder').style.top = top+'px';


	setTimeout("show();",500);
}

/* Kerrotaan popupille että suljettaessa päivitetään koko sivu */
function setReload(){
	_forceReload = true;
}

/* Sulkee popupin */
function closeMe(href){
	if (!href)
		href = '';
		
	var startWith = 0;
	fadeOut(startWith,href);
}

/* Himmentää popupin, poistaa sen ja ohjaa hrefiin tarvittaessa */
function fadeOut(opacity,href){
	var main = document.body;

	if ($('ajax_popup') && $('ajax_popup_holder'))
		$('ajax_popup_holder').removeChild($('ajax_popup'));

	if (!$('ajax_black_overlay')){									// Jos popupin kehystä ei ole olemassa, ei kyseessä ole oikeasti popup ja ohjataan vain takaisin
		window.location.href = 'javascript:history.go(-1)';
	}

	if (opacity > 0){
		var addition = 1.5;
		var op  = opacity-addition;

		if ($('ajax_black_overlay')){
			var Obj = $('ajax_black_overlay');

			Obj.style.opacity = op/10;
			Obj.style.filter = 'alpha(opacity=' + op*10 + ')';
		}

		setTimeout("fadeOut("+op+",'"+href+"')",10);
	}
	else{
		main.style.overflowX = 'auto';

		if ($('ajax_popup_holder'))
			main.removeChild($('ajax_popup_holder'));

		if ($('ajax_black_overlay'))
			main.removeChild($('ajax_black_overlay'));


		if (href)
			window.location.href = href;
		else if (_forceReload)
			window.location.href = 'javascript:history.go(0)';
	}
}

/* Näyttää popupin */
function show(from){

	if ($('ajax_popup')){
		if (from){
			var elem = $(from);

			$('ajax_popup').className = 'ajax_popup_visible';
			xPos = elem.offsetWidth;
			$('ajax_popup').style.width = (xPos)+'px';
		}
		else{
			$('ajax_popup').className = 'ajax_popup_visible';
		}
	}
}

/* Core code from - quirksmode.com */
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}

	arrayPageScroll = new Array(xScroll,yScroll)
	return arrayPageScroll;
}

/* Core code from - quirksmode.com */
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;

	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;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}
