function createPlayer(file) {
    swfobject.removeSWF("videocanvans");
    var attributes = {};
        attributes.wmode = "transparent";
        attributes.id = "player1";
        attributes.name = "player1";
    var flashvars = {};
        flashvars.controlbar = "none";
        flashvars.file = '../video/'+file+'.flv';
        flashvars.image = 'video/'+file+'.png';
    var params = {};
        params = "always";
    swfobject.embedSWF("js/player.swf", "videocanvans", "249", "188", "9.0.0", null, flashvars, params, attributes );
}

var player = null;
var videoStatus = 'stopped';
function playerReady(thePlayer) {
    player = document.getElementById(thePlayer.id);
    addListeners();
}
var playerStatus;
function addListeners() {
  if (player) { 
    player.addModelListener("STATE", "checkState");
  } else {
    setTimeout("addListeners()",200);
  }

}
function checkState(obj) {
    playerStatus = obj.newstate
    if(obj.newstate == 'PLAYING') {
        $("#video a[href='video/play']").html('stop');
    } else {
        $("#video a[href='video/play']").html('play');
    }
}
function videoControl() {
    if(videoStatus == 'playing') {
        $("#video a[href='video/play']").html('stop');
        player.sendEvent("PLAY","true");
        videoStatus = 'playing';
    } else {
        $("#video a[href='video/play']").html('play');
        player.sendEvent("STOP","true");
        videoStatus = 'stopped';
    }
}

$(document).ready(function() {

    var files = new Array();
    var activeBilder = new Array();
    
    var buchFile = Array();
    var buchFileActive = 0;
    var buchActive = 0;
    var buchActivePage = 'page_1';
    var buchActivePageNext = '';
    
    var coverFile = Array();
    var coverFileActive = 0;
    var coverActive = 0;
    var coverActivePage = 'page_1';
    var coverActivePageNext = '';
    
    var videoFile = new Array();
    var videoActive = 0;
    
    $.ajax({
        type: "GET",
        url: "xml/franzfalsch.xml",
        dataType: "xml",
        success: function(xml) {
            
            $(xml).find('bilder').each(function(){
                var idname = $(this).attr('id');
                files[idname] = new Array();
                var i = 0;
                $(this).find('bild').each(function(){
                    files[idname][i] = new Array();
                    files[idname][i]['img'] = $(this).attr('name');
                    files[idname][i]['text'] = $(this).text();
                    $('#cache').html(
                        $('#cache').html()+'<img src="img/'+idname+'/'+ files[idname][i]['img'] +'_grau.png" /><img src="img/'+idname+'/'+ files[idname][i]['img'] +'_farbe.png" />'
                    )
                    i++;
                });
                activeBilder[idname] = 0;
                $('#'+idname+' .pic').css('background-image','url(img/'+idname+'/'+ files[idname][0]['img'] +'_grau.png)');
                $('#'+idname+' .text').html( files[idname][0]['text'] );
            });
            
            $(xml).find('buch').each(function(){
                var i = 0;
                $(this).find('serie').each(function(){
                    var y = 0;
                    buchFile[i] = new Array();
                    buchFile[i]['img'] = new Array();
                    buchFile[i]['text'] = $(this).find('text').text();
                    $(this).find('eintrag').each(function(){
                        buchFile[i]['img'][y] = $(this).attr('name');
                        
                        $('#cache').html(
                                $('#cache').html()+'<img src="img/buch/'+ buchFile[i]['img'][y] +'.png" />'
                            )
                        y++;
                    });
                    $('#cache').html(
                                $('#cache').html()+'<img src="img/buch/'+ buchFile[i]['img'][0] +'_grau.png" />'
                            )
                    i++;
                });
                $('#buch .text').html( buchFile[0]['text'] );
                $('#buch .'+buchActivePage).css('background-image','url(img/buch/'+buchFile[buchActive]['img'][0]+'_grau.png)');
            });
            
            $(xml).find('cover').each(function(){
                var i = 0;
                $(this).find('serie').each(function(){
                    var y = 0;
                    coverFile[i] = new Array();
                    coverFile[i]['img'] = new Array();
                    coverFile[i]['text'] = $(this).find('text').text();
                    $(this).find('eintrag').each(function(){
                        coverFile[i]['img'][y] = $(this).attr('name');
                        
                        $('#cache').html(
                                $('#cache').html()+'<img src="img/cover/'+ coverFile[i]['img'][y] +'.png" />'
                            )
                        y++;
                    });
                    i++;
                });
                $('#cover .text').html( coverFile[0]['text'] );
                $('#cover .'+coverActivePage).css('background-image','url(img/cover/'+coverFile[coverActive]['img'][0]+'_grau.png)');
            });
            
            $(xml).find('videos').each(function(){
                var i = 0;
                $(this).find('video').each(function(){
                    var y = 0;
                    videoFile[i] = new Array();
                    videoFile[i]['name'] = $(this).attr('name');
                    videoFile[i]['text'] = $(this).text();
                    i++;
                });
                $('#video .text').html( videoFile[videoActive]['text'] );
                createPlayer(videoFile[videoActive]['name']);
            });
            
        }
    });
    
    var activeColor = 3;
    var cmyk = new Array('franzfalsch_cyan.png','franzfalsch_gelb.png','franzfalsch_magenta.png','franzfalsch.png');
    $('#franzfalsch').mouseenter(function() {
        $('#franzfalsch').everyTime(400, function () {
            activeColor++;
            if(activeColor == cmyk.length) {
                activeColor = 0;
            }
            $(this).find('h1').css('background-image','url(img/'+cmyk[activeColor]+')');
        });
    });
    $('#franzfalsch').mouseleave(function() {
        $(this).stopTime();
    });

    $("a[href='top']").live('click',function() {
        $(document).scrollTo( 0, 800 );
        return false;
    });

    var opacity = "0.80";
    $('.whitelayer').css('opacity',opacity);
    
    $('.bilder').mouseenter(function() {
        var newimage = 'img/'+$(this).attr('id')+'/'+files[ $(this).attr('id') ][ activeBilder[$(this).attr('id')] ]['img']+'_farbe.png'
        var idname = $(this).attr('id');
        $(':animated',this).stop(true,true);
        $(this).find('.whitelayer').css('opacity',0);
        $(this).find('.pic').css('background-image','url('+newimage+')');
        $('.text',this).html( files[$(this).attr('id')][activeBilder[$(this).attr('id')]]['text'] );
    });
    $('.bilder').mouseleave(function() {
        var newimage = 'img/'+$(this).attr('id')+'/'+files[ $(this).attr('id') ][ activeBilder[$(this).attr('id')] ]['img']+'_grau.png'
        var idname = $(this).attr('id');
        $(this).find('.whitelayer').animate({
            opacity: opacity
        }, 500, function() {
            $('#'+idname+' .pic').css('background-image','url('+newimage+')');
        });
        $('.text',this).html( files[$(this).attr('id')][activeBilder[$(this).attr('id')]]['text'] );
    });
    $(".bilder a[href*='next']").click(function() {
        if( activeBilder[$(this).parents('.bilder').attr('id')]+1 == files[$(this).parents('.bilder').attr('id')].length) {
            activeBilder[$(this).parents('.bilder').attr('id')] = 0;
        } else {
            activeBilder[$(this).parents('.bilder').attr('id')]++;
        }
        $(this).parents('.bilder').find('.pic').css('background-image','url(img/'+$(this).parents('.bilder').attr('id')+'/'+files[ $(this).parents('.bilder').attr('id') ][ activeBilder[$(this).parents('.bilder').attr('id')] ]['img']+'_farbe.png)');
        $(this).parents('.bilder').find('.text').html( files[$(this).parents('.bilder').attr('id')][activeBilder[$(this).parents('.bilder').attr('id')]]['text'] );
        return false;
    });
    $(".bilder a[href*='previous']").click(function() {
        if(activeBilder[$(this).parents('.bilder').attr('id')] == 0) {
            activeBilder[$(this).parents('.bilder').attr('id')] = files[$(this).parents('.bilder').attr('id')].length-1;
        } else {
            activeBilder[$(this).parents('.bilder').attr('id')]--;
        }
        $(this).parents('.bilder').find('.pic').css('background-image','url(img/'+$(this).parents('.bilder').attr('id')+'/'+files[ $(this).parents('.bilder').attr('id') ][ activeBilder[$(this).parents('.bilder').attr('id')] ]['img']+'_farbe.png)');
        $(this).parents('.bilder').find('.text').html( files[$(this).parents('.bilder').attr('id')][activeBilder[$(this).parents('.bilder').attr('id')]]['text'] );
        return false;
    });
    $('.bilder .whitelayer').click(function() {
        $("#history").html('<p><a href="top">&uarr;</a> <img src="img/'+$(this).parents('.bilder').attr('id')+'/'+files[ $(this).parents('.bilder').attr('id') ][ activeBilder[$(this).parents('.bilder').attr('id')] ]['img']+'.png" align="top" /></p>'+$("#history").html());
        $(document).scrollTo( $('#history'), 800);
    });


/***************
 * BUCH
 ***************/
    function buchAnimation() {
        $('#buch').everyTime(2000, function () {
            buchFileActive = buchFileActive+1;
            if(buchFileActive == buchFile[buchActive]['img'].length) buchFileActive = 0;
            if(buchActivePage=='page_1') {
                buchActivePageNext='page_2';
            } else {
                buchActivePageNext='page_1';
            }
            $('#buch .'+buchActivePageNext).css('background-image','url(img/buch/'+buchFile[buchActive]['img'][buchFileActive]+'.png)');
            $('#buch .'+buchActivePage).animate({ 
                left: "-304px"
            }, 300, function() {
                $('#buch .'+buchActivePageNext).animate({
                    left: "0px"
                }, 800, function() {
                    $('#buch .'+buchActivePage).css('left','304px');
                    buchActivePage = buchActivePageNext;
                });
            });
        });
    }
    $('#buch').mouseenter(function() {
        $(this).find('.whitelayer').stop(true,true);
        buchFileActive = 0;
        $('#buch .'+buchActivePage).css('background-image','url(img/buch/'+buchFile[buchActive]['img'][buchFileActive]+'.png)');
        $(this).find('.whitelayer').css('opacity',0);
        buchAnimation();
    });
    $('#buch').mouseleave(function() {
        $('#buch').stopTime();
        buchFileActive = 0;
        var idname = $(this).attr('id');
        $(this).find('.whitelayer').animate({
            opacity: opacity
        }, 400, function() {
            $('#'+idname+' .'+buchActivePage).css('background-image','url(img/buch/'+buchFile[buchActive]['img'][buchFileActive]+'_grau.png)');
        });
        buchFileActive = 0;
    });
    $("#buch a[href='buch/next']").click(function() {
        $('#buch').stopTime();
        buchActive = buchActive+1;
        if(buchActive == buchFile.length) buchActive = 0;
        buchFileActive = 0;
        $('#buch .page_1').stop(true,false).css('left','0px');
        $('#buch .page_2').stop(true,false).css('left','304px');
        buchActivePage = 'page_1';
        buchActivePageNext = 'page_2';
        $('#buch .page_1').css('background-image','url(img/buch/'+buchFile[buchActive]['img'][buchFileActive]+'.png)');
        $('#buch .text').html( buchFile[buchActive]['text'] );
        buchAnimation();
        return false;
    });
    $("#buch a[href='buch/previous']").click(function() {
        $('#buch').stopTime();
        buchActive = buchActive-1;
        if(buchActive < 0) buchActive = buchFile.length-1;
        buchFileActive = 0;
        $('#buch .page_1').stop(true,false).css('left','0px');
        $('#buch .page_2').stop(true,false).css('left','304px');
        buchActivePage = 'page_1';
        buchActivePageNext = 'page_2';
        $('#buch .page_1').css('background-image','url(img/buch/'+buchFile[buchActive]['img'][buchFileActive]+'.png)');
        $('#buch .text').html( buchFile[buchActive]['text'] );
        buchAnimation();
        return false;
    });
    $("#buch .whitelayer").click(function() {
        var html = '<p><a href="top">&uarr;</a> ';
        for (i=0; i < buchFile[buchActive]['img'].length; i++) {
            html = html + '<img src="img/buch/'+buchFile[buchActive]['img'][i]+'_gross.png" align="top" /><br />';
        }
        html = html + '</p>'; 
        $("#history").html(html+$("#history").html());
        $(document).scrollTo( $('#history'), 800);
    });
    
    

    function coverAnimation() {
        $('#cover').everyTime(2000, function () {
            coverFileActive = coverFileActive+1;
            if(coverFileActive == coverFile[coverActive]['img'].length) coverFileActive = 0;
            if(coverActivePage=='page_1') {
                coverActivePageNext='page_2';
            } else {
                coverActivePageNext='page_1';
            }
            $('#cover .'+coverActivePageNext).css('background-image','url(img/cover/'+coverFile[coverActive]['img'][coverFileActive]+'.png)');
            $('#cover .'+coverActivePage).animate({ 
                left: "-144px"
            }, 300, function() {
                $('#cover .'+coverActivePageNext).animate({
                    left: "0px"
                }, 800, function() {
                    $('#cover .'+coverActivePage).css('left','144px');
                    coverActivePage = coverActivePageNext;
                });
            });
        });
    }
    $('#cover').mouseenter(function() {
        $(this).find('.whitelayer').stop(true,true);
        coverFileActive = 0;
        $('#cover .'+coverActivePage).css('background-image','url(img/cover/'+coverFile[coverActive]['img'][coverFileActive]+'.png)');
        $(this).find('.whitelayer').css('opacity',0);
        coverAnimation();
    });
    $('#cover').mouseleave(function() {
        $('#cover').stopTime();
        coverFileActive = 0;
        var idname = $(this).attr('id');
        $(this).find('.whitelayer').animate({
            opacity: opacity
        }, 400, function() {
            $('#'+idname+' .'+coverActivePage).css('background-image','url(img/cover/'+coverFile[coverActive]['img'][coverFileActive]+'_grau.png)');
        });
        coverFileActive = 0;
    });
    $("#cover a[href='cover/next']").click(function() {
        $('#cover').stopTime();
        coverActive = coverActive+1;
        if(coverActive == coverFile.length) coverActive = 0;
        coverFileActive = 0;
        $('#cover .page_1').stop(true,false).css('left','0px');
        $('#cover .page_2').stop(true,false).css('left','144px');
        coverActivePage = 'page_1';
        coverActivePageNext = 'page_2';
        $('#cover .'+coverActivePage).css('background-image','url(img/cover/'+coverFile[coverActive]['img'][coverFileActive]+'.png)');
        $('#cover .text').html( coverFile[coverActive]['text'] );
        coverAnimation();
        return false;    
    });
    $("#cover a[href='cover/previous']").click(function() {
        $('#cover').stopTime();
        coverActive = coverActive-1;
        if(coverActive < 0) coverActive = coverFile.length-1;
        coverFileActive = 0;
        $('#cover .page_1').stop(true,false).css('left','0px');
        $('#cover .page_2').stop(true,false).css('left','144px');
        coverActivePage = 'page_1';
        coverActivePageNext = 'page_2';
        $('#cover .'+coverActivePage).css('background-image','url(img/cover/'+coverFile[coverActive]['img'][coverFileActive]+'.png)');
        $('#cover .text').html( coverFile[coverActive]['text'] );
        coverAnimation();
        return false;
    });
    $("#cover .whitelayer").click(function() {
        var html = '<p><a href="top">&uarr;</a> ';
        for (i=0; i < coverFile[coverActive]['img'].length; i++) {
            html = html + '<img src="img/cover/'+coverFile[coverActive]['img'][i]+'_gross.png" align="top" /><br />';
        }
        html = html + '</p>'; 
        $("#history").html(html+$("#history").html());
        $(document).scrollTo( $('#history'), 800);
    });
    

    $('#video').mouseenter(function() {
        $(this).find('.whitelayer').fadeOut(400,function(){});
    });
    $('#video').mouseleave(function() {
        if(playerStatus != 'PLAYING') {
            $(this).find('.whitelayer').fadeIn(400);
        }
    });
    $("#video a[href='video/play']").click(function() {
        //videoControl();
        if(playerStatus == 'PLAYING') {
            player.sendEvent("STOP","true");
        } else {
            player.sendEvent("PLAY","true");
        }
        return false;
    });
    $("#video a[href='video/next']").click(function() {
        player.sendEvent("STOP","true");
        videoActive = videoActive+1;
        if(videoActive == videoFile.length) videoActive = 0;
        $('#video .text').html( videoFile[videoActive]['text'] );
        var stream = {file:'../video/'+videoFile[videoActive]['name']+'.flv', image:'video/'+videoFile[videoActive]['name']+'.png'};
        player.sendEvent('LOAD',stream);
        player.sendEvent("PLAY","true");
        return false;
    });
    $("#video a[href='video/previous']").click(function() {
        player.sendEvent("STOP","true");
        videoActive = videoActive-1;
        if(videoActive < 0) videoActive = videoFile.length-1;
        $('#video .text').html( videoFile[videoActive]['text'] );
        var stream = {file:'../video/'+videoFile[videoActive]['name']+'.flv', image:'video/'+videoFile[videoActive]['name']+'.png'};            
        player.sendEvent('LOAD',stream);
        player.sendEvent("PLAY","true");
        return false;
    });
    
});
