
$(function(){

    if($("#slideshow").length>0){
        var pos=$("#slide0").position();
        var left=pos.left+'px';
        var top=pos.top+'px';
        $("#slide0").css({
            'opacity':1,
            'display':'block',
            'position':'absolute',
            'left':left,
            'top':top
        });
        $("#slide1").css({
            'opacity':0,
            'display':'block',
            'position':'absolute',
            'left':left,
            'top':top
        });
        $("#slide2").css({
            'opacity':0,
            'display':'block',
            'position':'absolute',
            'left':left,
            'top':top
        });
        setTimeout("startSlideshow(1)",3000);
    }


    var playItem = 0;

    var myPlayList = [
    {
        name:"1",
        mp3:"../media/music/dieruhe01.mp3"
    },

    {
        name:"2",
        mp3:"../media/music/dieruhe02.mp3"
    },

    {
        name:"3",
        mp3:"../media/music/dieruhe03.mp3"
    },

    {
        name:"4",
        mp3:"../media/music/dieruhe04.mp3"
    },

    {
        name:"5",
        mp3:"../media/music/dieruhe05.mp3"
    },

    {
        name:"6",
        mp3:"../media/music/dieruhe06.mp3"
    },

    {
        name:"7",
        mp3:"../media/music/dieruhe07.mp3"
    },

    {
        name:"8",
        mp3:"../media/music/dieruhe08.mp3"
    },

    {
        name:"9",
        mp3:"../media/music/dieruhe09.mp3"
    },

    {
        name:"10",
        mp3:"../media/music/dieruhe10.mp3"
    },

    {
        name:"11",
        mp3:"../media/music/dieruhe11.mp3"
    },

    {
        name:"12",
        mp3:"../media/music/dieruhe12.mp3"
    },

    {
        name:"1",
        mp3:"../media/music/dersturm01.mp3"
    },

    {
        name:"2",
        mp3:"../media/music/dersturm02.mp3"
    },

    {
        name:"3",
        mp3:"../media/music/dersturm03.mp3"
    },

    {
        name:"4",
        mp3:"../media/music/dersturm04.mp3"
    },

    {
        name:"5",
        mp3:"../media/music/dersturm05.mp3"
    },

    {
        name:"6",
        mp3:"../media/music/dersturm06.mp3"
    },

    {
        name:"7",
        mp3:"../media/music/dersturm07.mp3"
    },

    {
        name:"8",
        mp3:"../media/music/dersturm08.mp3"
    },

    {
        name:"9",
        mp3:"../media/music/dersturm09.mp3"
    },

    {
        name:"10",
        mp3:"../media/music/dersturm10.mp3"
    },

    {
        name:"11",
        mp3:"../media/music/dersturm11.mp3"
    },

    {
        name:"12",
        mp3:"../media/music/dersturm12.mp3"
    },

    {
        name:"13",
        mp3:"../media/music/dersturm13.mp3"
    }
    ];



    $("#jquery_jplayer").jPlayer({
        ready: function() {
            displayPlayList();
            playListInit(true); // Parameter is a boolean for autoplay.
        //demoInstanceInfo(this.element, $("#demo_info")); // This displays information about jPlayer's configuration in the demo page
        }
    })

    .jPlayer("onSoundComplete", function() {
        playListNext();
    });

    $("#jplayer_previous").click( function() {
        playListPrev();
        $(this).blur();
        return false;
    });

    $("#jplayer_next").click( function() {
        playListNext();
        $(this).blur();
        return false;
    });

    function displayPlayList() {
        // $("#jplayer_playlist ul").empty();
        for (i=0; i < myPlayList.length; i++) {
            //var listItem = (i == myPlayList.length-1) ? "<li class='jplayer_playlist_item_last'>" : "<li>";
            //listItem += "<a href='#' id='jplayer_playlist_item_"+i+"' tabindex='1'>"+ myPlayList[i].name +"</a></li>";
            //$("#jplayer_playlist ul").append(listItem);
            $("#jplayer_playlist_item_"+i).data( "index", i ).click( function() {
                var index = $(this).data("index");
                if (playItem != index) {
                    playListChange( index );
                } else {
                    $("#jquery_jplayer").jPlayer("play");
                }
                $(this).blur();
                return false;
            });
        }
    }

    function playListInit(autoplay) {
        if(autoplay) {
            playListChange( playItem );
        } else {
            playListConfig( playItem );
        }
    }

    function playListConfig( index ) {
        $("#jplayer_playlist_item_"+playItem).removeClass("jplayer_playlist_current").parent().removeClass("jplayer_playlist_current");
        $("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current").parent().addClass("jplayer_playlist_current");
        playItem = index;
        $("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3, myPlayList[playItem].ogg);
    }

    function playListChange( index ) {
        playListConfig( index );
        $("#jquery_jplayer").jPlayer("play");
        $(".jplayer_playlist_current").click(function(){
            $("#jquery_jplayer").jPlayer("pause");
        });
    }

    function playListNext() {
        var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0;
        playListChange( index );
    }

    function playListPrev() {
        var index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1;
        playListChange( index );
    }


});

function startSlideshow(index){
    var newSlide=$("#slide"+(index)%3);
    var currentSlide=$("#slide"+(index-1)%3);
    var backSlide=$("#slide"+(index+1)%3);
    backSlide.css("z-index", 0);
    backSlide.css("opacity", 0);

    currentSlide.css("z-index", 1);
    newSlide.css("z-index", 2);

    newSlide.animate({
        'opacity':1
    },1000);
    index++;
    setTimeout("startSlideshow("+index+")",6000);
}

function showOrderForm(){
    $('#graybg').show();
    $('#graybg').css({
        top:0,
        left:0,
        opacity:0.5,
        height:$(document).height()+'px'
    });
    $('#orderform').show(0);
    var posleft=($(document).width()/2)-(960/2);
    $('#orderform').css({
        left:posleft+'px'
    });

}
function hideOrderForm(){
    $('#graybg').hide(500);
    $('#orderform').hide(500);
}
function sendOrder(){
    if(checkForm('orderform')){
        $('p.ordertext').hide();
        $('#orderform_send').hide();
        $('iframe#orderanswer').height('120px');
        return true;
    }
    else{
        return false;
    }

}
function sendContact(){
    if(checkForm('contactform')){
        $('p.ordertext').hide();
        $('#contactform_send').hide();
        $('iframe#orderanswer').height('120px');
        return true;
    }
    else{
        return false;
    }

}
function sendMessage(){
    if(checkForm('guestbookform')){
        $('#contactform_send').hide();
        return true;
    }
    else{
        return false;
    }

}


function showPhoto(pid){
    $('#graybg').show();
    $('#graybg').css({
        top:0,
        left:0,
        opacity:0.5,
        height:$(document).height()+'px'
    });
    //$('#currentphoto').attr('src', name);
    var src= $('a#galery_item_'+pid).attr('href');
    $('#currentphoto').replaceWith('<img src="'+src+'" alt="" id="currentphoto" />');
    $('#currentphoto').load(function(){

        $('#currentphotolink').attr('title','showPhoto('+(pid++)%9+')');
        $('#photoshow').show(0,function(){
            $('#currentphotolink').click(function(){
                showPhoto((pid++)%9);
            }) ;
            var curwidth=60+($('#currentphoto').width());
            $('#photoshow').css({
                width:curwidth+'px'
            });
        });
    });
    return false;
}
function hidePhoto(){
    $('#graybg').fadeOut(100);
    $('#photoshow').fadeOut(100);
}

function checkForm(id){
    var correct=true;
    $('#'+id+' :input').each(function(index){
        var elem=$(this);
        if(elem.val().length>0){
            elem.removeClass('error');
            elem.addClass("checked");
        }
        else{
            correct=false;
            elem.addClass('error');
            elem.removeClass('checked');
        }
    });
    return correct;
}
