// JavaScript Document
$(document).ready(function() {
    var impressum_btn_b = false;
    var contact_btn_b = false;
    $('.text').attr('disabled','true');
    $('#submit').click(function () {
        var name = $('input[name=name]');
        var email = $('input[name=email]');
        var nachricht = $('textarea[name=nachricht]');
        if( $('input[name=sendcc]').attr('checked') == true)
            var sendcc = 'checked';
        else
            var sendcc = 'unchecked';
        var data = 'name=' + name.val() + '&email=' + email.val()
        + '&nachricht='  + encodeURIComponent(nachricht.val()) + '&sendcc=' + sendcc;
        $('.text').attr('disabled','true');
        $('.loading_icon').show();
        $.ajax({
            url: "saveData.php",
            type: "GET",
            data: data,
            success: function (reqCode) {
                if (reqCode== 'OK') {
                    $('.myForm').fadeOut('slow');
                    $('.done').fadeIn('slow');
                    window.setTimeout(resetForm, 5000);
                } else if (reqCode== 'Name'){
                    alert('Bitte geben Sie Ihren Namen an.');
                    $('.text').removeAttr('disabled');
                    $('.loading_icon').hide();
                } else if (reqCode== 'Email'){
                    alert('Bitte überprüfen Sie Ihre Email-Adresse.');
                    $('.text').removeAttr('disabled');
                    $('.loading_icon').hide();
                } else if (reqCode== 'Nachricht'){
                    alert('Bitte geben Sie Ihre Nachricht ein.');
                    $('.text').removeAttr('disabled');
                    $('.loading_icon').hide();
                }else{
                    alert('Fehler beim Abschicken des Formulares. Bitte versuchen Sie es später erneut.' + reqCode);
                    $('.text').removeAttr('disabled');
                    $('.loading_icon').hide();
                }
                function resetForm() {
                    $('.text').removeAttr('disabled');
                    $('.myForm').find(':input').each(function() {
                        switch(this.type) {
                            case 'text':
                            case 'textarea':
                                $(this).val('');
                                break;
                            case 'checkbox':
                                $('input[name=sendcc]').attr("checked","");
                        }
                    });
                    $('.loading_icon').hide();
                    $('.myForm').fadeIn('slow');
                    $('.done').fadeOut('slow');
                }
            }

        });
        return false;
    });
    $('div.content').css('display', 'block');

    var onMouseOutOpacity = 1;
    $('#thumbs ul.thumbs img, div.navigation a.pageLink').opacityrollover({
        mouseOutOpacity:   onMouseOutOpacity,
        mouseOverOpacity:  0.7,
        fadeSpeed:         'fast',
        exemptionSelector: '.selected'
    });
				
    var gallery = $('#thumbs').galleriffic({
        delay:                     5000,
        numThumbs:                 16,
        preloadAhead:              16,
        enableTopPager:            false,
        enableBottomPager:         false,
        imageContainerSel:         '#slideshow',
        controlsContainerSel:      '#controls',
        captionContainerSel:       '#caption',
        loadingContainerSel:       '#loading',
        renderSSControls:          true,
        renderNavControls:         true,
        playLinkText:              'Play Slideshow',
        pauseLinkText:             'Pause Slideshow',
        prevLinkText:              '&lsaquo; Previous Photo',
        nextLinkText:              'Next Photo &rsaquo;',
        nextPageLinkText:          'Next &rsaquo;',
        prevPageLinkText:          '&lsaquo; Prev',
        enableHistory:             false,
        autoStart:                 true,
        syncTransitions:           true,
        defaultTransitionDuration: 1500,
        onSlideChange:             function(prevIndex, nextIndex) {
            this.find('ul.thumbs').children()
            .eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
            .eq(nextIndex).fadeTo('fast', 1.0);

            // Update the photo index display
            this.$captionContainer.find('div.photo-index')
            .html('Photo '+ (nextIndex+1) +' of '+ this.data.length);
        },
        onPageTransitionOut:       function(callback) {
            this.fadeTo('fast', 0.0, callback);
        },
        onPageTransitionIn:        function() {
            var prevPageLink = this.find('a.prev').css('visibility', 'hidden');
            var nextPageLink = this.find('a.next').css('visibility', 'hidden');
						
            // Show appropriate next / prev page links
            if (this.displayedPage > 0)
                prevPageLink.css('visibility', 'visible');

            var lastPage = this.getNumPages() - 1;
            if (this.displayedPage < lastPage)
                nextPageLink.css('visibility', 'visible');

            this.fadeTo('fast', 1.0);
        }
    });

    gallery.find('a.prev').click(function(e) {
        gallery.previousPage();
        e.preventDefault();
    });

    gallery.find('a.next').click(function(e) {
        gallery.nextPage();
        e.preventDefault();
    });

    function pageload(hash) {
        if(hash) {
            $.galleriffic.gotoImage(hash);
        } else {
            gallery.gotoIndex(0);
        }
    }

    $.historyInit(pageload, "advanced.html");

    $("a[rel='history']").live('click', function(e) {
        if (e.button != 0) return true;

        var hash = this.href;
        hash = hash.replace(/^.*#/, '');

        $.historyLoad(hash);

        return false;
    });
    //Menu
    $("a[rel='impressum_btn']").click(function() {
        if(impressum_btn_b == false) {
            contact_btn_b = false;
            gallery.pause();
            $('.text').attr('disabled','true');
            $('#contact').fadeTo('slow', 0);
            $('#impressum').fadeTo('slow', 1);
            impressum_btn_b = true;
        } else {
            $('#impressum').fadeTo('slow', 0);
            impressum_btn_b = false;
            if(contact_btn_b == false || impressum_btn_b == false)
                gallery.play();
        }
    });
    $("a[rel='contact_btn']").click(function() {
        if(contact_btn_b == false){
            impressum_btn_b = false;
            gallery.pause();
            $('#impressum').fadeTo('slow', 0);
            $('.text').removeAttr('disabled');
            $('#contact').fadeTo('slow', 1);
            contact_btn_b = true;
        } else {
            $('#contact').fadeTo('slow', 0);
            $('.text').attr('disabled','true');
            contact_btn_b = false;
            if(contact_btn_b == false || impressum_btn_b == false)
                gallery.play();
        }
    });
    // /Menu
    $('#impressum_closebtn').click(function () {
        if(impressum_btn_b == true) {
            impressum_btn_b = false;
            gallery.play();
            $('#impressum').fadeTo('slow', 0);
        }
    })
    $('#contact_closebtn').click(function () {
        if(contact_btn_b == true) {
            contact_btn_b = false;
            gallery.play();
            $('#contact').fadeTo('slow', 0);
        }
    })
});
