﻿function CreateArea(top,bottom,left,right,color,reps) {
		var area
	
		area = new GPolygon([
            new GLatLng(top, left),
            new GLatLng(top, right),
            new GLatLng(bottom, right),
            new GLatLng(bottom, left)
		  
		  ], "#000000", 0, 0, '#' + color, 0.4);
		  if (reps.length <2) {
		  var rep
		  rep = reps[0]
			  GEvent.addListener(area, "click", function() {
				   map.openInfoWindowHtml(new GLatLng((top + bottom)/2, (left + right)/2 - .0007),'<img src="/images/contact/' + rep + '" width="220" height="150" />' );
			  })
		  
		  }else {
		  	var tabArray = new Array()
		  	for (var i = 0; i < reps.length; i++){
		  	tabArray[i] = new GInfoWindowTab(i + 1,'<img src="/images/contact/' + reps[i] + '" width="220" height="150" />')
		  	}
		  	GEvent.addListener(area, "click", function() {
		    	map.openInfoWindowTabsHtml(new GLatLng((top + bottom)/2, (left + right)/2 - .0007),tabArray);
		    })
		  }
  
		  return area

}

function loadAreas(x,y,map){
	if (! x[0]) {
	var top
	var bottom
	var left
	var right
	
	
	  var Gajax = GXmlHttp.create();
      Gajax.open("GET", "areas10.xml", true);
      Gajax.onreadystatechange = function() {
        if (Gajax.readyState == 4) {
          var reps = new Array()
          var xmlFragI
          var xmlFrag = GXml.parse(Gajax.responseText);
          var areas = xmlFrag.documentElement.getElementsByTagName("area");
          for (var i = 0; i < areas.length; i++) {
          			reps.length=0
          			top = parseFloat(areas[i].getAttribute("top"))
          			bottom = parseFloat(areas[i].getAttribute("bottom"))
          			left = parseFloat(areas[i].getAttribute("left"))
          			right = parseFloat(areas[i].getAttribute("right"))
          			color = areas[i].getAttribute("color")
   					xmlFragI = areas[i].getElementsByTagName('rep')
   					for (var j=0; j < xmlFragI.length; j++){
   					
   					
   						reps[j] = xmlFragI[j].getAttribute("img")
   						//alert(reps[j])
   					}
   						
					x[i] = CreateArea(top,bottom,left,right,color,reps)
					map.addOverlay(x[i]);
    				y[i] = new ELabel(new GLatLng((top + bottom)/2, (left + right)/2 - .0007), 'Area&nbsp;' + (i+1), 'areaLabelStyle');
    			    map.addOverlay(y[i]);


         }
        }
      }
      Gajax.send(null);
	}
	else {
	  alert('no areas')
	}

}

function showAreas(){
	if (document.getElementById('sa').checked) {
		for (i=0;i<areas.length;i++){
			if (areas[i]){
				areas[i].show()
				areasLabel[i].show()
			}
		}
	}	
	else{
		for (i=0;i<areas.length;i++){
			areas[i].hide()
			areasLabel[i].hide()
		}
		
	}
    }

function loadPointsOfInterest(x){
    if (! x[0]) {
        var baseIcon = new GIcon();
        var iconimg, iconshad, iconloc



	  var Gajax = GXmlHttp.create();
      Gajax.open("GET", "pointsOfInterest.xml", true);
      Gajax.onreadystatechange = function() {
        if (Gajax.readyState == 4) {
        //alert("ttt")
          var poi = new Array()
          var xmlFragI
          var htmlTemp
          var titleTemp
          var xmlFrag = GXml.parse(Gajax.responseText);
          var pois = xmlFrag.documentElement.getElementsByTagName("poi");
          for (var i = 0; i < pois.length; i++) {
              iconimg = pois[i].getElementsByTagName("icon")[0].getAttribute("filename")
              iconshad = pois[i].getElementsByTagName("icon")[0].getAttribute("shadowName")
              iconloc = new GLatLng(pois[i].getAttribute("lat"), pois[i].getAttribute("lng"))
              baseIcon.iconSize=new GSize(32,32);  //needs to be set from xml!!!!
              baseIcon.shadowSize=new GSize(56,32);
              baseIcon.iconAnchor=new GPoint(16,32);
              baseIcon.infoWindowAnchor=new GPoint(16,0);
            
            
		      xmlFragI = pois[i].getElementsByTagName('info');
			 
			  if (pois[i].getAttribute("name"))
			       titleTemp = pois[i].getAttribute("name") + ', ' + pois[i].getAttribute("value")  
			  else
			       titleTemp = pois[i].getAttribute("value") 
              x[i] = new GMarker(iconloc, {icon: new GIcon(baseIcon, iconimg, null, iconshad), title:titleTemp,bouncy:false});
              if (xmlFragI.length < 2) {
                  htmlTemp = xmlFragI[0].childNodes[0].nodeValue
                  markerClosureListener(x[i],htmlTemp,null)
    		  
	          }else {
	  	        var tabArray = new Array()
	  	        for (var j = 0; j < xmlFragI.length; j++){
	  	            htmlTemp = xmlFragI[j].childNodes[0].nodeValue
	  	            tabArray[j] = new GInfoWindowTab(j + 1,htmlTemp)
	  	        }
                markerClosureListener(x[i],null,tabArray)
	          }
              map.addOverlay(x[i]);
          }
          showPoi()
        }//readystate =4



      }
      Gajax.send(null);
      
   }//end if points not loaded

}

function markerClosureListener(x,h,t){
    if (t){
      GEvent.addListener(x, "click", function() {
            x.openInfoWindowTabsHtml(t);
      })    
    
    }
    else{
      GEvent.addListener(x, "click", function() {
           x.openInfoWindowHtml(h,{maxWidth:500});
      })    
    }

}

function showPoi(){
	if (true || document.getElementById('sp').checked) {
		for (i=0;i<pointsOfInterest.length;i++){
			if (pointsOfInterest[i]){
				pointsOfInterest[i].show()
			}
		}
	}	
	else{
		for (i=0;i<pointsOfInterest.length;i++){
			pointsOfInterest[i].hide()
		}
		
	}
    }
