// JavaScript Document
var mapsloaded=false;
var map;
var mapmarkers=new Array();
var mapmarkersinfo=new Array();
var paths=new Array();
var pathsmarkers=new Array(null,
						new Array(140,139,138,161,145,106,107,113,159,160,148,147,101,100,149,103,150,112,104,137,136,158,157,156,154,155),
						new Array(122,123,124,115,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,117,116,118),
						new Array(140,139,132,133,134,135,136,137,158,157,156,155,154,105,151,110,162,159,160,113,107,106,145,143,109,142,144),
						new Array(142,144,109,143,145,106,107,108,114,146,102,148,147,101,100,149,112,104,150,103,105,151,110,153,152)
						);
var baseIcon=new Array();
var shownpath=null;
var mapcenter,mapzoomlevel;
var alreadyloaded=false;

function loadmaps() {
	if (!mapsloaded) {
		google.load("maps", "2.x", {"language" : lang });
		//dynamicly load mymarker
		/*var fileref=document.createElement('script');
		fileref.setAttribute("type","text/javascript");
		fileref.setAttribute("src", "/inc/mymarker.js");
		document.getElementsByTagName("head")[0].appendChild(fileref);
		*/
		mapsloaded=true;
		
	}
}
loadmaps();

google.load("swfobject", "2.2");

var pagebodyFade;
var mailform_submit;

function onPageLoadFunc() {

	pagebodyFade=new fadeInOut("pagebody","loading");
	pagebodyFade.fadeIn();
	//create div;
	alreadyloaded=true;
}

function onPageUnLoadFunc() {
		//destroy any maps or whatever
	GUnload();
}

function urlpopup(url) {
	window.open(url,'_blank','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, width=800, height=600, left=50, top=50');
	//return false;
}


function fadeInOut(tElementId,tLoadingId) {
	this.tObject=document.getElementById(tElementId);
	this.lObject=document.getElementById(tLoadingId);
	this.lObject.style.width=this.tObject.scrollWidth;
	this.lObject.style.left=this.tObject.style.left;
	this.lObject.style.top=this.tObject.style.top;
	if (this.tObject.scrollHeight<this.tObjectminheight) {this.tObject.style.height=this.tObjectminheight;}
}


fadeInOut.prototype.oAmount = 1;
fadeInOut.prototype.working =0;
fadeInOut.prototype.targetDivOpacity = 100;
fadeInOut.prototype.divOpacity=10;
fadeInOut.prototype.steps=5;
fadeInOut.prototype.tObject = 0;
fadeInOut.prototype.tObjectminheight=500;
fadeInOut.prototype.lObject = 0;
fadeInOut.prototype.intervalInMs = 10;
fadeInOut.prototype.dir = 0;

fadeInOut.prototype.setOpacity = function(objectOpacity) {
	if (document.all){
		this.tObject.filters.alpha.opacity = objectOpacity;
		this.lObject.filters.alpha.opacity = 100-objectOpacity;
	} else if (!document.all && document.getElementById) {		
		this.tObject.style.MozOpacity = objectOpacity/100;
		this.lObject.style.MozOpacity = (100-objectOpacity)/100;
		this.tObject.style.opacity = objectOpacity/100;
		this.lObject.style.opacity = (100-objectOpacity)/100;		
	}
}

fadeInOut.prototype.getOpacity  = function() {
	objectOpacity=100;
	if (document.all){
		objectOpacity=this.tObject.filters.alpha.opacity ;
	} else if (!document.all && document.getElementById) {		
		objectOpacity =this.tObject.style.MozOpacity *100;
	}
	return objectOpacity;
}


fadeInOut.prototype.calcSteps = function() {
	this.oAmount=Math.round((this.targetDivOpacity-this.divOpacity)/this.steps);
}


fadeInOut.prototype.fade = function() {
	var instant = this;
	this.divOpacity = this.divOpacity + this.oAmount
	if ((( this.divOpacity < this.targetDivOpacity) && (this.oAmount>0)) || 
				(( this.divOpacity > this.targetDivOpacity) && (this.oAmount<0)))  {
		this.setOpacity(this.divOpacity);
		
		setTimeout(function() { instant.fade(); },this.intervalInMs);
		this.working=1;
	} else {
		this.setOpacity(this.targetDivOpacity);
		this.working=0;
		if (this.dir) {this.lObject.style.zIndex=-1;}
	}
}

fadeInOut.prototype.fadeIn = function() {
	var instant = this;	
	if (!this.working) {
		this.working=1;
		this.targetDivOpacity = 100;
		this.tObject.style.visibility="visible";
		this.divOpacity=this.getOpacity();
		this.lObject.style.height=this.tObject.scrollHeight ;
		this.lObject.style.zIndex=1;
		this.dir=1;
		this.calcSteps();
		this.fade();
	} else {
		setTimeout(function() { instant.fadeIn(); },this.intervalInMs*2+1)
	}
}
fadeInOut.prototype.fadeOut = function() {
	var instant = this;	
	if (!this.working) {
		this.working=1;
		this.targetDivOpacity = 0;
		this.lObject.style.visibility="visible";
		this.divOpacity=this.getOpacity();
		this.lObject.style.zIndex=1;
		this.dir=0;
		this.calcSteps();
		this.fade();
	} else {
		setTimeout(function() { instant.fadeOut(); },this.intervalInMs*2+1)
	}
}

fadeInOut.prototype.loadpage = function(page_request) {
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {
			this.tObject.style.visibility="hidden";
			
			this.tObject.innerHTML=page_request.responseText;
		 	var x = this.tObject.getElementsByTagName("script");   
			for(var i=0;i<x.length;i++)  
			{  
				eval(x[i].text);  
			}  			
			if (this.tObject.scrollHeight<this.tObjectminheight) {/*this.tObject.style.height=this.tObjectminheight;*/}
			this.fadeIn();
	}
}

fadeInOut.prototype.changeContent=function (url) {
	var page_request = false;
	var instant = this;
	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;
	this.fadeOut();
	page_request.onreadystatechange=function(){
		instant.loadpage(page_request);
	}
	page_request.open('GET', url, true)
	page_request.send(null)
}


var loadedobjects="";
var rootdomain="http://"+window.location.hostname;
var myHistory=new Array();

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
		}
	}
}

function displayContentForButton(buttonNum ) {
	var content = {
		'1': "pagebodyFade.changeContent('/mmenus/m1.php')",
		'2': "pagebodyFade.changeContent('/mmenus/m2.php')",
		'3': "pagebodyFade.changeContent('/mmenus/m3.php')",
		'4': "pagebodyFade.changeContent('/mmenus/m4.php')",
		'5': "Modalbox.show('/mcont/modalcontent.php?category=5&noclosebtn=1',{title: 'Contact', width: 650, height:575})",
		'6': "pagebodyFade.changeContent('/mcont/content.php?name=stories&myset=srv-&category=6')",
		'terms' : "Modalbox.show('/mcont/modalcontent.php?category=7', {title: 'Terms of Usage', width: 800, height:580})",

		'm1-1': "pagebodyFade.changeContent('/mcont/content.php?name=events&myset=events&category=73')",
		'm1-2': "pagebodyFade.changeContent('/mcont/content.php?name=events&myset=events&category=74')",
		'm1-3': "pagebodyFade.changeContent('/mcont/content.php?name=events&myset=events&category=75')",
		'm1-4': "pagebodyFade.changeContent('/mcont/content.php?name=events&myset=events&category=79')",
		'm1-5': "pagebodyFade.changeContent('/mmenus/movents.php?year=2005')",
		'm1-6': "pagebodyFade.changeContent('/mmenus/movents.php?year=2006')",
		'm1-7': "pagebodyFade.changeContent('/mmenus/movents.php?year=2007')",
		'm1-8': "pagebodyFade.changeContent('/mmenus/movents.php?year=2008')",
		'm1-9': "pagebodyFade.changeContent('/mmenus/movents.php?year=2009')",
		'm1-10': "pagebodyFade.changeContent('/mmenus/movents.php?year=2010')",
		'm1-97': "window.open('/uploaded-files/files/gpp2011.pdf','_blank')",
		'm1-98': "pagebodyFade.changeContent('/mmenus/m98.php')",
		'm1-99': "Modalbox.show('/mcont/modalcontent.php?category=8', {title: 'Video', width: 650, height:575})"
	};

	//load new content
	//alert(buttonNum);
	if (buttonNum in Array('2')) loadmaps();
	eval(content[buttonNum]);

	
}

function displayContentForButtonOE(eyear,cat) {
	var bcat=0;
	if (eyear>2) {
		bcat=(eyear-1)*10;
	}
	bcat+=cat;
	pagebodyFade.changeContent('/mcont/content.php?name=events_old&myset=events&category='+bcat)
}

function displayContentForCalButton(num) {
	pagebodyFade.changeContent('/mcont/contentbydate.php?day='+num)
}


function goBack() {
	var lastpos1;
	eval("lastpos1=myHistory.pop()");
	var lastpos
	eval("lastpos=myHistory.pop()");
	if (lastpos) {
		pagebodyFade.changeContent(lastpos);
	} else {
		if (lastpos1) {
			pagebodyFade.changeContent(lastpos1);
		} else {
		 	myHistory.push(lastpos1);
		}
	}
	//return false;
}

function mapinit(amap) {
		amap.removeMapType(G_SATELLITE_MAP);
		amap.removeMapType(G_PHYSICAL_MAP);
		amap.setMapType(G_HYBRID_MAP);
		
		amap.addControl(new GSmallZoomControl3D());
		amap.addControl(new GMapTypeControl());
		//amap.enableScrollWheelZoom();
		mapcenter = new google.maps.LatLng(41.14420, 24.8877);
		mapzoomlevel = 17;
		
		amap.setCenter(mapcenter, mapzoomlevel);
		
		google.maps.Event.addListener(amap,"zoomend",function(oldLevel, newLevel) {
			//alert("zoom level "+newLevel);
			if (newLevel>18) {
				
				this.setZoom(18);
			}
		});
		//initialize markers
		mapmarkers=new Array();
		mapmarkersinfo=new Array();
		
		if (baseIcon.length==0) {
			baseIcon[1] = new google.maps.Icon(google.maps.DEFAULT_ICON);
			baseIcon[1].shadow = "/images/buildings/shadow50.png";
			baseIcon[1].iconSize = new GSize(20, 34);
			baseIcon[1].shadowSize = new GSize(37, 34);
			baseIcon[1].iconAnchor = new GPoint(9, 34);
			baseIcon[1].infoWindowAnchor = new GPoint(9, 2);
			baseIcon[1].image = "/images/buildings/m1.png";
			for (var i=2; i<=18; i++ ) {
				baseIcon[i] = new google.maps.Icon(baseIcon[1]);
				baseIcon[i].image = "/images/buildings/m"+i+".png";
			}
		}
}


function showallmarkers() {
	map.closeInfoWindow();
	clearroutes();
    for (var i = 0; i < mapmarkers.length; i++)
    {
      if (i in mapmarkers)
        mapmarkers[i].show();
    }
	map.setCenter(mapcenter, mapzoomlevel);
}
function hideallmarkers() {
	map.closeInfoWindow();
    for (var i = 0; i < mapmarkers.length; i++)
    {
      if (i in mapmarkers) {
        mapmarkers[i].hide();
	  }
    }
}
function showmarker(id) {
	hideallmarkers();
	clearroutes();
  if (id in mapmarkers) {
	if (mapmarkersinfo[id]['id']==id) {
		mapmarkers[id].show();
		//center map to marker
		map.setCenter(mapmarkers[id].getLatLng());
		
		//show info window...

		mapmarkers[id].openInfoWindowHtml('<img src="images/spinner.gif" width="50" height="50" align="center"/>');
		markershowcontent(id);
	}
  }
}
function showmarkertype(type) {
	map.closeInfoWindow();
	clearroutes();
    for (var i = 0; i < mapmarkers.length; i++)
    {
      if (i in mapmarkers) {
        if (mapmarkersinfo[i]['type']==type) {
			mapmarkers[i].show();
		} else {
			mapmarkers[i].hide();
		}
	  }
    }
	map.setCenter(mapcenter, mapzoomlevel);
}

function markersselect(mid) {
	if (mid==0) {
		showallmarkers();
	} else if (mid<0) {
		showmarkertype(mid*(-1));
	} else {
		showmarker(mid);
	}
}
function markershowcontent(mid) {
	if (mid in mapmarkers) {
		var params = "table="+mapmarkersinfo[mid]['table']+"&id="+mid+"&info="+mapmarkersinfo[mid]['info'];
		var url= "/maps/getbuildingbrief.php";
		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(){
			markershowcontentloaded(page_request,mid);
		}
		
		page_request.open('GET', url+"?"+params, true);
		page_request.send(null);
		
	}
}
var newresponcetext;
var newmidid;
function markershowcontentloaded(page_request,mid) {
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {
			
			// this is a hack as it seems maps are not ready to receive updates in such sort notice
			newresponcetext=page_request.responseText;
			newmidid=mid;
			setTimeout(function() { map.updateInfoWindow( new Array( new google.maps.InfoWindowTab(newmidid,newresponcetext) ) ); },1000)
			//map.updateInfoWindow( new Array( new google.maps.InfoWindowTab(mid,page_request.responseText) ) );
	}
}
function clearroutes() {
	if (shownpath) {
		map.removeOverlay(paths[shownpath]);
		shownpath=null;
	}
}
function mapshowroute(rid) {
	clearroutes();
	map.addOverlay(paths[rid]);
	shownpath=rid;
	hideallmarkers();
	var bounds=new google.maps.LatLngBounds();
    for (var i = 0; i < (pathsmarkers[rid]).length; i++) {
		if (pathsmarkers[rid][i] in mapmarkers) {
			mapmarkers[pathsmarkers[rid][i]].show();
			bounds.extend(mapmarkers[pathsmarkers[rid][i]].getLatLng());
		}		
	}
	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
	var list=document.getElementById('buildingslist');
	list.selectedIndex=-1;
}
