var MapDiv = null;   
var drawMapParameter;  
var geocoder = null;  
var Marker;
    
function loadGoogleMap() 
{
    if(!MapDiv)
        MapDiv = getHtmlElement("map");
        
    if (!MapDiv)
        return;
    if (!GBrowserIsCompatible()) 
    {
        MapDiv.innerText = "Sorry, your browser doesn't support our maps";
        return;
    }

	drawMapParameter="small";
    if (typeof(lat)=="undefined" || typeof(lng)=="undefined" || lat=="" || lng=="" )
    {
        if (!geocoder)
            geocoder = new GClientGeocoder();
                
        geocoder.getLatLng(address,drawMap);
    }
    else
    {
        latLng = new GLatLng(lat,lng);
        drawMap(latLng);
    }
}  
    
function drawMap(point)
{
    MapDiv.innerText="";
    MapDiv.innerHtml="";
        
    var map = new GMap2(MapDiv);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
        
    try
    {
        map.setCenter(point, 13);
    }
    catch(err)
    {
        alert("Google got an error plotting this location");
        map=null;
        MapDiv.innerText="Google got an error plotting this location";
        return;
    }
		
    var marker= new GMarker(point);//after set center! 
    GEvent.addListener(marker,"click", function() {marker.openInfoWindowHtml(BuildInfoWindow(drawMapParameter))});
        
    map.addOverlay(marker);
    if (drawMapParameter=="small")
    {
        Marker=marker;
    }
    else
    {
        marker.openInfoWindowHtml(BuildInfoWindow(drawMapParameter));
    }
}
    
function GetDirections()
{
     try
     {
        Marker.openInfoWindowHtml(BuildInfoWindow("small"));
        setTimeout("GetAddress('show','small')",500);//otherwise doesn't work in IE
      }
      catch(err)
      {
        return;
      }
}
    
    
    function GetAddress(mode, param)
    {
        var m=document.getElementById("MallAddress"+param);
        var u=document.getElementById("UserAddress"+param);

        if (mode=='show')
        {
            m.style.display = "none";
            u.style.display = "inline";
        }
        else
        {
            m.style.display = "inline";
            u.style.display = "none";
        }
    }
    
    function GoogleDirections(param)
    {
        var userAddr=document.getElementById("uA"+param).value;
        var url='http://maps.google.com/maps?';
        
        if (userAddr!="")
        {	
			url+='saddr=';
			url+=escape(userAddr);
			url+='&daddr=';
	        
			if (typeof(lat)=="undefined" || typeof(lng)=="undefined" ||
				lat=="" || lng=="" )
			{
				url+=escape(address);
			}
			else
			{
				url+=escape(lat+","+lng);
			}        
        }else //no 'to' direction- generate map link with description
        {
			url+='oi=map&q=';
			if (typeof(lat)=="undefined" || typeof(lng)=="undefined" ||
				lat=="" || lng=="" )
			{
				url+=escape(address);
			}
			else
			{
				url+=escape(lat+","+lng);
			}        
			url+='('+escape(name)+')';	
        }
        
        window.open(url,'GoogleMapWindow');
    }
        
    function BuildInfoWindow(param)
    {
        
        var html="";
        html+='<b>';
        html+=name;
        html+='</b><br /><div id="MallAddress'+param+'" style="display:inline">';
        html+=addressFormatted;
        html+='<br /><a href="javascript:GetAddress(\'show\',\''+ param +'\');">Directions to here</a></div>';
        html+='<div id="UserAddress'+param+'" style="display:none">Tell us where are you coming from:<br />';
        html+='<input id="uA'+param+'" type="text" style="width:200px" /><br />';
        html+='<a href="javascript:GoogleDirections(\''+param+'\');">Submit</a>&nbsp;&nbsp;<a href="javascript:GetAddress(\'none\',\''+ param +'\');">Cancel</a>';
        html+='</div>';
                
        return html;    
    }
      
        
        
    var ClientWindowWidth=0;
    var ClientWindowHeight=0;
    function GetClientWindowSize() 
    {
        //var myWidth = 0, myHeight = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        ClientWindowWidth = window.innerWidth;
        ClientWindowHeight = window.innerHeight;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        ClientWindowWidth = document.documentElement.clientWidth;
        ClientWindowHeight = document.documentElement.clientHeight;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        ClientWindowWidth = document.body.clientWidth;
        ClientWindowHeight = document.body.clientHeight;
        }
        //window.alert( 'Width = ' + myWidth );
        //window.alert( 'Height = ' + myHeight );
    }
    
    
    function ShowBigMap(param)
    {
        var m=document.getElementById("BigMap");
        var mi=document.getElementById("BigMapInner")
        
        if (!m) 
        {
            alert("No BigMap div");
            return;
        }
        
        if (param=="show")
        {    
            m.style.display="block";
            m.style.position="absolute";
            SetDropDownsDisplay("hidden");               
            
                
            myWidth=600;//(myWidth>600)? 600: myWidth;//limit my size to 600 x 600
            myHeight=400;//(myHeight>600)? 600: myHeight;
            
                
            m.style.width=myWidth +"px";//set size popup window
            m.style.height=myHeight +"px";
            
            m.style.left="75px";//(ClientWindowWidth-myWidth)/2 +"px";//set location popup window (with top header)
            m.style.top="0px";//(ClientWindowHeight-myHeight)/2 +"px";
              
            mi.style.width=myWidth +"px";//set size for actual map 
            mi.style.height=(myHeight-17) +"px";
           
            
            MapDiv=document.getElementById("BigMapInner");//set global variable where to draw the map ("BigMap");
            drawMapParameter="big"; //set global variable which map to draw
            
			if (typeof(lat)=="undefined" || typeof(lng)=="undefined" ||
				lat=="" || lng=="" )
			{
				if (!geocoder)
					geocoder = new GClientGeocoder();
	                
				geocoder.getLatLng(address,drawMap);
			}
			else
			{
				latLng = new GLatLng(lat,lng);
				drawMap(latLng);
			}
			positionPop("BigMap");
        }
        else
        {
            m.style.display="none";
            SetDropDownsDisplay("visible");
            drawMapParameter="small";
        }
    }
        
function SetDropDownsDisplay(sMode)
{
	var x=0;
	var y=0;
	
	for(x=0;x<document.forms.length;x++)
	{
		for(y=0;y<document.forms[x].elements.length;y++)
		{
			if(document.forms[x].elements[y].type=="select-one") { document.forms[x].elements[y].style.visibility = sMode; } 			
		}
	}
}

function getHtmlElement(str) {
	var o;
	if (document.all){
		eval('o = document.all.' + str);
	} else if (document.getElementById){
		o = document.getElementById(str);
	}
	return o;
}

function positionPop(popName) {
		
	var dvpop = document.getElementById(popName);
	
	if (dvpop) {	
		var myWidth = 0, myHeight = 0;
		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;
		}
		dvpop.style.left = parseInt(myWidth/2) - parseInt(dvpop.clientWidth/2) + "px";;
		dvpop.style.top = parseInt(myHeight/2) - parseInt(dvpop.clientHeight/2) + "px";			

	}
}
