/* JW Player Setup */
var currentState = 'NONE';
var player = null;
function playerReady(thePlayer) {
    player = window.document[thePlayer.id];
    addListeners();
}
function addListeners() {
    if (player) { 
        player.addModelListener("STATE", "stateListener");
    } else {
        setTimeout("addListeners()",100);
    }
}
function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
    currentState = obj.newstate;
    previousState = obj.oldstate;
    if((currentState=='COMPLETED') && (jQuery("#cinema_view").dialog("isOpen"))){
        jQuery('#cinema_view').dialog('close');
    }
    if((currentState=='COMPLETED') && (jQuery("#cinema_view-small").dialog("isOpen"))){
        jQuery('#cinema_view-small').dialog('close');
    }
    if (currentState=='PLAYING' && $.onPlayerPlaying != undefined) {
        $.onPlayerPlaying();
    }
    if (currentState=='COMPLETED' && $.onPlayerComplete != undefined) {
        $.onPlayerComplete();
    }
}
function createPlayer(webRoot, playerSkin, playerFile, playerImg, allowFs, playerId, playerDivId, playerWidth, playerHeight, flashVars) {
    if (flashVars == null) {
     flashVars = {};
    } 
    flashVars = jQuery.extend({
        skin: webRoot+playerSkin,
        image: playerImg,
        file: playerFile,
        bufferlength: "2",
        abouttext: "Condor Films AG",
        aboutlink: "http://www.condorfilms.com/",
        screencolor: "#ffffff",
        autostart: "true"
    }, flashVars);
    var params = {
        menu: "false",
        allowfullscreen: allowFs,
        allowscriptaccess: "always",
        wmode: "transparent"
        //,bgcolor: "#ffffff"
    };
    var attributes = {
        id:playerId,    
        name:playerId
    };
    swfobject.embedSWF(webRoot+"_swf/player.swf", playerDivId, playerWidth, playerHeight, "9.0.115", webRoot+"_swf/expressInstall.swf", flashVars, params, attributes);
}

/* jQuery Setup */
$(document).ready(function() {
    $.fn.replaceHeaderLink = function() {
        $(this).find('.tit').each(function() {
            var repl = $('<span>'+$(this).html()+'</span>');
            repl.attr('class', $(this).attr('class'));
            repl.data('replaced', $(this));
            $(this).replaceWith(repl);
        });
    };
    $.fn.restoreHeaderLink = function() {
        $(this).find('.tit').each(function() {
            if ($(this).data('replaced')) {
                $(this).replaceWith($(this).data('replaced'));
            }
        });
    };
    $('.accordion').accordion({
        autoHeight: false
        ,changestart: function(event, ui) {
            ui.oldHeader.restoreHeaderLink();
            ui.newHeader.replaceHeaderLink();
        }
    });
    $('.accordion').find('.tit:first').each(function() {
        $(this).closest('h1, h2').replaceHeaderLink();
    });
    if ($.jcarousel) {
            var initCallback = function(carousel)
            {
                    carousel.buttonNext.bind('click', function() {
                            carousel.options.auto = false;
                    });
                    carousel.buttonPrev.bind('click', function() {
                            carousel.options.auto = false;
                    });
                    $('#matrix_carousel').mouseenter(function() {
                        carousel.options.auto = false;
                    });
            }
            var matrixClassName = $('#matrix_carousel').attr('class');
            if (matrixClassName != undefined) {
                var rx = /([0-9])x\1/;
                var matrixSize = parseInt(rx.exec(matrixClassName)[1]);
                jQuery('#matrix_carousel').jcarousel({
                    //auto: (matrixSize == 6 ? 5 : false),
                    scroll: matrixSize,
                    initCallback: initCallback
                });
            }

            jQuery('#category_carousel').jcarousel({
                auto: 5,
                scroll: 1,
                //wrap:'circular',
                        initCallback: initCallback
            });
            jQuery('#category_carousel_sub').jcarousel({
                //auto: 5,
                scroll: 1,
                //wrap: 'circular',
                        initCallback: initCallback
            });
            jQuery('#team_carousel').jcarousel({
                auto: 5,
                scroll: 1,
                wrap:'circular',
                        initCallback: initCallback
            });

        jQuery('.jcarousel-skin-cc li a img').fadeIn(600);
        jQuery('.jcarousel-skin-cc li a span').each(function() {
            if (!$(this).hasClass('inactive')) {
                $(this).fadeIn(600);
            }
        });
    }

    jQuery("#cinema_view").dialog({
        autoOpen:false,
        draggable:false,
        modal:true,
        resizable:false,
        width:1024,
        height:595
    });
    jQuery("#cinema_view-small").dialog({
        autoOpen:false,
        draggable:false,
        modal:true,
        resizable:false,
        width:768,
        height:595
    });
    jQuery(window).resize(function(){
        jQuery("#cinema_view").dialog("option","position",jQuery("#cinema_view").dialog("option","position","center"));
        jQuery("#cinema_view-small").dialog("option","position",jQuery("#cinema_view-small").dialog("option","position","center"));
    });
});
