/* You Tube Ids */
//EfwQFrchc5w -- Video 1  
//prh9Nj0rPV0 -- Video 2
//k5-faE_UyHI -- Video 3

var gdata = "https://gdata.youtube.com/feeds/api/videos/";
var jparam = "?v=2&alt=json";
var videoID = "";
var interval;
var videoName;
var videoMetaData = [];

if (($.browser.msie)) {
    $("#videoShowHide").removeClass("visibility");
	$("#loading").css({opacity: 0});
}

// Update a particular HTML element with a new value
function updateHTML(elmId, value) {
    document.getElementById(elmId).innerHTML = value;
}

// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
    alert("An error occured of type:" + errorCode);
}

// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
	$.ajax({ type:"GET", url:gdata + videoID + jparam , dataType:"json", 
			success: function(data){
				//videoName = data.entry[ "title" ].$t;
				//videoMetaData.push(videoName);
			}
	});
		
    ytplayer = document.getElementById("ytPlayer");
    ytplayer.addEventListener("onError", "onPlayerError");
    ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
	
	initVideoMenu();
	
	if (!($.browser.msie)) {
        $("#videoShowHide").removeClass("visibility");
		$("#loading").remove();
    }
}

function initVideoMenu(){
	$(".about-video-menu > li > a").each(function () {
	    var $this = $(this);

	    // Loads the selected video into the player.
	    $this.bind("click", function (event) {
	        var id = event.target.id;
			var vidTxt = $("#videoTxt").text();

	        switch (id) {
		        case "video1":
					videoName = "the argentina video";
		            videoID = "EfwQFrchc5w";
					$("#videoTxt").text("Watch Positivity in Action as We Bring Solar Light to Young Students in Rural Argentina");

		            _gaq.push(['_trackEvent', 'tpe_gnr_2011', 'click', 'the argentina video']);

		        break;

		        case "video2":
                    videoName = "the phillipines video";
		            videoID = "k5-faE_UyHI";
					$("#videoTxt").text("Share in the excitement of the Energizer Night Race");

					_gaq.push(['_trackEvent', 'tpe_gnr_2011', 'click', 'the phillipines video']);
		        break;

		        case "video3":  		
					videoName = "one million lights";
	            	 videoID = "prh9Nj0rPV0";
				     $("#videoTxt").text("Experience the One Million Lights Story");

				     _gaq.push(['_trackEvent', 'tpe_gnr_2011', 'click', 'one million lights']);  
		        break;
	        }

	        if (ytplayer) {
	            ytplayer.loadVideoById(videoID);
				
				//Get meta data for videos
				$.ajax({ type:"GET", url:gdata + videoID + jparam , dataType:"json", 
						success: function(data){
							//videoName = data.entry[ "title" ].$t;
							//videoMetaData.push(videoName);
						}
				});
	        }
	    });
	});
}

function onPlayerStateChange(newState) {
    var unstarted = -1;
    var ended = 0;
    var playing = 1;
    var paused = 2;
    var buffering = 3;
    var videoCued = 5;
	
	if(newState == playing){
		_gaq.push(['_trackEvent', 'tpe_gnr_2011', 'play', videoName]);
		interval = setInterval(updatePlayerInfo, 1150);
	    updatePlayerInfo();
	}
	
	if(newState == paused){
		_gaq.push(['_trackEvent', 'tpe_gnr_2011', 'stop', videoName]);
		clearInterval(interval);
	}
	
	/*if(newState == ended){
		clearInterval(interval);
	}*/
	
	if(newState == buffering) {
		clearInterval(interval); 
	}
}

function updatePlayerInfo() {
    var currentTime = ytplayer.getCurrentTime();
	var duration = ytplayer.getDuration();
	var videoPercentage = Math.round((currentTime / duration) * 100);
	
	switch(videoPercentage) {
		case 25:
			//console.log("25 fired " + videoName);
			_gaq.push(['_trackEvent', 'tpe_gnr_2011', '25', videoName]);
		break;
		
		case 50: 
			//console.log("50 fired " + videoName);
			_gaq.push(['_trackEvent', 'tpe_gnr_2011', '50', videoName]);
		break;
		
		case 75:
			//console.log("75 fired " + videoName);
		    _gaq.push(['_trackEvent', 'tpe_gnr_2011', '75', videoName]);
		break;
		
		case 100: 
			//console.log("100 fired " + videoName);
		    _gaq.push(['_trackEvent', 'tpe_gnr_2011', '100', videoName]);
		
			clearInterval(interval);
		break;
	}
}

function loadPlayer() {	
    videoID = "EfwQFrchc5w";
 
    //Initial Video
    videoName = "one million lights";

    var params = {
        allowScriptAccess: "always",
        wmode: "opaque",
		bgcolor: "#000"
    };
    var atts = {
        id: "ytPlayer"
    };
    swfobject.embedSWF("http://www.youtube.com/v/" + videoID + "?theme=light&color=red?version=3&enablejsapi=1&playerapiid=player1", "ytVideo", "455", "371", "9", null, null, params, atts);
}

function _run() {
    loadPlayer();
}

google.setOnLoadCallback(_run);
