

//
// HYPERLINK FUNCTION FOR RESULT WINDOW
//

// Array
var g_event = false,incID,decID,el,c;

String.prototype.wordWrap = function(m, b, c){
    var i, j, l, s, r;
    if(m < 1)
        return this;
    for(i = -1, l = (r = this.split("\n")).length; ++i < l; r[i] += s)
        for(s = r[i], r[i] = ""; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : ""))
            j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length
            || c == 1 && m || j.input.length + (j = s.slice(m).match(/^\S*/)).input.length;
    return r.join("\n");
};

String.prototype.stripslashes = function(){
	return (this+'').replace(/\0/g, '0').replace(/\\([\\'"])/g, '$1');
 }

Array.prototype.in_array = function(obj){
	return new RegExp('(^|\,)'+obj+'(\,|$)','gi').test(this);
}

function checkMediaLink(str){
	return new RegExp('(^/http:\/\/www.youtube.com/$)','gi').test(str);
}

function openHyperlink(layer, fldName, fldValue) {
    
    switch(layer) {
        case 'cities':
            //if (fldName == 'CITY_NAME') {
                window.open('http://en.wikipedia.org/wiki/' + fldValue, 'wikiquery');
            //}
            break;
            
        default:
            alert ('See function openHyperlink in custom.js: ' + layer + ' - ' + fldName + ' - ' + fldValue);
    }
}


function collapseExpand(id,elClass,collapseclass,expandclass){ 
	var el =  document.getElementById(id);
	if (el){
		if(el.style.visibility){
			if (el.style.visibility == "visible"){
				el.style.visibility = "hidden";
				el.style.display = "none";
				elClass.className = expandclass; // custom class on element
			}
			else{
				el.style.visibility = "visible";
				el.style.display = "block";
				elClass.className = collapseclass;
			}
		}
		else{
			el.style.visibility = "hidden";
			el.style.display = "none";
			elClass.className = expandclass; // custom class on element
		}
	}
}



function dropdown(id){
	var el =  document.getElementById(id);
	if (el){
		if(el.style.visibility){
			var v = el.style.visibility;
			var d = (v == "visible")?"none":"block";	
			v = (v == "visible")?"hidden":"visible";
			
		}
		else{
			var v = "hidden";
			var d = "none";
		}
		el.style.visibility = v;el.style.display = d;
	}
}


function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}

function closePanel(id){
	var Layer =  document.getElementById(id);
	Layer.style.visibility = "hidden";
	Layer.style.display = "none";	
}

function changePMapSize(todo) {
    if (todo = 'up') {
        Layout.MasterWidth = ''
    } else {
    
    }
}


// AONE MODIFIED

function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}

function closeProcDialog(id){
	closeDialog('MIPRTranslucent',id);
}

function showProcDialog(str,idPanel,idContent){
	
	//document.body.disabled=true;	
	var x = document.getElementById('MIPRTranslucent');
    if (x == null){
		var arrMatrices = getPageSizeWithScroll();
		var divBg = document.createElement("div");
		divBg.id = "MIPRTranslucent";
		document.body.appendChild(divBg);
		if (is_ie){ 
			divBg.style.position = "absolute";
			divBg.style.left = "0px";
			divBg.style.top = "0px";	
			divBg.style.width = arrMatrices[0]+"px";
			divBg.style.height = arrMatrices[1]+"px";
		}
		else
			divBg.className = "transOther";	
			
		
		divBg.innerHTML = "<!--[if lte IE 6.5]><iframe></iframe><![endif]-->";
		
	}

	x = document.getElementById(idPanel);
    if (x == null){
		var divDialog = document.createElement("div");
		divDialog.className = idPanel;
		divDialog.id = idPanel;
		document.body.appendChild(divDialog);
	}

	if(divDialog){
		divDialog.innerHTML = "<div class=\"MIPRInfoPanelBorder\"><div id=\"MIPRInfoPanelCaption\" class=\"MIPRInfoPanelCaption\"><div class=\"MIPRInfoPanelClose\"><span class=\"MIPRInfoPanelButtonClose\" onclick=\"closeDialog('MIPRTranslucent','"+idPanel+"')\">&nbsp;&nbsp;&nbsp;</span><span class=\"MIPRInfoPanelTitle\">"+str+"</span></div></div><div id=\""+idContent+"\" class=\"MIPRInfoPanelContent\"><div id=\"MIPRInfoPanelLoading\">Loading ...</div></div></div></div></div>";
		centerLayer(divDialog);
		var caption = document.getElementById("MIPRInfoPanelCaption");
		if (caption){
			setContentPanelInfo(idPanel);
			caption.onmousedown = doCaptionPanelMouseDown;
			caption.onmouseup = doCaptionPanelMouseUp;
			caption.onmousemove = doCaptionPanelMouseMove;
		}
		
	}
	
}



function showProcDialogPos(str,parentobj,idPanel,idContent){
	
	x = document.getElementById(idPanel);
    if (x == null){
		var divDialog = document.createElement("div");
		divDialog.className = idPanel;
		divDialog.id = idPanel;
		document.body.appendChild(divDialog);
	}

	if(divDialog){
		divDialog.innerHTML = "<div class=\"MIPRInfoPanelBorder\"><div class=\"MIPRInfoPanelClose\"><span class=\"MIPRInfoPanelButtonClose\" onclick=\"closeDialog('MIPRTranslucent','"+idPanel+"')\">&nbsp;&nbsp;&nbsp;</span><span class=\"MIPRInfoPanelTitle\">"+str+"</span></div><div id=\""+idContent+"\" class=\"MIPRInfoPanelContent\"><div id=\"MIPRInfoPanelLoading\">Loading ...</div></div></div></div></div>";
		setsearchpos(divDialog,parentobj);
	}

	
}

function setsearchpos(child,parent){
	var ch = (typeof(child) == "string")?document.getElementById(child):child;
	var prnt = (typeof(parent) == "string")?document.getElementById(parent):parent;
	ch.style.display = "block";
	ch.style.visibility = "visible";
	// Bottom - Align Right
	ch.style.left = findPosX(prnt) + prnt.clientWidth - ch.clientWidth+30+"px";
	ch.style.top = findPosY(prnt) + prnt.clientHeight+20+"px";	
}

function centerLayer(layer){
    var clientH=0,clientW=0;
    var offsetT=0, offsetL=0; 
    var top=0, left=0;
    if (typeof layer == "string")
        layer=document.getElementById(layer);
        if(layer){
            var parent=layer.parentNode;
                if(parent && parent.nodeType==1){
                    
					
					if ( typeof( window.innerWidth ) == 'number' ) {
				        //Non-IE
				        clientW = window.innerWidth;
				        clientH = window.innerHeight;
				    } else if( document.documentElement && ( document.documentElement.clientWidth 				|| document.documentElement.clientHeight ) ) {
				        //IE 6+ in 'standards compliant mode'
				        clientW = parseInt(document.documentElement.clientWidth);
				        clientH = parseInt(document.documentElement.clientHeight);
				    } else if( document.body && ( document.body.clientWidth || document.body.				clientHeight ) ) {
				        //IE 4 compatible
				        clientW = document.body.clientWidth;
				        clientH = document.body.clientHeight;
				    }                    
					
					offsetT=document.body.scrollTop; 
                    offsetL=document.body.scrollLeft;
                    top=Math.abs(parent.offsetTop + ((clientH/2) - (layer.offsetHeight/2)));
                    left=Math.abs(parent.offsetLeft + ((clientW/2) - (layer.offsetWidth/2)));
                    layer.style.top=top+'px';
                    layer.style.left=left+'px';
                };
        }
}

function closeDialog(bg,dlg){
    x = document.getElementById(dlg);
	if (x != null)
		document.body.removeChild(x);
    y = document.getElementById(bg);
    if (y != null)
		document.body.removeChild(y);
}

function changeTab(activeTab,numTab,nameTab,lnameTab,classname){
    for(var i=1;i<=numTab;i++){
        l = document.getElementById(nameTab + i);
        l.style.display = "none";
        l.style.visibility = "hidden";
        lst = document.getElementById(lnameTab + i);
        lst.className = '';
    }
    x = document.getElementById(nameTab + activeTab);
    x.style.display = "block";
    x.style.visibility = "visible";
    lst = document.getElementById(lnameTab + activeTab);
    lst.className = classname;
}

function changeInfoTab(activeTab,numTab,nameTab,lnameTab,classNameActive,classNameNormal){
    for(var i=0;i<=numTab;i++){
        l = document.getElementById(nameTab + i);
        l.style.display = "none";
        l.style.visibility = "hidden";
        lst = document.getElementById(lnameTab + i);
        lst.className = classNameNormal;
    }
    x = document.getElementById(nameTab + activeTab);
    x.style.display = "block";
    x.style.visibility = "visible";
    lst = document.getElementById(lnameTab + activeTab);
    lst.className = classNameActive;
}


function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      
	  for(var i=0; i<document.images.length; i++)
      {
        
		 var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}


function FadeHz(id,content){
	var el = document.getElementById(id);
	var c = document.getElementById(content);
	
	if (el.style.visibility == ""){
		c.style.visibility = "hidden";
		el.style.visibility = "visible";
		el.style.display = "block";	
		el.style.width = 0+"px";
		incID = setInterval("incWidth('"+id+"','"+content+"')",1);
	}
	else{
		
		var v = el.style.visibility;
		var d = el.style.display;
		if ( v == "hidden"){
			c.style.visibility = "hidden";
			el.style.visibility = "visible";
			el.style.display = "block";	
			el.style.width = 0+"px";
			incID = setInterval("incWidth('"+id+"','"+content+"')",1);
		}		
		else{
			c.style.visibility = "hidden";
			incID = setInterval("decWidth('"+id+"','"+content+"')",1);	
		}
		
	}
}

function incWidth(id,content){
	if (!el)
		var el = document.getElementById(id);
	var i = parseInt(el.style.width) + 10;
	el.style.width = i+"px";
	if (i >=260) {
		window.clearInterval(incID);
		if (!c)
			c = document.getElementById(content);
		c.style.visibility = "visible";
	}
}

function decWidth(id,content){
	if (!el)
		var el = document.getElementById(id);
	var i = parseInt(el.style.width) - 10;
	el.style.width = i+"px";
	if (i <= 0) {
		window.clearInterval(incID);
		if(!c)
			c = document.getElementById(content);
		c.style.visibility = "hidden";
		el.style.visibility = "hidden";
		el.style.display = "block";
	}
}

function showInfoDialog(title,url){
	showProcDialog(title,"MIPRIdentifyPanel","infoZone");
	var req = new AjaxRequest(); 
	req.loadTextDoc(url,'getInfoText');
}



function showMapData(title,url){
	var height  = 400;
	var width = 2*height;
	window.open(title,url,"scrollbars=yes,resizable=1,width=800, height=300");
}

function showMapData2(title,url){
	var height  = 400;
	window.open(title,url,"scrollbars=yes,resizable=1,width=780, height=800");
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
	while(1)
	{
	  curtop += obj.offsetTop;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
	}
else if(obj.y)
	curtop += obj.y;
return curtop;
}




