﻿/* Gestion de la recherche principale */
jQuery(document).ready(function() {
    /* Init*/
    Init();
    /* EVENEMENTS */
    /* Home */
    jQuery(".Button.Inactif").click(ChangePanel);
    /* Liens generaux sur la home */
    jQuery(".HomeFormBottom").hover(HomeFormBottomHover, HomeFormBottomOut);
    /* Detail Search */
    jQuery(".SearchDepButton").click(SearchDepButtonClick);
});

/* Ajout timeout sur jQuery */
jQuery.fn.idle = function(time) {
    var o = $(this);
    o.queue(function() {
        setTimeout(function() {
            o.dequeue();
        }, time);
    });
    return this;
};

/* Initialisation */
function Init() {
    /* Panel preselectionne sur la home */
    InitSelectedTab();
    /* Panel preselectionne sur la home */
    InitLinkPanel();
    /* Auto Completion sur la ville */
    InitAllAutoComplete();
};

function InitLinkPanel() {
    jQuery(".HomeFormBottom").data("isOpened", false);
};

function InitAllAutoComplete() {
    var AllLocalisationTextbox = jQuery("*.LocalisationTextBox");
    AllLocalisationTextbox.each(function() {
        var CurrentTextbox = jQuery(this);
        var LocalisationParent = CurrentTextbox.parent();
        var LocalisationName;
        var LocalisationId;
        LocalisationParent.find("input:hidden").each(function() {
            if (jQuery(this).attr("id").indexOf("LocalisationIdHiddenFieldId") != -1)
                LocalisationId = jQuery(this);
            if (jQuery(this).attr("id").indexOf("LocalisationNameHiddenFieldId") != -1)
                LocalisationName = jQuery(this);
            if (LocalisationName != null && LocalisationId != null)
                return false;
            });
            SetAutoComplete(LocalisationName, LocalisationId, CurrentTextbox);
    });
};

function SetAutoComplete(HiddenNameField, HiddenIdField, LocalisationTextbox) {
    LocalisationTextbox.val("");
    var Settings = { hintText: "Ville, Code postal, Département", noResultsText: "Aucun résultat", searchingText: "Recherche en cours...", tokenLimit: 1, minChars: 3 };
    if (HiddenNameField != null && HiddenIdField != null && HiddenNameField.val() != '' && HiddenIdField.val() != '')
        Settings["prePopulate"] = [{"name": HiddenNameField.val(), "id": HiddenIdField.val()}];
    LocalisationTextbox.tokenInputMoteurAnnonce("/GetVille.ashx", Settings);
};

function InitSelectedTab() {
    var HiddenField;
    jQuery("input:hidden").each(function() {
        if (jQuery(this).attr("id").indexOf("hiddenDivers") != -1) {
            HiddenField = jQuery(this);
            return false;
        }
    });
    if (HiddenField != null) {
        switch (HiddenField.val()) {
            case "location":
                ShowPanel("Location");
                break;
            case "neuf":
                ShowPanel("New");
                break;
            case "carte":
                ShowPanel("Map");
                break;
            case "inscription":
                Inscription_Connexion('', '');
                break;
            default:
                ShowPanel("Sale");
                break;
        }
    }
};

/* Selection du bon panel en fonction du clic */
function ChangePanel(elemEvent) {
    var ButtonClicked = jQuery(this);
    var Position = ButtonClicked.prevAll().length;
    (Position < 0) ? Position = 0 : Position = Position;
    var SelectedPanel = jQuery(".Panel").eq(Position);
    ShowPanel(SelectedPanel.attr("id"));
};

/* Affiche un panel en fonction de son Id */
function ShowPanel(IdPanel) {
    SelectedPanel = jQuery("#" + IdPanel);
    var VisiblePanel = jQuery(".Panel:visible");
    VisiblePanel.hide();
    SelectedPanel.show();
    /* Bug sur la liste des départements */
    if (IdPanel == "Map")
        jQuery(".Scroll").jScrollPane({ showArrows: false, scrollbarWidth: 13 });
};

function SetLinkPanelIsOpen(Value) {
    jQuery(".HomeFormBottom:visible").data("isOpened", Value);
}

function LinkPanelEffect(LinkPanel, Options, IsOpen) {
    LinkPanel.stop();
    LinkPanel.animate(Options, 300, "swing", function() { SetLinkPanelIsOpen(IsOpen); });
};

function LinkPanelEffect(LinkPanel, Options, IsOpen, Timeout) {
    LinkPanel.stop();
    LinkPanel.idle(Timeout).animate(Options, 300, "swing", function() { SetLinkPanelIsOpen(IsOpen); });
};

function HomeFormBottomHover() {
    var LinkPanel = jQuery(this);
    if (LinkPanel.data("isOpened") == false) {
        var QuickLinksHeight = jQuery(this).find(".QuickLinks").height();
        if (QuickLinksHeight > 40) {
            var Options = { height: QuickLinksHeight };
            LinkPanelEffect(LinkPanel, Options, true);
        }
    } else {
    if (LinkPanel.queue("fx").length == 2) {
        LinkPanel.queue("fx", []);
        LinkPanel.stop();
        LinkPanel.dequeue();
    }
    else
    { ; }
    }
};

function HomeFormBottomOut() {
    var Options = { height: "34px" };
    var LinkPanel = jQuery(this);
    if (LinkPanel.data("isOpened") == true) {
        LinkPanelEffect(LinkPanel, Options, false, 3000);
    }
    else {
        LinkPanelEffect(LinkPanel, Options, false);
    }
};
