﻿var map1;
var map2;
var index=0;

//Application related event handlers
//Constants
var eventsMapDIV="eventsMapdiv";
var eventsMapIFRAME="eventsMapIFrame";
var eventsDIV="eventsMap";
var viewMapLink="lnkViewMap";
var closeLINK="lnkClose";
var minMaxLINK="lnkMin";
var minMaxIMAGE="imgMinMax";
var mapWidth="500px";
var mapHeight="350px";
var samePage=true;
var pushpinmap=false;
var _wweUtils = new _wweUtilClass(); 

function SetCenterAndZoom(level,lat,lon)
{
    map1.SetCenterAndZoom(new VELatLong(parseFloat(lat),parseFloat(lon)),level);
}
    
function RefreshMap()
{  
   if(pushpinmap) HideDiv(eventsMapDIV,eventsMapIFRAME);
    if(AnyEventLocations())
    {        
        AddMapLink();
        $(viewMapLink).style.visibility="visible";
    }
    else
    {
        $(viewMapLink).style.visibility="hidden";
        HideDiv(eventsMapDIV,eventsMapIFRAME);
    }
    if($(eventsMapDIV).style.display == "block")
    {
        var success = PopulateMap();
        if(!success) ShowNoMapMessage(eventsMapDIV);
    }
    samePage=false;
    pushpinmap=false;
}

function AddMapLink()
{
    if($('firsttime'))
    {
        if($('firsttime').value == "0")
        {
            $('firsttime').value = "1";
            var scriptTag=document.createElement('script');
            scriptTag.type = 'text/javascript';
            scriptTag.src=AppStrings.mapurl;
            var element = document.getElementsByTagName("head")[0];
            element.appendChild(scriptTag);
        }
    }
}

function PopulateMap()
{
    var eventLocs = GetEventLocations();  
    if(eventLocs && eventLocs.length > 0)
    {       
        if(!map1) 
            map1 = new Map(eventsDIV,eventsMapDIV,true);
        var success=map1.ShowLoc(eventLocs);
        return success;
    }
    else
    {
        return false;
    }
}

function ShowMap(e)
{
    if(_wweUtils.is_ie)
        DisplayMap();
    else
        DisplayMap(e);
    
    $(minMaxLINK).title="Minimize";
    $(minMaxLINK).backgroundImage = "./Resources/Images/minbox.gif";
    var success=PopulateMap();
    if(!success) ShowNoMapMessage(eventsMapDIV);
    pushpinmap=false;
}

function ShowMapForPushpin(e,eventId,latitude,longitude,categoryId,flag,zoom,mapstyle) {
    if(!eventId || !latitude || !longitude) return;
    if(_wweUtils.is_ie)
        DisplayMap();
    else
        DisplayMap(e);
    
    $(minMaxLINK).title="Minimize";
    $(minMaxLINK).className="min";
    
    var number="";
    if($("event"+eventId))
        number=$("event"+eventId).innerHTML;
    var eventLoc = new EventLocation(eventId,latitude,longitude,number,flag); 
    if(!map1)
        map1 = new Map(eventsDIV, eventsMapDIV, true);
    var success=map1.ShowSingleLoc(eventLoc,zoom,mapstyle);
    if(!success) ShowNoMapMessage(eventsMapDIV);
    pushpinmap=true;
}

function DisplayMap(e)
{    
    var offsetXY;
    offsetXY=GetBestXY(e,mapWidth,mapHeight);
        
    $(eventsMapDIV).style.top = offsetXY[1] +"px";
    $(eventsMapDIV).style.left = offsetXY[0] + "px";
    $(eventsMapDIV).style.width=mapWidth;
    $(eventsMapDIV).style.height=mapHeight;
    $(eventsMapDIV).style.display = "block";
    $(eventsDIV).style.display="block";
    
    if($(eventsMapIFRAME))
    {
        $(eventsMapIFRAME).style.top = offsetXY[1] +"px";
        $(eventsMapIFRAME).style.left = offsetXY[0] + "px";
        $(eventsMapIFRAME).style.width=mapWidth;
        $(eventsMapIFRAME).style.height=mapHeight;
        $(eventsMapIFRAME).style.display = "block";
    }
}

function GetBestXY(e,width,height)
{
    if(!width || width.length == 0 || !height || height.length == 0) return [0,0];
    var mouseXY,w,h,offsetX,offsetY;
    if(_wweUtils.is_ie){
        mouseXY=getMouseXY();
    }
    else{
        mouseXY=getMouseXY(e);
    }
    if(width.indexOf("px") == width.length-2)
        w=width.substring(0,width.length-2);
    else
        w=width;
    if(height.indexOf("px") == height.length-2)
        h=height.substring(0,height.length-2);
    else
        h=height;
        
    if(isNaN(w) || isNaN(h)) return mouseXY;
    w=parseFloat(w);
    h=parseFloat(h);
    offsetX = mouseXY[0];
    offsetY = mouseXY[1];
    var ht=new Array();
    var wl=new Array();
    
    if(_wweUtils.is_ie){
        ht = [document.body.clientHeight,document.body.scrollTop];
        wl = [document.body.clientWidth,document.body.scrollLeft];
    }
    else{
        ht = [window.innerHeight,window.pageYOffset];
        wl = [window.innerWidth,window.pageXOffset];
    }
    
    if((mouseXY[1]+(h/2))<(ht[0]+ht[1]))
        offsetY=mouseXY[1]-(h/2);
    else
        offsetY=ht[0]+ht[1]-h-10;
    
    if(offsetY < ht[1])
        offsetY = ht[1]+10;
        
    if((mouseXY[0]+w+10)>(wl[0]+wl[1]))
        offsetX=mouseXY[0]-w-40;
    
    if(offsetX < wl[1])
        offsetX = wl[0]+wl[1]-w-20;
        
    offsetY=offsetY>0?offsetY:MouseXY[1];
    offsetX=offsetX>0?offsetX+20:wl[1];
    return [offsetX,offsetY];
}

function AnyEventLocations()
{
    var eventsTable = $("eventsTable");
    if(!eventsTable || !eventsTable.firstChild || !eventsTable.firstChild.childNodes) return false;
    var nodes = eventsTable.firstChild.childNodes;
    for(var i=0;i<nodes.length;i++)
    {
        var eventID = nodes[i].getAttribute("id");
        if(isNullOrEmpty(eventID) || isNaN(eventID)) continue;
        var val = nodes[i].getAttribute("val");
        if(isNullOrEmpty(val)) continue;
        var values=val.split("|");
        if(values.length == 3&&!isNullOrEmpty(values[0])&& !isNullOrEmpty(values[1]))
        {
            return true;
        }
    }    
    return false;
}

function GetEventLocations()
{    
    var eventsTable = $("eventsTable");
    if(!eventsTable || !eventsTable.firstChild || !eventsTable.firstChild.childNodes) return;
    var nodes = eventsTable.firstChild.childNodes;
    var j=0,index1=1;
    
    var eventLocs=new Array();
    for(var i=0;i<nodes.length;i++)
    {
        var eventID = nodes[i].getAttribute("id");
        if(isNullOrEmpty(eventID) || isNaN(eventID)) continue;
        var val = nodes[i].getAttribute("val");
        if(isNullOrEmpty(val)) continue;
        var values=val.split("|");
        if(values.length == 3&&!isNullOrEmpty(values[0])&&!isNullOrEmpty(values[1]))
        {
            eventLocs[j] = new EventLocation(eventID,values[0],values[1],index1++,null);
            j++;
        } 
    }
    
    return eventLocs;
}

function HideDiv(divName,iframeName)
{
    HideInfo();
    if($(divName)) $(divName).style.display = "none";
    if($(iframeName)) $(iframeName).style.display = "none";
}

function MinOrMaxDiv(div,link)
{
    if(link.title == "Minimize")
    {
        HideInfo();
        link.title="Maximize";
        $(div).style.height="30px";
        if($(eventsMapIFRAME)) $(eventsMapIFRAME).style.height="30px";
        $(eventsDIV).style.display="none";
        link.className = "max";
        samePage=true;
    }
    else
    {
        link.title="Minimize";
        $(div).style.height=mapHeight;
        if($(eventsMapIFRAME)) $(eventsMapIFRAME).style.height=mapHeight;
        $(eventsDIV).style.display="block";
        link.className = "min";
        
        if(!samePage)
        {
            var success = PopulateMap();
            if(!success) ShowNoMapMessage(eventsMapDIV);
        }
    }
}

function LoadSingleMap(mapDiv,latlong)
{
    if(!$(latlong) || !$(mapDiv)) return;
    var geocode=$(latlong).value;
    if(geocode=='') 
    {
        HideDiv(mapDiv);
        return;
    }
    var values=geocode.split("|");
    if(values.length!=4 || values[0]==''|| values[1]=='') 
    {
        HideDiv(mapDiv);
        return;
    }
    
    var eventLoc = new EventLocation(null,values[0],values[1],null,null);
     
     if(!map2)
         map2 = new Map(mapDiv, eventsMapDIV, false);
    var success = map2.ShowSingleLoc(eventLoc, values[2], values[3]);
     if(!success) HideDiv(mapDiv);
}

function ShowNoMapMessage(div)
{
    HideDiv(div,eventsMapIFRAME);
    ShowWWEMessage(AppStrings.unabletoloadmap, false);
}

var fa=null;
function SetupHiddenMap(hiddenDIV,resultsDIV,loadingDIV)
{
    if($(hiddenDIV) && !fa)
        fa=new FindAddress(hiddenDIV,resultsDIV,loadingDIV,'SetAddress');
}

function ValidateAddress(address)
{
    var resultsDIV='addressResults';
    $('lat').value='';
    $('lon').value='';
    
    if(address.trim().length == 0 && $(resultsDIV)) 
    {
        $(resultsDIV).style.display="none";
        return;
    }
    address = address.replace('+', '"');
    address = address.replace("&", " and ");
    if(!fa || !(fa.Find(address)))
    {
        $(resultsDIV).innerHTML = AppStrings.unabletoresolve;
        $(resultsDIV).className = "errormessage";
        $(resultsDIV).style.display="block";
    }
}

function SetAddress(address,lat,lon)
{
    if(!address || !lat || !lon) 
    {
        $('address').value = "";
        return;
    }
    if($('address')) $('address').value=unescape(address);
    if($('lat')) $('lat').value=lat;
    if($('lon')) $('lon').value=lon;
}

function ShowWWEMessage(message,isModal,top,left)
{
    var tbl = $('wwemessage');
    if(!tbl)
    {
        tbl = document.createElement('table');
        tbl.id="wwemessage";
        document.body.appendChild(tbl);
    }
    if(tbl)
    {
        if(tbl.rows.length > 0)
            tbl.deleteRow(0);
        tbl.style.zIndex = 200;
        tbl.style.position = "absolute";
        
        if(!top) 
        {
            if(_wweUtils.is_ie){
                top = document.body.scrollTop + (document.body.clientHeight/2)-10;
            }
            else{
                top = window.pageYOffset + (window.innerHeight/2)-10;
            }
        }
        if(!left) 
        {
            if(_wweUtils.is_ie){
                left = document.body.scrollLeft + (document.body.clientWidth/2) - 200;
            }
            else{
                left = window.pageXOffset + (window.innerWidth/2)-200;
            }
        }
        tbl.style.left = left + "px";
        tbl.style.top = top + "px";
        var row = tbl.insertRow(-1);
        var col1 = row.insertCell(-1);
        var col2 = row.insertCell(-1);
        col1.innerHTML = message;
        col1.style.width = "490px";
        col2.style.width = "10px";
        col2.className = "closetop";
        addEvent(col2, "click", CloseMessageBox); 
        tbl.style.display = "block";
        
        setTimeout("CloseMessageBox()", 5000);
    }
    else
    {
        alert(message);
    }    
}

function CloseMessageBox(e)
{
    if($("wwemessage")) $("wwemessage").style.display='none';
}

function addEvent(obj, type, fn) {
   if (obj.addEventListener) 
    {
        obj.addEventListener(type, fn, false);
    }
    else if (obj.attachEvent) 
    {
        obj["e"+type+fn] = fn;
        obj[type+fn] = function() {obj["e"+type+fn](window.event);}
        obj.attachEvent("on"+type, obj[type+fn]);
    }
    else 
    {
        obj["on"+type] = obj["e"+type+fn];
    }
}

document.onkeydown = function(e)
    {
        var keycode;
        if (window.event) keycode = window.event.keyCode;
        else if (e) keycode = e.which;
        if(!keycode) keycode=e.keyCode;
        
        if(keycode && keycode == 27)
            HideDiv(eventsMapDIV,eventsMapIFRAME);
    }
