var salioalerta25=false;
var salioalerta50=false;
var salioalerta75=false;
var salioalerta100=false;

// YWA Wrapper Function
function captureOnclick(actionnumber) {   
  var YWATracker = YWA.getTracker("10001515792362"); 
  YWATracker.setAction(actionnumber); 
  YWATracker.submit_action(); 
  //Action 13 = 25% del video
  //Action 14 = 50% del video
  //Action 15 = 75% del video
  //Action 16 = 100% del video
}   

     

// Display information about the current state of the player
function updatePlayerInfo(playerId) {
        // Also check that at least one function exists since when IE unloads the
        // page, it will destroy the SWF before clearing the interval.
        //console.log(playerId);
       
        if(document.getElementById(playerId) && document.getElementById(playerId).getDuration) {
          var porcentaje = (document.getElementById(playerId).getCurrentTime()/document.getElementById(playerId).getDuration())*100;
          var playerState = document.getElementById(playerId).getPlayerState();
          if (porcentaje>25 && salioalerta25==false && playerState==1) {
          	/* Llamada a Analytics */
          	pageTracker._trackPageview("/videos/vista_25_porciento");
          	captureOnclick("13");
          	console.log(playerId + ": 25%");
          	/* --------------------*/
          	salioalerta25=true;
		  }
		  if (porcentaje>50 && salioalerta50==false && playerState==1) {
          	/* Llamada a Analytics */
          	pageTracker._trackPageview("/videos/vista_50_porciento");
          	captureOnclick("14");
          	console.log(playerId + ": 50%");
          	/* --------------------*/
          	salioalerta50=true;
		  }
		  if (porcentaje>75 && salioalerta75==false && playerState==1) {
          	/* Llamada a Analytics */
          	pageTracker._trackPageview("/videos/vista_75_porciento");
          	captureOnclick("15");
          	console.log(playerId + ": 75%");
          	/* --------------------*/
          	salioalerta75=true;
		  }
          if (porcentaje>=98 && salioalerta100==false && playerState==1) {
          	/* Llamada a Analytics */
          	pageTracker._trackPageview("/videos/vista_100_porciento");
          	captureOnclick("16");
          	console.log(playerId + ": 100%");
          	/* --------------------*/
          	salioalerta100=true;
		  }
        }
}
      
      // This function is automatically called by the player once it loads
      function onYouTubePlayerReady(playerId) {
      	//console.log(playerId);
        //ytplayer = document.getElementById(playerId);
        // This causes the updatePlayerInfo function to be called every 250ms to
        // get fresh data from the player
        setInterval("updatePlayerInfo('" + playerId + "')", 1400);
        updatePlayerInfo(playerId);
		//document.getElementById(playerId).addEventListener("onStateChange", "onPlayerStateChange");
      }
      
      // The "main method" of this sample. Called when someone clicks "Run".
      function loadPlayer(videoid, videodiv, width, height) {
        // The video to load
        var videoID = videoid;
        // Lets Flash from another domain call JavaScript
        var params = { allowScriptAccess: "always" };
        // The element id of the Flash embed
        var atts = { id: "ytPlayer" + videodiv };
        // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
        swfobject.embedSWF("http://www.youtube.com/v/" + videoID + "&enablejsapi=1&playerapiid=ytPlayer" + videodiv, videodiv, width, height, "8", null, null, params, atts);
      }
