$(document).ready(function ()
{

    // ! ---------------------------------------------------------------------
    // ! Default Variablen setzen
    $debug = $("#debug");
    $menu = false;
    $(".editS").hide();
    i = -1;
    var $cancel = $("#cancelA").text();
    var $OK = $("#saveA").text();
    wieviel = $("div#linker").size();
    $debug.attr('value', i);

    // ! ---------------------------------------------------------------------

    $("#menu").dialog(
    {
        autoOpen: false,
        resizable: false,
        position: [100, 180],
        show: 'fade',
        hide: 'clip',
        zIndex: 100,
        beforeClose: function ()
        {
            $("#plus").fadeIn(2000);
        }
    });

    // ! ---------------------------------------------------------------------
    // ! make config dialog
    $("#confPanel").dialog(
    {
        autoOpen: false,
        modal: true,
        resizable: false,
        show: 'fade',
        hide: 'clip',
        zIndex: 100,
        width: 500,
        draggable: true,
        buttons: [
        {
            text: $cancel,
            click: function ()
            {
                $(this).dialog("close");
            }
        }, {
            text: $OK,
            click: function ()
            {
                saveConfig();
            }
        }]
    });

    $("#impressum").dialog(
    {
        autoOpen: false,
        modal: true,
        resizable: false,
        show: 'fade',
        hide: 'clip',
        zIndex: 100,
        width: 709,
        draggable: true,
        beforeClose: function ()
        {
            showAlles();
        }
    });

    // ! ---------------------------------------------------------------------
    $("#veloc").slider(
    {
        max: 30,
        min: 3,
        value: 6,
        animate: true,
        stop: function (event, ui)
        {
            setShow();
        }
    });

    // ! ---------------------------------------------------------------------
    $("#CV").dialog(
    {
        autoOpen: false,
        resizable: false,
        show: 'fade',
        hide: 'fade',
        width: 500,
        zIndex: 100,
        draggable: false,
        title: $("#qnin").text(),
        beforeClose: function ()
        {
            showAlles();
        }
    });

    // ! ---------------------------------------------------------------------
    // ! Button Init
    $("#menuopen").button();
    $("button").button();
    $("#velo").buttonset();
    $("#buttonC").buttonset();

    // ! ---------------------------------------------------------------------
    // ! Init Accordion
    $("#accordion").accordion(
    {
        autoHeight: false,
        collapsible: true,
        header: 'h3',
        autoOpen: true,
        active: 1,
        clearStyle: true
    });

    // ! ---------------------------------------------------------------------
    // ! call runZufall once
    /* runLoop(i, wieviel); */


    checkRandom();
    $("#alles").fadeIn('slow');
    $loopID = window.setInterval(function ()
    {
        checkRandom();
    }, 6000);

    // ! ---------------------------------------------------------------------
    // ! click play
    $("#abspielen").click(function ()
    {
        quitTip();

        var what = $(this).text();
        if (what !== 'Play')
        {
            stopShow();
            cVideo();
            return false;
        }
        else
        {
            setShow();
            cVideo();
            return false;
        }
    });
    $("#abspielen").tinyTips('title');

    // ! ---------------------------------------------------------------------
    // ! Play Button klicken
    $("#playP").click(function ()
    {
        quitTip();

        var what = $(this).attr('value');
        if (what !== 'play')
        {
            stopShow();
            cVideo();
            return false;
        }
        else
        {
            setShow();
            cVideo();
            return false;
        }
    });
    $("#playP").tinyTips('title');

    // ! ---------------------------------------------------------------------
    // ! click toogle Accordion
    $('.accordion .head').click(function ()
    {
        $(this).next().toggle('slow');
        return false;
    }).next().hide();

    // ! ---------------------------------------------------------------------
    // ! click open Menu
    $("#plus").click(function ()
    {
        quitTip();

        $("#menu").dialog('open');
        moveAcc();
        $("#plus").fadeOut('slow');
        return false;
    });

    $("#plus").tinyTips('title');
    $("#bImpressum").tinyTips('title');

    // ! ---------------------------------------------------------------------
    // ! click show Submenu

    $("li").click(function ()
    { 
        var istDa = $(this).next('.hidden').css('display');
        if (istDa === 'none')
        {
            stopShow();
            $(".hidden").slideUp('fast');
            $(this).parent().children(".hidden").slideDown('fast');
        }
    });

    // ! ---------------------------------------------------------------------
    // ! click open Image/Video
    $("div#linker").click(function ()
    {
        var k = 0;
        quitTip();
        stopShow();
        zeig = $(this).attr('value');
        exts = $(this).attr('name');
        $name = $(this).text();
        var wo = $(this).attr('alt');
        $("div#linker").removeClass('aktiv');
        $(this).addClass('aktiv');
        $("#content, #titel").hide('fade', 200, function ()
        {
            if (k === 0)
            {
                checkMenu();
                showMedia(zeig, exts);
                i = (1 * wo + 1);
                $debug.attr('value', wo);
            }
            k++;
        });
        $("#containerA").fadeOut('slow', function ()
        {
            $("#textedit").attr('value', '');
        });
        var edit = $("#logout").css('display');
        if (edit !== 'none')
        {
            $(".editS").hide();
            $(this).next('#admEdit').show();
        }
    });


    // ! ---------------------------------------------------------------------
    // ! Preview durch Mousemove
/*
    $("div#linker").mouseover(function (e)
    {
        var plink = $(this).attr('value');
        var pext = $(this).attr('name');
        if ((pext === ".jpg") || (pext === ".gif"))
        {
            $("div#linker").tinyTips("<img src='" + plink + "' width='80' />");
            return false;
        }
        else
        {
            $("div#linker").tinyTips('Video');
            return false;
        }
    });
*/


    // ! ---------------------------------------------------------------------
    // ! Mail senden (einloggen)
    $("#send").click(function (event)
    {
        event.preventDefault();
        event.stopPropagation();
        var emailA = $("#mail").next('#validEmail').attr('value');
        var passA = $("#pass").attr('value');
        var to = $("#to").attr('value');

        if ((emailA === true) || (passA !== ""))
        {

            $.post("site/php/mail.php", {
                name: $("#name").attr('value'),
                pass: $("#pass").attr('value'),
                mail: $("#mail").attr('value'),
                subj: $("#subj").attr('value'),
                mailc: $("#mailc").attr('value'),
                to: to
            }, function (data)
            {

                if (data === 'pass')
                {
                    $("#mailform").slideUp('slow');
                    $("#config").show();
                    $("#logout").show();
                    $("div#linker").css('width', '85%');
                    $('.aktiv').next('#admEdit').show(); /* moveAcc(); */
                    return false;

                }
                else
                {

                    $("#mailform").slideUp('slow');
                    $("#meldung").slideDown('slow');
                    return false;
                }
                return false;
            });

        }
        else
        {

            $("#mail").animate(
            {
                backgroundColor: "red"
            }, 500, function ()
            {
                $("#mail").animate(
                {
                    backgroundColor: "white"
                }, 500);
            });
            return false;
        }
    });

    // ! ---------------------------------------------------------------------
    // ! Slider slide
    $("#veloc").bind('slide', function ()
    {
        quitTip();

        var velos = $("#veloc").slider("option", "value");
        $("#langsamer").text(velos);
    });
    $("#vCont").tinyTips('title');

    // ! ---------------------------------------------------------------------
    // ! Name Blur
    $("#name").bind('blur', function ()
    {
        var name = $(this).attr('value');
        if (name === 'danielbuehl')
        {
            $("#mail").hide();
            $("#subj").hide();
            $("#mailc").hide();
            $("#pass").show();
            $("h3#kontakt").html('<a href="#">Admin</a>');
            return false;
        }
    });

    // ! ---------------------------------------------------------------------
    // ! Input Focus
    $(".eingb").bind('focus', function ()
    {
        $behalten = $(this).attr('value');
        $(this).val('');
        return false;
    });

    // ! ---------------------------------------------------------------------
    // ! Input Blur
    $(".eingb").bind('blur', function ()
    {
        var inhalt = $(this).attr('value');
        if (inhalt === '')
        {
            $(this).val($behalten);
            return false;
        }
    });

    // ! ---------------------------------------------------------------------
    // ! Logout
    $("#logout").bind('click', function ()
    {
        $("#name").val("Ihr Name:");
        $("div#linker").css('width', '100%');
        $(this).hide();
        $("#pass").val("").hide();
        $("#config").hide();
        $("#mail").show();
        $("#subj").show();
        $("#mailc").show();
        $("#mailform").slideDown('slow');
        var kontakt = $("h3#kontakt").attr('value');
        $("h3#kontakt").html('<a href="#">' + kontakt + '</a>');
        $(".editS").hide();
        $("#containerA").fadeOut('slow');
        return false;
    });

    // ! ---------------------------------------------------------------------
    // ! Accordion Kontakt click
    $("h3#kontakt").click(function ()
    {
        stopShow();
        return false;
    });

    // ! ---------------------------------------------------------------------
    // ! Edit Button click
    $("#admEdit").live('click', function ()
    {
        var item = $("#titel").html();
        $("#textedit").attr('value', item);
        $("#containerA").fadeIn('slow');
        return false;
    });

    // ! ---------------------------------------------------------------------
    // ! Auf Namen klicken
    $("#qnin").click(function ()
    {
        quitTip();

        $("#CV").dialog('open');
        $("#box").fadeOut('slow');
        stopShow();
        var offen = $("#menu").dialog('isOpen');
        if (offen === true)
        {
            $("#menu").dialog('close');
            $menu = true;
            return false;
        }
        else
        {
            $menu = false;
            return false;
        }
    });
    $("#qnin").tinyTips('title');
    // ! ---------------------------------------------------------------------
    // ! Tastenabfrage
    $(document).bind('keyup', function (event)
    {
        var das = document.activeElement;

        /*	alert(event.keyCode); */

        if ((das !== "[object HTMLInputElement]") && (das !== "[object HTMLTextAreaElement]"))
        {
            switch (event.keyCode)
            {
            case 77:
                if ($("#menu").dialog('isOpen'))
                {
                    $("#menu").dialog('close');
                    return false;
                }
                else
                {
                    $("#menu").dialog('open');
                    moveAcc();
                    $("#plus").fadeOut('slow');

                    return false;
                }
                break;

            case 32:
                var runstop = $("#playP").attr('value');
                if (runstop === 'pause')
                {
                    stopShow();
                    cVideo();
                    return false;
                }
                else
                {
                    setShow();
                    cVideo();
                    return false;
                }
                break;

            case 39:
                weiter();
                break;
            case 40:
                weiter();
                break;
            case 37:
                zurueck();
                break;
            case 38:
                zurueck();
                break;
                // Taste a = admin
            case 65:
                admin();
                break;
            }
        }
        else
        {
            switch (event.keyCode)
            {
            case 13:

                break;
            }
        }
    });

    // ! ---------------------------------------------------------------------
    // ! Validate Mail
    $("#mail").bind('keyup', function ()
    {
        var emailA = $(this).attr('value');
        if (emailA !== 0)
        {
            if (isValidEmailAddress(emailA))
            {
                $("#validEmail").css(
                {
                    "background-image": "url('site/img/validYes.png')"
                }).val(true);
            }
            else
            {
                $("#validEmail").css(
                {
                    "background-image": "url('site/img/validNo.png')"
                }).val(false);
            }
        }
        else
        {
            $("#validEmail").css(
            {
                "background-image": "none"
            }).val(false);
        }
    });

    //---------------------------------------------------------------------------
    $("#saveA").click(function (event)
    {
/*
event.preventDefault();
event.stopPropagation();
*/
        var link = $(".aktiv").attr('value');
        var lnk = (link.replace(/ /gi, "%20"));
        var newStr = lnk.substring(0, lnk.length - 3);
        var txtLink = newStr.concat("txt");
        var newText = $("#textedit").attr('value');

        if (newText !== "")
        {
            $.post("site/php/changetext.php", {
                newText: newText,
                txtLink: txtLink
            }, function (data)
            {
                $("#titel").html(data);
            });
        }
    });

    $("#cancelA").click(function ()
    {
        var item = $("#titel").html();
        $("#textedit").attr('value', item);
    });

    //---------------------------------------------------------------------------
    $("#config").click(function ()
    {
        $("#confCont").load('site/php/config.php', function ()
        {
            checkMenu();
            $("#confPanel").dialog('open');
            // ! Init Config
            $(".accordion").accordion(
            {
                autoHeight: false
            });
        });
    });

    //---------------------------------------------------------------------------
    $("#konOK").click(function ()
    {
        $("#mailform").slideDown('slow');
        $("#meldung").slideUp('slow');
    });

    $("#bImpressum").click(function ()
    {
        $("#impCont").load('usr/impressum.php', function ()
        {
            $("#impressum").dialog('open');
            quitTip();
            $("#box").fadeOut('slow');
            stopShow();

            $("#iAcc").accordion(
            {
                autoHeight: false,
                collapsible: true,
                header: 'h3',
                active: 'none'
            });

            var offen = $("#menu").dialog('isOpen');
            if (offen === true)
            {
                $("#menu").dialog('close');
                $menu = true;
                return false;
            }
            else
            {
                $menu = false;
                return false;
            }
        });
    });
    //---------------------------------------------------------------------------
    /* ENDE SCRIPT */
}); // onload End

