/**********************************************************************************************
| Mkportal Ajax Boxes & Tips
| by Luponero aka Meo oct 2007
|
|----------------------------
| Based on Original Ideas:
| by DTHMLGoodies.com, Alf Magne Kalleland
| by Lokesh Dhakar - http://www.huddletogether.com
*************************************************************************************************/	


//Meo: Mkportal settings ----------------------------
var BalloonImg =  "/mkportal/templates/Forum/images/mkajax/freccia.gif";
var BalloonImg2 =  "/mkportal/templates/Forum/images/mkajax/frecciad.gif";
var LoadingImg =  "/mkportal/templates/Forum/images/mkajax/loadspin.gif";
var OverlayImg =  "/mkportal/templates/Forum/images/mkajax/overlay.png";
var CloseImg =  "/mkportal/templates/Forum/images/mkajax/closepl.gif";

//Meo: General settings
var popshowdelay = 800;
var tipshowdelay = 400;

// Meo: Inizialization
var objOverlay = false;
var objLightbox = false;
var ajax_tooltipObj = false;
var ajax_tooltipObj_iframe = false;
var ajax_tooltip_MSIE = false;
var ajax_tooltip_Myo = "";
if(navigator.userAgent.indexOf('MSIE')>=0)ajax_tooltip_MSIE=true;


// Meo: added in C 0.1.b
// General utility Functions called
var MkUtilsLib = {

	getPageScroll: function()
	{
		var yScroll;
		if(self.pageYOffset)
		{
			yScroll = self.pageYOffset;
		}
		else if(document.documentElement && document.documentElement.scrollTop) // Explorer 6
		{
			yScroll = document.documentElement.scrollTop;
		}
		else if(document.body) // all other Explorers
		{
			yScroll = document.body.scrollTop;
		}
		arrayPageScroll = new Array('',yScroll);
		return arrayPageScroll;
	},

	getPageSize: function()
	{
		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;
	}

}

// Ajax Spinner loading indicator
var objMkspinner = false;
function mkportal_Spinner_Show() {
	if(!objMkspinner) {
		var image =  "/mkportal/templates/Forum/images/mkajax/loadspin.gif";;
		var height = 180;
		var width = 180;
		objMkspinner = document.createElement("div");
		objMkspinner.style.position = "absolute";
		objMkspinner.style.zIndex = 1000;
		objMkspinner.style.textAlign = "center";
		objMkspinner.style.verticalAlign = "middle";
		objMkspinner.innerHTML = "<div style=\"text-align: center; border:2px solid #698490; padding: 6px; background: #FFF;\"><br /><img src=\"" + image + "\" border=\"\"><br /><br /><b>... Loading... </b><br /></div>";
		objMkspinner.style.width = width + "px";
		objMkspinner.style.height = height + "px";
		objMkspinner.style.display = 'none';
		objMkspinner.id = "mkspinner";
		var owner = document.getElementsByTagName("body").item(0);
		owner.insertBefore(objMkspinner, owner.firstChild);
	}
	var arrayPageSize = MkUtilsLib.getPageSize();
	var arrayPageScroll = MkUtilsLib.getPageScroll();
	var top = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - 180) / 2);
	var left = ((arrayPageSize[0] - 20 - 180) / 2);
	objMkspinner.style.top = top + "px";
	objMkspinner.style.left = left + "px";
	objMkspinner.style.display = 'block';
}

function mkportal_Spinner_Hide() {
 	document.getElementById('mkspinner').style.display = 'none';
}

// Ajax Core Engine
function MKP_ajax(url, options) {	

	var mka_postData = options.postBody || '';
	var mka_method = options.method || 'post';
	var mka_Complete = options.onComplete || null;
	var mka_update = options.update || null;
	var mka_sendReq = Mka_getXmlHttpRequestObject();

	function Mka_returnOut() {

		if (mka_sendReq.readyState == 4 && mka_sendReq.status == 200) {
			if (mka_Complete) 
				setTimeout(function(){mka_Complete(mka_sendReq);}, 10);
			if (mka_update)
				setTimeout(function(){mka_update.innerHTML = mka_sendReq.responseText;}, 10);
			mka_sendReq.onreadystatechange = function(){};
		}

	}

	function Mka_start(url){

		mka_sendReq.open(mka_method, url, true);
		mka_sendReq.onreadystatechange = Mka_returnOut;
		if (mka_method == 'post') {
			mka_sendReq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			if (mka_sendReq.overrideMimeType) mka_sendReq.setRequestHeader('Connection', 'close');
		}
		mka_sendReq.send(mka_postData);

	}
	

	function Mka_getXmlHttpRequestObject() {
		if(window.XMLHttpRequest) {	return new XMLHttpRequest(); }
		else if(window.ActiveXObject) {
			try { req = new ActiveXObject('Msxml2.XMLHTTP.4.0'); } catch(e) { try {	req = new ActiveXObject('Microsoft.XMLHTTP'); } catch(e) {req = false; }} return req; }
		else {	return false; }
	}
	
	Mka_start(url);
};


/********************** Meo ShowPop ********************************/

function ajax_showPop(externalFile, delayno)
{
// Meo: Clear all the external calling ant timeouts delay
	ajaxPop_clear();

	if(!objOverlay)
	{
		var objBody = document.getElementsByTagName("body").item(0);
		objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','mkoverlay');
		objOverlay.onclick = function () {ajax_hideLightbox(); return false;}
		objOverlay.style.position = 'absolute';
		objOverlay.style.display = 'none';
		objOverlay.style.top = '0';
		objOverlay.style.left = '0';
		objOverlay.style.zIndex = '90';
 		objOverlay.style.width = '100%';

		//Meo: This method seems to be very much faster than using a png background
		objOverlay.style.backgroundColor = '#111';
		objOverlay.style.opacity = '.80';
		objOverlay.style.filter = 'alpha(opacity=80)'; 
		objOverlay.style.MozOpacity = '0.8';
		objBody.insertBefore(objOverlay, objBody.firstChild);
		
		objLightbox = document.createElement("div");
		objLightbox.setAttribute('id','mklightbox');
		objLightbox.style.display = 'none';
		objLightbox.style.position = 'absolute';
		objLightbox.style.zIndex = '100';
		
		objLightbox.style.border = '1px solid #698490';
		objLightbox.style.backgroundColor = '#f1f6fb';
		objLightbox.style.padding = '4px';
		objLightbox.style.fontSize = '0.8em';

		objBody.insertBefore(objLightbox, objOverlay.nextSibling);

		var objLink = document.createElement("a");
		objLink.setAttribute('href','#');
		objLink.setAttribute('title','Click to close');
		objLink.onclick = function () {ajax_hideLightbox(); return false;}
		objLink.style.textAlign = 'right';
		objLightbox.appendChild(objLink);

		var objCloseButton = document.createElement("img");
		objCloseButton.src = CloseImg;
		objCloseButton.setAttribute('id','mkcloseButton');
		objCloseButton.style.position = 'absolute';
		objCloseButton.style.zIndex = '200';
		objLink.appendChild(objCloseButton);

		var objContent = document.createElement("div");
		objContent.setAttribute('id','mklightboxContent');
		objContent.style.overflow = 'auto';
		objLightbox.appendChild(objContent);


	}
		
	var arrayPageSize = MkUtilsLib.getPageSize();
	var arrayPageScroll = MkUtilsLib.getPageScroll();

	objOverlay.style.width = (arrayPageSize[0] + 'px');
	objOverlay.style.height = (arrayPageSize[1] + 'px');
			
//Meo: Reset runtime changed values
	document.getElementById('mklightboxContent').style.height = "";
	document.getElementById('mklightboxContent').style.width = "";
	objLightbox.style.width =  "";
	objLightbox.style.height = "";
	
// Meo: immediate or delay Calling?
	if (delayno != null){
		ajax_showPop2(externalFile);
	} else {
		delayshow=setTimeout("ajax_showPop2('" + externalFile + "')",popshowdelay);
	}

}

function ajax_showPop2(externalFile)
{
	mkportal_Spinner_Show();
	objOverlay.style.display = 'block';
	objLightbox.style.display = 'none';
	ajaxPopup_loadContent(externalFile);

}

function ajax_hideLightbox()
{
	document.getElementById('mkoverlay').style.display = 'none';
	document.getElementById('mklightbox').style.display = 'none';
	mkportal_Spinner_Hide();
	if (typeof delayshow!="undefined")
		clearTimeout(delayshow);
}


/********************** Meo ShowToolTip ********************************/
function ajax_showTooltip(externalFile,inputObj)
{
	// Meo: Clear all the external calling ant timeouts delay
	ajaxPop_clear();
	 
	if(!ajax_tooltipObj)
	{
		ajax_tooltipObj = document.createElement('DIV');
		ajax_tooltipObj.style.position = 'absolute';
		ajax_tooltipObj.style.zIndex = '1000000';
		ajax_tooltipObj.style.textAlign = 'left';
		ajax_tooltipObj.id = 'ajax_tooltipObj';			
		document.body.appendChild(ajax_tooltipObj);

		var leftDiv = document.createElement('DIV');
		leftDiv.setAttribute('id','ajax_tooltip_arrow');
		leftDiv.style.backgroundImage = "url('" + BalloonImg + "')";
		leftDiv.style.width = '28px';
		leftDiv.style.position = 'absolute';
		leftDiv.style.left = '0px';
		leftDiv.style.top = '20px';
		leftDiv.style.zIndex = '1000005';
		leftDiv.style.height = '26px';
		leftDiv.style.backgroundRepeat = 'no-repeat';
		leftDiv.style.backgroundPosition = 'center left';
		ajax_tooltipObj.appendChild(leftDiv);
		
		
		var contentDiv = document.createElement('DIV');
		contentDiv.style.border = '1px solid #698490';
		contentDiv.style.left = '-20px';
		contentDiv.style.top = '45px';
		contentDiv.style.position = 'absolute';
		contentDiv.style.backgroundColor = '#f1f6fb';
		contentDiv.style.padding = '5px';
		contentDiv.style.fontSize = '0.8em';
		contentDiv.style.zIndex = '1000001';

		ajax_tooltipObj.appendChild(contentDiv);
		contentDiv.id = 'ajax_tooltip_content';
		
		if(ajax_tooltip_MSIE){
			ajax_tooltipObj_iframe = document.createElement('<IFRAME frameborder="0">');
			ajax_tooltipObj_iframe.style.position = 'absolute';
			ajax_tooltipObj_iframe.border='0';
			ajax_tooltipObj_iframe.frameborder=0;
			ajax_tooltipObj_iframe.style.backgroundColor='#FFF';
			ajax_tooltipObj_iframe.src = 'about:blank';
			contentDiv.appendChild(ajax_tooltipObj_iframe);
			ajax_tooltipObj_iframe.style.left = '0px';
			ajax_tooltipObj_iframe.style.top = '0px';
		}	
	}
	
	if(ajax_tooltip_MSIE){
		ajax_tooltipObj_iframe.style.width = ajax_tooltipObj.clientWidth + 'px';
		ajax_tooltipObj_iframe.style.height = ajax_tooltipObj.clientHeight + 'px';
	}

	ajax_tooltipObj.style.display='none';
	
	// Meo: delay function
	ajax_tooltip_Myo = inputObj;
	delayshow=setTimeout("ajaxTooltip_loadContent('" + externalFile + "')",tipshowdelay);

}

function ajax_hideTooltip()
{
	
	ajax_tooltipObj.style.display='none';
	if (typeof delayshow!="undefined")
		clearTimeout(delayshow);
}

function ajax_positionTooltip(inputObj)
{

//Meo: Reset runtime changed values
	ajax_tooltipObj.style.left = '';
	ajax_tooltipObj.style.top = '';
	document.getElementById('ajax_tooltip_arrow').style.backgroundImage = "url('" + BalloonImg + "')";
	document.getElementById('ajax_tooltip_arrow').style.left = '0px';
	document.getElementById('ajax_tooltip_arrow').style.top = '20px';
	document.getElementById('ajax_tooltip_content').style.width = "";
	ajax_tooltipObj.style.width = "";
	ajax_tooltipObj.style.height = "";

	var leftPos = (ajaxTooltip_getLeftPos(inputObj) + inputObj.offsetWidth);
	var topPos = ajaxTooltip_getTopPos(inputObj);
	var myw = document.getElementById('ajax_tooltip_content').offsetWidth;
	if (myw < 1) myw = 150;
	var myh = document.getElementById('ajax_tooltip_content').offsetHeight;
	if (myh < 1) myh = 150;

// Meo: outscreen correction x
	var arrayPageSize = MkUtilsLib.getPageSize();
	var arrayPageScroll = MkUtilsLib.getPageScroll();
	var maxw = arrayPageSize[0];
	if ((myw + leftPos) > maxw) {
		var mydiff = (myw + leftPos) - maxw;
		var arrowdiff = 10;
		if (ajax_tooltip_MSIE == true) arrowdiff = 20;
		leftPos = leftPos - (mydiff +5);
		document.getElementById('ajax_tooltip_arrow').style.left = (mydiff - arrowdiff) +'px';
	}
// Meo: outscreen correction y
	var maxy = ((arrayPageScroll[1] + arrayPageSize[3]) -5);
	if ((topPos + myh + 45) > maxy) {
		topPos = topPos - (myh +67);
		document.getElementById('ajax_tooltip_arrow').style.backgroundImage = "url('" + BalloonImg2 + "')";
		document.getElementById('ajax_tooltip_arrow').style.top = myh + 44 + "px";
	}

	ajax_tooltipObj.style.left = leftPos + 'px';
	ajax_tooltipObj.style.top = topPos + 'px';

}


function ajaxTooltip_getTopPos(inputObj)
{		
  	var returnValue = inputObj.offsetTop;
  	while((inputObj = inputObj.offsetParent) != null){
  		if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
  	}
  	return returnValue;
}

function ajaxTooltip_getLeftPos(inputObj)
{
  	var returnValue = inputObj.offsetLeft;
  	while((inputObj = inputObj.offsetParent) != null){
  		if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
  	}
  	return returnValue;
}

// Meo: clear the timeouts
function ajaxPop_clear(){

	if (typeof delayshow!="undefined")
		clearTimeout(delayshow);
}


/*
**********************MEO AJAX INTEGRATION******************************
*/

//----------Meo: Popup Box -------------
function ajaxPopup_loadContent(url)
{
	new MKP_ajax(url, {method: 'get',  onComplete: function(request) { ajaxPopup_showContent(request); }});	
}

function ajaxPopup_showContent(request)
{
	mkportal_Spinner_Hide();
	objLightbox.style.display = 'block';
	objLightbox.style.visibility='hidden';
	
	document.getElementById('mklightboxContent').innerHTML = request.responseText;
	var arrayPageSize = MkUtilsLib.getPageSize();
	var arrayPageScroll = MkUtilsLib.getPageScroll();
	var myw = document.getElementById('mklightboxContent').offsetWidth;
	var myh = document.getElementById('mklightboxContent').offsetHeight;


	if (myh > arrayPageSize[3]) {
		myh = myh -170;
		document.getElementById('mklightboxContent').style.height = myh + "px";
		objLightbox.style.height = myh + 'px';		
	}

	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 20 - myh) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - myw) / 2);

	objLightbox.style.width =  myw + 'px';
	objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";

	document.getElementById('mkcloseButton').style.top = "8px";	
	document.getElementById('mkcloseButton').style.left = (myw -28) + "px";
	objLightbox.style.visibility='visible';

}

//-------------Meo: ToolTip---------------
function ajaxTooltip_loadContent(url)
{
	ajax_tooltipObj.style.display='block';
	document.getElementById('ajax_tooltip_content').innerHTML = "<div style=\"text-align: center; border:1px solid #698490; padding: 6px; background: #FFF; color: #666;\"><br /><img src=\"" + LoadingImg + "\" border=\"\"><br /><br /><b>...Loading...</b><br /></div>";
	ajax_positionTooltip(ajax_tooltip_Myo);
	new MKP_ajax(url, {method: 'get',  onComplete: function(request) { ajaxTooltip_showContent(request); }});	
	
}

function ajaxTooltip_showContent(request)
{
	document.getElementById('ajax_tooltip_content').innerHTML = request.responseText;
	ajax_tooltipObj.style.visibility='hidden';
	ajax_positionTooltip(ajax_tooltip_Myo);
	ajax_tooltipObj.style.visibility='visible';
}


