﻿var _wweUtils = new _wweUtilClass(); 

function EventLocation(eventId,lat,lon,number,flag)
{
    this.eventId=eventId;
    this.lat=lat.toString().replace(',','.');
    this.lon=lon.toString().replace(',','.');
    this.number=number;
    this.flag=flag;
}

function Map(divName,containerDivName,showInfoBox)
{
    this.divName = divName;
    this.showInfoBox = showInfoBox;
    this.containerDivName=containerDivName;
    var map=null;
    function GetMap()
    {
        try
        {
            map = new VEMap(divName);
            map.SetDashboardSize(VEDashboardSize.Small);
            map.LoadMap();
        }catch(e)
        {
            return false;
        }
        return true;
    }
    
    function AttachEvents()
    {
        if(!map) return;
        if(showInfoBox != null && showInfoBox)
        {
            map.AttachEvent("onmouseover", function(e){ShowInfoBox(e);return true;});
            map.AttachEvent("onmouseout", function(e){HideInfoBox(1000); return true;});
        }
        else
        {
            map.AttachEvent("onmouseover", function(e){if(e.elementID!=null) return true;});
            map.AttachEvent("onmouseout", function(e){if(e.elementID!=null) return true;});
        } 
    }
    
    this.ShowLoc = function(loc)
    {
        if(!loc || !loc instanceof Array) return false;
        if(!map)
        {
            var isLoaded=GetMap();
            if(!isLoaded) return false;      
            AttachEvents();
            map.Clear();
            map.ClearInfoBoxStyles(); 
        } 
        map.DeleteAllShapes();
        
        var i,j=0;
        var points=new Array();
        
        for(i=loc.length-1;i>=0;i--)
        {
            var latlon=null;
            try
            {
                if(!loc[i] instanceof EventLocation) continue;
                latlon=new VELatLong(parseFloat(loc[i].lat), parseFloat(loc[i].lon));
            }catch(e){}
            if(latlon)
            {
                if(CreatePin(latlon,loc[i].eventId,loc[i].number,loc[i].flag))
                {
                    points[j] = latlon;
                    j++;
                }   
            }
        }
        
        if(points.length > 0)
        {
            SetMapView(points);
            return true;
        }
        return false;
    }

    this.ShowSingleLoc = function(loc, zoom, style) {
        if (!loc || !loc instanceof EventLocation) return false;
        if (!map) {
            var isLoaded = GetMap();
            if (!isLoaded) return false;
            AttachEvents();
            map.Clear();
            map.ClearInfoBoxStyles();
        }
        map.DeleteAllShapes();

        var latlon = null;
        try {
            latlon = new VELatLong(parseFloat(loc.lat), parseFloat(loc.lon));
        } catch (e) { return false; }

        if (latlon) {
            if (CreatePin(latlon, loc.eventId, loc.number, loc.flag)) {
                if (zoom == undefined || zoom == '' || parseInt(zoom) == NaN)
                    zoom = 14;
                if (style == undefined || style == '')
                    style = 'r';
                map.SetCenterAndZoom(latlon, zoom);
                map.SetMapStyle(style);
                return true;
            }
        }
        return false;
    }
    
    function PositiveInt(val)
    {
        if(isNaN(parseInt(val))) return null;
        var value=parseInt(val);
        if(value >0)
            return value;
        return null;            
    }
    
    function CreatePin(latlong, eventId, number,flag)
    {
        if(!map || !latlong || !latlong instanceof VELatLong) return false;
        var eventIdCon=PositiveInt(eventId);
        try
        {
            var pin = new VEShape(VEShapeType.Pushpin, latlong);
            if(eventIdCon)
            {
                var flagGo = (flag==null || (!isNaN(parseInt(flag)) && (parseInt(flag)==1 || parseInt(flag)==2 || parseInt(flag)==3)));
                if(flagGo&&flag) 
                    pin.SetTitle(eventIdCon.toString()+";"+ flag.toString());
                else
                    pin.SetTitle(eventIdCon.toString()+";");
            }
            if(number)
                pin.SetCustomIcon("<table cellspacing='0' cellpadding='0' class='pushpintable'><tr><td class='pushpin'>" + number + "</td></tr></table>");
            else
                pin.SetCustomIcon("<table cellspacing='0' cellpadding='0' class='pushpintable'><tr><td class='pushpin'></td></tr></table>");
            
            map.AddShape(pin);
            return true;
        }
        catch(e)
        {
            ShowWWEMessage(e.message);
            return false;
        }
    }    
    
    function SetMapView(points)
    {
        if(!map || !points || !points instanceof Array) return;
        if(points.length == 0) return;
        if(points.length == 1)
        {
            map.SetCenterAndZoom(points[0], 14);
        }
        else
        {
            map.SetMapView(points);
        }
    }
       
    function ShowInfoBox(e)
    {   
        if(e)   
        {
            if(!e.elementID) return;
            var shape = map.GetShapeByID(e.elementID);
            if(!shape) return;
            var eventIDFlag=shape.GetTitle();
            if(eventIDFlag)
            {
                var eventIDFlagArray=eventIDFlag.split(";");
                var eventID=PositiveInt(eventIDFlagArray[0]);
                if(!eventID) return;
                var flag=eventIDFlagArray[1];
                var flagGo = (flag==null || (!isNaN(parseInt(flag)) && (parseInt(flag)==1 || parseInt(flag)==2 || parseInt(flag)==3)));
                if(!flagGo) flag=null;
                var mapXY = [e.clientX, e.clientY];
                var xy = GetBestXY(e,"300px","200px");
                
                if(!_wweUtils.is_ie)
                {
                    xy[1]=mapXY[1]-100;
                    if(xy[1] < window.pageYOffset)
                        xy[1]=window.pageYOffset+10;
                    if(xy[1]+200 > window.innerHeight+window.pageYOffset)
                        xy[1]=window.innerHeight+window.pageYOffset-200-10;
                }
                var newdiv = CreateInfoBox(xy[0],xy[1]);
                var culture = getQueryString(location.search,"&",1)["culture"];
                var eventDetail = new EventDetail(eventID,culture,flag);
                eventDetail.ShowEventDetails(newdiv,mapXY[0],mapXY[1],$('infoboxframe'));
            }
        }
        return true;        
    }
    
    function CreateInfoBox(clientX,clientY)
    {
        var left = $(containerDivName).style.left;
        var top = $(containerDivName).style.top;
        left = parseFloat(left.substring(0,left.length-2));
        top = parseFloat(top.substring(0,top.length-2));
        
        var divIdName = 'infobox';
        var newdiv =$(divIdName);
        var newIframe =$('infoboxframe');
        if(!newdiv)
        {
            var newdiv = document.createElement('div')
            newdiv.setAttribute('id',divIdName);
            newdiv.className="infobox1";
            newdiv.onmouseover = ShowInfo;
            newdiv.onmouseout=HideInfoBox;
            $(containerDivName).appendChild(newdiv);
            
            //Fix IE6 bug
            if(_wweUtils.is_ie && !_wweUtils.is_ie7up)
            {
                var newIframe = document.createElement('iframe')
                newIframe.setAttribute('id','infoboxframe');
                newIframe.className="infoboxback";
                $(containerDivName).appendChild(newIframe);
            }
        }
        ShowInfo();
        newdiv.style.left = clientX -left + "px";
        newdiv.style.top =  clientY -top + "px";
        //newdiv.style.display = "block";
        if(newIframe)
        {
            newIframe.style.left = clientX -left + "px";
            newIframe.style.top =  clientY -top + "px";
            newIframe.style.height =  "200px";
            newIframe.style.height =  "200px";
            //newIframe.style.display = "block";
        }
        
        return newdiv;
    }
    
    this.SetCenterAndZoom = function(latlong,level)
    {
        if(!latlong || !latlong instanceof VELatLong || !level) return;
        map.SetCenterAndZoom(latlong,level);
    }
}

var t,closeFlag=false;
function HideInfoBox(time)
{
    if(time==null || time == undefined) time=1000;
    closeFlag=true;
    t=setTimeout("HideInfo()", time);
}

function HideInfo()
{
    if($('infobox') != null && closeFlag)
    {
        $('infobox').style.display="none";
        if($('infoboxframe')) $('infoboxframe').style.display="none";        
    }
}

function ShowInfo()
{
    closeFlag=false;
    if(t) clearTimeout(t);
}