﻿var playerOpinion = null;
var playerCulture = null;
var playlistOpinion = null;
var playlistCulture = null;
var valueCategoryCustom = null;
var valueActionCustom = null;
var valueLabelCustom = null;
var valueOptValueCustom = null;

function playerReady(obj) {
    if (obj.id != null) {
        if (obj.id == "swfplayer") {
            playerOpinion = gid(obj.id);            
        }
        else {
            playerCulture = gid(obj.id);            
        }
        addListenersCustom(obj.id);
    }
    else {
        if (playerCulture == null) {
            playerCulture = gid("Player_Culture");
            addListenersCustom("Player_Culture");                      
        }
        else {
            playerOpinion = gid("swfplayer");
            addListenersCustom("swfplayer");  
        }       
        
    }
};

function addListenersCustom(id) {
    if (id == "swfplayer") { 
        playlistOpinion = playerOpinion.getPlaylist();
        if ((playlistOpinion != null) && (playlistOpinion.length > 0)) {
            playerOpinion.addModelListener('STATE', 'stateMonitorOpinion');
        }
        else {
            setTimeout("addListeners("+id+");", 100);
        }
    }
    else {
        playlistCulture = playerCulture.getPlaylist();
        if ((playlistCulture != null) && (playlistCulture.length > 0)) {
            playerCulture.addModelListener('STATE', 'stateMonitorCulture');
        }
        else {
            setTimeout("addListeners("+id+");", 100);
        }
    }
};

function stateMonitorOpinion(obj) {
    if ((obj.newstate == "PLAYING" || obj.newstate == "BUFFERING") && playerCulture != null) {
        playerCulture.sendEvent('STOP');
    }
    stateMonitor(obj);
};


function stateMonitorCulture(obj) {
    if ((obj.newstate == "PLAYING" || obj.newstate == "BUFFERING") && playerOpinion != null) {
        playerOpinion.sendEvent('STOP');
    }
    stateMonitorCustom(obj);
};

function stateMonitorCustom(obj) {
    if (((obj.newstate == "PLAYING" || obj.newstate == "BUFFERING") && (obj.oldstate == "IDLE" || obj.oldstate == "COMPLETED")) && pageTracker != null) {
        gaGetValuesCustom();
        pageTracker._trackEvent(valueCategory, valueAction, valueLabel,parseInt(valueOptValue));
    }
};

function gaGetValuesCustom() {    
    valueCategoryCustom = document.getElementById('hdnCategoryCustom').value
    valueActionCustom = document.getElementById('hdnActionCustom').value;
    valueLabelCustom = document.getElementById('hdnLabelCustom').value;
    valueOptValueCustom = document.getElementById('hdnValueCustom').value;
};

function gaSetValuesCustom(category, action, label, value) {
    document.getElementById('hdnCategoryCustom').value = category;
    document.getElementById('hdnActionCustom').value = action;
    document.getElementById('hdnLabelCustom').value = label;
    document.getElementById('hdnValueCustom').value = value;
};

function loadVideoCulture(pathSwObject, src, longStreamer, thumbnail) {

    var indexOf = src.substring(longStreamer).indexOf("?");
    var fileUrl;
    var serverUrl;
    var flashvars = {};

    var streamer = src.substring(0, 4);

    if (streamer == "rtmp") {

        if (indexOf > 0)
            fileUrl = src.substring(longStreamer, indexOf + longStreamer);
        else
            fileUrl = src.substring(longStreamer);
        serverUrl = src.substring(0, longStreamer);
        flashvars.streamer = serverUrl;
    }
    else
        fileUrl = src.substring(0, indexOf + longStreamer); ;

    flashvars.file = fileUrl;
    flashvars.autostart = "false";
    flashvars.fullscreen = "false";
    flashvars.image = thumbnail;
    var params = {};
    var attributes = {};
    params.menu = "false";
    params.wmode = "opaque";
    params.scale = "noscale";
    params.allowfullscreen = "true";
    attributes.id = "Player_Culture";

    swfobject.embedSWF(pathSwObject, "cultureplayer", "285", "235", "10.0.0", "expressInstall.swf", flashvars, params, attributes);
};

function setDetalleRelevante(title, desc) {
    document.getElementById('titleRelevante').style.display = 'block';
    document.getElementById('title').innerHTML = title;
    document.getElementById('desc').innerHTML = desc;
    
};
