﻿//**************************************************************************************************************************************************************************************
//                                                                          MAP AND BUBBLE CODE START
//**************************************************************************************************************************************************************************************
var _currentArea = null; //Tracks the current area moused over
var _bubble = null; //Store the bubble reference in variable so we get faster page response times on mouseover and mouseout events
var _bubbleTable = null;
var _bubbleHoverOn = false;
var _bubbleDelayMax = 5;
var _bubbleDelayCount = 0;
var _jurisdictions = new Array();

//Cache images for mouseovers, we do not need to cache the flag since they are loaded into the dropdown so they would already be in the cache:
var _updatedBoxImage = new Image(72, 26);
var _websiteLinkOutImage = new Image(73, 9);
var _websiteLinkOverImage = new Image(73, 9);
_updatedBoxImage.src = "images/usmap/updated_box.png";
_websiteLinkOutImage.src = "images/usmap/website_link_out.png";
_websiteLinkOverImage.src = "images/usmap/website_link_over.png";

//--------------------------------------------------------------------------------------------------------------------------------------------------
//ONLOAD - Event:
if (window.onload) {
    var funWindowLoad = window.onload;
    window.onload = new Function("funWindowLoad(); Window_OnLoad();");
}
else
    window.onload = Window_OnLoad;
//--------------------------------------------------------------------------------------------------------------------------------------------------
function Window_OnLoad() {

    //Set the _bubble and _bubbleTable objects for quick reference later:
    _bubble = document.getElementById("divBubble");
    _bubbleTable = document.getElementById("tblBubble");

    //Parse the data stored in the hidden input control hidJurisdictionData into an array of client side Jurisdiction objects:
    var data = HtmlDecode(document.frmMain.hidJurisdictionData.value);

    //Split the data into rows of data:
    var rows = data.split("<data_row>");

    //Loop thru each row and split into columns and then build the client side jurisdiction array that will be used
    //by the help bubble whenever it is shown for a specific jurisdiction:
    for (var r = 0; r < rows.length; r++) {

        var cols = rows[r].split("<data_col>");
        _jurisdictions[_jurisdictions.length] = new Jurisdiction(cols[0], cols[1], cols[2], cols[3], cols[4], cols[5], cols[6], cols[7], cols[8], cols[9], cols[10]);

    }    

}
//--------------------------------------------------------------------------------------------------------------------------------------------------
//Define the client side Jurisdiction class and its parameters:
function Jurisdiction(jurisdictionId,
                      jurisdictionName,
                      agencyName,
                      division,
                      address,
                      city,
                      stateCode,
                      postalCode,
                      phone,
                      website,
                      isUpdated) {

    this.JurisdictionId = CInt(jurisdictionId);
    this.JurisdictionName = jurisdictionName;
    this.AgencyName = agencyName;
    this.Division = division;
    this.Address = address;
    this.City = city;
    this.StateCode = stateCode;
    this.PostalCode = postalCode;
    this.Phone = phone;
    this.Website = website;
    this.IsUpdated = CBln(isUpdated);

}
//--------------------------------------------------------------------------------------------------------------------------------------------------
function GetJurisdictionByMapAreaId(areaId) {

    //Cleanup name for DC and Hawaii:
    if (areaId.indexOf("DistrictOfColumbia") != -1) areaId = "mapDistrictOfColumbia";
    if (areaId.indexOf("Hawaii") != -1) areaId = "mapHawaii";

    for (var i = 0; i < _jurisdictions.length; i++) {

        if ("map" + Replace(_jurisdictions[i].JurisdictionName.toLowerCase(), " ", "") == areaId.toLowerCase()) {

            return _jurisdictions[i];

        }

    }

    return null;

}
//--------------------------------------------------------------------------------------------------------------------------------------------------
function MapArea_MouseOver(area, e) {

    //Reset the bubble hide delay:
    ResetBubbleDelay();

    //Check to see if the the bubble is already displayed for this area, if so, then do not move it:
    if (_currentArea != null) {

        if (_currentArea.id == area.id) return;

    }

    //Its a new area so update the _currentArea value:
    _currentArea = area;   

    //If its hidden, then show it:
    if (_bubble.style.visibility.toLowerCase() == "hidden") _bubble.style.visibility = "visible";

    //Get the browser based x and y coordinates of the mouse cursor (Note IE needs scroll position to be considered, other browsers do not):
    var xPos = (e.x == null ? e.pageX : e.x + document.body.scrollLeft);
    var yPos = (e.y == null ? e.pageY : e.y + document.body.scrollTop) - _bubble.offsetHeight;

    //Load the bubble's html into its table object via the dom tree:
    var jurisdiction = GetJurisdictionByMapAreaId(area.id);   

    //Set the bubble controls::
    document.getElementById("tdBubble_JurisdictionName").innerHTML = jurisdiction.JurisdictionName;
    document.images["imgBubble_UpdatedBox"].src = (jurisdiction.IsUpdated ? "images/usmap/updated_box.png" : "images/usmap/1ptrans.gif");
    document.images["imgBubble_Flag"].src = "images/usmap/customflags/stateflag_" + Replace(jurisdiction.JurisdictionName.toLowerCase(), " ", "-") + ".png";

    document.getElementById("tdBubble_Location").innerHTML = jurisdiction.AgencyName + "<br />" +
                                                             jurisdiction.Division + "<br />" +
                                                             jurisdiction.Address + "<br />" +
                                                             jurisdiction.City + ", " + jurisdiction.StateCode + " " + jurisdiction.PostalCode + "<br />" +
                                                             "Phone: " + jurisdiction.Phone;

    document.getElementById("lnkBubble_Website").href = jurisdiction.Website;

    //Show the bubble at the new coordinates:
    _bubble.style.left = xPos;
    _bubble.style.top = yPos;

}
//--------------------------------------------------------------------------------------------------------------------------------------------------
function MapArea_MouseOut(area) {

    _bubbleHoverOn = false;

    HideBubbleWithDelay();

}
//--------------------------------------------------------------------------------------------------------------------------------------------------
function Bubble_MouseOver() {

    ResetBubbleDelay();

}
//--------------------------------------------------------------------------------------------------------------------------------------------------
function Bubble_MouseOut() {

    _bubbleHoverOn = false;

    HideBubbleWithDelay();

}
//--------------------------------------------------------------------------------------------------------------------------------------------------
function ResetBubbleDelay() {

    _bubbleDelayCount = 0;
    _bubbleHoverOn = true;

}
//--------------------------------------------------------------------------------------------------------------------------------------------------
function HideBubbleWithDelay() {

    if (!_bubbleHoverOn) {

        _bubbleDelayCount = _bubbleDelayCount + 1;


        if (_bubbleDelayMax >= _bubbleDelayCount) {

            window.setTimeout("HideBubbleWithDelay();", 100);

        }
        else {

            //Null out the current area object:
            _currentArea = null;

            //Hide the bubble:
            _bubble.style.visibility = "hidden";

            //Reset the delay for future use:
            ResetBubbleDelay();

        }

    }

}

//**************************************************************************************************************************************************************************************
//                                                                          MAP AND BUBBLE CODE END
//**************************************************************************************************************************************************************************************

//Telerik.RadComboBox cmbJurisdictions.OnClientSelectedIndexChanged event handler: 
function cmbJurisdictions_SelectedIndexChanged(radComboBox, e) {

    //Get the JurisdictionId stored in the value of the RadComboBoxItem and make sure its not 0, 
    //if 0, then simply return as they haven't selected a state to view yet:
    if (radComboBox.get_value() == "0") return;

    //If we are here, then they selected a state so let's get the state name and build the url of the state-registered-agents.com site to view:
    var jurisdictionName = Replace(radComboBox.get_text(), " ", "-").toLowerCase();
    var url = "http://www." + jurisdictionName + "-registered-agents.com/";

    document.location.href = url;

}
//--------------------------------------------------------------------------------------------------------------------------------------------------
