/* Generic omniture event registration function */

var clipCategory = "Video";
var previousClip = "n/a";
var currentClipPlaying = "n/a";
var currentClipSecondsPlayed = "0";
var currentClipDuration = "0";
var currentTrigger = "none";
var objInc=0;
var isPlaying = 0;
var userActions = false;
var playlistEnd = false;
var omniEvent = true;
// vistor ID
function getHexString() {
	var hD="0123456789ABCDEF";
	r = Math.floor(16777216*Math.random());
	var h = hD.substr(r&15,1);
	while(r>15) {r>>=4;h=hD.substr(r&15,1)+h;}
	while (h.toString().length<6) {h="0"+h;}
	return h;
}

function doOmnitureCapture(clip) {
	if(clip) {
		createClipInfoObject(clip);
	}
}
//clip Object to store current clip data
function newClipInfo(name,duration,elapsedTime) {
	this.name = name;
	this.duration = duration;
	this.elapsedTime = elapsedTime;
}
var clipsContainer = new Object();
clipsContainer[0] = new newClipInfo("n/a",0,0);

function omniStates(s) {
	//document.getElementById("debug").innerHTML+="<br>Checking States: "+ s;
	
	isPlaying=( ( s==8 && isPlaying==0 ) || ( s==9 && isPlaying==1 ) || s==10)?isPlaying+1:0;
	if(isPlaying==3) {
		//Finished
		//set playlist end 
		playlistEnd = true;
		//document.getElementById("debug").innerHTML+="<br>Playlist End";
		doOmniEvent("Complete",clipsContainer[objInc].name,clipsContainer[objInc].elapsedTime,clipsContainer[objInc].duration )
	}
	if(s==1) currentTrigger = "stop",userActions=true;
	if(s==2) currentTrigger = "pause"; 
	if(s==3 && (currentTrigger=="stop")) currentTrigger = "start",userActions=true; else userActions=false;
	if(s==6) currentTrigger = "none";
	if( s==1 || s==3 ) {
	 doOmnitureCapture(Player.currentMedia);
	}
}
// creates a clip Object to track.
function createClipInfoObject(clip) {
	  	
	d = Math.round(clip.duration);
	previousClip = currentClipPlaying;
	currentClipsDuration = clipsContainer[objInc].duration;
	currentClip = ((d<10 && d!=0)?clip.name+"-sting":((clip.name=="Advert")?"Adverts":clip.name))
	if(d!=currentClipsDuration || currentClip!=clipsContainer[objInc].name) {
		//current clip object modified
		objInc+=1;
		clipsContainer[objInc] = new newClipInfo(currentClip,d)
		if(clipsContainer[objInc].name!="n/a") {
			if(objInc!=1 && !userActions) {
		 	// document.getElementById("debug").innerHTML+="<br>End/Change: "+clipsContainer[objInc-1].name + " :" + clipsContainer[objInc-1].elapsedTime; 
			 doOmniEvent("Stopped",clipsContainer[objInc-1].name,clipsContainer[objInc-1].elapsedTime,clipsContainer[objInc-1].duration)
			 userActions=false;	
			}
			
		}
		if(objInc!=0) {
			//document.getElementById("debug").innerHTML+="<br>Started: "+clipsContainer[objInc].name;
			doOmniEvent("Started",clipsContainer[objInc].name)
		}
		currentClipPlaying = objInc;
		
		
		currentTrigger="playing";
		
	} else {
		//on same clip
		if(currentTrigger=="stop") {
		//document.getElementById("debug").innerHTML+="<br>Stopped: "+clipsContainer[objInc].name + " :" + clipsContainer[objInc].elapsedTime;
		doOmniEvent("Stopped",clipsContainer[objInc].name,clipsContainer[objInc].elapsedTime,clipsContainer[objInc].duration)
		userActions=true;
		  //currentTrigger="start";
	
		}
		if(currentTrigger=="start" && !omniEvent) {
			//restarted same clip
			//document.getElementById("debug").innerHTML+="<br>Restart: "+clipsContainer[objInc].name;
			doOmniEvent("Started",clipsContainer[objInc].name)
			currentTrigger="restart";
		}
	}
	
}

function doOmniEvent (eventCodes,clipName,clipWatched,clipDuration) {
var s=s_gi("channel4dotcom");
omniParams = "";
omniVideoEvent = "";
if(eventCodes=="Error") {
	s.eVar10 = "";
	s.prop17 = "";
	s.events = "event10";
	omniVideoEvent = "Player Closed"
}
if(eventCodes=="Started") {
	s.prop17 = clipName.replace(/&\S+;|;|,/g, "");
	s.eVar10 = "1";
	s.events = "event4";
	if(clipName.indexOf("Adverts")==-1 && clipName.indexOf("-sting")==-1) {
		s.events = "event4,event11";
	}
	omniVideoEvent = "Clip Start"
	omniEvent=true;
}
if(eventCodes=="Stopped" || eventCodes=="Complete") {
	s.eVar10 = "";
	s.prop17 = "";
	omniEvent=false;
	wpercent = getClipPercentage(clipDuration,clipWatched);
	if(wpercent>95) {
		s.events = "event2,event5,event6";
		omniVideoEvent = "Clip Complete"
	} else {
		s.events = "event3,event5,event6";
		omniVideoEvent = "Clip Stopped"
	}
	
	if (wpercent != undefined) {
		omniParams += ";;;event5=" + wpercent + "|event6=" + clipWatched; 
	} 
	
}
if(eventCodes!="Error"){
clipID="";
for(i=0;i<playlistClips.length;i++) {
	if(playlistClips[i].indexOf(clipName)!=-1) {
		clipID = playlistClips[i].split("|")[1];
	} else {
		if(playlistClips.length==1) {
			clipID = playlistClips[0].split("|")[1];
		}
	}
}
if(clipName.indexOf("-sting")!=-1) {
	s.products =  clipCategory + ":sting;" + clipName.replace(/&\S+;|;|,/g, "") + omniParams;
} else if(clipName.indexOf("Adverts")!=-1) {
	s.products =  clipCategory + ":Advert;" + clipName.replace(/&\S+;|;|,/g, "") + omniParams;
} else {
	s.products =  clipCategory + ";" + clipID + omniParams;
}
}
s.linkTrackVars="products,events,prop17,eVar10";
s.linkTrackEvents="event2,event3,event4,event5,event6,event10,event11";
//s.events='event2,event5,event6';
//s.products='Video; Audi TT Review;;;event5=97|event6=123';

debugHTML = "<br><br>pagename: "+s.pageName +
		"<br>hier1: "+s.hier1 +
		"<br>prop17: "+s.prop17 +
		"<br>products: "+s.products +
		"<br>events: "+s.events;
		
//document.getElementById("debug").innerHTML+=debugHTML;

s.tl(this,'o','Video Event: '+omniVideoEvent);


}
//returns percentage of clip viewed
function getClipPercentage(x,p) {
	try {
		percentComplete = (x != undefined && x > 0) ? Math.round(100 * p / x) : 0;
		return percentComplete;
	} catch(e) {
		return 0;
	} 
}	

function updateClock(theTime) {
	viewClock = theTime;
}
/* Window close handler */
function closeHandler() {
	if (Player.playState>=2 && Player.playState<=7 && !playlistEnd) { // Only if current clip hasn't already been stopped or ended
		 doOmniEvent("Stopped",clipsContainer[objInc].name,clipsContainer[objInc].elapsedTime,clipsContainer[objInc].duration)
	}
	doOmniEvent("Error");
	omnitureEventsOn = false;
}

//Event.observe(window, 'unload', closeHandler); 
