/**
 * 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 = 1;

var _forceReload = false;
var _backgroundColor = '#526250';
var imgpath = 'template/html/default/img/datepicker/' // directory with calendar images


/* 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;
		div_popup_overlay.style.display = 'none';
		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
			}
		}
	}
	
	// Rakennetaan varjoaluetta
	div_shade = document.createElement('DIV');
	div_shade.setAttribute('id','ajax_popup_shadow');
	div_shade.innerHTML = b_ie6
		? ''
		: '<table border="0" style="width:100%; height:100%;" cellspacing="0" cellpadding="0"><tr><td rowspan="2" width="6"><img src="' + imgpath + 'pixel.gif"></td><td rowspan="2"><img src="' + imgpath + 'pixel.gif"></td><td width="7" style="background: url(' + imgpath + 'shade_tr.png) bottom left no-repeat; height:7px;"><img src="' + imgpath + 'pixel.gif"></td></tr><tr><td style="background: url(' + imgpath + 'shade_mr.png) top right repeat-y;"><img src="' + imgpath + 'pixel.gif"></td></tr><tr><td style="background: url(' + imgpath + 'shade_bl.png) top right no-repeat;"><img src="' + imgpath + 'pixel.gif"></td><td style="background: url(' + imgpath + 'shade_bm.png) top right repeat-x; height:7px;" align="left"><img src="' + imgpath + 'pixel.gif"></td><td style="background: url(' + imgpath + 'shade_br.png) top left no-repeat;"><img src="' + imgpath + 'pixel.gif"></td></tr></table>';
	$('ajax_popup_holder').appendChild(div_shade);					// Liitetään varjoalue popin kanssa samaan holderiin
	

	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){
	var startWith = 10;
	fadeOut(startWith,href);
}

/* Himmentää popupin, poistaa sen ja ohjaa hrefiin tarvittaessa */
function fadeOut(opacity,href){
	var main = document.body;
	$('ajax_popup_shadow').style.display = 'none';
	
	// Vapautetaan key-listener
	document.onkeypress = releaseKeyEvent;
	
	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)';
	}
}

/* Vapauttaa mahdollisesti asetetut keylistenerit */
function releaseKeyEvent(key){
}

/* 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';
		}
	}
	
	$('ajax_popup_shadow').style.display = 'block';
}

function newWindow(url, myname, w, h, scroll) {

	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 3;
	//var h = (screen.height) / 3 * 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=yes, '
	win = window.open(url, myname, winprops)

	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

/* 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;
}


var s_userAgent = navigator.userAgent.toLowerCase(),
	re_webkit = /WebKit\/(\d+)/i;
var b_mac = s_userAgent.indexOf('mac') != -1,
	b_ie6 = s_userAgent.indexOf('msie 6') != -1 && s_userAgent.indexOf('opera') == -1;