/********************************************************
For more info & download: http://www.ibegin.com/blog/p_ibox.html
Created for iBegin.com - local search done right
MIT Licensed Style
*********************************************************/
//var indicator_img_path = "/static/elsevier/ibox/images/indicator.gif";
var indicator_img_path = "/static/elsevier/ibox/images/indicator.gif";
var indicator_img_html = "<img name=\"ibox_indicator\" src=\""+indicator_img_path+"\" alt=\"Loading...\" style=\"width:128px;height:128px;\"/>"; // don't remove the name

function init_ibox() {
	var elem_wrapper = "ibox";

	createIbox(document.getElementsByTagName("body")[0]); //create our ibox

	//	elements here start the look up from the start non <a> tags
	//var docRoot = (document.all) ? document.all : document.getElementsByTagName("*");

	// Or make sure we only check <a> tags
	var docRoot = document.getElementsByTagName("a");
	var ibAttr = "rel"; 	// our attribute identifier for our iBox elements

	var e;
	for (var i = 0; i < docRoot.length - 1; i++) {
			e = docRoot[i];
			if(e.getAttribute(ibAttr)) {
				var t = e.getAttribute(ibAttr);
				if ((t.indexOf("ibox") != -1)  ||  t.toLowerCase() == "ibox") { // check if this element is an iBox element
						e.onclick = function() { // rather assign an onclick event
							var t = this.getAttribute(ibAttr);
							var params = parseQuery(t.substr(5,999));
							var url = this.href;
							if(this.target != "") {url = this.target}
							var title = this.title;
							showBG();
							showIbox(url,title,params);	// show ibox
							window.onscroll = maintPos;
							window.onresize = maintPos;
							return false;
						};

				}
			}
     }

}

showBG = function() {
	var box_w = getElem('ibox_w');

	box_w.onclick = function() {hideIbox();}

	var opacity_level = 4;/* How light or dark (1-10) should the bg be? */
	box_w.style.opacity = 0;
	box_w.style.filter = 'alpha(opacity=0)';

	setBGOpacity = setOpacity;
	for (var i=0;i<=opacity_level;i++) {setTimeout("setIboxOpacity('ibox_w',"+i+")",70*i);} // from quirksmode.org

	box_w.style.display = "";
	var pagesize = new getPageSize();
	var scrollPos = new getScrollPos();
	var ua = navigator.userAgent;
	if(ua.indexOf("MSIE ") != -1) {box_w.style.width = pagesize.width+'px';}
	else {box_w.style.width = pagesize.width-20+'px';}
	box_w.style.height = pagesize.height+scrollPos.scrollY+'px';

	selectVisibility("hidden");
}

/* Scrollbar hiding by Heidi http://liquidlead-art.com/ */
selectVisibility = function(v) {
	var selectElems = document.getElementsByTagName('select');
	for(var i = 0; i < selectElems.length; ++i) {
		selectElems[i].style.visibility = v;
	}
}

hideBG = function() {
	var box_w = getElem('ibox_w');
	box_w.style.display = "none";
	selectVisibility("visible");
}

var loadCancelled = false;
showIndicator = function() {
	var ibox_p = getElem('ibox_progress');
	ibox_p.style.display = "";
	posToCenter(ibox_p);
	ibox_p.onclick = function() {hideIbox();hideIndicator();loadCancelled = true;}
}


hideIndicator = function() {
	var ibox_p = getElem('ibox_progress');
	ibox_p.style.display = "none";
	ibox_p.onclick = null;
}

createIbox = function(elem) {
	// a trick on just creating an ibox wrapper then doing an innerHTML on our root ibox element
	var strHTML = "<div id=\"ibox_w\" style=\"display:none;\"></div>";
	strHTML +=	"<div id=\"ibox_progress\" style=\"display:none;\">";
	strHTML +=  indicator_img_html;
	strHTML +=  "</div>";
	strHTML +=	"<div id=\"ibox_wrapper\" style=\"display:none\">";
	strHTML +=	"<div id=\"ibox_content\"></div>";
	strHTML +=	"<div id=\"ibox_footer_wrapper\">";
	strHTML +=  "<div id=\"ibox_footer\">&nbsp;</div></div></div></div>";

	var docBody = document.getElementsByTagName("body")[0];
	var ibox = document.createElement("div");
	ibox.setAttribute("id","ibox");
	ibox.style.display = '';
	ibox.innerHTML = strHTML;
	elem.appendChild(ibox);
}

var ibox_w_height = 0;
showIbox = function(url,title,params) {
var ibox = getElem('ibox_wrapper');
var ibox_type = 0;

	// set title here
	var ibox_footer = getElem('ibox_footer');
	if(title != "") {ibox_footer.innerHTML = title;} else {ibox_footer.innerHTML = "&nbsp;";}

	url = url.toLowerCase(); // have to lowercase

	// file checking code borrowed from thickbox
	//var urlString = /\.jpg|\.jpeg|\.png|\.gif|\.html|\.htm|\.php|\.cfm|\.asp|\.aspx|\.jsp|\.jst|\.rb|\.txt/g;
	var urlString = /\.jpg|\.jpeg|\.png|\.gif|\.html|\.htm|\.php|\.cfm|\.asp|\.aspx|\.jsp|\.jst|\.rb|\.txt|web\/show/g;
	var urlType = url.match(urlString);

	if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif'){
		ibox_type = 0;


		showIndicator();

		var imgPreloader = new Image();

		imgPreloader.onload = function(){

			imgPreloader = resizeImageToScreen(imgPreloader);
			hideIndicator();

			getElem('ibox_content').style.overflow = "hidden";

			var strHTML = "<a href=\"javascript:void(null);\"><img name=\"ibox_img\" src=\""+url+"\" style=\"width:"+imgPreloader.width+"px;height:"+imgPreloader.height+"px;border:0;\"/></a>";

			if(loadCancelled == false) {
				// set width and height
				ibox.style.height = imgPreloader.height+'px';
				ibox.style.width = imgPreloader.width+'px';
				ibox.style.display = "";
				ibox.style.visibility = "hidden";
				posToCenter(ibox);
				ibox.style.visibility = "visible";
				setIBoxContent(strHTML);
			}

		}

		loadCancelled = false;
		imgPreloader.src = url;



	} else if(url.indexOf("#") > 0) {
			var strHTML = "";
			ibox_type = 1;

			if(params['height']) {ibox.style.height = params['height']+'px';}
			else {ibox.style.height = '525px';}

			if(params['width']) {ibox.style.width = params['width']+'px';}
			else {ibox.style.width = '630px';}

			ibox.style.display = "";
			ibox.style.visibility = "hidden";
			posToCenter(ibox);
			ibox.style.visibility = "visible";

			var elemSrcId = url.substr(url.indexOf("#") + 1,1000);
			var elemSrc = getElem(elemSrcId);

			if(elemSrc) {
				strHTML = elemSrc.innerHTML;
			}

			setIBoxContent(strHTML);

	}else if(urlType=='.htm'||urlType=='.html'||urlType=='.php'||
			 urlType=='.asp'||urlType=='.aspx'||urlType=='.jsp'||
			 urlType=='.jst'||urlType=='.rb'||urlType=='.txt'||
			 urlType=='.cfm'||urlType=='web\/show') {


			ibox_type = 2;

			showIndicator();
			http.open('get',url,true);

			http.onreadystatechange = function() {
				if(http.readyState == 4){
					hideIndicator();

					if(params['height']) {ibox.style.height = params['height']+'px';}
					else {ibox.style.height = '525px';}

					if(params['width']) {ibox.style.width = params['width']+'px';}
					else {ibox.style.width = '630px';}

					ibox.style.display = "";
					ibox.style.visibility = "hidden";
					posToCenter(ibox);
					ibox.style.visibility = "visible";

					var response = http.responseText;
					//hv.mutated by gx:
					//setIBoxContent(response);
					gxSetIBoxContentPre(response);

					gxCreateRubricList();

					gxSetIBoxContentPost();

					var iboxc = document.getElementById("ibox_content");
					try{
						if (iboxc.scrollHeight && ibox.offsetHeight){
							var intScroll = parseInt(iboxc.scrollHeight, 10);
							if (intScroll > parseInt(ibox.offsetHeight, 10)){
								ibox.style.height = intScroll + 'px';
							}
						}
					}
					catch(err){}
					//end.mutated by gx
				}
			}

			http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
			http.send(null);
	}

	ibox.style.opacity = 0;
	ibox.style.filter = 'alpha(opacity=0)';
	var ibox_op_level = 10;

	setIboxOpacity = setOpacity;
	for (var i=0;i<=ibox_op_level;i++) {setTimeout("setIboxOpacity('ibox_wrapper',"+i+")",30*i);}

	/*
	if(ibox_type == 1 || ibox_type == 2) {
		ibox.onclick = null;getElem("ibox_close_a").onclick = function() {hideIbox();}
	} else {
		ibox.onclick = hideIbox;getElem("ibox_close_a").onclick = null;
	}
	*/
}

setOpacity = function (elemid,value)	{
		var e = getElem(elemid);
		e.style.opacity = value/10;
		e.style.filter = 'alpha(opacity=' + value*10 + ')';
}

resizeImageToScreen = function(objImg) {


	var pagesize = new getPageSize();

	var x = pagesize.width - 100;
	var y = pagesize.height - 100;

	if(objImg.width > x) {
		objImg.height = objImg.height * (x/objImg.width);
		objImg.width = x;
		if(objImg.height > y) {
			objImg.width = objImg.width * (y/objImg.height);
			objImg.height = y;
		}
	}

	else if(objImg.height > y) {
		objImg.width = objImg.width * (y/objImg.height);
		objImg.height = y;
		if(objImg.width > x) {
			objImg.height = objImg.height * (x/objImg.width);
			objImg.width = x;
		}
	}

	return objImg;
}

maintPos = function() {
	var ibox = getElem('ibox_wrapper');
	var box_w = getElem('ibox_w');
	var pagesize = new getPageSize();
	var ua = navigator.userAgent;

	if(ua.indexOf("MSIE ") != -1) {box_w.style.width = pagesize.width+'px';}
	else {box_w.style.width = pagesize.width-20+'px';}

	if(ua.indexOf("Opera/9") != -1) {box_w.style.height = document.body.scrollHeight+'px';}
	else {box_w.style.height = document.body.scrollHeight+50+'px';}
	posToCenter(ibox);
}

hideIbox = function() {
	hideBG();
	var ibox = getElem('ibox_wrapper');
	ibox.style.display = "none";

	clearIboxContent();
	window.onscroll = null;
}

posToCenter = function(elem) {
	var scrollPos = new getScrollPos();
	var pageSize = new getPageSize();
	var emSize = new getElementSize(elem);

	var x = Math.round(pageSize.width/2) - (emSize.width /2) + scrollPos.scrollX;
	var y = Math.round(pageSize.height/2) - (emSize.height /2) + scrollPos.scrollY;
	elem.style.left = x+'px';
	elem.style.top = y+'px';
}

getScrollPos = function() {
	var docElem = document.documentElement;
	this.scrollX = self.pageXOffset || (docElem&&docElem.scrollLeft) || document.body.scrollLeft;
	this.scrollY = self.pageYOffset || (docElem&&docElem.scrollTop) || document.body.scrollTop;
}

getPageSize = function() {
	var docElem = document.documentElement
	this.width = self.innerWidth || (docElem&&docElem.clientWidth) || document.body.clientWidth;
	this.height = self.innerHeight || (docElem&&docElem.clientHeight) || document.body.clientHeight;
}

getElementSize = function(elem) {
	this.width = elem.offsetWidth ||  elem.style.pixelWidth;
	this.height = elem.offsetHeight || elem.style.pixelHeight;
}

setIBoxContent = function(str) {
	clearIboxContent();
	var e = getElem('ibox_content');
	e.innerHTML = str;
	//e.style.overflow = "auto";

	/* callBack to general.js some functions should start now! */
	iBoxCallback();

}
clearIboxContent = function() {
	var e = getElem('ibox_content');
	e.innerHTML = "";
	var ua = navigator.userAgent;
	if(ua.indexOf("MSIE ") == -1){
		e.style.overflow = "hidden";
	}
}

getElem = function(elemId) {
	return document.getElementById(elemId);
}

// parseQuery code borrowed from thickbox, Thanks Cody!
parseQuery = function(query) {
   var Params = new Object ();
   if (!query) return Params;
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;

   }

   return Params;
}

/********************************************************
 Make this IE7 Compatible ;)
 http://ajaxian.com/archives/ajax-on-ie-7-check-native-first
*********************************************************/
createRequestObject = function() {
	var xmlhttp;
		/*@cc_on
	@if (@_jscript_version>= 5)
			try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
					try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
					catch (E) {xmlhttp = false;}
			}
	@else
		xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != "undefined") {
			try {xmlhttp = new XMLHttpRequest();} catch (e) {xmlhttp = false;}
	}
	return xmlhttp;
}

var http = createRequestObject();

/*
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
   obj.addEventListener(evType, fn, false);
   return true;
 } else if (obj.attachEvent){
   var r = obj.attachEvent("on"+evType, fn);
   return r;
 } else {
   return false;
 }
}
*/

/* Init on load */
$(window).bind("load",init_ibox);

/********************************************************
Added by GX:
*********************************************************/
gxSetIBoxContentPre = function(str) {
	clearIboxContent();
	var e = getElem('ibox_content');
	e.innerHTML = str;
	//e.style.overflow = "auto";
}

gxSetIBoxContentPost = function() {
	/* callBack to general.js some functions should start now! */
	iBoxCallback();

}

function gxIBoxWriteSettings() {
	var col = document.getElementsByName("hdn_popup_rubriek_id");
	var astrNewValue = new Array();
	for (var i=0; i < col.length;i++){
		//alert(col[i].value);
		var strId=col[i].value;
		var elmInpShow = document.getElementById("hdn_popup_rubriek_show_"+strId);
		var strShow = elmInpShow? elmInpShow.value: "1";
		var elmInpItems = document.getElementById("hdn_popup_rubriek_items_"+strId);
		var strItems = elmInpItems? elmInpItems.value: "3";
		astrNewValue[i*3]=strId;
		astrNewValue[(i*3)+1]=strShow;
		astrNewValue[(i*3)+2]=strItems;
	}

	var strNewValue = astrNewValue.join(",");

	//set values in hidden field:
	var elm = document.getElementById("hdn_listvalues");
	if(elm){
		elm.value=strNewValue;
	}

	//set cookie:
	gxSetCookie(strNewValue);

	//show changes:
	gxShowUserSetting();

	//close popup:
	hideIbox();
}

function gxToggleShow(myObject){
	if (myObject){
		var strId = myObject.id;
		if (strId) {
			var elmInpShow = document.getElementById("hdn_popup_rubriek_show_"+strId);
			if (elmInpShow){
				elmInpShow.value=myObject.checked?"1":"0";
			}
		}
	}
}

function gxMoveUp(){
	if (this){
		var strCmd = this.id;
		var strPref = "cmdUp_";
		if (strCmd.indexOf(strPref)==0){
			var strId = strCmd.substr(strPref.length);
			var elmP = document.getElementById("p_popup_"+strId);
			var elm = document.getElementById(strId);
			var blnChecked = elm? elm.checked: false;
			var elmTarget = document.getElementById("rubrieken_volgorde");

			if (elmTarget && elmP && elmP.previousSibling){
				elmTarget.insertBefore(elmP, elmP.previousSibling);
			}
			if (elm){
				elm.checked = blnChecked;
			}
		}
	}
}

function gxMoveDown(){
	if (this){
		var strCmd = this.id;
		var strPref = "cmdDown_";
		if (strCmd.indexOf(strPref)==0){
			var strId = strCmd.substr(strPref.length);
			var elmP = document.getElementById("p_popup_"+strId);
			var elm = document.getElementById(strId);
			var blnChecked = elm? elm.checked: false;
			var elmTarget = document.getElementById("rubrieken_volgorde");

			if (elmTarget && elmP && elmP.nextSibling && elmP.nextSibling.nextSibling){
				elmTarget.insertBefore(elmP, elmP.nextSibling.nextSibling);
			}
			if (elm){
				elm.checked = blnChecked;
			}
		}
	}
}

/********************************************************
Build dynamic html

<p class="clearfix volgorde_items" id="p_nederland">
	<input type="checkbox" checked="checked" name="volgorde_aanpassen" id="nederland" />
	<a href="#" title="" id="up_nederland" onclick=""><img class="button_omhoog" src="${staticFilesUrl}/stdimg/button_omhoog_small.gif" alt="omhoog" /></a>
	<a href="#" title="" id="down_nederland"><img class="button_omlaag" src="${staticFilesUrl}/stdimg/button_omlaag_small.gif" alt="omlaag" /></a>
	<label for="nederland" class="rubriek">nederland</label>
</p>
*********************************************************/
gxCreateRubricList = function() {
	var elm					= document.getElementById("hdn_listvalues");
	var strListOld	= elm? elm.value: "";
	var astrList		= strListOld.split(",");
	var elmTarget		= document.getElementById("rubrieken_volgorde");
	if (elmTarget){
		for (var i=0; i < astrList.length/3; i++){
			var strId = astrList[i*3];
			if (gxIsRubricPresent(strId)) {
				var blnShow		= parseInt(astrList[(i*3)+1],10)==1;
				var intItems	= parseInt(astrList[(i*3)+2],10);

				var elmP = document.createElement("p");
				elmP.className = "clearfix volgorde_items" + (blnShow? " active":"");
				elmP.id = "p_popup_" + strId;
				elmTarget.appendChild(elmP);

				var elmInp = document.createElement("input");
				elmInp.type = "checkbox";
				elmInp.name = "volgorde_aanpassen";
				elmInp.id = strId;
				elmP.appendChild(elmInp);
				elmInp.checked = blnShow;

				var aUp = document.createElement("a");
				//aUp.href="#";
				aUp.id = "cmdUp_" + strId;
				aUp.title="";
				aUp.onclick = gxMoveUp;
				elmP.appendChild(aUp);

				var imgUp = document.createElement("img");
				imgUp.className = "button_omhoog";
				imgUp.src = staticfilesurl + "/stdimg/button_omhoog_small.gif";
				imgUp.alt = "omhoog";
				imgUp.style.marginRight="0.1em";
				aUp.appendChild(imgUp);

				var aDown = document.createElement("a");
				//aDown.href="#";
				aDown.id = "cmdDown_" + strId;
				aDown.title="";
				aDown.onclick = gxMoveDown;
				elmP.appendChild(aDown);

				var imgDown = document.createElement("img");
				imgDown.className = "button_omlaag";
				imgDown.src = staticfilesurl + "/stdimg/button_omlaag_small.gif";
				imgDown.alt = "omlaag";
				imgDown.style.marginLeft="0.1em"
				aDown.appendChild(imgDown);

				var elmTitle = document.getElementById("hdn_rubriek_title_"+strId);
				var strTitle = strId;
				if (elmTitle && elmTitle.value && elmTitle.value != ""){
					strTitle = elmTitle.value;
				}

				var label = document.createElement("label");
				label.className = "rubriek";
				label.htmlFor = strId;
				elmP.appendChild(label);
				var text = document.createTextNode(strTitle);
				label.appendChild(text);

				var elmInpId = document.createElement("input");
				elmInpId.type = "hidden";
				elmInpId.name = "hdn_popup_rubriek_id";
				elmInpId.id = "hdn_popup_rubriek_id";
				elmInpId.value = strId;
				label.appendChild(elmInpId);

				var elmInpShow = document.createElement("input");
				elmInpShow.type = "hidden";
				elmInpShow.name = "hdn_popup_rubriek_show";
				elmInpShow.id = "hdn_popup_rubriek_show_"+strId;
				elmInpShow.value = blnShow?"1":"0";
				label.appendChild(elmInpShow);

				var elmInpItems = document.createElement("input");
				elmInpItems.type = "hidden";
				elmInpItems.name = "hdn_popup_rubriek_items";
				elmInpItems.id = "hdn_popup_rubriek_items_"+strId;
				elmInpItems.value = intItems;
				label.appendChild(elmInpItems);
			}
		}
		var elmSave = document.getElementById("p_popupSave");
		if (elmSave){
			elmTarget.appendChild(elmSave);
		}
	}
}
