﻿var mapSolution = null;
var MM = Microsoft.Maps;
$jQ(document).ready(function () {

    /* FrontSearch  */
    $jQ("#body").focus();
    $jQ("#txtFrontQuery").val($jQ("#hiddenSearchFieldDefaultText").val());
    $jQ("#txtFrontQuery").focus();


    $jQ("#txtFrontQuery").blur(function () {
        if ($jQ("#txtFrontQuery").val() == "") {
            $jQ("#txtFrontQuery").val($jQ("#hiddenSearchFieldDefaultText").val());
        }
    });


    $jQ("#txtQuery").val($jQ("#hiddenSearchFieldDefaultText").val());


    $jQ("#txtQuery").blur(function () {
        if ($jQ("#txtQuery").val() == "") {
            $jQ("#txtQuery").val($jQ("#hiddenSearchFieldDefaultText").val());
        }
    });
    $jQ("#txtQuery").focus(function () {
        if ($jQ("#txtQuery").val() == $jQ("#hiddenSearchFieldDefaultText").val()) {
            $jQ("#txtQuery").val('');
        }
    });




    /*Directions search*/

    $jQ("#hclInputTextDirectionsFrom").val($jQ("#hiddenLanguageDirectionFrom").val());

    $jQ("#hclInputTextDirectionsFrom").focus(function () {
        if ($jQ("#hclInputTextDirectionsFrom").val() == "") {
            $jQ("#hclInputTextDirectionsFrom").val($jQ("#hiddenLanguageDirectionFrom").val());
        }
    });


    $jQ("#hclInputTextDirectionsFrom").blur(function () {
        if ($jQ("#hclInputTextDirectionsFrom").val() == "") {
            $jQ("#hclInputTextDirectionsFrom").val($jQ("#hiddenLanguageDirectionFrom").val());
        }
    });

    $jQ("#divDirectionSuggestionDropdown").hide();

    $jQ('#hclInputTextDirectionsFrom').unbind("keydown").bind("keydown", function (event) {
        if ($jQ("#hclInputTextDirectionsFrom").val() == $jQ("#hiddenLanguageDirectionFrom").val()) {
            $jQ("#hclInputTextDirectionsFrom").val("");
        }
        switch (event.keyCode) {
            case 13:
                MakeGeocodeRequestDirection();
                return false;
                break;
            case 37:
            case 38:
            case 39:
                break;
            case 40:
                if ($jQ("#divDirectionSuggestionDropdown").html().length > 0) {
                    $jQ("#divDirectionSuggestionDropdown").show();
                    $jQ('#linkDirection_0').focus();
                    $jQ('#linkDirection_0').addClass('ComboBoxDropDownActive');
                }
                break;
            default:
                $jQ("#divDirectionSuggestionDropdown").html("");
                $jQ("#divDirectionSuggestionDropdown").hide();
                break;
        }
    });


    $jQ('#hclInputTextDirectionsTo').unbind("keydown").bind("keydown", function (event) {
        switch (event.keyCode) {
            case 13:
                return false;
                break;
            default:
                break;
        }
    });


    var sLat = $jQ("#hiddenBingMapCountryLatitude").val();
    var sLon = $jQ("#hiddenBingMapCountryLongitude").val();
    var sBingKey = $jQ("#hiddenBingMapKey").val();
    var map = null;
    // Initialize the map
    map = new Microsoft.Maps.Map(document.getElementById("divFrontMap"), { credentials: sBingKey, backgroundColor: '#ffffff', showCopyright: false, enableClickableLogo: false, disableKeyboardInput: true, disableTouchInput: true, showDashboard: false, showMapTypeSelector: false, showScalebar: false, fixedMapPosition: true, disableUserInput: true, disableMouseInput: false, enableSearchLogo: false });
    var loc = new Microsoft.Maps.Location(sLat, sLon);
    map.setView({ center: loc, zoom: 3 });


    $jQ('#txtFrontQuery').keydown(function (event) {

        if ($jQ("#txtFrontQuery").val() == $jQ("#hiddenSearchFieldDefaultText").val()) {
            $jQ("#txtFrontQuery").val("");
        }

        switch (event.keyCode) {
            case 13:
                MakeGeocodeRequestFront();
                return false;
                break;
            case 37:
            case 38:
            case 39:
                break;
            case 40:
                if ($jQ("#divFrontResult").html().length > 0) {
                    $jQ("#divFrontResult").show();
                    $jQ('#link_0').focus();
                    $jQ('#link_0').addClass('ComboBoxDropDownActive');
                }
                break;
            default:
                $jQ("#divFrontResults").html("");
                $jQ("#divFrontResult").hide();
                break;
        }
    });


    /* --- */




    $jQ('#txtQuery').keydown(function (event) {
        //  $jQ('#msg-keydown').html('keydown() is triggered!, keyCode = '
        //     + event.keyCode + ' which = ' + event.which);
        switch (event.keyCode) {
            case 13:

                MakeGeocodeRequest();
                return false;
                break;
            case 37:
            case 38:
            case 39:
                break;
            case 40:
                if ($jQ("#divResults").html().length > 0) {
                    $jQ("#divResults").show();
                    $jQ('#link_0').focus();
                    $jQ('#link_0').addClass('ComboBoxDropDownActive');
                }
                break;
            default:
                $jQ("#divResults").html("");
                $jQ("#divResults").hide();
                break;
        }
    });




    //Building default minimaps
    appendMiniMapToClinicList($jQ("#hiddenClinicShow").val());

    //Enable click event on big pins
    var objBigPins = $jQ("#divItemContainerList").find(".imgPinBigInactive, .imgPinBigActive");
    for (var i = 0; i < objBigPins.length; i++) {
        var sEnumValue = $jQ(objBigPins[i]).html();
        var arrUniqe = $jQ(objBigPins[i]).attr("id").split("_");
        var sLat = $jQ("#liDataItem_" + arrUniqe[1]).attr("lat");
        var sLon = $jQ("#liDataItem_" + arrUniqe[1]).attr("lon");
        $jQ(objBigPins[i]).bind("click", { IdUnique: arrUniqe[1], Enum: sEnumValue, Lat: sLat, Lon: sLon }, function (event) {
            showLargeMap(event.data.IdUnique, event.data.Lat, event.data.Lon);
        });
    }

    //If no results found we move search results div and search div to this div :P
    if ($jQ("#divOptionsBarNoResults").is(':visible')) {
        $jQ("#divOptionsBarNoResults").append($jQ("#divContainerSearchResultText"));
        $jQ("#divOptionsBarNoResults").append($jQ("#divContainerSearchInput"));
        $jQ("#divContainerSearchResultText").addClass("addSmallTopMargin")
    }
    else {
        $jQ("#divContainerSearchResultText").removeClass("addSmallTopMargin")

    }


    SetToolTipWithExternal(".divDistanceText", "#hiddenLanguageToolTipDistance")
    SetToolTip("#divContainerSearchResultText");

    window.setTimeout(function () {
        $jQ("#hclContent").find(".MapTypeId_m").find(":input").hide()
    }, 100);


    $jQ(".liPrintLink").click(function () {

        var sId = $jQ(this).attr("id");

        if (sId.length > 0) {
            sId = sId.replace("liPrintLink_", "");
        }


        $netminers.push(
        ['postPageView', 'HCL > ' + $jQ('#hiddenViewType').val() + ' > Print > ' + sId]
        );


    });

    //Used to determine if you are on the resultpage
    var sContainerResultDisplay = $jQ(".containerResult").attr("display");
    if (sContainerResultDisplay == "block") {


        $netminers.push(
        ['postPageView', 'HCL > pageResult > ' + $jQ("#hiddenBingMapCountryRegion").val() + ' > ' + encodeURIComponent($jQ("#hiddenSearchAddress").val())]
        );

    }




    $jQ('#hiddenViewType').val("list");

    $jQ('#divMenuList').click(function () {
        $jQ('#hiddenViewType').val("list");
        doShowListSolution();
        $netminers.push(
        ['postPageView', 'HCL > ListTab']
        );

    });

    $jQ('#divMenuMap').click(function () {
        $jQ('#hiddenViewType').val("map");
        doShowMapSolution();
        $netminers.push(
        ['postPageView', 'HCL > MapTab']
        );

    });

    $jQ('#divShowMoreClinics').click(function () {
        showMoreClinics();
        $netminers.push(
        ['postPageView', 'HCL > ' + $jQ('#hiddenViewType').val() + ' > show more clinics']
        );

    });

    $jQ('#divShowLessClinics').click(function () {
        showLessClinics();
        $netminers.push(
        ['postPageView', 'HCL > ' + $jQ('#hiddenViewType').val() + ' > show less clinics']
        );

    });



    $jQ('#btnFrontPageSearch').click(function () {
        var sSearch = $jQ('#txtFrontQuery').val();
        $netminers.push(
        ['postFieldChange', 'Search', sSearch]
        );
        MakeGeocodeRequestFront();
    });

    $jQ('#btnMainPageSearch').click(function () {
        var sSearch = $jQ('#txtQuery').val();
        $netminers.push(
        ['postFieldChange', 'Search', sSearch]
        );
        MakeGeocodeRequest();
    });

    $jQ('#hclSelectSortBy').change(function () {
        hclSortClinicsBy();
    });

    $jQ('.MinusImageButton').click(function () {

        resetAllFields();
    });






    var iCountriesReturnedFromConfig = $jQ('#hiddenNumberOfCountriesReturnedFromConfig').val() - 0;


    if (iCountriesReturnedFromConfig == 0) {//No records found.. what to do ?
        $jQ('#spanErrorNoData').show();
    }
    else if (iCountriesReturnedFromConfig == 1) {
        $jQ('#divFrontCountryDropdownContainer').hide();
        $jQ('#divFrontSearcContainer').show();

    }
    else {//More than 1 result - international sites will land here
        $jQ('#divFrontCountryDropdownContainer').show();
        $jQ('#divFrontSearcContainer').hide();
    }



    if ($jQ('#selectCountryDropdown').children().length == 2) {

    }



    //Preload images..
    (function ($) {
        var cache = [];
        // Arguments are image paths relative to the current page.
        $.preLoadImages = function () {
            var args_len = arguments.length;
            for (var i = args_len; i--; ) {
                var cacheImage = document.createElement('img');
                cacheImage.src = arguments[i];
                cache.push(cacheImage);
            }
        }
    })(jQuery)
    jQuery.preLoadImages("images/pin_magenta.png", "images/pin_magenta.png");
    jQuery.preLoadImages("images/pin_std.png", "images/pin_std.png");



})





