/**
 * Luotu: 5.5.2008 Marko Behm
 * Tarvitsee toimiakseen:
 * - prototype.js
 * - ajax.js
 * - ajax-dynamic-content.js
 * - ajax_popup.css
 *
 * Käyttöesimerkki:
 * openContent('{$www_base}ajax/popup/language','100px',1);
 *
 * Muokattu:
 * 3.9.2008 MB	Yksikertaistettu toimintaa ja lisätty toimintavarmuutta
 * 8.7.2009 MB	Lisätty setReaload -metodi
*/

function openContent(action,margintop){
	var Stamp = new Date();
	var tim   = Stamp.getHours()+'-'+Stamp.getMinutes()+'-'+Stamp.getSeconds();
	var main  = document.body;

	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');
		$('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 (action.indexOf('/-/') < 1){									// Muokataan ajaxille lähtevää requestia. Huomioidaan su3-parametrit
		if (action.charAt(action.length-1) == '/'){
			action = action+'-/';
		}
		else
			action = action+'/-/';
	}
	else if (action.charAt(action.length-1) != '/'){
		action = action+'/';
	}

	action = action+'time/'+tim;									// Lisätään urliin dummy-parametri, jotta selaimet eivät onnistu kutsun cacheamisessa

	if(arguments[2]){												// 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')){
		ajax_loadContent('ajax_popup',action);						// Ladataan sisältö ajax-kutsulla
	}
	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';

	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);
}

/* Sulkee popupin */
function closeMe(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{
		if ($('ajax_popup_holder'))
			main.removeChild($('ajax_popup_holder'));

		if ($('ajax_black_overlay'))
			main.removeChild($('ajax_black_overlay'));

		if (href)
			window.location.href = href;
	}
}

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;
}

/* Kerrotaan popupille että suljettaessa päivitetään koko sivu */
function setReload(){
	_forceReload = true;
}
