var centerPoint = new GLatLng(-29.764377, 24.213867);
var map;
var countryPoint;
var viewportwidth;
var viewportheight;
var nn=(navigator.appName.indexOf("Netscape")!=-1);
var dD=document,dH=dD.html,dB=dD.body,px=dD.layers?'':'px';
var y;
var stopscroll = false;
var myWidth = 0, myHeight = 0;
var fullscreen = false;
var closedBox = true;

function getwinsize(){
		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.addEvent("domready", function(){
	doLoad();
	getwinsize();
	mapGoTo(2);

	var ScrollTop = document.body.scrollTop;

	$("mapclose").style.display = "none";
	$("breadsep").style.display = "none";

	$("mapMessage").style.top = "-" + (500 - (500 - 310) + ( (500 - 310) / 2) + 10 ) + "px"; // (map height - (mapheight-windowheight) - ((mapheight-windowheight)/2) + buffer)
	try{
		$("mapMessage").style.left = ( (myWidth - 750) / 2 ) + "px";
	}catch(e){}

	$("mapmax").addEvent("click", function(){
		fullscreen = true;
		if (ScrollTop == 0){
			if (window.pageYOffset)
			    ScrollTop = window.pageYOffset;
			else
			    ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}
		getwinsize();

		window.scrollTo(0,0);
		stopscroll = false;
		$("googlewindow").style.position = "absolute";
		$("googlewindow").style.display = "block";
		$("googlewindow").style.width = "100%";
		$("googlewindow").style.height = myHeight + "px";
		$("googlemapPlatform").style.height = (myHeight-58) + "px";
		$("map").style.height = (myHeight-58) + "px";
		$("mapKML").style.height = (myHeight-58) + "px";
		$("googlemapPlatform").style.backgroundColor = "#4a0147";

		$("header").style.display = "none";
		$("menuwrapper").style.display = "none";
		$("container").style.display = "none";
		$("footer").style.display = "none";

		$("mapMessage").style.top = "-" + ((myHeight-60) - ((myHeight-60) - 310) + ( ((myHeight-60) - 310) / 2) + 10 ) + "px"; // (map height - (mapheight-windowheight) - ((mapheight-windowheight)/2) + buffer)
		try{
			$("mapMessage").style.left = ( (myWidth - 750) / 2 ) + "px";
		}catch(e){}

		$("mapclose").style.display = "block";
		$("mapmax").style.display = "none";
		floatLayer(0,0,'googlewindow').Fm();
	});
	$("mapclose").addEvent("click", function(){
		fullscreen = false;
		if (ScrollTop == 0){
			if (window.pageYOffset)
			    ScrollTop = window.pageYOffset;
			else
			    ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}

		getwinsize();
		$("googlewindow").style.position = "relative";
		$("mapclose").style.display = "none";
		$("mapmax").style.display = "block";
		$("header").style.display = "block";
		$("footer").style.display = "block";
		$("container").style.display = "block";
		$("menuwrapper").style.display = "block";
		$("googlewindow").style.height = "560px";
		$("container").style.display = "block";
		$("googlemapPlatform").style.height = "500px";
		$("map").style.height = "500px";
		$("mapMessage").style.top = "-" + (500 - (500 - 310) + ( (500 - 310) / 2) + 10 ) + "px"; // (map height - (mapheight-windowheight) - ((mapheight-windowheight)/2) + buffer)
		try{
			$("mapMessage").style.left = ( (myWidth - 750) / 2 ) + "px";
		}catch(e){}
		$("mapKML").style.height = "500px";
		window.scrollTo(0, ScrollTop);
//		var myFx = new Fx.Scroll(window).toBottom();
		stopscroll = true;
	});
});
window.addEvent("resize", function(){
	if(fullscreen){
		getwinsize();
		$("googlemapPlatform").style.height = (myHeight-58) + "px";
		$("googlewindow").style.height = (myHeight-58) + "px";
		$("map").style.height = (myHeight-58) + "px";
		$("mapKML").style.height = (myHeight-58) + "px";
		$("googlemapPlatform").style.backgroundColor = "#4a0147";

		$("mapMessage").style.top = "-" + ((myHeight-60) - ((myHeight-60) - 310) + ( ((myHeight-60) - 310) / 2) + 10 ) + "px"; // (map height - (mapheight-windowheight) - ((mapheight-windowheight)/2) + buffer)
		try{
			$("mapMessage").style.left = ( (myWidth - 750) / 2 ) + "px";
		}catch(e){}
	}else{
		myHeight = 500;
		$("mapMessage").style.top = "-" + ((myHeight-60) - ((myHeight-60) - 310) + ( ((myHeight-60) - 310) / 2) + 10 ) + "px"; // (map height - (mapheight-windowheight) - ((mapheight-windowheight)/2) + buffer)
		try{
			$("mapMessage").style.left = ( (myWidth - 750) / 2 ) + "px";
		}catch(e){}
	}

});


function floatLayer(iX,iY,id){
	var L=dD.getElementById?dD.getElementById(id):dD.all?dD.all[id]:dD.layers[id];
	this[id+'O']=L;

	if(dD.layers)L.style=L;

	L.nX=L.iX=iX;
	L.nY=L.iY=iY;

	L.P=function(y){
		this.style.top=y+px;
	}
	L.Fm=function(){
		if(stopscroll == false){
			var pX, pY;
			pX=(this.iX >=0)?0:nn?innerWidth:nn&&dH.clientWidth?dH.clientWidth:dB.clientWidth;
			pY=nn?pageYOffset:nn&&dH.scrollTop?dH.scrollTop:dB.scrollTop;
	//		if(this.iY<0)pY+=nn?innerHeight:nn&&dH.clientHeight?dH.clientHeight:dB.clientHeight;
	//		this.nY+=.1*(pY+this.iY-this.nY);
	//		this.P(this.nY);
			this.P(pY);
			setTimeout(this.id+'O.Fm()',33);
		}
	}
	return L;
}


function mapGoTo(country){
	$("map").style.display = "block";
	$("mapKML").style.display = "none";
	$("mapMessage").style.display = "none";
	closedBox = true;
	lock = false;
	country_id = country;
	dont_load_country_city = false;
	var sitMap = function(JSONModel){
	    map.clearOverlays()
		myModel = JSONModel;
	    JSONModel.each(function(row) {
			var countryPoint = new GLatLng(Number(row.fields.geo_x), Number(row.fields.geo_y));
		    var flag_icon = new GIcon(G_DEFAULT_ICON);
		    flag_icon.image = '/static/img/flag.png';
			flag_icon.iconSize = new GSize(32, 32);
			flag_icon.shadow = '';
			flag_icon.iconAnchor = new GPoint(3,30);
			var marker = new GMarker(countryPoint,{icon: flag_icon,title:row.fields.name,draggable:false});	

			GEvent.addListener(marker,'click',function(){
				setTimeout(function(){
					if(dont_load_country_city == false){
						LoadCountryCity(row.pk);
					}
				}, 500);
			});
			GEvent.addListener(marker,'dblclick',function(){
				loadCityKML(row.pk, country_name, row.fields.name);
				dont_load_country_city = true;
			});

			map.addOverlay(marker);
		});
	}
	

	$("breadhome").style.fontWeight = "bold";
	$("breadcountry").style.display = "block";
	$("breadsep").style.display = "block";

	if(country == "nocountry"){
		country = 2;
		map.setZoom(3); 
		map.panTo(centerPoint);
		$("breadsep").style.display = "none";
		$("breadcountry").style.display = "none";
		$("breadhome").style.fontWeight = "bold";
	}
	
	map.clearOverlays();

	var request = new Request.JSON({
        method:     'post',
        url:        "/admin/load/countryphoto/",
		onComplete: function(request){
			var c=0;
		    request.each(function(row) {
				if(c == 0){
					if( row.fields.country_x != "" && row.fields.country_y != "" && row.fields.country_zoom != ""){
						var photoPoint = new GLatLng(Number(row.fields.country_x), Number(row.fields.country_y));

						map.setZoom(Number(row.fields.country_zoom)); 
						map.panTo(photoPoint);

						$("breadcountry").removeEvents("click");
						$("breadcountry").removeEvents("mouseover");
						$("breadcountry").removeEvents("mouseleave");
						
						$("breadcountry").addEvent("click", function(){
							mapGoTo(row.pk);
						});
						$("breadcountry").addEvent("mouseover", function(){
							$("breadcountry").style.textDecoration = "underline";
						});
						$("breadcountry").addEvent("mouseleave", function(){
							$("breadcountry").style.textDecoration = "none";
						});
					}
				}
				c = c + 1;
			});

		},
    }).send('id=' + country);


	var request = new Request.JSON({
		url: '/admin/map/city/load/',
		onComplete: function(jsonObj) {
			sitMap(jsonObj);
		}
    }).send('id=' + country);

	var request = new Request.JSON({
		url: '/admin/map/city/getcountry/',
		onComplete: function(request){
		    request.each(function(row) {
				country_name = row.fields.title;
				$("breadhome").innerHTML = country_name;
			});
		},
    }).send('id=' + country);
}

function loadCityKML(city_id, country_name, city_name){
	$("map").style.display = "none";
	$("mapKML").style.display = "block";
	$("mapMessage").style.display = "none";
	$("mapKML").src = "http://www.sdinet.org/maps/" + city_id + "/";
	$("breadhome").innerHTML = '<strong>' + country_name + '</strong> - <u>' + city_name + ' Settlements </u>';
}

function LoadCountryCity(id){
	$("mapMessage").innerHTML = '<div class="preloadersmall"><img style="border:0px" src="/static/img/ajax-loader.gif"/><br/>Loading Content...</div>';
	
    var myRequest = new Request({
        method:     'post',
        url:        '/ajaxCity/' + id + '/',
        onComplete: function(request){
			if(closedBox == true){
				$("mapMessage").style.display = "block";
				var fadeFx = new Fx.Style("mapMessage", 'opacity', {wait: false, duration: 1000, onComplete:function(){
					closedBox = false;
				}}).set(0);
				fadeFx.start(1);
			}
			$("mapMessage").innerHTML = request;
			$("mapMessageClose").removeEvents("click");
			$("mapMessageClose").addEvent("click", closeMapMessage);
		}
    }).send();	
}


function closeMapMessage(){
	var fadeFx = new Fx.Style("mapMessage", 'opacity', {wait: false, duration: 1000, onComplete:function(){
		$("mapMessage").style.display = "none";
		closedBox = true;
	}}).set(1);
	fadeFx.start(0);
}