/*********************************
	JB 2009
	Nielsen Image Loader for BrightCove
	requires JQuery 1.3 or above.
**********************************/

function removespecialchars(str){
	str =  str.replace(/[^a-zA-Z 0-9]+/g,'');
	return str;
}

var bcrtoken = "qlFD0r-mVHaUADkVtZB1uMJN1fnDK39VC-EUpeaZYn0.";
var bcwtoken = "qlFD0r-mVHbL9x9ah0XOxMsxxLqiDm1u5NRyuJsTAAM.";

var bcplayer;
var bccontent;
var bcexperience;
var bcvideo;  

function onTemplateLoaded(player) {
	bcplayer = brightcove.getExperience(player);
	bcvideo = bcplayer.getModule(APIModules.VIDEO_PLAYER);
	bcexperience = bcplayer.getModule(APIModules.EXPERIENCE);		
	bccontent = bcplayer.getModule(APIModules.CONTENT);
	bcexperience.addEventListener(BCExperienceEvent.TEMPLATE_READY, onTemplateReady);		
	bccontent.addEventListener(BCContentEvent.VIDEO_LOAD, onVideoLoad);
}

function onTemplateReady(event) {
	bcvideo.addEventListener(BCMediaEvent.BEGIN, onMediaBegin);
}	

function playTitleFromList(id) {
	bccontent.getVideoAsynch(id);
}

function onVideoLoad(event) {
	bcvideo.loadVideo(event.video.id);
}

if (typeof $jq == "undefined") {
	var $jq = jQuery.noConflict();
}

$jq(document).ready(function(){
	$jq(".bcpause").click(function () { 
		bcvideo.pause(true);
	});
	$jq.fn.image = function(src, f){ 
		return this.each(function(){ 
			var i = new Image(); 
			i.src = src; 
			i.onload = f; 
			this.appendChild(i);
		}); 
	}
	$jq.fn.create = function(src, f){ 
		if (arguments.length == 0) return [];
		var args = arguments[0] || {}, elem = null, elements = null;
		var siblings = null;
	
		// In case someone passes in a null object,
		// assume that they want an empty string.
		if (args == null) args = "";
		if (args.constructor == String) {
			if (arguments.length > 1) {
				var attributes = arguments[1];
					if (attributes.constructor == String) {
								elem = document.createTextNode(args);
								elements = [];
								elements.push(elem);
								siblings =
								jQuery.create.apply(null, Array.prototype.slice.call(arguments, 1));
								elements = elements.concat(siblings);
								return elements;
	
						} else {
								elem = document.createElement(args);
	
								// Set element attributes.
								var attributes = arguments[1];
								for (var attr in attributes)
								jQuery(elem).attr(attr, attributes[attr]);
	
								// Add children of this element.
								var children = arguments[2];
								children = $jq.fn.create.apply(null, children);
								jQuery(elem).append(children);
	
								// If there are more siblings, render those too.
								if (arguments.length > 3) {
										siblings =
										jQuery.create.apply(null, Array.prototype.slice.call(arguments, 3));
										return [elem].concat(siblings);
								}
								return elem;
						}
				} else return document.createTextNode(args);
		  } else {
				elements = [];
				elements.push(args);
				siblings =
				jQuery.create.apply(null, (Array.prototype.slice.call(arguments, 1)));
				elements = elements.concat(siblings);
				return elements;
		  }

	}

});

function nlsnrandomnumgen() {
	var rnum = Math.random() * 1000000000000000000;
	return rnum;
}

function callnielsen(ci,c6,cg,tl) {
	//this function should be called after every vid.
	
	var nlsn={
		ci:		ci.toString(), 
		c6:	c6.toString(),
		cg:	removespecialchars(cg.toString()),					//Program/Section Name
		tl:		tl.toString()													//title of the stream
	};

	var nlsnurl = "http://secure-us.imrworldwide.com/cgi-bin/m?cc=1&cg=" + nlsn.cg  + "&tl=dav0-" + nlsn.tl  + "&ci=" + nlsn.ci  + "&c6=" + nlsn.c6  + "&rnd=" + nlsnrandomnumgen();

	if (!$jq(".nlsn").length) {
		//div.nlsn doesn't exist yet. Add it.
		var o = $jq.fn.create('div', {'class':'nlsn'});
		$jq('body').append($jq(o));
	}
	$jq(".nlsn").image(nlsnurl,function(){});
}	