var countDownTimer;
var countDown = 90;

$(document).ready(function() {

    videoPlayerHandlers(664, 415, true);
    $('#newsTicker').bxSlider({
        alignment: 'vertical',        // 'horizontal', 'vertical' - direction in which slides will move
        controls: false,                // determines if default 'next'/'prev' controls are displayed
        speed: 500,                     // amount of time slide transition lasts (in milliseconds)
        pager: false,                   // determines if a numeric pager is displayed (1 2 3 4...)
        pager_short: false,             // determines if a 'short' numeric pager is displayed (1/4)
        pager_short_separator: ' / ',   // text to be used to separate the short pager
        margin: 0,                      // if 'horizontal', applies a right margin to each slide, if 'vertical' a
                                        // bottom margin is applied. example: margin: 50
        next_text: 'next',              // text to be displayed for the 'next' control
        next_image: '',                 // image to be used for the 'next' control
        prev_text: 'prev',              // text to be displayed for the 'prev' control
        prev_image: '',                 // image to be used for the 'prev' control
        auto: true,                     // determines if slides will move automatically
        pause: 3500,                    // time between each slide transition (auto mode only) 
        auto_direction: 'next',         // order in which slides will transition (auto mode only)
        auto_hover: true,               // determines if slideshow will pause while mouse is hovering over slideshow
        auto_controls: false,           // determines if 'start'/'stop' controls are displayed (auto mode only)
        ticker: false,                  // determines if slideshow will behave as a constant ticker
        ticker_controls: false,         // determines if 'start'/'stop' ticker controls are displayed (ticker mode only)
        ticker_direction: 'next',       // order in which slides will transition (ticker mode only)
        ticker_hover: true,             // determines if slideshow will pause while mouse is hovering over slideshow
        stop_text: 'stop',              // text to be displayed for the 'stop' control
        start_text: 'start',            // text to be displayed for the 'start' control
        wrapper_class: 'bxslider_wrap'  // class name to be used for the outer wrapper of the slideshow
    });


    $("#banner175x150").flashembed({
        version             : [9],
        allowscriptaccess   : true,
        allowfullscreen     : true,
        expressInstall      : '/swf/expressInstall.swf',
        src                 : '/swf/banner175x150.swf',
        width               : 175,
        height              : 150,
        wmode               : 'opaque',
        cachebusting        : false
    });
    
    $("#estrellaLogo").flashembed({
        version             : [9],
        allowscriptaccess   : true,
        allowfullscreen     : true,
        expressInstall      : '/swf/expressInstall.swf',
        src                 : '/swf/logoEstrella.swf',
        width               : 10,
        height              : 10,
        wmode               : 'transparent',
        cachebusting        : false
    });

    if (system_home_live == 1) {
        // Codistream LIVE

        var flashTarget = "videoPlayerHolder";
        $("#" + flashTarget).html('').append('<div id="videoPlayer_holder"></div>');

        var videoWidth  = 664;
        var videoHeight = 415;

        /*
                   showLogo   : 0,
                   applicationPath: 'rtmp://streaming01.codistream.net:80/santamonicaapilive',
                   streamName: 'live',
                   bitrate: 300,
                   idUser : 'santamonicaapi',
                   playerLogo : 'http://progressive01.codistream.net/userdata/santamonicaapi/skin/mosca.png',
                   thumbnail : '',
                   loadThumbnail: 'false',
                   autoPlay: true,
                   logoLink: '',
                   linkSource: ''

         */
        var vars        = {videoWidth: videoWidth,
                            videoHeight: videoHeight

        };
        var params      = {scale: "noScale",
                                         menu: "false",
                              allowFullScreen: "true",
                            allowScriptAccess: "always"};
        var attributes  = {id: "swfID",
                            name: "swfID"};

        swfobject.switchOffAutoHideShow();
        //swfobject.embedSWF("http://api.codistream.net/videoplayergenerator/clubseleccionLivePlayer.swf", "videoPlayer_holder", videoWidth, videoHeight, "10", "http://progressive01.codistream.net/generalFiles/swfjs/expressInstall.swf", vars, params, attributes);
        swfobject.embedSWF("http://progressive01.codistream.net/userdata/santamonicaapi/PlayerLive.swf", "videoPlayer_holder", videoWidth, videoHeight, "10", "http://progressive01.codistream.net/generalFiles/swfjs/expressInstall.swf", vars, params, attributes);
        //swfobject.embedSWF("http://api.codistream.net/videoplayergenerator/codistream-live-player-dyn-2.1.swf", "videoPlayer_holder", videoWidth, videoHeight, "10", "http://progressive01.codistream.net/generalFiles/swfjs/expressInstall.swf", vars, params, attributes);

//    } else {
//        loadVideoById(system_home_firstVideo, "videoPlayerHolder", 664, 415, true);
    }

    if (!system_userIsLogged && system_home_live == 1) {
        countDownHandler();
        countDownTimer = setInterval("countDownHandler()", 1000);
    }

    $(".playTransmision").click(function(){

        // Grab title from currently clicked transmision and place it in the header
        var titulo = $(this).closest('li').find('.transmisionTitulo').html();
        $("#currentVideoTitle").html(titulo);

        var transmisionId = getSuffix($(this).attr('id'));
        loadTransmisionById(transmisionId, 'videoPlayerHolder', 720, 421, true);
        return false;
    });

    // setup ul.tabs to work as tabs for each div directly under div.panes
    $("ul.tabs").tabs("div.panes > div",{
        rotate: true
    }).slideshow({
        interval: 7000,
        autoplay: true,
        clickable: false,
        autopause: false
    });

    $("ul.tabsLive").tabs("div.panesLive > div");

    $("ul.tabs").tabs("div.panes > div").click(function(){
        setTimeout(function(){ $("ul.tabs").data("slideshow").stop() }, 100)        
    })

});


function loadVideoHomeById(videoId, targetElement, videoWidth, videoHeight, withTouch) {
    $("ul.tabs").data("slideshow").stop()
    // Since SWFObject 2.0 the WHOLE container is replaced, so this is a
    // simple hack that will create a dummy container in which to load the videos
    // var flashTarget = "videoPlayerHolder";
    $("#" + targetElement).html('').append('<div id="videoPlayer_holder"></div>');

    // Codistream ON DEMAND player with "touch"
    var vars        = { videoToShow: videoId,
                           isLogged: system_userIsLogged,
                         videoWidth: videoWidth,
                        videoHeight: videoHeight };

    var params      = {             scale: "noScale",
                                     menu: "false",
                          allowFullScreen: "true",
                        allowScriptAccess: "always" };

    var attributes  = {   id: "swfID",
                        name: "swfID" };

    swfobject.switchOffAutoHideShow();

    if (withTouch) {
        playerUrl = "http://progressive01.codistream.net/userdata/santamonicaapi/PlayerTouch.swf";
    } else {
        playerUrl = "http://progressive01.codistream.net/userdata/santamonicaapi/PlayerOnDemand.swf";
    }

    swfobject.embedSWF(playerUrl, "videoPlayer_holder", videoWidth, videoHeight, "10",
    "http://progressive01.codistream.net/generalFiles/swfjs/expressInstall.swf", vars, params, attributes);

}

function switchToLoginState() {
    $(".mensaje1").hide();
    $(".mensaje2").show();
    loginHandlers('.formLoginLive');
}

function countDownHandler() {
    if (countDown <= 0) {
        switchToLoginState();
        clearTimeout(countDownTimer);
    }

    if (countDown < 10) {
        var countDownDisplay = '0' + countDown;
    } else {
        var countDownDisplay = countDown;
    }
    $("#countdown").html(countDownDisplay);
    
    countDown--;
   
}


