// JavaScript Document
// Can Koluman, 2009
// This code is copyright protected 
// It may be used for non-commercial purposes
// commercial entities and/or commercial use
// please contact me for licensing arragements
// http://www.strawberryfin.co.uk 

// many roads lead to Rome
// this is but one of them


var arr_detach = null;
var strInlineRenderer = strAppRoot + '/renderer.php'

function wndInlinePopUp (strURL,intWidth,intHeight) {
	
	var boolDetach = (arguments[3]) ? arguments[3] : false;
	
	if (boolDetach) {
		if (document.getElementById('p_frame').style.display = 'block') 
			closePopUp('p_frame');		
		wndPopUp(strURL, intWidth, intHeight);
	} else {
		preloader();
		execInlineAjaxCall('src_uri=' + escape(strURL) + '&src_width=' + intWidth + '&src_height=' + intHeight);	
	}
	
}

function detach() {
	
	if (arr_detach) {
		wndInlinePopUp (arr_detach[0] /* src */, arr_detach[1] /* width */,arr_detach[2] /* height */, true);
	} else {
		alert('Nothing to detach.');
	}
		
}

function preloader () {
	if (document.getElementById('p_frame').style.display = 'block') 
		closePopUp('p_frame');
		
	document.getElementById('p_frame').style.display = 'block';
	document.getElementById('p_frame').style.visibility = 'visible';

	document.getElementById('p_tr2').innerHTML = "<div class='p_preloader'><img src='" + strAppRoot + "/media/im_preloader.gif' height='75' width='75' /></div><div class='p_pl_text'>Loading...</div>";
	
	setInlinePopFormat('p_tr2', 'p_cr', 3);
}


function activateScripts () {

	if (document.getElementById('p_frame').style.display = 'block') 
		closePopUp('p_frame');
		
	document.getElementById('p_frame').style.display = 'block';
	document.getElementById('p_frame').style.visibility = 'visible';

	document.getElementById('p_tr2').innerHTML = strInline;
	var theScripts = document.getElementById('p_tr2').getElementsByTagName("script");
	var intScriptsLoop = theScripts.length;
	for (var x=0; x < intScriptsLoop; x++){
		// good option but slow
		//but need it to init global js vars
		var newScript = document.createElement('script');
		newScript.type = "text/javascript";
		newScript.text = theScripts[x].text;
		document.getElementById('p_tr2').appendChild (newScript);
		
		// just a double tap, this is quicker
		// do it for ie mainly...
		eval(theScripts[x].text);
	}
	//scripts may need time to execute
	//lan conservative
	//setTimeout ( "setInlinePopFormat('p_tr2', 'p_cr', 3);", 250 );	
	//online slow
	setTimeout ( "setInlinePopFormat('p_tr2', 'p_cr', 3);", 500 );
	
}

function setInlinePopFormat (strCenter, strCorner, intCascade) {

	var intFlatHeight = 90; 
	
	//convert this to function
	var intscrollTop = getScrollTop();
	
	var intCenterWidth = document.getElementById(strCenter).offsetWidth;
	if (intCenterWidth < 640) intCenterWidth = 640;
	var intCornerWidth = document.getElementById(strCorner).offsetWidth;
	var intRowWidth = 2*intCornerWidth + intCenterWidth + 4; /* ie6 needs the 4 */
	
	var intCenterHeight = document.getElementById(strCenter).offsetHeight;
	var intCornerHeight = document.getElementById(strCorner).offsetHeight;
	var intColumnHeight = 2*intCornerHeight + intCenterHeight;
	
	for (var i=0; i<intCascade; i++) {
		document.getElementById('p_tr'+(i+1)).style.width = intCenterWidth + 'px';
	}
	
	//adjustment for buttons
	document.getElementById('p_btn').style.left = (intRowWidth - 54 - document.getElementById('p_btn').offsetWidth)+'px';
	
	document.getElementById(strCenter).style.height = intCenterHeight + 'px';
	document.getElementById(strCenter+'a').style.height = intCenterHeight + 'px';
	document.getElementById(strCenter+'b').style.height = intCenterHeight + 'px';
	
	document.getElementById('p_tr').style.width = intRowWidth + 'px';
	document.getElementById('p_tr').style.height = intColumnHeight + 'px';
	
	//set container size
	document.getElementById('p_cont').style.width = intRowWidth + 'px';
	
	//get rid of expansion area
	document.getElementById('p_frame').style.height = 'auto';
	document.getElementById('p_clip').style.height = (document.getElementById('p_clip').offsetHeight-intColumnHeight)+'px';
	
	// set top
	var intTop = (document.getElementById('p_clip').offsetHeight - intFlatHeight - intscrollTop);
	
	document.getElementById('p_frame').style.top = -intTop + 'px';
	
	//re-adjust bounds for overflow
	if (intTop < intColumnHeight) 
		document.getElementById('p_clip').style.height = (document.getElementById('p_clip').offsetHeight + (intColumnHeight-intTop))+'px';
		
	document.getElementById('p_frame').style.visibility = 'visible';
	
	//set animation
	intTravel = parseInt(intCenterHeight/2 - document.getElementById('p_img_vr').offsetHeight) + 33;
	
	vanitymation('p_img_vr', 'p_img_vl');

}

function getScrollTop () {
	// adjust for all kinds of IEs
	return (window.pageYOffset ? window.pageYOffset : 
			 	document.documentElement &&	document.documentElement.scrollTop ? document.documentElement.scrollTop :
					document.body.scrollTop ? document.body.scrollTop : 0);
}

// because we are using relative positioning
// we need to reset loads of stuff
function closePopUp(strObj) {
	var obj = document.getElementById(strObj);
	document.getElementById('p_tr2').innerHTML = '&nbsp;';
	document.getElementById('p_tr2').style.height = 'auto';
	document.getElementById('p_tr2').style.width = 'auto';
	document.getElementById('p_clip').style.height = 'auto';
	obj.style.top = '0px';
	document.getElementById('p_img_vr').style.top = '0px';
	document.getElementById('p_img_vl').style.top = '0px';
	document.getElementById('p_frame').style.height = '0px';
	obj.style.visibility = 'hidden';
	obj.style.display = 'none';
	clearInterval(hwndMotion);
	hwndMotion = null;
	index = 0;
	intDelta = 0;	
		//obj.style.display = 'block';
	//obj.style.visibility = 'visible';
}

//vanity related
var intTravel = 0;
var intDelta = 0;
var index = 0;
var motionLUT = [0, 0, 0, 0.4, 0.133, 0.133, 0.133, 0.06, 0.03, 0.015];
var hwndMotion = null;

function vanitymation() {
	var vanity_obj = [];
	for(var i=0; i<arguments.length; i++)
      vanity_obj[i] = (document.getElementById(arguments[i] ))  ? (document.getElementById(arguments[i] )) : null;

	hwndMotion = setInterval(function () {timed(vanity_obj);}, 33);	  

}

function timed(arrObj) {
	intDelta += parseInt(intTravel*motionLUT[index]);

	arrObj[0].style.top = arrObj[1].style.top = intDelta + 'px';
	
	index++;
	if (index > motionLUT.length-1) {
		clearInterval(hwndMotion);
		hwndMotion = null;
		index = 0;
		intDelta = 0;
	}
}

// ajax related
function execInlineAjaxCall(strPostData) {
  var xmlHttp;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e) {
    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e) {
      try {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e) {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
	
	
    xmlHttp.onreadystatechange=function() {
      if(xmlHttp.readyState==4) {
			
			//user error messages
			var re = /error/ig;
			
			//server/client error messages
			var status_re = /[4-5][0-9]{2}/;
			
			//DEBUG
			//alert(xmlHttp.responseText);
			//notifyInline(xmlHttp.responseText);	
			//alert(xmlHttp.status);
			
			if (status_re.exec(xmlHttp.status)) {
				notifyInline(xmlHttp.status + ': ' + xmlHttp.statusText);
				return;
			}
			
			if (re.exec(xmlHttp.responseText)) {
				notifyInline(xmlHttp.responseText);
			}
			else {
				notifyInline(xmlHttp.responseText);
			}
      }
    }
	
    xmlHttp.open("POST",strInlineRenderer,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset:UTF-8");
    xmlHttp.setRequestHeader("Content-length", strPostData.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(strPostData);	
 }
 
 function notifyInline(strMsg) {
	 //alert(strMsg);
	 strInline = strMsg;
	 activateScripts();
 }

