/*!
 * bsd javascript library
 * http://www.bsd.ch
 *
 * benötigt jquery ab version 1.4.2
 *
 * aktualisiert am: 20101214 / hia
 */

/* Öffnet/Schliesst das übergebene Control als Modales Popup */
function showPopup(controlID) {
    var $popup = $('#' + controlID);

    $popup.modal({
        overlayCss: { backgroundColor: '#000', cursor: 'wait' },
        persist: true
    });

    $popup.children('.bsdPopupBg').height($popup.height() - 30);
}

function closePopup() {
    $.modal.close();
}

/* erstellt einen Bubble für den Warekorb Knopf (5Sekunden) */
function showWarenkorbBubble(controlID, msg) {

    var bubbleOptions = {
        width: '200px',
        innerHtml: msg,
        innerHtmlStyle: { 'text-align': 'center' },
        position: 'bottom',
        themeName: 'azure',
        themePath: '../../MasterPage/css/jquery/jquerybubblepopup-theme/'
    };

    $('#shopnav').CreateBubblePopup(bubbleOptions);
    $('#shopnav').ShowBubblePopup();
    $('#shopnav').FreezeBubblePopup();

    var sec = 10;
    function doCountdown() {
        var timer = setTimeout(function() {
            sec--;
            if (sec > 0) {
                doCountdown();
            } else {
                $('#shopnav').HideBubblePopup();
                $('#shopnav').RemoveBubblePopup();
            };
        }, 1000);
    };
    doCountdown();
}

/* setzt dem parent des aufrufenden objekts die classe 'collapsed' oder entfernt sie und schliesst den enthaltenen gridview oder öffnet ihn */
function setCollapsableClickFunction() {

    if ($("[id$='hdnCollapsed']").val() != "") {

        var arr = $("[id$='hdnCollapsed']").val().split(",");

        jQuery.each(arr, function() {
            var $collapsable = $("#" + this).parent().parent();
            $collapsable.removeClass('collapsed');
            $collapsable.children('div').show();
        });
    }

    if ($("[id$='hdnSingleCollapsed']").val() != "") {
        var $collapsable = $('#' + $("[id$='hdnSingleCollapsed']").val()).parent().parent();
        $collapsable.removeClass('collapsed');
        $collapsable.children('div').show();
    }

    $('.collapsable').unbind('click');
    $('.collapsable').click(function() {

        /* Der Typ collapsable lässt mehrere offene Boxen zu
        Die Offenen Boxen werden in einem Hiddenfield gespeichert
        damit sie nach einem Postback wieder geöffnet werden können */

        var $id = $(this).attr('id');
        var $collapsable = $(this).parent().parent();

        /* Die Offenen Tabs werden aus dem Hiddenfield geladen */
        var val = $("[id$='hdnCollapsed']").val();
        var arr;
        if (val.length > 0) {
            arr = val.split(",");
        }
        else {
            arr = new Array();
        }

        /* Wenn die aktuelle Box drin ist wird sie geschlossen */
        if (jQuery.inArray($id, arr) > -1) {

            $collapsable.addClass('collapsed');
            $collapsable.children('div').hide('fast');

            /* ID aus Array entfernen */
            arr = jQuery.grep(arr, function(value) {
                return value != $id;
            });
        }
        /* Ansonsten wird sie geöffnet */
        else {

            $collapsable.removeClass('collapsed');
            $collapsable.children('div').show('fast');

            /* ID in Array Speichern */
            arr.push($id);
        }

        /* Array in String konverieren und in HiddenField speichern */
        $("[id$='hdnCollapsed']").val(arr.join(","));

        return true;
    });

    $('.single_collapsable').unbind('click');
    $('.single_collapsable').click(function() {

        var $id = $(this).attr('id');
        var $collapsable = $(this).parent().parent();

        if ($collapsable.hasClass('collapsed')) {
            $('.single_collapsable').parent().parent().addClass('collapsed');
            $('.single_collapsable').parent().parent().children('div').hide('fast');
            $collapsable.removeClass('collapsed');
            $collapsable.children('div').show('fast');
            $("[id$='hdnSingleCollapsed']").val($id);
        }
        else {
            $collapsable.addClass('collapsed');
            $collapsable.children('div').hide('fast');
            $("[id$='hdnSingleCollapsed']").val("");
        }

        return true;
    });
}

$(document).ready(function () {

    /* Collapsable Click Function aufrufen */
    setCollapsableClickFunction();

    /* Tabs konfigurieren */
    $(".tab_content").hide();

    var index = 0;
    var tabIndex = $("[id$='hdnSelectedTabIndex']").val();

    if (tabIndex != '') {
        index = tabIndex;
    }

    $("ul.tabs li:eq(" + index + ")").addClass("active").show();
    $(".tab_content:eq(" + index + ")").show();

    $("ul.tabs li").click(function () {

        $("[id$='hdnSelectedTabIndex']").val($("ul.tabs li").index(this));

        $("ul.tabs li").removeClass("active");
        $(this).addClass("active");
        $(".tab_content").hide();

        var activeTab = $(this).find("a").attr("href");

        $(activeTab).fadeIn();
        return false;
    });

    /* ermöglicht bei allen Textboxen mit class="numberFloat" nur die Eingabe von Nummern */
    $('.numberFloat').numeric();

    /* Fügt eine Tooltip Box ein */
    $('.tooltip').qtip({
        position: {
            target: 'mouse',
            adjust: { mouse: false },
            my: 'bottom left'
        },
        style: {
            classes: 'ui-tooltip-light ui-tooltip-rounded ui-tooltip-shadow'
        },
        show: { delay: 750 },
        hide: { delay: 500 }

    });


});

