//XXX there shouldn't be any global functions in connect apart from the one
//      dojo.addOnLoad() call.
var cacheResults;
var searchResults;
var expandCContainer = false;
var expandSContainer = false;
var expandFMNContainer = true;
var locatorControl;
var slider = null ;
var sliderIcon = null;
var iconClassName = null;
var mapLocatorPopup = null;
dojo.addOnLoad(function()
{
    if (!mapConfigLocation)
    {
        mapConfigLocation="map.xml";
    }
    if (!locatorConfigLocation)
    {
        locatorConfigLocation="locator.xml";
    }
    var callback = function(test)
    {
        return function(myMap1)
        {                  
            loadControls(myMap1);
        }
    }
    
    var catalog = new stratus.catalog.CatalogFactory().newInstance();
    
    if(catalog.isLayerAvailable())
    {
      searchResultsControl = new stratus.SearchResultsControl({distanceUnit : resources["meters"]}, dojo.byId("findNearestResults"));
    }
    else
    {
        dojo.style("findNearestContainer", {"height": 0, "display":"none", "visibility" : "hidden"});
    }
   
    dojo.connect(searchResultsControl, "onResultsUpdated", "showSearchNearestResults");
    dojo.connect(searchResultsControl, "onSearchResultsRowSelected", "showFMNMarkerPopup");    
    var mapLocatorPopupContainer = document.createElement("div");
    dojo.byId('myMap').appendChild(mapLocatorPopupContainer);
    myMap = new stratus.MapControl({configFile: mapConfigLocation, callBackFunction: callback()}, dojo.byId('myMap'));
    mapLocatorPopup = new stratus.MapMarkerPopup({ searchResultsControl :searchResultsControl, showLayerPicker: catalog.isLayerAvailable(), catalog: catalog, mapControl: myMap }, mapLocatorPopupContainer );    
    markerManager = myMap.getMarkerManager();
    if(window.badmapconfig && window.badmapconfig == true)
    {
        dojo.byId('content').style.overflow="visible";
        dojo.byId('content').innerHTML = "<div id='badconfig' class='errorPanel'>Bad Config File</div>"
        dojo.byId('badconfig').innerHTML = resources.badconfigfile;
    }
});

/**
 * This method expands the FMN container when SearchResultsControl 
 * is populated with the results. 
 */  
function showSearchNearestResults()
{
    // If no results in search results control then do not display clear results link
    var display = searchResultsControl.isEmpty() ? "hidden" : "visible";
    dojo.byId("clearFMNResults").style.visibility = display;
    this.expandFMNContainer = true;
    toggleNearestResultsContainer();
}

/** 
 * When a user selects a findmyNearest result, 
 * this method shows the marker at the center of the map
 * and displays the callout for the marker.
 */
function showFMNMarkerPopup(selectedFeature)
{ 
   var marker = markerManager.getMapMarkerById(selectedFeature.properties.stratusid);     
   markerManager.showPopupAndRecenterMarker(marker);
}

/* 
 * This function clears the results from the Find Nearest results
 * container and associated markers. 
 */
function clearFindNearestResults()
{
    searchResultsControl.clearResults();
    markerManager.removeAllMarkers();
    dojo.byId("clearFMNResults").style.visibility = "hidden";
    this.expandFMNContainer = false;
    toggleNearestResultsContainer();
}

/*
 * Load all other controls that are depended on map control.
 */
function loadControls(myMap)
{
    if (!markerManager)
    {
        markerManager = myMap.getMarkerManager();
        //dojo.connect(markerManager, "onMarkerHightlighted", "highlightSRCRow");
        //dojo.connect(markerManager, "onMarkerUnHighlighted", "unHighlightSRCRow");
    }
    var legendControl = new stratus.LegendControl({}, dojo.byId('legend'), myMap);  // loading legend control
    
    locatorControl = new stratus.LocatorControl({configFile: locatorConfigLocation}, dojo.byId('locator'));  //locator Control

    locatorControl.setErrorHandler({
    
        reset: function()
        {
            dojo.byId('error').innerHTML = '';
        },
        handle: function(err, msg)
        {
            if (msg)
            {
                dojo.byId('error').innerHTML = msg;
            }
            else
            {
                dojo.byId('error').innerHTML = err;
            }
            
            expandSContainer = true;
            
            toggleLocatorContainer();
        }
    });

    searchResults = new stratus.LocatorResultsControl({showID: false, showXY: false, showScore: false}, dojo.byId('result'));
    cacheResults = new stratus.LocatorResultsControl({showID: false, showXY: false, showScore: false}, dojo.byId('cacheResultContainer'));
    
    dojo.connect(locatorControl, "onSearchPerformed", searchResults, "updateTable");
    dojo.connect(locatorControl, "onSearchPerformed",  "showResults");
    dojo.connect(searchResults, "onRowSelected", myMap,  "moveTo");  // Move to location
    dojo.connect(searchResults, "onRowSelected", "createLocatorMarker"); // Add Marker
    dojo.connect(searchResults, "onRowSelected", "selectedLocatorResult"); // Write cache cookie
    dojo.connect(cacheResults, "onRowSelected", myMap,  "moveTo");
    dojo.connect(cacheResults, "onRowSelected", "createLocatorMarker");  
    this.slider = dojo.byId("sliderButton");
    this.sliderIcon = dojo.byId("sliderIcon");
    this.iconClassName = 'sliderIconCollapse';
    dojo.connect(this.slider, "onmouseenter","showHover");
    dojo.connect(this.slider, "onmouseleave","showNormal");
   
    //resizeBrowser();
    new stratus.util.HeightCalculator(dojo.byId('content'), [dojo.byId('banner')]).updateHeight(); 

    var listener = dojo.hitch(myMap, myMap.refreshMap);
    new stratus.util.HeightCalculator(dojo.byId('myMap'), null, listener).updateHeight();
    var f = function()
    {
        myMap.resetCenterAndZoomLevel();
    };
    window.setTimeout(f, 0);
    
    new stratus.util.HeightCalculator(dojo.byId('slider'), null).updateHeight(); 
    new stratus.util.HeightCalculator(dojo.byId('sliderButton'), null).updateHeight();
    var lpc = new stratus.util.HeightCalculator(dojo.byId('leftPanel'), null);
    
    dojo.connect(legendControl, "onLegendControlUpdated", lpc,  "updateHeight");
    
    lpc.updateHeight();
    collapseResults();
    
    showCacheResults();
}
/*
*  Event handler raised on selecting an address in the LocatorResultsControl.
*/
function createLocatorMarker(location)
{
    var hub = stratus.RiaHub.getInstance();
    
    // create icon
    var mapTransparentImage = hub.getResourceUrl("/widgets/stratus/img/transparentPixel.png");
    var size = new OpenLayers.Size(35, 41);
    var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
    var icon = new OpenLayers.Icon(mapTransparentImage, size, offset);
    dojo.addClass(icon.imageDiv, "mapMarker");
    icon.imageDiv.id = "MapMarker";
    // get point
    var point = new stratus.geometry.Point({x:location.x, y:location.y, srs:location.crs});
    
    // subTitle to show on callout
    var subTitle = (mapLocatorPopup.showLayerPicker)?resources["locatorMarker-title"] :this.resources["locatorMarker-moreInformation"];
   
    //create a new marker
    var marker = new stratus.Marker({icon: icon, point:point, title: location.name,
                                     description: subTitle,
                                     deleteLinkSwitch:true,deleteLinkMessage:resources["locatorMarker-delete"]});      
    markerManager.addLocatorMarker(marker, mapLocatorPopup);
}


//FIXME What's all this toggling for? Why do we hardcode the container height here?
function showResults()
{
    this.expandSContainer = true;
    document.getElementById("result").style.height = "245px";
    
    // hide recent results.
    document.getElementById("cachedAddressContainer").style.visibility  = "visible";
    // hide cache results
    expandCContainer = false;
    toggleCacheContainer();
    toggleLocatorContainer();
} 
          
function showCacheResults()
{ 
    var cookieName = "locatorResults";
    var locatorSelectedResultString = readCookie(cookieName);
    
    if ( locatorSelectedResultString == null )
    {
        expandCContainer = false;
        toggleCacheContainer();
        return;
    }
    
    var locatorResults = locatorSelectedResultString.split(";")
  
    var results = "[";
                    
    if ( locatorResults.length  > 1)
    {
        for(var i = 0; i < locatorResults.length - 1; i++) 
        {
        var splitLocatorResult = locatorResults[i].split(":");
        results += "{ \"name\" : \"" + splitLocatorResult[1] 
                                     + "\", \"x\"  : \"" 
                                     + splitLocatorResult[2] 
                                     + "\", \"y\" : \"" 
                                     + splitLocatorResult[3] 
                                     + "\", \"crs\" : \"" 
                                     + unescapeColon(splitLocatorResult[4]) 
                                     + "\" },"                                       
        }
        results = results.substring(0,results.length - 1);
        results += "]";

        cacheResults.updateTable(eval(results), true);
                    
        document.getElementById("cachedAddressContainer").style.visibility  = "visible";
        toggleCacheContainer()
    }
}
          
function resizeBrowser()
{
    var contentdiv = document.getElementById('myMap');    
    resizeHeight(contentdiv);
    resizeWidth(contentdiv);
    resizeScaler(contentdiv);                    
}

function calculateOffsetTop(mapElement) 
{
    var y = mapElement.offsetTop;  
    var elementParent = mapElement.offsetParent;
    
    while (elementParent != null)
    {
        if(document.all)
        {
            if( (elementParent.tagName.toLowerCase() == "div") )
            {
                y += elementParent.clientTop;
            }
        }
        else // Netscape/DOM
        {
            if(elementParent.tagName.toLowerCase() == "div" )
            {
                var parentBorder = parseInt(elementParent.border);
                if(isNaN(parentBorder))
                {
                    var parentFrame = elementParent.getAttribute('frame');
                    if(parentFrame != null)
                    {
                        y += 1;
                    }
                }
                else if(parentBorder > 0)
                {                                
                    y += parentBorder;
                }
            }
        }
        
        if (elementParent.tagName.toLowerCase() == "div") {                        
            y += elementParent.offsetTop;
        }
        
        elementParent = elementParent.offsetParent;
    }
    
    return y;                
}

function calculateOffsetLeft(mapElement) {
  
    var x = mapElement.offsetLeft;  
    var elementParent = mapElement.offsetParent;
    
    while (elementParent != null)
    {
        if(document.all)
        {
            if( (elementParent.tagName.toLowerCase() == "div") )
            {
                x += elementParent.clientLeft;
            }
        }
        else // Netscape/DOM
        {
            if(elementParent.tagName.toLowerCase() == "div" )
            {
                var parentBorder = parseInt(elementParent.border);
                if(isNaN(parentBorder))
                {
                    var parentFrame = elementParent.getAttribute('frame');
                    if(parentFrame != null)
                    {
                        x += 1;
                    }
                }
                else if(parentBorder > 0)
                {                                
                    x += parentBorder;
                }
            }
        }
        if (elementParent.tagName.toLowerCase() == "div") {                        
            x += elementParent.offsetLeft;
        }
        
        elementParent = elementParent.offsetParent;
    }
    
    return x;                
}

function resizeHeight(mapElement)
{
    var browserHeight = getBrowserHeight();
    var offsetTopValue = calculateOffsetTop(mapElement) + 20;
    
    if ( browserHeight >= offsetTopValue ) {
        mapElement.style.height = browserHeight - offsetTopValue + "px" ;
    }
}
          
function resizeWidth(mapElement)
{

  var offsetleftValue = calculateOffsetLeft(mapElement) + 20;  
  var browserWidth = getBrowserWidth();
  
  if ( browserWidth >= offsetleftValue ) {
      mapElement.style.width = browserWidth - offsetleftValue + "px";
  }
}
        
function resizeScaler(mapElement) 
{
    var mapHeight = parseInt(mapElement.style.height);
    dojo.byId('slider').style.paddingTop = mapHeight / 2 + "px";
    dojo.byId('slider').style.paddingBottom = mapHeight / 2 + "px";
}

function getBrowserHeight() 
{
    if (window.innerHeight)      
    {
          return window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight != 0)  
    {
              return document.documentElement.clientHeight;
    }
    else if (document.body)      
    {
        return document.body.clientHeight;
    }
    return 0;
}
         
function getBrowserWidth() 
{
    if (window.innerWidth)      
    {
        return window.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientWidth != 0)
    {
        return document.documentElement.clientWidth;
    }
    else if (document.body)      
    {
        return document.body.clientWidth;
    }
    return 0;
}
    
function wipeOut() 
{
    var map = dijit.byId('myMap');
    var mapContainer = dojo.byId("mapContainer");
    map.pinTopRight();
    dojo.style(mapContainer, "paddingLeft", "15px");
    var f = function()
    {
      dojo.animateProperty({node: "leftPanel", duration: 1000, properties: {width: "0"}}).play();
      dojo.animateProperty({node: "slider", duration: 1000, properties: {left: "0"}}).play();
    }
    window.setTimeout(f, 0);
    myMap.refreshMap();
    map.pinTopLeft();
}

function wipeIn() 
{               
    var f = function()
    {
        var map = dijit.byId('myMap');
        var mapContainer = dojo.byId("mapContainer");
        map.pinTopRight();
        dojo.style(mapContainer, "paddingLeft", "300px");
        myMap.refreshMap();
        map.pinTopLeft();
    }
    dojo.animateProperty({node: "leftPanel", duration: 1000, 
            properties: {width: "285"}, onEnd: f}).play();
    dojo.animateProperty({node: "slider", duration: 1000, 
            properties: {left: "285"} }).play();
}

var tog = false;  // TODO: need to replace this with something more dynamic
function toggleSlider(event) {
   //this.slider = dojo.byId("sliderButton"); 
    if ( tog == true) {
        wipeIn();
        this.removeClassNames();
        dojo.addClass(this.sliderIcon, "sliderIconCollapse");
        this.iconClassName = 'sliderIconCollapse';
        tog = false;
    } else {
        wipeOut();
        this.removeClassNames();
        dojo.addClass(this.sliderIcon, "sliderIconExpand");
        this.iconClassName = 'sliderIconExpand';
        tog = true;
    }                               
}

function showHover()
{
    if ( this.iconClassName == 'sliderIconCollapse')
    {
        this.removeClassNames();
        //dojo.removeClass(this.sliderIcon, "sliderIconCollapse");
        dojo.addClass(this.sliderIcon, "sliderIconCollapseHover");
        this.iconClassName = 'sliderIconCollapseHover';
    }
    else
    {
        this.removeClassNames();
        //dojo.removeClass(this.sliderIcon, "sliderIconExpand");
        dojo.addClass(this.sliderIcon, "sliderIconExpandHover");
        this.iconClassName = 'sliderIconExpandHover';
    }
}

function showNormal()
{
    if ( this.iconClassName == 'sliderIconCollapseHover' || this.iconClassName == 'sliderIconCollapse')
    {
        this.removeClassNames();
        //dojo.removeClass(this.sliderIcon, "sliderIconCollapseHover");
        dojo.addClass(this.sliderIcon, "sliderIconCollapse");
        this.iconClassName = 'sliderIconCollapse';
    }
    else
    {
        this.removeClassNames();
        //dojo.removeClass(this.sliderIcon, "sliderIconExpandHover");
        dojo.addClass(this.sliderIcon, "sliderIconExpand");
        this.iconClassName = 'sliderIconExpand';
    }
}

function removeClassNames()
{
    dojo.removeClass(this.sliderIcon, "sliderIconCollapse");
    dojo.removeClass(this.sliderIcon, "sliderIconCollapseHover");
    dojo.removeClass(this.sliderIcon, "sliderIconExpand");
    dojo.removeClass(this.sliderIcon, "sliderIconExpandHover");
}

/* 
 * Resize the map width after the last slide control has finished 
 */
function resizeMapControl()
{
    var mapElement = dojo.byId("myMap");
    resizeWidth(mapElement);
    myMap.refreshMap();                   
}

function expandResults()
{                        
    dojo.animateProperty({node:"result", properties: {height: "245"} }).play();            
}

function collapseResults()
{
    dojo.animateProperty({node:"result", properties: {height: "0"} }).play();
}
        
function toggleCacheContainer()
{
    if (this.expandCContainer == true)
    {
        dojo.animateProperty({node:"cacheResultContainer", properties: {height: "210"} }).play();
        this.expandCContainer = false;
    }
    else
    {
        dojo.animateProperty({node:"cacheResultContainer", properties: {height: "0"} }).play();
        this.expandCContainer = true;
    }
}

/*
 * Change the images when mouse over on recent selected address accordion
 */          
function changeCacheTreeExpandHover()
{
    if (this.expandCContainer == true )
    {
        document.getElementById("cacheContainer").className="treeExpandPlusHover";
        document.getElementById("cacheContainer").alt="+";
    }
    else
    {
        document.getElementById("cacheContainer").className="treeExpandMinusHover";
        document.getElementById("cacheContainer").alt="-";
    }
}

/* 
 * Change the images when mouse out from recent selected address accordion
 */          
function changeCacheTreeExpandOut()
{
    if (this.expandCContainer == true)
    {
        document.getElementById("cacheContainer").className="treeExpandPlus";
        document.getElementById("cacheContainer").alt="+";
    }
    else
    {
        document.getElementById("cacheContainer").className="treeExpandMinus";
        document.getElementById("cacheContainer").alt="-";
    }
}

function toggleNearestResultsContainer()
{
    var controlHeight = "300";

    if (this.expandFMNContainer == true)
    {
        dojo.animateProperty({node:"findNearestResultContainer", properties: {height: controlHeight} }).play();
        this.expandFMNContainer  = false;
    }
    else
    {
        dojo.animateProperty({node:"findNearestResultContainer", properties: {height: "0"} }).play();
        this.expandFMNContainer  = true;
    }
}

function changeNearestResultsExpandHover()
{
    if (this.expandFMNContainer == true )
    {
        document.getElementById("findNearestContainerIcon").className="treeExpandPlusHover";
        document.getElementById("findNearestContainerIcon").alt="+";
    }
    else
    {
        document.getElementById("findNearestContainerIcon").className="treeExpandMinusHover";
        document.getElementById("findNearestContainerIcon").alt="-";
    }
}

/* 
 * Change the images when mouse out from recent selected address accordion
 */          
function changeNearestResultsExpandOut()
{
    if (this.expandFMNContainer == true)
    {
        document.getElementById("findNearestContainerIcon").className="treeExpandPlus";
        document.getElementById("findNearestContainerIcon").alt="+";
    }
    else
    {
        document.getElementById("findNearestContainerIcon").className="treeExpandMinus";
        document.getElementById("findNearestContainerIcon").alt="-";
    }
}

 /* @TODO Vinh - This method seems a little dodgy so we
 * will need to refactor later at some stage
 */
function toggleLocatorContainer()
{
    var h = document.getElementById("result").style.height;
                  
    var resultContainer = 280;
    var resultInner = document.getElementById("result").innerHTML.length;
    if ( resultInner > 100 ) 
    {
          h = parseFloat(h.slice(0, (h.length-2)));                  
          h = h + document.getElementById("locator").offsetHeight;
    } 
    else if ( resultInner == 0 )
    {
          h = 2+document.getElementById("locator").offsetHeight;
    }
    else
    {
          h = 20 + document.getElementById("locator").offsetHeight;
    }
    
    if (document.getElementById("error").innerHTML.length > 0)
    {
        h = 5+document.getElementById("error").offsetHeight + document.getElementById("locator").offsetHeight;
        document.getElementById("result").style.height ="0px";
    }
    if (this.expandSContainer == true)
    {
        dojo.animateProperty({node:"resultsContainer", properties: {height: h, unit: "px" } }).play();
        this.expandSContainer = false;
    }
    else
    {
        dojo.animateProperty({node:"resultsContainer", properties: {height: "0"} }).play();
        this.expandSContainer = true;
    }
}

/* Change the images when mouse over on search for an address accordion*/ 
function changeAddressTreeExpandHover()
{
    if (this.expandSContainer == true)
    {
        document.getElementById("locationContainer").className="treeExpandPlusHover";
        document.getElementById("locationContainer").alt="+";
    }
    else
    {
        document.getElementById("locationContainer").className="treeExpandMinusHover";
        document.getElementById("locationContainer").alt="-";
    }
}

/* Change the images when mouse out from search for an address accordion*/ 
function changeAddressTreeExpandOut()
{
    if (this.expandSContainer == true)
    {
        document.getElementById("locationContainer").className="treeExpandPlus";
        document.getElementById("locationContainer").alt="+";
    }
    else
    {
        document.getElementById("locationContainer").className="treeExpandMinus";
        document.getElementById("locationContainer").alt="-";
    }
}

/* Change the images when mouse over on map legend accordion*/ 
function changeMapLegendExpandHover()
{
    document.getElementById("mapLegend").className="treeExpandMinusHover";
}

/* Change the images when mouse out from map legend accordion*/ 
function changeMapLegendTreeExpandOut()
{
    document.getElementById("mapLegend").className="treeExpandMinus";
}

function readCookie(name) 
{
    
    var nameEQ = name + "=";
    var cookieSplit = document.cookie.split(';');
    
    for(var i=0;i < cookieSplit.length;i++) {
        
        var cookieName = cookieSplit[i];
                                    
        while (cookieName.charAt(0)==' ') cookieName = cookieName.substring(1,cookieName.length);
            
        if (cookieName.indexOf(nameEQ) == 0) {
        
            return unescape(cookieName.substring(nameEQ.length,cookieName.length));                          
        }
    }
    return null;
}

function writeCookie(name, value) {
                   
    var cookieString = name + "=" + escape ( value );

    cookieString += "; expires="

    document.cookie = cookieString;
                
    showCacheResults();
    this.expandCContainer = false;
    toggleCacheContainer();
}
           
function selectedLocatorResult(location) {

    try {
    
        var cookieName = "locatorResults";
        var locatorCookieString = readCookie(cookieName);
        var locationNewString = "1:" + location.name + ":" + location.x + ":" + location.y +":"+ escapeColon(location.crs)+ ";";
        var currentCookie = location.name + ":" + location.x + ":" + location.y + ":"+escapeColon(location.crs)+";";
        if ( locatorCookieString == null ) {

        // There are no cookies, add the new locator result as number 1                    
        writeCookie(cookieName, locationNewString)

        }
        else
        {
            var locatorCookieStrings = locatorCookieString.split(";");
            var dup = false;
            for ( var i = 0; i < locatorCookieStrings.length - 1; i++ ) {
    
                if ( i + 2 == 6 ) {
         
                    break;
                }
    
                var splitLocationResult = locatorCookieStrings[i].split(":")
    
                splitLocationResult[0] = i + 2;
                var newCookie =splitLocationResult[1] + ":" + splitLocationResult[2] + ":" + splitLocationResult[3] + escapeColon(splitLocationResult[4]) +";";
                if (currentCookie != newCookie )
                {
                        locationNewString += splitLocationResult[0] + ":" + splitLocationResult[1]
                        + ":" + splitLocationResult[2] + ":" + splitLocationResult[3]+ ":"+escapeColon(splitLocationResult[4]) + ";";
                }
                else
                {
                dup = true;
                }
            }
            if (!dup)
            {
                writeCookie(cookieName, locationNewString);
            }
        }
    }
    catch (e)
    {
        
    }
}

/**
 * Escapes colon with unicode string for cookies
 */
function escapeColon(/*String*/ value)
{
    if (value)
    {
        return value.replace(":","\\u003A");
    }
    else 
    {
        return "";
    }
}

/**
 * Unescapes colon with unicode string for cookies
 */
function unescapeColon(/*String*/ value)
{
    if (value)
    {
        return value.replace("\\u003A",":");
    }
    else 
    {
        return "";
    }
}

