// service routines
// *****************************************************************************
brIE = "Microsoft Internet Explorer"; brNS = "Netscape"; brOP = "Opera"; brN = "undefined"; brM = 1 ; brV=0;
if (navigator.appName == brIE) {brN = "IE"; brV = parseFloat(navigator.appVersion.substr (navigator.appVersion.lastIndexOf("MSIE") + 4, 4)); brM = 1;}
if (navigator.appName == brNS) {brN = "NS"; brV = parseFloat(navigator.appVersion.substr(0,4)); brM = 2;}
if (navigator.appName == brOP) {brN = "OP"; brV = parseFloat(navigator.appVersion.substr(0,4)); brM = 1;}
if (brN == 'NS' && brV >= 5) {brM = 3;}

var PopupArray = new Array()
var PopupModes = new Array()
var PopupCnt = 0; 
var isBacking = false;

function errorHandler(message, url, line) {
   return true;
}

function doBack() {
	if (PopupCnt>1) {
		  PopupCnt=PopupCnt-1;
			isBacking = true;
	    if(PopupModes[PopupCnt]==1) { LinkIt(PopupArray[PopupCnt], 3); } else {ShowProject(PopupArray[PopupCnt]);}
			isBacking = false;
		} else {
			PopupCnt = 0
		  HideModalPopup()	
	  }
}

var popUpWin=0;
function popUpImage(URLStr)
{
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open("popup.asp?url=" + URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=640,height=480,top=0,left=0,screenX=,screenY=0');
}

function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function setCookie(name,value,hours) {
  var expire = "";
  expire = new Date((new Date()).getTime() + hours * 3600000);
  expire = "; expires=" + expire.toGMTString();
  document.cookie = name + "=" + escape(value) + expire;
  return true;
}

function GetObj(theObj, theDoc) {  
  var p, i, foundObj;
	foundObj = null;
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  return foundObj;
}

function SetVis() { 
  var i,p,v,obj,args=SetVis.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=GetObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}	

function Show(TheID) { SetVis(TheID,'','show') }  
function Hide(TheID) { SetVis(TheID,'','hide') }  

function GetX(o) {
  if(brN == 'NS') {
    if(brV == 5) return(parseInt(o.style.left));
    if(brV == 4) return(o.left);
  } else { return(o.style.pixelLeft); }
}

function GetY(o) {
  if(brN == 'NS') {
		if(brV == 5) return(parseInt(o.style.top));
		if(brV == 4) return(o.top);
  } else { return(o.style.pixelTop); }
}

function GetW(o) {
if(brN == 'NS') {
		if(brV == 5) return(o.offsetWidth);		     
    if(brV == 4) return(o.clip.width);                      
  } else { 
	  if(navigator.appVersion.indexOf('MSIE 4') > 0) return(o.style.pixelWidth);
		else return(o.offsetWidth); 
	}
}
    

function getImageWidth(myImage) {
	var x, obj;
	if (document.layers) {
		var img = getImage(myImage);
		return img.width;
	} else {
		return getElementWidth(myImage);
	}
	return -1;
}

function Move(x, y, o) {
  if(brN == 'NS') {
    if(brV == 5) { o.style.left = x + "px"; o.style.top = y + "px"; }
    if(brV == 4) { o.left = x + "px"; o.top = y + "px";}
  } else {	
    o.style.pixelLeft = x; o.style.pixelTop = y; 
	}
}

function Size(x, y, o) {
  if(brN == 'NS') {
    if(brV == 5) { o.style.width = x + "px"; o.style.height = y + "px";}
    if(brV == 4) { o.width = x + "px"; o.height = y + "px"; }
  } else { o.style.width = x; o.style.height = y; }
}

var winW = 630, winH = 460;

function GetGeo() {
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    winW = window.innerWidth;
    winH = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    winW = document.documentElement.clientWidth;
    winH = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    winW = document.body.clientWidth;
    winH = document.body.clientHeight;
  }
}

function init() {
	window.onerror = errorHandler;
	MaximizeWnd();
	LastMenu = getCookie("LastMenu");
	if (!LastMenu) {LastMenu = 0;}
	if (LastMenu!=0) {OpenMenu(LastMenu);}
	GetGeo();
	Size(38, winH-190, GetObj("imgSizer"));
}

function MaximizeWnd() {
	top.window.moveTo(0,0);
	if (document.all) { top.window.resizeTo(screen.availWidth,screen.availHeight);
	} else if (document.layers||document.getElementById) {
		if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth){
			top.window.outerHeight = screen.availHeight;
			top.window.outerWidth = screen.availWidth;
			}
	}
}

function AlternateImage(TheFile) {
	GetObj("imgSlot").src = TheFile;
}

// menu
// *****************************************************************************

var LastMenu = 0;
var InMenu = 0;

function OpenMenu(TheID) {
	for(var i=1;i < 22; i++) {
		 if (document.getElementById("ms" + i)) {
			 document.getElementById("ms" + i).style.display="none";
			 document.getElementById("ts" + i).className ="MenuTableLo";
		 }
	}
  document.getElementById("ms" + TheID).style.display="inline";
  document.getElementById("ts" + TheID).className ="MenuTableHi";
	LastMenu = TheID;
	setCookie("LastMenu",TheID, 1);
}

function HoverMenu(TheID,TheState) {
	if (TheState) {
		InMenu=TheID
		tmpColor="#666666"
	  if (LastMenu==TheID) {tmpSrc="/images/api/mb-dv.jpg"} else {tmpSrc="/images/api/mb-dh.jpg"}
	} else {
		InMenu=0
	  if (LastMenu==TheID) {tmpSrc="/images/api/mb-lv.jpg"} else {tmpSrc="/images/api/mb-lh.jpg"}
		tmpColor="#979392"
		}
  SetImage("imgMenu" + TheID, tmpSrc);		
	GetObj("trRow" + TheID).style.backgroundColor=tmpColor;
}

function SetImage(ObjName, Url) {
  if (GetObj(ObjName)) {GetObj(ObjName).src=Url}	
}


function LinkIt(TheID, TheKind) {
	
try {
var pageTracker = _gat._getTracker("UA-6438554-1");
pageTracker._trackPageview("id-" + TheID);
} catch(err) {
	
	}


	if (TheKind==3 || TheKind==10 || TheKind==2) {
		ShowModalPopup("ajax.asp?id=" + TheID + '&rnd=' + Math.random(), 760, 400)
		if (isBacking==false) {
			PopupCnt = PopupCnt + 1;
		  PopupArray[PopupCnt] = TheID;
      PopupModes[PopupCnt] = 1;	
		  }
		}

	if (TheKind==7 || TheKind==1 ) {
		document.location.href= "/index.asp?id=" + TheID ;
		}

	if (TheKind==4) {
		ShowModalPopup("ajax.asp?id=" + TheID + '&rnd=' + Math.random(), 700, 452)
		if (isBacking==false) {
			PopupCnt = PopupCnt + 1;
		  PopupArray[PopupCnt] = TheID;
      PopupModes[PopupCnt] = 1;	
		  }
	  }
}

// Projects
// *****************************************************************************

function OpenSearchBox() {
//	alert("search");
}

function SearchEnter(e) {
  var keynum;
  if(window.event) {keynum = e.keyCode;}
  else if(e.which) {keynum = e.which; }
  if (keynum==13) {
		Key=GetObj("key").value;
		if (Key!="") {ListProjects(0, 1, 0, Key);}}
}						 

function ListProjects(TheKind, ThePage, TheWhat, TheKey) {
	setCookie("LastListKind",TheKind, 1);
	setCookie("LastListPage",ThePage, 1);
	setCookie("LastListWhat",TheWhat, 1);
	setCookie("LastListKey",TheKey, 1);
	tmpQuery = "cmd=l&c=" + TheKind + "&pg=" + ThePage + "&what=" + TheWhat + "&key=" + TheKey
	ajaxpage("project.asp?"+ tmpQuery, "divPro");
	setCookie("LastList",tmpQuery, 1);
}

function CallLastList() {
  tmpQuery = "cmd=l&c=" + getCookie("LastListKind") + "&pg=" + getCookie("LastListPage") + "&what=" + getCookie("LastListWhat") + "&key=" + getCookie("LastListKey")
  ajaxpage("project.asp?"+ tmpQuery, "divPro");
}

function SearchKey(TheKey) {
  HideModalPopup()	
	ListProjects(0, 0, 0, TheKey)
}
	
function ShowProject(TheID) {
  ajaxpage("project.asp?cmd=v&id=" + TheID, "divPro");
}

// modal popup
// *****************************************************************************

function ShowModalPopup(TheUrl, TheWidth, TheHeight) {
  popW=TheWidth; popH =TheHeight ; 
  GetGeo();
  Size(winW , winH, GetObj("divDark"));
  Size(popW , popH, GetObj("divPop"));
	Move(winW/2-popW/2 , winH/2-popH/2, GetObj("divPop"));
//	Move(winW/2-popW/2 , winH/2-popH/2, GetObj("divPop"));
  Show("divDark");
  Show("divPop");
  ajaxpage(TheUrl, "divPop")	
}

function HideModalPopup() {
   ajaxpage("/empty.htm", "divPop")	
   Hide("divDark");
   Hide("divPop");
}

// ajax
// *****************************************************************************

var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar)
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
  if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {
		document.getElementById(containerid).innerHTML=page_request.responseText;
		if (GetObj("DivScrollContent")) {ScrollerMax = GetObj("DivScrollContent").offsetHeight - 160;}
		if (ScrollerMax<0) {ScrollerMax=0;}
		if (PopupCnt>1) {	GetObj("imgBack").src = "/images/api/t-back.jpg"; }
    }
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}

// scroller
// *****************************************************************************

var ScrollerPos = 0;
var ScrollerSpeed = 24;
var ScrollerMax = 300;
var ScrollerMouseDownVar;
var ScrollerMouseUpVar;

function ScrollerDown() {
	ScrollerPos = ScrollerPos - ScrollerSpeed;
	if (ScrollerPos<-ScrollerMax) {ScrollerPos=-ScrollerMax};
	Move(1, ScrollerPos, GetObj("DivScrollContent"));
	ScrollerMouseDownVar = setTimeout("ScrollerDown()",200)
}

function ScrollerUp() {
	ScrollerPos = ScrollerPos + ScrollerSpeed;
	if (ScrollerPos>=0) {ScrollerPos=0};
	Move(1, ScrollerPos, GetObj("DivScrollContent"));
	ScrollerMouseUpVar = setTimeout("ScrollerUp()",200)
}


