var teaser_active_page = 1;
var teaser_timer = 5000;
var teaser_pages = 5;
var teaser_src = '';
var mouse_x = 0;
var mouse_y = 0;
var teaser_autochange_disabled = false;

var teaser_numbers = {
    'mod_teaser_link1' : 0,
    'mod_teaser_link2' : 1,
    'mod_teaser_link3' : 2,
    'mod_teaser_link4' : 3,
    'mod_teaser_link5' : 4
};

function teaserLoadPage(teaser) {
    if (teaser == 'auto') {
        window.setTimeout("teaserLoadPage('auto')", teaser_timer);
        if (mouseonobject($('#mod_teaser_home')) || teaser_autochange_disabled)
            return;
        teaser = ((teaser_active_page + 1) % teaser_pages);
    }
    if (teaser == teaser_active_page)
        return;
    $('.mod_teaser_navigation li').removeClass('highlighted');
    $('#mod_teaser_link' + (teaser + 1)).parent().addClass('highlighted');
    $('.mod_teaser_content').css('z-index', 1);
    $('.mod_teaser_content').stop(true, true);
    $('#mod_teaser_content' + (teaser + 1)).css('z-index', 2);
    $('#mod_teaser_content' + (teaser_active_page + 1)).css('z-index', 3);
    $('#mod_teaser_content' + (teaser + 1)).fadeIn(0, function() {
        for (i = 0; i < teaser_pages; i++)
            if (i != teaser)
                $('#mod_teaser_content' + (i + 1)).fadeOut(500);
    });
    teaser_active_page = teaser;
}

function mouseonobject(object) {
    position = object.offset();
    dimension = {width: object.width(), height: object.height()};
    return mouse_x > position.left && mouse_x < position.left + dimension.width && mouse_y > position.top && mouse_y < position.top + dimension.height;
}
    
function loadTeaser() {
    //Inhalte laden
    jQuery.getJSON(
        teaser_src,
        function(data){
            teaser_pages = 0;
            $.each(data, function(i,item){
                if (item.title)
                    teaser_pages++;
                else
                    $('#mod_teaser_link' + (i + 1)).parent().hide();
                $('#mod_teaser_link' + (i + 1)).html(item.title);
                if (typeof(item.left) == 'undefined')
                    item.left = 75;
                if (item.video)
                    item.content += '<div style="position: absolute; top: 0px; left: '+(item.left)+'px; width: 455px; height: 256px;"><div id="mod_teaser_home_player" style="width: 455px; height: 256px;"></div></div>';
                $('#mod_teaser_content' + (i + 1)).html(
                    '<div class="mod_teaser_image inner" style="background-image: url('+item.background+');">'+item.content+'</div><div class="border_left"></div><div class="border_right"></div><div class="border_top"></div><div class="corner_left"></div><div class="corner_right"></div>'
                );
                if (item.borders)
                    $('#mod_teaser_content' + (i + 1)).addClass('borders');
                if (item.video) {
                  var videopath = 'http://shop.fcbayern.de/';
                  var skinpath = 'http://shop.fcbayern.de/';
                  var configpath_xml = 'http://shop.fcbayern.de/';
                  var configpath_json = 'http://shop.fcbayern.de/';
                  
                  var flashvars = {
                      skin: skinpath + 'flash/vplayer/skin_fcbshop.swf',
                      config: configpath_xml + escape('conf.php?video='+item.video+'&state=fcb-free&template=templateRem.xml&preview='+item.preview+'&id=1')
                  };
                  var params = {
                      menu              : "false",
                      scale             : "noScale",
                      allowFullscreen   : "true",
                      allowScriptAccess : "always",
                      bgcolor           : "#000000",
                      wmode             : "transparent"
                  };
                  var attributes = {
                      id:"Multiplayer"
                  };
                  swfobject.embedSWF(videopath + "flash/vplayer/vplayer.swf", "mod_teaser_home_player", "455", "256", "9.0.0", videopath + "flash/vplayer/expressInstall.swf", flashvars, params, attributes);
                  var player = new Player(
                      "mod_teaser_home_player",
                      configpath_json + ('conf.php?video='+item.video+'&state=fcb-free&template=template.json&preview='+item.preview+'&id=1')
                  );
                  $('#mod_teaser_content' + (i + 1) + ' *').bind('click', function() {
                      teaser_autochange_disabled = true;
                  });
                }
            });
            var width = 715;
            var widths = new Array();
            $('#mod_teaser_link' + teaser_pages).parent().css('margin-right', '0');
            for (i = 0; i < teaser_pages; i++) {
                width -= $('#mod_teaser_link' + (i + 1)).parent().width() + 20;
                widths[i] = $('#mod_teaser_link' + (i + 1)).parent().width();
            }
            for (i = 0; i < teaser_pages; i++) {
                $('#mod_teaser_link' + (i + 1)).parent().width(widths[i] + Math.floor(width / teaser_pages));
            }
            teaserLoadPage(0);
            $('.mod_teaser_navigation a').hover(function() {
                teaserLoadPage(teaser_numbers[$(this).attr('id')]);
            });
            $('.mod_teaser_navigation a').click(function() {
                $(this).blur();
            });
            $(document).mousemove(function(event) {
                mouse_x = event.pageX;
                mouse_y = event.pageY;
            });
            window.setTimeout("teaserLoadPage('auto')", teaser_timer);
        }
    );
}

