﻿var searchStarted = false;
var searchkeywords = "";

$(function () {
    $("#lblRestartSearch").hide();
    if (typeof window.event != 'undefined') // IE
    {
        $("#searchedTags").keydown(function (e) {
            var kc = e.which;
            if (kc == 13) {
                $("#cmdSearch").click();
                return false;
            }
            else {
                return true;
            }

        });
    }
    else {
        $("#searchedTags").keypress(function (e) {
            var kc = e.which;
            if (kc == 13) {
                $("#cmdSearch").click();
                return false;
            }
            else {
                return true;
            }

        });
    }

});

// This method warns the user that the research must be restarted to take 
// into account the new potention result following the connection.
function connectionNotificationSearchPage() {
    if (searchStarted) {
        $.saturneoNotification("Bienvenue, relancez la recherche pour voir les recommandations.");
    }
}
function checkEnter(e) { //e is event object passed from function invocation
}

function ReadLocationFromCookie() {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == "QJRSEARCHLOCATION") {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
                var splittedValues = cookie_value.split('|');
                if (splittedValues.length >= 3) {
                    $("#latitude").val(splittedValues[0]);
                    $("#longitude").val(splittedValues[1]);
                    $("#currentAddress").html(splittedValues[2]);
                }
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}

$(document).ready(function () {

    $.updnWatermark.attachAll();

    $("#cmdSearch").click(function (event) {
        //if (!searchStarted) {
        $('#divResultDetails').empty();
        $('#divResultDetails').attr('detailId', '');
        $('#divResultDetailsAccueil').fadeOut(1000, function () {
            $('#divResultDetailsNoDetails').fadeIn(3000);
        });
        //$('.search').removeClass('search-wide');
        //$('#searchedTags').css('overflow', 'auto');
        //}

        searchStarted = true;
        var returnValue;
        searchkeywords = $('#searchedTags').val();
        _gaq.push(['_trackPageview', '/Search?q=' + encodeURI(searchkeywords)]);
        returnValue = jAjaxSubmitOnclick(this, event, "/Search/GetExaleadSearchResults", "divLoader", "divResults");
        return returnValue;
    });

    ReadLocationFromCookie();
});

var config = {};
if (typeof (getHoverIntentConfig) != "undefined" && $.isFunction(getHoverIntentConfig)) {
    config = getHoverIntentConfig();
} else {
config = {
    sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
    interval: 200, // number = milliseconds for onMouseOver polling interval
    over: function () {
        var id = $(this).find('span.id').text();
        var currentDetailId = $("#divResultDetails").attr("detailId");
        if (currentDetailId != id) {

            // Hide index.aspx's page background
            $('#divResultDetailsNoDetails').hide();
            $('#divResultDetails').fadeOut(500, function () {
                var url = '/Professional/PageByBuildingProfessional/' + id;
                jAjaxSubmitOnclick($(this).find('.searchresulttext'), null, url, 'divLoader', "divResultDetails");
                $('#divResultDetails').fadeIn(500);
            });
            

            
            $("#divResultDetails").attr("detailId", id);

            // Display selection marks for the displayed detail
            var $element = $(this);
            if (!$element.is('tr[role="searchresult"]')) {
                $element = $element.parents('TR[role="searchresult"]:first');
            }
            $('table.searchresult .displayed').removeClass('displayed-on');
            $('table.searchresult .searchresulticon').removeClass('searchresulticon-selected').addClass('searchresulticon-unselected');
            $element.find('.displayed').addClass('displayed-on');
            $element.find('.searchresulticon').removeClass('searchresulticon-unselected').addClass('searchresulticon-selected');

            //$('table.searchresult .displayed').removeClass('displayed-on');
            //$(this).find('.displayed').addClass('displayed-on');
            //$('table.searchresult .searchresulticon').removeClass('searchresulticon-selected').addClass('searchresulticon-unselected');
            //$(this).find('.searchresulticon').removeClass('searchresulticon-unselected').addClass('searchresulticon-selected');

            // Ensure detail is displayed in the screen, even if user has scrolled through the list
            $("#divResultDetails").place(480);
            _gaq.push(['_trackEvent', 'SearchFocused', '_' + id + '_']);
        }
    }, // function = onMouseOver callback (REQUIRED)
    timeout: 500, // number = milliseconds delay before onMouseOut
    out: function () {

    } // function = onMouseOut callback (REQUIRED)    
};
}


