var gmarkers = new Array();
var gwindows = new Array();
var gphotos = new Array();
var htmls = new Array();
var map;
var tooltip;
var projectLat=40.985049229555194;
var projectLng=29.120678901672363;
var projectZoom=13;
var custommap;
var fload=0;
var mapTypeControl = null; 
var mapGLargeMapControl=null;
var mapGSmallMapControl=new GSmallZoomControl();
var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(12,12)); 
var topLeft = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(12,12)); 
var myIcon;
var projectPoint=new Array();
var customStatus=false;
var defaultCenter=new GLatLng(projectLat, projectLng);
var autoInfoId=null;
var cevreler=false;
//xxxxxxxxxxxxxxxxxxxxxxxxx XML
function getXML(url,type){
var request = GXmlHttp.create();
      request.open("GET", "inc/_xml/"+url, true);
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          var xmlDoc = GXml.parse(request.responseText);
          // obtain the array of markers and loop through it
          var markers = xmlDoc.documentElement.getElementsByTagName("marker");
          var mh = xmlDoc.documentElement.getElementsByTagName("infoHtml");
          for (var i = 0; i < markers.length; i++) {
            // obtain the attribues of each marker
			var category = markers[i].getAttribute("category");
			var pointID = markers[i].getAttribute("pointID");
			var plan = markers[i].getAttribute("plan");
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
            var point = new GLatLng(lat,lng);
            var html = mh[i].childNodes[0].nodeValue;
			var imagemap = markers[i].getAttribute("imagemap");
			var icontype = markers[i].getAttribute("icontype");
			var iconpro = markers[i].getAttribute("iconpro");
			var iconsize = parseFloat(markers[i].getAttribute("iconsize"));
			var shadowsize = parseFloat(markers[i].getAttribute("shadowsize"));
			var iconanchor = parseFloat(markers[i].getAttribute("iconanchor"));
			var iconname = markers[i].getAttribute("iconname");
			var imgname = markers[i].getAttribute("imgname");
			var label = markers[i].getAttribute("label");
			var display = markers[i].getAttribute("display");
			var z = parseFloat(markers[i].getAttribute("z"));
    

			//standard marker
			if(icontype=="custominlifeicon" && cevreler==true){
			//
			var marker = createMarker(i,iconsize,shadowsize,iconanchor,imagemap,point,label,html,iconname,category,pointID,display,z);

			}
			// custom marker
			else if(icontype=="customicon"){

				projectPoint[i]=point;
				var marker = createMarker(i,iconsize,shadowsize,iconanchor,imagemap,point,label,html,iconname,category,pointID,display,z);
				
			}
			else if(icontype=="infowindow" && cevreler==false){//custom info window
				        var info;
	
			if(category=="inlife"){
			info = new Infowin(point, 'base', pointID, iconname, iconpro, '<a href="#" onClick="map.panTo(defaultCenter); openThickbox(\'inc/_content/inlife-popup.php?height=484&amp;width=800\',\'Inlife\'); return false;"><img src="img/window/a7.jpg" height="108" border="0" title="Inlife"></a>');
			}else if(category=="plans"){
		    info = new Infowin(point, category, pointID, iconname, iconpro, '<img src="img/dot.png"><a style="line-height:18px;" href="#" onClick="openThickbox(\'inc/_content/vaziyet.php?width=800&amp;height=484\',\'Vaziyet Planı\'); return false;" title="Vaziyet Planı" class="menu">Vaziyet Planı</a><br><img src="img/dot.png"><a style="line-height:18px;" href="#" onClick="openThickbox(\'inc/_content/katplanlari.php?width=800&amp;height=484\',\'Kat planları\'); return false;" title="Kat Planları" class="menu">Kat Planları</a><br><img src="img/dot.png"><a style="line-height:18px;" href="/pdf/'+plan+'.pdf" target="_blank" class="menu">PDF Çıkışı</a>');
			}else if(category=="vaziyetplani"){
		    info = new Infowin(point, category, pointID, iconname, iconpro, html);
			}else if(category=="photo"){
		    info = new Infowin(point, category, pointID, iconname, iconpro, '<a href="#" class="menu" onClick="openThickbox(\'inc/_content/civar.php?height=484&amp;width=800&amp;id='+i+'\',\'Yakın Çevre\'); return false;"><img src="foto/fotograflar/thumb/'+imgname+'" border=0 height="34"></a>');
			}
			//add map

			map.addOverlay(info);
			if(category!="inlife"){gwindows.push(info);}

		  }




		  }
		 
if(cevreler==true){
			cevreler=false;
}

        }
      }
      request.send(null);

		
}
//xxxxxxxxx


//xxxxxxxxxxxxxxxxxx ICON SET
function setIcon(type, iconsize, shadowsize, iconanchor, imagemap){

	var h=37;
	if(type=="cevremiz"){
	h=26;
	}
var myIcon = new GIcon();
myIcon.image = '/mapicons/'+type+'/image.png';
myIcon.shadow = '/mapicons/'+type+'/shadow.png';
myIcon.iconSize = new GSize(iconsize,h);
myIcon.shadowSize = new GSize(shadowsize,h);
myIcon.iconAnchor = new GPoint(iconanchor,h);
myIcon.infoWindowAnchor = new GPoint(iconanchor,0);
myIcon.printImage = '/mapicons/'+type+'/printImage.gif';
myIcon.mozPrintImage = '/mapicons/'+type+'/mozPrintImage.gif';
myIcon.printShadow = '/mapicons/'+type+'/printShadow.gif';
myIcon.transparent = '/mapicons/'+type+'/transparent.png';
myIcon.imageMap = [imagemap];

return myIcon;
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

		//xxxxxxxxxxxxxxxxxxxx  TOOL TIP
function showTooltip(marker) {
      	tooltip.innerHTML = marker.tooltip;
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var height=tooltip.clientHeight;
	var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height)); 
	pos.apply(tooltip);
	tooltip.style.visibility="visible";
      }

function mymouseover(i) {
        showTooltip(gmarkers[i])
      }
      // ===== This function is invoked when the mouse leaves an entry in the side_bar =====
      // It hides the tooltip      
      function mymouseout() {
	tooltip.style.visibility="hidden";
      }

function createToolTip(){
	tooltip = document.createElement("div");
	map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
	tooltip.style.visibility="hidden";
}


/*
marker.tooltip = '<div class="tooltip"><nobr>'+label+'<\/nobr><\/div>';
GEvent.addListener(marker,"mouseover", function() {
          showTooltip(marker);
        });        
        GEvent.addListener(marker,"mouseout", function() {
		tooltip.style.visibility="hidden"
        });

		createToolTip();
*/

//xxxxxx

//xxxxxxxxxxxxxxxxxxxxxx CREATE MARKER xxxxxxxxxxxxxxxxxxxxxxxxxxx

function git(id, z) {
tb_remove();
clearWindows();

//autoInfoId=id;
				if(customStatus==true){
			
			createMap(projectPoint[id], z);
			
			//resetMap();
			customStatus=false;
		}else{

			
			map.setCenter(projectPoint[id], z);
			gmarkers[id].openInfoWindowHtml(htmls[id]);

				}
		
      }


function testThickbox(url, label){
	alert(url);
openThickbox(url,'Yakın Çevre');
}


function createMarker(id,ie,ss,ia,mp,point,label,html,img,category,pointID,display,z) {

        var marker = new GMarker(point, setIcon(img, ie, ss, ia, mp));

//var marker = new GMarker(point, {title:label});
       marker.mycategory = category;
	   marker.markerID = pointID;

if(img=="cevremiz"){
gphotos.push(marker);
 }else{
 	gmarkers.push(marker);
	    htmls[id] = html;
 }

if(img=="cevremiz"){
marker.tooltip = '<div class="tooltip"><nobr>'+label+'<\/nobr><\/div>';
GEvent.addListener(marker,"mouseover", function() {
          showTooltip(marker);
        });        
        GEvent.addListener(marker,"mouseout", function() {
		tooltip.style.visibility="hidden"
        });

		createToolTip();
}

		GEvent.addListener(marker, "click", function() {

		  if(img=="cevremiz"){
			  closeWindows();
openThickbox('inc/_content/civar.php?height=484&amp;width=800&photoid='+id,'Yakın Çevre');
		  }else if(img=="inlife"){
			  closeWindows();
openThickbox('inc/_content/inlife-popup.php?height=484&amp;width=800','Inlife');
map.setCenter(point, z);
		  }else{
		  marker.openInfoWindowHtml(html);
		  //map.panTo(marker.getLatLng());
		  map.setCenter(point, z);
		  }
						
						
						});


	

if(autoInfoId!=null && Number(autoInfoId)==id){
marker.openInfoWindowHtml(html);
autoInfoId=null;
}

		//gmarkers[id]=marker;


		map.addOverlay(marker);

      }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxx


//xxxxxxxxxxxxxxxxxxSHOW-HIDE-CLEAR OVERLAYS xxxxxxxxxxxxxxxxx

function hideMarkers(){
for (var i = 0; i < gmarkers.length; i++) {
	gmarkers[i].hide();
}
}



function clearWindows(){
		
for (var i = 0; i < gphotos.length; i++) {
	map.removeOverlay(gphotos[i]);
}

for (var i = 0; i < gwindows.length; i++) {
	map.removeOverlay(gwindows[i]);
}

}

function clearAll(){
clearWindows();
}


function closeWindows(){
map.closeInfoWindow() 
}

function panMap(lat,lng){
	var point = new GLatLng(lat,lng);
map.panTo(point);
}



//xxxxxxxxxxxxxxx CREATE MAP xxxxxxxxxxxxxxxx
function createMap(p, z){

map = new GMap2(document.getElementById("map_canvas"),{backgroundColor:'#FFFFFF'});

	//map.clearOverlays();
gmarkers.length=0;
gphotos.length=0;
gwindows.length=0;


mapTypeControl = new GMapTypeControl();
mapGLargeMapControl = new GLargeMapControl(); 

map.addControl(mapTypeControl, topRight); 
map.addControl(mapGLargeMapControl, topLeft); 

//map.enableScrollWheelZoom();

map.setCenter(p, z);


        GEvent.addListener(map,"click", function(overlay,latlng) {
          if (overlay) {
            // ignore if we click on the info window
            return;
          }
          var tileCoordinate = new GPoint();
          var tilePoint = new GPoint();
          var currentProjection = G_NORMAL_MAP.getProjection();
          tilePoint = currentProjection.fromLatLngToPixel(latlng, map.getZoom());
          tileCoordinate.x = Math.floor(tilePoint.x / 256);
          tileCoordinate.y = Math.floor(tilePoint.y / 256);
          var myHtml = latlng.lat() + "," + latlng.lng() + " " + tileCoordinate.x + "x" + tileCoordinate.y + " Level: " + map.getZoom();	
          
	var lat=latlng.lat();
	var lng=latlng.lng();
	var zoom=map.getZoom();
	var tilecoor=tileCoordinate.x + "x" + tileCoordinate.y;
	fillData(lat, lng, zoom, tilecoor);

        });

getXML("data.php?category=dundar,inlife", 0);
document.getElementById("menuBar").style.display="block";
customStatus=false;
}
//xxxxxxx


//xxxxxxxxxxxxxxx CREATE CUSTOM MAP xxxxxxxxxxxxxxxx
function createCustomMap(cat, plan){

map.clearOverlays();
//gmarkers.length=0;
//gphotos.length=0;


tb_remove();
var LT=66;

//map = new GMap2(document.getElementById("#mapcustom_canvas"),{backgroundColor:'#FFFFFF'});

var copyright = new GCopyright(1, new GLatLngBounds(new GLatLng(-90, -180), new GLatLng(90, 180)),1, "&copy; 2010");



var copyrightCollection = new GCopyrightCollection('Kat Planları');
copyrightCollection.addCopyright(copyright);

CustomGetTileUrl=function(p,z){
          return "tiles/"+plan+"/"+z+"_"+p.x+"_"+p.y+".jpg"
      }

var tilelayers = [new GTileLayer(copyrightCollection,2,3)];
      tilelayers[0].getTileUrl = CustomGetTileUrl;

custommap = new GMapType(tilelayers, new GMercatorProjection(4), "Old OS");

//map.addMapType(custommap);
map.removeControl(mapTypeControl); 
map.removeControl(mapGLargeMapControl); 

map.addControl(mapGSmallMapControl, topLeft); 

   map.setCenter(new GLatLng(LT,-50), 2, custommap);

   getXML("data.php?category="+cat+"&plan="+plan, 1);
   customStatus=true;
}
//xxxxxxx

// xxxxxxxxxxxxxx SWITCH CREATE CUSTOM MAP xxxxxxxxxxxxxxxx
function prepareCustomMap(cat, plan){
if (GBrowserIsCompatible()) {
createCustomMap(cat, plan);
}else {
      alertUser();
    }

}
//xxxxxxxxx

function resetMap(){

	tb_remove();
	autoInfoId=null;
createMap(defaultCenter, projectZoom);
}


function getMap(id){
	tb_remove();
clearWindows();
closeWindows();

	//if(customStatus==true){
createMap(defaultCenter, 15);
customStatus=false;
//	}else{
	//map.setCenter(defaultCenter, 15);
	//}
	
	cevreler=true;
	autoInfoId=null;
	getXML("data.php?category="+id, 0);

}

//xxxxxxxxxxxxxxxxxxxxxxxxx INIT xxxxxxxxxxxxxxxxxxxxxxx
function initialize() { 
		  if (GBrowserIsCompatible()) { 
			createMap(defaultCenter, projectZoom);
		} else {
      alertUser();
    }

	} 



function alertUser(){
alert("Sorry, the Google Maps API is not compatible with this browser");
}
