﻿var player = null;
var playlist = null;
var valueCategory = null;
var valueAction = null;
var valueLabel = null;
var valueOptValue = null;
var changeProduct;


function playerReady(obj) {
    player = gid(obj.id);
    addListeners();    
};

function addListeners() {
    playlist = player.getPlaylist();
    if ((playlist != null) && (playlist.length > 0)) {
        player.addModelListener('STATE', 'stateMonitor');
    }
    else {
        setTimeout("addListeners();", 100);
    }    
};

function stateMonitor(obj) {
    
    if ((changeProduct || ((obj.newstate == "PLAYING" || obj.newstate == "BUFFERING") && (obj.oldstate == "IDLE" || obj.oldstate == "COMPLETED"))) && pageTracker != null) {
        changeProduct = false;
        gaGetValues();
        pageTracker._trackEvent(valueCategory, valueAction, valueLabel, parseInt(valueOptValue));
        //alert(document.title);
        if (document.title == "#Reproductor") {
           
            setTimeout("document.title = 'Uno Noticias'", 1);
        }
    }
};

function gid(name) {
    return document.getElementById(name);
};

function gaGetValues() {

    valueCategory = document.getElementById('hdnCategory').value;
    valueAction = document.getElementById('hdnAction').value;
    valueLabel = document.getElementById('hdnLabel').value;
    valueOptValue = document.getElementById('hdnValue').value;
};

function gaSetValues(category, action, label, value) {

//    if (valueOptValue == value) {
//        if (valueCategory == category)
//            change = false;
//        else
//            change = true;
//    }
//    else
    //        change = true;
    
    if (valueOptValue == value)
        changeProduct = false     
    else
        changeProduct = true;
        
    document.getElementById('hdnCategory').value = category ;
    document.getElementById('hdnAction').value = action;
    document.getElementById('hdnLabel').value = label;
    document.getElementById('hdnValue').value = value;
};

function downloadMonitor(loadView) {
    gaGetValues();
    pageTracker._trackEvent("Descargas", valueAction.substring(5, valueAction.length), valueLabel,parseInt(valueOptValue));
    
};

function gaCustomVar(index, name, value, scope,isHome) {
    pageTracker._setCustomVar(parseInt(index), name, value, parseInt(scope));
    
    if (isHome) {
        pageTracker._setCustomVar(2, 'Principal', value, parseInt(scope));    
    }
    //pageTracker._trackPageview();
};

function gaSetValuesFlash(event, section, name, id, considerar) {
    var sectionName = section;
    if (considerar.toString() == "true") {
        sectionName = "Seccion Error";
    }
   pageTracker._trackEvent(event, sectionName , name + ' - ' + id.toString(), parseInt(id));
};