(function ($) {

    $.fn.activeTextInput = function (options, colorboxoptions) {
        return this.each(function () {
            // Use REL attribute as reference
            $(this).attr('rel', $(this).val());
            // Bind FOCUS event
            $(this).bind('focus', function () {
                if ($(this).val() == $(this).attr('rel')) {
                    if (this.name != "txtLink1")
                        $(this).val('');
                }
            });
            // Bind BLUR event
            $(this).bind('blur', function () {
                var _val = $(this).val().replace(/^\s+/, '').replace(/\s+$/, '');
                if (_val == null || _val == '') {
                    $(this).val($(this).attr('rel'));
                } else {
                    $(this).val(_val);
                }
            });
        });
    }
})(jQuery);

/**
*
* Validation Application
*
*/

var emcValidator = {

    // Define empty array for error objects
    errors: [],

    //  CSS predefined styles
    css: {
        // Styles for input when error(s) are found
        active: {
            current: { borderColor: '#F00' },
            parent: { borderColor: '#F00' }
        },
        // Styles for input on "normal" state
        inactive: {
            current: { borderWidth: '0px' },
            parent: { borderWidth: '0px' }
        }
    },

    // String selectors for jQuery's message elements
    selectors: {
        messageWrapper: '.error_message',
        messageList: '.error_list'
    },
    classNames: {
        errorState: 'error'
    },

    // Wrapper function for errors array.
    length: function () { return this.errors.length },

    // Create an errorObject, and append it to the error array.
    createError: function (i, m) {
        if (typeof m == 'function') {
            m = m.call(m, i);
        }
        var e = { id: i, message: m };
        this.errors.push(e);
    },

    // Create a wrapper for jQuery's id selector.
    // 
    // This Sharepoint creates random element id's based on UUID prefix.
    getElementById: function (id) {
        return $('#' + id);
    },
    getElementByAttr: function (attr, val) {
        return $('[' + attr + '=' + val + ']');
    },

    // Compare all elements in an array.
    //
    // $idArray$ an array of element IDs
    // $errorMessage$ a string to be displayed on error
    match: function (idArray, errorMessage) {
        var _A = "";
        for (var j = 0; j < idArray.length; j++) {
            if (j == 0) {
                _A = this.getElementById(idArray[j]).val();
            } else {
                var tmp = this.getElementById(idArray[j]);
                if (_A != tmp.val()) {
                    this.createError(idArray[j], errorMessage);
                }
            }
        }
    },


    func: function (idArray, execFunction, errorMessage) {
        if (typeof execFunction != "function") { return false; }
        for (var j = 0; j < idArray.length; j++) {
            if (!execFunction.call(this.getElementById(idArray[j]))) {
                this.createError(idArray[j], errorMessage);
            }
        }
    },
    /*
    eval: function(idArray,evalFunction,errorMessage) {
    if ( typeof evalFunction != "function" ) {
    return false;
    }
    for ( var j = 0; j < idArray.length ; j++ )
    {
    var v = evalFunction.call(this.getElementById(idArray[j]))
    if ( v == false ) 
    {
    this.createError(idArray[j],errorMessage);
    }
    }
    },
    */
    // Evaluate if element values is present.
    // Strips whitespace, and counts strings length
    //
    // $idArray$ an array of element IDs
    // $errorMessage$ a string to be displayed on error
    require: function (idArray, errorMessage) {
        for (var j = 0; j < idArray.length; j++) {
            var element = this.getElementById(idArray[j])
            switch (element.attr('type')) {
                case "checkbox":
                    if (!element.is(':checked')) {
                        this.createError(idArray[j], errorMessage);
                    }
                    break;

                case "select-one":
                    if (element.val() == "0") {
                        this.createError(idArray[j], errorMessage);
                    }
                    break;
                case "text":
                case "hidden":
                    var valueString = element.val().replace(/(\s)/, '');
                    if (valueString.length < 1) {
                        this.createError(idArray[j], errorMessage);
                    }
                    break;
                default: ;
            }
        }
    },


    // Pass element value through RegEx. Display error on null-match
    //
    // $idArray$ an array of element IDs
    // $errorMessage$ a string to be displayed on error
    reg: function (idArray, regEx, errorMessage) {
        for (var j = 0; j < idArray.length; j++) {
            var valueString = this.getElementById(idArray[j]).val();
            if (!regEx.test(valueString)) {
                this.createError(idArray[j], errorMessage);
            }
        }
    },

    // Resets all forms error messages
    // 
    // $callBack$ Executes a function if passed
    clear: function (callBack) {
        $(this.selectors.messageWrapper).hide();
        $(this.selectors.messageList).html(' ');
        for (var j = emcValidator.length(); j > 0; j--) {
            this.getElementById(this.errors[j - 1].id).removeClass(this.classNames.errorState);
            this.getElementByAttr('for', this.errors[j - 1].id).removeClass(this.classNames.errorState);
            this.errors.pop();
        }
        if (typeof callBack == "function") {
            callBack.call();
        }
    },

    // Displays all error messages
    // 
    // $onError$ Executes a function if passed, and errors where discovered
    render: function (onError) {
        if (this.length() == 0) { return true; }
        for (var j = 0; j < this.length(); j++) {
            this.getElementById(this.errors[j].id).addClass(this.classNames.errorState);
            this.getElementByAttr('for', this.errors[j].id).addClass(this.classNames.errorState);
            $(this.selectors.messageList).append('<li>' + this.errors[j].message + '</li>');
            $(this.selectors.messageWrapper).fadeIn();
        }
        if (this.length() > 0 && typeof onError == "function") {
            onError.call();
        }
        return false;
    }
}

var map;
var geocoder;
var directionDisplay;
var directionsService;
var init_address;
var init_searchaddress;
var init_radius;
var regionIDToLocate;
var Stores_All = [];
var WaitTime_All = [];
var gSearch;
var watcher;
var clinics = [];
var init_marker;
var clinic_icon = new google.maps.MarkerImage(
					'images/icons/markers/clinic.png',
					new google.maps.Size(34, 34),
					new google.maps.Point(0, 0),
					new google.maps.Point(17, 17)
				 );
var clinic_image = new google.maps.MarkerImage(
  'images/icons/markers/google-map-icon.png',
  new google.maps.Size(29, 47),
  new google.maps.Point(0, 0),
  new google.maps.Point(16, 30)
);

var clinic_image_shadow = new google.maps.MarkerImage(
  'images/icons/markers/google-map-shadow.png',
  new google.maps.Size(40, 32),
  new google.maps.Point(0, 0),
  new google.maps.Point(0, 15)
);
var gModal = new google.maps.InfoWindow;
var IsClinicForState = "False";
function init_map(lat, lon, radius) {
    var z = 15;
    switch (radius) {
        case 5: z = 12; break;
        case 10: z = 11; break;
        case 25: z = 10; break;
        case 50: z = 8; break;
    };
    var opts = {
        zoom: z,
        center: new google.maps.LatLng(parseFloat(lat), parseFloat(lon)),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("google_map_canvas"), opts);
    // Limit zoom
    google.maps.event.addListener(map, 'zoom_changed', function () {
        if (map.getZoom() < 8) {
            map.setZoom(8);
        }
    });
    return map;
}

function mark_clinic(location) {
    return new google.maps.Marker({
        icon: clinic_image,
        shadow: clinic_image_shadow,
        map: map,
        position: location
    });
}

function map_centered_at_address(address, radius) {
    var short_statename = null;
    if (!radius) radius = 5;
    init_radius = radius;
    init_searchaddress = address;
    geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': address, 'region': 'us' }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[0].address_components.length > 0) {
                for (address_count = 0; address_count < results[0].address_components.length; address_count++) {
                    if (results[0].address_components[address_count].short_name.length == 2 && results[0].address_components[address_count].short_name != "US") {
                        if (results[0].address_components[address_count + 1] != null && results[0].address_components[address_count + 1].long_name == "United States") {
                            short_statename = results[0].address_components[address_count].short_name;
                            break;
                        }
                    }
                }
            }
            if (short_statename != null) {
                IsClinicExistForState(short_statename);
            }
            else {
                IsClinicForState = null;
            }
            init_address = results[0].formatted_address;
            init_map(results[0].geometry.location.lat(), results[0].geometry.location.lng(), parseInt(radius));
            /* Mark starting point */
            init_marker = new google.maps.Marker({ map: map, position: results[0].geometry.location });
            google.maps.event.addListener(init_marker, 'click', function () {
                gModal.close();
                gModal.setContent('<p style="padding:0;margin:0;">' + results[0].formatted_address + '</span>');
                gModal.open(map, init_marker);
            });

        } else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
            $('.clinics:first').html(false);
            $('#location_search_summary').html(clinics.length + " Locations within " + init_radius + " miles of " + init_address);
            $('#dvSearchResultAlerts').html('There are no Take Care Clinics located in this state. To search again, please enter a valid city <u>and</u> state or zip code.');
            $('#dvSearchResult').show();
            $('#google_map_canvas').hide();

        } else {
            try {
                console.log("Geocode was not successful for the following reason: " + status);
            } catch (no_console) { /* Silence */ }
        }
    });
}

function map_by_address(address, radius,Latitude,Longitude) {

    geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': address, 'region': 'us' }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            $('#location_error_message').hide();
            var latlng;
            if (Latitude != null && Longitude != null) {
                init_map(Latitude, Longitude);
                latlng = new google.maps.LatLng(Latitude, Longitude);
            }
            else {
                init_map(results[0].geometry.location.lat(), results[0].geometry.location.lng());
                latlng  = results[0].geometry.location
            }

            var clinic_marker = mark_clinic(latlng)
            var address1 = "";
            var address2 = "";

            for (i = 0; i < results[0].address_components.length; i++) {
                if (results[0].address_components[i].types[0] == "street_number")
                    address1 = address1 + results[0].address_components[i].short_name + ' ';

                if (results[0].address_components[i].types[0] == "route")
                    address1 = address1 + results[0].address_components[i].short_name + ' ';

                if (results[0].address_components[i].types[0] == "locality")
                    address2 = address2 + results[0].address_components[i].short_name + ', ';
                if (results[0].address_components[i].types[0] == "administrative_area_level_1")
                    address2 = address2 + results[0].address_components[i].short_name + ', ';
                if (results[0].address_components[i].types[0] == "country")
                    address2 = address2 + results[0].address_components[i].short_name + ' ';
                if (results[0].address_components[i].types[0] == "postal_code")
                    address2 = address2 + results[0].address_components[i].short_name + ' ';
            }
            //            var address1 = results[0].address_components[0].short_name + ' ' + results[0].address_components[1].short_name;
            //            var address2 = results[0].address_components[2].short_name + ', ' + results[0].address_components[5].short_name + ' ' + results[0].address_components[7].short_name;
            HookPopupToMarker(clinic_marker, address1, address2);
        } else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
            $('#location_error_message').html('<p>Unable to find Clinic at this address</p>').fadeIn()
        } else {
            try {
                console.log("Geocode was not successful for the following reason: " + status);
            } catch (no_console) { /* Silence */ }
        }
    });
}

function HookPopupToMarker(clinic_marker, address1, address2) {
    google.maps.event.addListener(clinic_marker, 'click', function () {
        gModal.close();
        var html_str = '<div class="clinic nonumber">\
   		<div class="clinic_description">\
		<div class="clinic_full_address">\
		<h6><a>' + $('[id$=_lblAddress1]')[0].innerHTML + '</a></h6>';
        if ($('[id$=_lblAddress3]')[0].innerHTML.length > 0) {
            html_str = html_str + '<p>' + $('[id$=_lblAddress3]')[0].innerHTML + '</p>';
        }
        html_str = html_str + '<p>' + $('[id$=_lblAddress2]')[0].innerHTML + '</p>\
		</div>\
		<div class="clear"></div>\
		<div class="meta"><strong>Wait Time:</strong> <span>' + $('[id$=_dvWaitTimeDesc]')[0].innerHTML + '</span></div>\
		</div></div>';
        html_str = '<div id="info_wrapper" style="text-align:left" >' + html_str;
        gModal.setContent('<p style="padding:0;margin:0;">' + html_str + '</p>');
        html_str += '</div>';
        gModal.open(map, clinic_marker);
    })
}

function updateMapByBounds() {
    watcher = setInterval(function () {
        $('#location_search_summary').html(' ');
        $('.clinics:first').html(false);
        // return if map is not loaded
        if (!map) return;
        // exit loop
        clearInterval(watcher);

        var _max_distance = 0.0;
        var bounds = map.getBounds();
        data = [];
        Stores_All = [];
        WaitTime_All = [];
        // Clear all the clinics stored in the array based on th previous search.
        clinics = [];
        //WI-#1724 Since we are using T_QueueWaitTime to get the store wait time, the below function has been commented
        //LoadWaitTime();
        LoadStoresForRegion();

        for (var _i = 0; _i < Stores_All[0].length; _i++) {
            //if (bounds.contains(new GLatLng(Stores_All[0][_i].Latitude, Stores_All[0][_i].Longitude))) {
            data.push(Stores_All[0][_i]);
            //}
        }
        if (data.length > 0) {
            $('.clinics:first').html(false);
            clinics = [];
            toRemove = [];
            for (var _count = 0; _count < data.length; _count++) {
                var clinic = new Clinic(data[_count]);
                if (parseFloat(clinic.distance_from_search_point) > parseFloat(init_radius)) {
                    if (clinic != null) {
                        try {
                            clinic.marker_.setVisible(false);
                            init_marker.setVisible(false);
                        }
                        catch (marker_err) { }
                    }
                }
                else {
                    clinics.push(clinic);
                }
            }


            //clinics.sort(function(a, b) { return parseFloat(a.distance_from_search_point) > parseFloat(b.distance_from_search_point) });


            for (i = 0; i < clinics.length - 1; i++) {
                for (j = i + 1; j < clinics.length; j++) {
                    if (parseFloat(clinics[i].distance_from_search_point) > parseFloat(clinics[j].distance_from_search_point)) {
                        temp = clinics[i];
                        clinics[i] = clinics[j];
                        clinics[j] = temp;
                    }
                }
            }

            $('.clinics:first').html(false);
            for (var icount = 0; icount < clinics.length; icount++) {
                $('.clinics:first').append(clinics[icount].result_node);

            }
            if (clinics.length > 0) {
                var date = new Date();
                date.setTime(date.getTime() + (30 * 24 * 60 * 60 * 10000));
                var expires = "; expires=" + date.toGMTString();
                if (document.cookie.indexOf('ClosestClinic') == -1) {
                    document.cookie = "ClosestClinic" + "=" + clinics[0].data.StoreId + expires + "; path=/";
                }
            }
            $('#location_search_summary').html(clinics.length + " Locations within " + init_radius + " miles of " + init_address);

        } else {
            $('.clinics:first').html(false);
            $('#location_search_summary').html(clinics.length + " Locations within " + init_radius + " miles of " + init_address);
            if (IsClinicForState == "True") {
                $('#dvSearchResultAlerts').html('Please narrow your search by entering a valid city <u>and</u> state or zip code.');
                $('#dvSearchResult').show();
                $('#google_map_canvas').hide();
            }
            else if (IsClinicForState == "False") {
                $('#dvSearchResultAlerts').html('There are no Take Care Clinics located in this state. To search again, please enter a valid city <u>and</u> state or zip code.');
                $('#dvSearchResult').show();
                $('#google_map_canvas').hide();
            }
            else if (IsClinicForState == null) {
                $('#dvSearchResultAlerts').html('There are no Take Care Clinics located in this state. To search again, please enter a valid city <u>and</u> state or zip code.');
                $('#dvSearchResult').show();
                $('#google_map_canvas').hide();
            }
        }


    }, 333);
}


function IsStoreActive(dateValue) {
    var Storeyear = dateValue.substr(0, 4);
    var Storemonth = dateValue.substr(4, 2);
    var Storedate = dateValue.substr(6, 2);
    var dd = Storedate;
    var mm = Storemonth;
    var yy = Storeyear;
    var correctDate = mm + "-" + dd + "-" + yy;

    var date1 = new Date(correctDate);
    var date2 = new Date();
    date3 = Date.parse(date1) - Date.parse(date2);
    date3 = Math.round(date3 / 1000 / 60 / 60 / 24);
    return (date3 > 0);
}

function SortSearchResults() {
    if ($('#hdSortOrder').val() == null)
        $('#hdSortOrder').val('asc');
    else if ($('#hdSortOrder').val() == 'asc')
        $('#hdSortOrder').val('desc');
    else
        $('#hdSortOrder').val('asc');

    if (clinics.length > 0) {
        if ($('#hdSortValue').val() == 'Time') {
            //clinics.sort(function(a, b) { return parseFloat(a.data.WaitTime) <= parseFloat(b.data.WaitTime) });

            for (i = 0; i < clinics.length - 1; i++) {
                for (j = i + 1; j < clinics.length; j++) {
                    if ($('#hdSortOrder').val() == 'asc') {
                        if (parseFloat(clinics[i].data.WaitTime) > parseFloat(clinics[j].data.WaitTime)) {
                            temp = clinics[i];
                            clinics[i] = clinics[j];
                            clinics[j] = temp;
                        }
                    }
                    else {
                        if (parseFloat(clinics[i].data.WaitTime) < parseFloat(clinics[j].data.WaitTime)) {
                            temp = clinics[i];
                            clinics[i] = clinics[j];
                            clinics[j] = temp;
                        }
                    }
                }
            }
        }
        else {
            //clinics.sort(function(a, b) { return parseFloat(a.distance_from_search_point) <= parseFloat(b.distance_from_search_point) });

            for (i = 0; i < clinics.length - 1; i++) {
                for (j = i + 1; j < clinics.length; j++) {
                    if ($('#hdSortOrder').val() == 'asc') {

                        if (parseFloat(clinics[i].distance_from_search_point) > parseFloat(clinics[j].distance_from_search_point)) {
                            temp = clinics[i];
                            clinics[i] = clinics[j];
                            clinics[j] = temp;
                        }
                    }
                    else {
                        if (parseFloat(clinics[i].distance_from_search_point) < parseFloat(clinics[j].distance_from_search_point)) {
                            temp = clinics[i];
                            clinics[i] = clinics[j];
                            clinics[j] = temp;
                        }
                    }
                }
            }
        }

        $('.clinics:first').html(false);
        for (var _i = 0; _i < clinics.length; _i++) {
            $('.clinics:first').append(clinics[_i].result_node);

        }
    }

}

// Ajax call to load all stores from the xml file.
function LoadStoresForRegion() {
    //TODO - Populate the Locations from xml instead of hardcoding the array. 
    // TODO - Updated for WI-1738-CWS-Release3 Enhancement.
    $.ajax({ async: false, type: "GET", url: "WaitTimeHandler.ashx?GoogleMapBounds=" + map.getBounds().toUrlValue(), error: onErrorXML,
        dataType: "json", success: GetStores
    });
}


// Add all stores in the xml into the Stores_All array which will hold all store information.
// These store information in the xml elements will be converted to JSON objects before adding the same to the colelction.
function GetStores(/*xml*/valJSON) {
    // TODO - Updated for WI-1738-CWS-Release3 Enhancement.
    Stores_All.push(valJSON);
}


function GetStoreWaitTime(time) {
    return time;
}

//This method handles the error that might occur while reading the 
// XML file to get the store information. 
function onErrorXML(xhr, ajaxOptions, thrownError) {

}

function GetQWaitTime(storeID) {
    return ($.ajax({ async: false, type: "GET", url: "WaitTimeHandler.ashx?StoreId=" + storeID, error: onErrorXML,
        dataType: "text", success: function (time) { GetStoreWaitTime(time); }
    }));
}
// The function is used to convert the XML element to JSON object.
// Each Store will converted to JSON object so that each attribute 
// becomes a property which makes parsing easy.
function XMLtoJSON(xml) {

    var JSONObj = new Object;
    for (var _j = 0; _j < xml.attributes.length; _j++) {

        var name = xml.attributes[_j].name;
        var value = xml.attributes[_j].value;
        JSONObj[name] = value;
        //        if (name == "StoreID") { JSONObj["WaitTime"] = GetWaitTimeForStore(value); }
    }
    return JSONObj;
}

//WI-#1724 Since we are using T_QueueWaitTime to get the store wait time, the below function has been commented
//function WaitTimetoJSON(xml) {

//    var JSONObj = new Object;
//    for (var _j = 0; _j < xml.attributes.length; _j++) {

//        var name = xml.attributes[_j].name;
//        var value = xml.attributes[_j].value;
//        JSONObj[name] = value;

//    }

//    return JSONObj;
//}

//function GetWaitTimeForStore(storeID) {
//    for (var _i = 0; _i < WaitTime_All.length; _i++) {
//        if (WaitTime_All[_i].StoreId == storeID) {
//            return WaitTime_All[_i].WT
//        }
//    }
//}
function map_directions(_start, latitude, longitude) {
    watcher = setInterval(function () {

        // return if map is not loaded
        if (!map) return;
        // exit lop
        clearInterval(watcher);
        var endpoint = new google.maps.LatLng(latitude, longitude);
       
        
        directionsService = new google.maps.DirectionsService();

        var opts = {
            origin: _start,
            destination: endpoint,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        };
        directionsService.route(opts, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                /* Initialize dipslay ( We're assuming that {map} is defined ) */
                directionsDisplay = new google.maps.DirectionsRenderer();
                directionsDisplay.setMap(map);
                directionsDisplay.setDirections(response);

                /* Clear inner HTML */
                $('.directions:first').html('')

                /* Gather end points */
                var start_address = $('<div>').addClass('end_point').addClass('point_a').html(response.routes[0].legs[0].start_address);
                var end_address = $('<div>').addClass('end_point').addClass('point_b').html(response.routes[0].legs[0].end_address);

                /* Add starting point */
                $('.directions:first').append(start_address);

                /* Gather steps */
                var steps = response.routes[0].legs[0].steps;

                /* Loop through all steps, and build HTML */
                for (var index = 0; index < steps.length; index++) {
                    var number = $('<div>').addClass('point_number').html(index + 1+"<noscript></noscript>");
                    // var duration = $('<div>').addClass('point_duration').html(steps[index].durations.text);
                    var distance = $('<div>').addClass('point_distance').html(steps[index].distance.text + "<noscript></noscript>");
                    var description = $('<div>').addClass('point_description').html(steps[index].instructions + "<noscript></noscript>");
                    var point = $('<div>').addClass('point b').append(number).append(distance).append(description);
                    $('.directions:first').append(point);
                }

                /* 	Add End point */
                $('.directions:first').append(end_address);

                /* Build & Insert direction summary */
                $('#directions_summary').html(response.routes[0].legs[0].distance.text + ' (' + response.routes[0].legs[0].duration.text + ')');
            } else if (status == google.maps.DirectionsStatus.ZERO_RESULTS) {
                throw_no_directions();
            } else if (status == google.maps.DirectionsStatus.NOT_FOUND) {
                throw_no_directions();
            }
        });
    }, 333);
}

function find_locations() {
    watcher = setInterval(function () {
        // return if map is not loaded
        if (!map) return;
        // exit loop
        clearInterval(watcher);
        updateMapByBounds();
        google.maps.event.addDomListener(map, 'idle', function () { });
    }, 333);
}

function Clinic(data) {
    var _self = this;
    _self.data = data;
    //WI-#1724 Google API Enahancement
    //_self.storewaittime = GetQWaitTime(_self.data.StoreID).responseText.split(",");
    _self.storewaittime = GetQWaitTime(_self.data.StoreId).responseText.split(",");

    _self.data.WaitTime = _self.storewaittime[0];
    _self.dom_id = _self.gen_id();
    _self.wait_time = _self.wait_time();
    _self.marker_ = _self.marker();
    _self.distance_from_search_point = _self.distance();
    _self.init_radius = init_radius;
    _self.result_node = _self.node();

    if (parseFloat(_self.distance_from_search_point) < init_radius) {
        $('.clinics:first').append(_self.result_node);
        google.maps.event.addDomListener(document.getElementById(_self.dom_id), 'click', function () {
            _self.select();
        });
    }
}
Clinic.prototype.gen_id = function () {
    return 'clinic_' + clinics.length;
}
Clinic.prototype.marker = function () {
    var _self = this;
    if (_self.marker_) return _self.marker_;
    var _m = _self.marker_ = new google.maps.Marker({
        //WI-#1724 Google API Enahancement
        //position: new google.maps.LatLng(parseFloat(_self.data.lat), parseFloat(_self.data.lng)),
        position: new google.maps.LatLng(parseFloat(_self.data.Latitude), parseFloat(_self.data.Longitude)),
        icon: clinic_image,
        shadow: clinic_image_shadow,
        map: map
    });
    google.maps.event.addListener(_m, "click", function () {
        _self.select();
    });
    return _m;
}
Clinic.prototype.select = function () {
    gModal.close();
    this.selected_ = true;
    gModal.setContent(this.html(true));
    gModal.open(map, this.marker_);
    $('#directions_start').bind('focus', function () { $(this).val('') });
}
Clinic.prototype.html = function (include_search) {
    var _self = this;
    if (_self.data.WaitTime == "") {
        _self.data.WaitTime = 9999;
    }

    var html_str = '<div id="' + (include_search ? 'a_' + _self.dom_id : _self.dom_id) + '" class="clinic ' + (include_search ? 'no_number' : '') + '">\
		<div class="clinic_number"></div>\
		<div class="clinic_description">\
		<div class="clinic_full_address">\
		<h6><a href="ClinicDefaultAndDirections.aspx?Reg=' + _self.data.MarketId + '&Str=' + _self.data.StoreId + '&Loc=' + init_searchaddress + '&LocDist=' + _self.init_radius + '" >' + _self.data.Address1 + '</a></h6>\
		<p style="clinic_results_address">' + (_self.data.Address2.length > 0 ? _self.data.Address2 + '<br />' : '') + _self.data.City + ' ' + _self.data.State + ' ' + _self.data.ZipCode + '</p>\
        </div>\
        <div class="clinic_distance">' + (_self.distance_from_search_point ? _self.distance_from_search_point + ' mi(s)' : '') + '</div>\
        <div class="clear"></div>\
		<div class="meta"><strong>Wait Time:</strong> ' + _self.wait_time + ' </div>\
		</div>\
		</div>';
    if (include_search) {
        html_str = '<div id="info_wrapper" style="text-align:left" >' + html_str;
        html_str += _self.directions_from();
        html_str += '</div>';
    }

    return html_str;
}
Clinic.prototype.wait_time = function () {

    //WI - 1738 Code Clean Up for the WaitTime functionality
    var _self = this;
    
        var _r = "";
        if (_self.storewaittime.length > 0) {
            _r = '<span>' + _self.storewaittime[2] + '</span>';
        }   

    return _r;
};
Clinic.prototype.directions_from = function () {
    var _self = this;
    var html_str = '<a class="with_arrow" href="ClinicDefaultAndDirections.aspx?Reg=' + _self.data.MarketId + '&Str=' + _self.data.StoreId + '&Loc=' + init_searchaddress + '&LocDist=' + _self.init_radius + '" >Details</a>\
		<div class="gray_line"></div>\
		<form class="in_info_window" action="ClinicDefaultAndDirections.aspx" method="GET">\
		<label for="directions_start">City and State or ZIP</label>\
		<input type="text" class="text_input_142x25" name="start" id="txtDirectionsStart" value="City and State or ZIP" onfocus="clearText(this);" onblur="clearText(this);" />\
        <input type="hidden" name="Reg" id="Reg" value="' + _self.data.MarketId + '" />\
        <input type="hidden" name="Str" id="Str" value="' + _self.data.StoreId + '" />\
        <input type="hidden" name="Loc" id="Loc" value="' + init_searchaddress + '" />\
		<input class="go_button" type="submit" value="Go" />\
		</form>';
    return html_str;
}

Clinic.prototype.rad = function (x) { return x * Math.PI / 180; }

Clinic.prototype.distance = function () {
    if (this.distance_from_search_point) return this.distance_from_search_point;
    if (typeof init_marker != 'object') return false;
    var _self = this;
    var p1 = _self.marker_.getPosition();
    var p2 = init_marker.getPosition();
    var R = 6371;
    var m = 0.621371192;
    var d_lat = _self.rad(p2.lat() - p1.lat());
    var d_long = _self.rad(p2.lng() - p1.lng());

    var a = Math.sin(d_lat / 2) * Math.sin(d_lat / 2) +
		  Math.cos(_self.rad(p1.lat())) * Math.cos(_self.rad(p2.lat())) * Math.sin(d_long / 2) * Math.sin(d_long / 2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    var d = R * c;

    return parseFloat((d * m).toFixed(1));
}
Clinic.prototype.node = function () {
    if (this.result_node) return this.result_node;
    return this.html(false);
}



function throw_no_locations() {
    $('#location_error_message').html('<p>Unable to find Clinic at this address</p>').fadeIn()
    $('#locations_list .clinics').html('');
    $('#locations_list .darkblue_title').removeClass('darkblue_title').addClass('error_title').html('Unable to locate clinics')
	.append($('<span>')).after($('<div>').addClass('smaller_error').html('Try searching 5-digit zipcode'));
    if (!!map) {
        var z = map.getZoom()
        if (z < 8) { return false };
        map.setZoom(z - 1);
    }
}
function throw_no_directions() {
    $('#directions_summary').prev().removeClass('darkblue_title').addClass('error_title');
    $('#directions_summary').removeClass('smaller_darkblue').addClass('smaller_error').html('Unable to locate starting address');
    $('.directions:first').html('');
}

$(document).ready(function () {

    //$('#header').bind('mouseout', function() {
    //    if ($('#location_drop_down').is(':visible')) {
    //        $('#location_drop_down').slideUp('fast')
    //    }
    //});

    $('#main_nav .locations').each(function () {
        $('#location_drop_down').css({
            left: $(this).position().left,
            top: $(this).height() + 95
        });
    }).bind('mouseenter', function () {
        $('#location_drop_down').slideToggle('fast');
    });
    $('#wrapper').bind('mouseleave', function () {
        if ($('#location_drop_down').is(':visible')) {
            $('#location_drop_down').slideUp('fast')
        }
    });
    $('#top_panel').bind('mouseover', function () {
        if ($('#location_drop_down').is(':visible')) {
            $('#location_drop_down').slideUp('fast')
        }
    });
    $('#ctl00_lnkPrice').bind('mouseover', function () {
        if ($('#location_drop_down').is(':visible')) {
            $('#location_drop_down').slideUp('fast')
        }
    });
    $('#content').bind('mouseover', function () {
        if ($('#location_drop_down').is(':visible')) {
            $('#location_drop_down').slideUp('fast')
        }
    });


    $('input:text').activeTextInput();

    $('.hedis_rating, .hedis_help').bind('click', function () {
        $.get('xml/hedis_modal.xml', function (resp, status) {
            if (status == 'success') {
                openModal($('content:first', resp).text());
            }
        }, 'xml');
        return false;
    });

    $('.download_media a').hover(function () {
        $(this).css({ borderBottomWidth: 0 }).next('p.active_note').slideDown();
    }, function () {
        $(this).css({ borderBottomWidth: 1 }).next('p.active_note').slideUp();
    })

    /*
    $('.active_content_toggle p').hide();
    $('.active_content_toggle h6').hover(function(){
    $(this).next('p').slideDown('fast');
    }, function(){
    $(this).next('p').slideUp('fast');
    });
    */
});

function openModal(html_str) {
    var overlay = $('#modal_overlay').length > 0 ? $('#modal_overlay') : $('<div>').attr('id', 'modal_overlay').appendTo('body');
    var modal = $('#modal').length > 0 ?
			$('#modal').html('<div id="modal_bottom"><div id="modal_middle">' + html_str + '</div></div>')
		  : $('<div>').attr('id', 'modal').appendTo('body').html('<div id="modal_bottom"><div id="modal_middle">' + html_str + '</div></div>');
    var close_button = $('#modal_close_button').length > 0 ? $('#modal_colse_button') : $('<a>').attr({ 'id': 'modal_close_button', 'href': '#' }).appendTo(modal);
    var browserWidth = $(window).width();
    var browserHeight = $(window).height();
    overlay.css({ width: browserWidth, height: browserHeight });
    modal.css({ left: (browserWidth / 2) - ($(modal).width() / 2), top: (browserHeight / 2) - ($(modal).height() / 2) });
    overlay.fadeIn('fast');
    modal.fadeIn('fast');
    $('#modal_overlay').bind('click', closeModal);
    $(close_button).bind('click', closeModal);
}
function closeModal() {
    $('#modal_overlay, #modal').fadeOut('fast');
    $('#modal').unbind('mouseleave');
    $('#modal_overlay').unbind('click');
    return false;
}

function clearText(field) {

    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;
}


function setDropDownHeight() { // set height of drop down if the id "dvNearestStore" exists (which is displayed conditionally if cookie has a clinic to show (ZN)
    if (document.getElementById('dvNearestStore')) {
        document.getElementById('location_drop_down').className = "with_clinic";
    } else {
        document.getElementById('location_drop_down').className = "without_clinic";
    }
}

function IsClinicExistForState(stateName) {
    $.ajax({ async: false, type: "GET", url: "WaitTimeHandler.ashx?StateName=" + stateName, error: onErrorXML,
        dataType: "text", success: returnOutput
    });
}
function returnOutput(output) {
    IsClinicForState = output;
}
