﻿jQuery(function ($) {

    $.supersized({

        //Functionality
        slideshow: 1, 	//Slideshow on/off          
        random: 1,
        performance: 3,
        slide_interval: 30000, //Length between transitions
        transition: 1, 		//0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
        transition_speed: 1000, //Speed of transition
        slides: [
                    { image: '_img/Slideshow/Background/IMG_7332.jpg' },
                    { image: '_img/Slideshow/Background/IMG_0064.JPG' },
                    { image: '_img/Slideshow/Background/Ipone 119.jpg' },
                    { image: '_img/Slideshow/Background/IMG_7319.jpg' },
                    { image: '_img/Slideshow/Background/IMG_7322.jpg' },
                    { image: '_img/Slideshow/Background/IMG_7323.jpg' },
                    { image: '_img/Slideshow/Background/IMG_7325.jpg' },
                    { image: '_img/Slideshow/Background/IMG_7328.jpg' },
                    { image: '_img/Slideshow/Background/IMG_7330.jpg' },
                    { image: '_img/Slideshow/Background/IMG_7333.jpg' },
                    { image: '_img/Slideshow/Background/IMG_7338.jpg' },
                    { image: '_img/Slideshow/Background/IMG_7342.jpg' }
                ]
    });

    var lLinks = $('#UL_navi li a').click(function () {
        $('a.aktive').removeClass('aktive');
        $(this).addClass('aktive')
        window.location.hash = $(this).attr("ID");
        return false;
    });

    $(window).hashchange(function () {
        if (window.location.hash == '') {
            lLinks.first().click();
        }
        else {
            zLoadContent(lLinks.filter('[id=' + window.location.hash.substr(1) + ']').attr('href'), false);
        }

        document.title = 'Lehmofen Ahlen - Das Original. Seit 1992. - ' + window.location.hash.substr(1);
    });

    $(window).hashchange();

    $('#UL_navi li a:visible').last().css('border', 'none')

    $(window).resize(function () {
        zRePosContent();
    });

    $('#A_Login').click(function () {
        zShowLogin($(this));
    });

    if ($('#A_Login').hasClass('isLoggedIn')) {
        zGetAdminScript();
    }

    $('#loadingDiv').hide().ajaxStart(function () { $(this).show(); }).ajaxStop(function () { $(this).hide(); });
    
//    jQuery.getScript('_js/galleria/themes/classic/galleria.classic.min.js');

});

function zRePosContent() {
    try {
        zRePosContentWithTimeOut(100, false);
    } catch (e) {

    }
}

var lResizingAktive = false;
function zRePosContentWithTimeOut(Timeout, Break) {
    window.setTimeout(function() {
        zInitContentHeight(Break);
    }, Timeout);
}

function zInitContentHeight(Break) {    
    if (!lResizingAktive) {
        lResizingAktive = true;

        var lCurHeight = $('#DIV_ContentHolder').height() + 60;

        var lGenHeight = 0;
        var lWindowHeight = $(window).height();

        if (lWindowHeight > lCurHeight) {

            lGenHeight = $(window).height() - lCurHeight;
        }

        $('#DIV_ContentTmpTopMargin').css({ 'height': lGenHeight + 'px' });

        // zur sicherheit
        window.setTimeout(function () {            
            lResizingAktive = false;
        }, 200);


        if (Break == true)
            return;

        zRePosContentWithTimeOut(1500, true);
        zRePosContentWithTimeOut(1000, true);
        zRePosContentWithTimeOut(500, true);        
    }
}

function log(s) {
    window.console ? window.console.log(s) : alert(s);
}



var gCurrentPath = null;
function zReloadContent() {
    zLoadContent(gCurrentPath, false);
}

function zLoadContent(Path, IsReInit) {
    $.ajax({
        url: Path,
        dataType: 'html',
        cache: false,
        success: function (data) {
            $('#DIV_Content').html(data);

            $('#DIV_Content h1').each(function () {
                $(this).attr('title', $(this).text());
                $(this).html('<embed type="application/x-shockwave-flash" src="Ueberschrift.swf" width="500px" height="50px" style="undefined" id="Ueberschrift" name="Ueberschrift" bgcolor="#FFFFFF" quality="high" wmode="transparent" flashvars="Text=' + $(this).text() + '" />').addClass('FlashHeader');
            });

            zInitGallery(IsReInit);

            zInitGB();

            gCurrentPath = Path;

            if (typeof zInitCMS == 'function') {
                
                zInitCMS();
            }


            zRePosContent();
        }
    });

}

function zInitGB() {
    var lGaestebuch = $('#DIV_Geastebuch', '#DIV_Content');
    if (lGaestebuch.length > 0) {
        zGetGB(lGaestebuch);
    }
}

var lGalleryIndex = 0;
function zInitGallery(IsReInit) {
    var lGalerieContainer = $('#gallerie', '#DIV_Content');
    if (lGalerieContainer.length > 0) {
        lGalerieContainer.each(function () {
            var lCurGallery = $(this);
            $.ajax({
                url: '_exe/CMS.ashx?Type=GetGallery&Path=' + lCurGallery.attr("Path"),
                dataType: 'html',
                cache: false,
                success: function (data) {
                    if (data != "") {
                        var lzSetData = function () {
                            lCurGallery.html('');
                            var lImageHolderId = "ImageHolder";
                            lCurGallery.append('<div id="' + lImageHolderId + '" />');
                            $('#' + lImageHolderId, lCurGallery).html(data);
                        };

                        if (typeof zInitCMSGallery == 'function') {
                            lzSetData();
                            zInitCMSGallery(IsReInit);
                        }
                        else {
                            // Sicherstellen, dass man nicht angemeldet ist.                            
                            if (!IsReInit) {
                                
                                lzSetData();
                                $('#ImageHolder', lCurGallery).galleria({
                                    width: 860,
                                    height: 500
                                });

                                zRePosContent();
                            }
                        }

                        lGalleryIndex++;
                    }
                }
            });

        });
    }
}

function zGetAdminScript() {
    $.getScript('_js/_lehmofen/admin.js', function () {
        zLoadContent(gCurrentPath, false);
    });
}

function zReloadPage() {
    window.location.reload()
}

function zGetGB(Holder) {
    $.ajax({
        url: '_exe/CMS.ashx',
        dataType: 'text',
        cache: false,
        data: 'Type=GetGB',
        success: function (data) {
            Holder.html(data);
            if (typeof zInitGBAdmin == 'function')
                zInitGBAdmin();

            zRePosContent();
        }
    });
}

function zLogin(Sender) {
    $.ajax({
        url: '_exe/CMS.ashx',
        dataType: 'text',
        cache: false,
        data: 'Type=LOGIN&Passwort=' + Sender.val(),
        success: function (data) {
            Sender.val('');
            if (data == 'True') {
                zGetAdminScript();
                $('#A_Login').text('Abmelden').addClass('isLoggedIn');
                $('#DIV_Login').slideUp();
            }
        }
    });
}

function zShowLogin(sender) {
    if (sender.hasClass('isLoggedIn')) {
        sender.text('Login').removeClass('isLoggedIn');
        zLogout();
    }
    else {
        $('#DIV_Login').slideToggle();
    }
}

function zAddGbItem(autor, text) {
    if (autor == "" || text == "")
        return;

    $.ajax({
        url: '_exe/CMS.ashx',
        dataType: 'text',
        cache: false,
        data: 'Type=AddGbItem&autor=' + encodeURIComponent(autor) + '&text=' + encodeURIComponent(text),
        success: function (data) {

            var lGaestebuch = $('#DIV_Geastebuch', '#DIV_Content');
            if (lGaestebuch.length > 0) {
                zGetGB(lGaestebuch);
            }

        }
    });
}

function zAddGbItemFromForm() {
    zAddGbItem($('#I_Autor').val(),
                       $('#I_Text').val());
    $('#I_Autor').val('');
    $('#I_Text').val('');
    $('#GBItemInsert').slideUp();
    return false;
}
