/**
* Copyright (c) 2009 Glanceguide, Inc.  All rights reserved. 
* http://www.glanceguide.com
* Decompiling, reverse engineering, copying or unauthorized redistribution is prohibited.    
*
* Prebuilt Event Handler for Silverlight. Supports GG analytics data..  
*
**/

function bindEvents(plugIn, userContext, sender) {
	var media = sender.findName(userContext.mediaName);
	var ggSL1 = new ggSL(userContext);
	var arg;
	media.AddEventListener("MediaOpened", function() {ggSL1.onMediaOpened(sender, arg)});
	if(plugIn && plugIn.content) {
		plugIn.content.onfullscreenchange = function() {ggSL1.OnFullScreenChange(sender, arg)};
	}
}

function ggSL(bindObj) {
	this.mediaName = bindObj.mediaName;
	this.gg = bindObj.gg;
	this.useSWF = bindObj.useSWF;
	this.media;
	this.ie = navigator.userAgent.indexOf("MSIE") != -1;
	this.cur_movie;
	this.cur_position;
	this.cur_volume;
	this.cur_mute;
	this.timer;
	this.playStateTimer;
	this.queryInterval = 2000;
	this.duration;
	this.fullScreen;
	this.cur_movie_url; 
	this.content = "content";
	this.playerStr = "";
	this.metaData={};
	this.prevState = 0;
	this.movieStatus = {loaded:false, played:false, stopped:true};
	this.uniqueID = '';
}

ggSL.prototype.onCurrentStateChanged = function(sender, args) {
	var media = sender.findName(this.mediaName);
	if(media && media.CurrentState != "Buffering" && this.prevState != 'Closed' && this.prevState != media.CurrentState && this.movieStatus.loaded) {
		var cur_pos = parseFloat(media.Position.Seconds.toFixed(1));
		var send_pos;
		if(parseFloat(cur_pos) >= parseFloat(this.cur_position) && parseFloat(cur_pos) <= parseFloat(this.duration)) {
			send_pos = cur_pos;
		} else if(parseFloat(cur_pos) > parseFloat(this.duration) && parseFloat(this.duration) > 0) {
			send_pos = this.duration;
		} else {
			send_pos = this.cur_position;
		}
		switch (media.CurrentState) {
			case 'Stopped':
				if(!this.movieStatus.stopped) {
					this.movieStatus.stopped = true;
					this.gg.ggPM(7,send_pos);
					if(this.timer) {
						clearInterval(this.timer);
					}
					if(this.playStateTimer) {
						clearInterval(this.playStateTimer);
					}
				}
			break;
			case 'Paused':
				if(this.prevState == "Playing") {
					this.gg.ggPM(6,send_pos);
					if(this.timer) {
						clearInterval(this.timer);
					}
					if(this.playStateTimer) {
						clearInterval(this.playStateTimer);
					}
				}
			break;
			case 'Playing':
				if(this.movieStatus.loaded && !this.movieStatus.played) {
					var attributes = media.Attributes
					this.cur_movie = attributes.getItemByName("Title") ? attributes.getItemByName("Title").value : media.Source;
					this.duration = parseFloat(media.NaturalDuration.Seconds.toFixed(1));
					this.cur_movie_url = attributes.getItemByName("uurl") ? attributes.getItemByName("uurl").value : media.Source;
					this.cur_mute;
					this.cur_volume;
					this.uniqueID = attributes.getItemByName("uniqueID") ? attributes.getItemByName("uniqueID").value : '';
					  
					var type = "content";
					if(attributes.getItemByName("contentType") && (attributes.getItemByName("contentType").value == "ad")) { 
						type = "ad";
					}
					this.type = type;
					var l_metaData = "";
					if(attributes.getItemByName("category")) {
						l_metaData += '<category>'+attributes.getItemByName("category").value+'</category>';
					}
					if(attributes.getItemByName("subcategory")) {
						l_metaData += '<subcategory>'+attributes.getItemByName("subcategory").value+'</subcategory>';
					}
					this.prevState = 0;				
					this.gg.ggPM(3, this.cur_movie_url, type, "<title>"+this.cur_movie+"</title><uurl>"+this.cur_movie_url+"</uurl><length>"+String(this.duration)+"</length>"+l_metaData);
					this.movieStatus.played = true;
				}					
				this.movieStatus.stopped = false;
				this.gg.ggPM(5,send_pos);
				if(this.timer) {
					clearInterval(this.timer);
				}
				if(this.playStateTimer) {
					clearInterval(this.playStateTimer);
				}
				var that = this;
				this.timer = setInterval(function() {that.updatePos(that.timer)}, 250);
				this.playStateTimer = setInterval(function() {that.updatePos(that.playStateTimer)}, that.queryInterval);
			break;
			case 'Closed':
				if(!this.movieStatus.stopped) {
					this.movieStatus.stopped = true;
					this.gg.ggPM(7,send_pos);
		
					if(this.timer) {
						clearInterval(this.timer);
					}
					if(this.playStateTimer) {
						clearInterval(this.playStateTimer);
					}
				}
			break;
		}
		this.prevState = media.CurrentState;
	}
}

ggSL.prototype.onMediaOpened = function(sender, args) {
	var media = sender.findName(this.mediaName); 
	if(this.media) { this.onMediaUnload(); };
	this.media = media;
	var that = this;
	media.AddEventListener("CurrentStateChanged", function() {that.onCurrentStateChanged(sender, args)});
	media.AddEventListener("MediaEnded", function() {that.onMediaEnded(sender, args)});
	this.cur_position = parseFloat(media.Position.Seconds.toFixed(1));
	this.prevState = "Loading";
	this.movieStatus.loaded = true;
	this.movieStatus.played = false;
}

ggSL.prototype.onMediaEnded = function(sender, args) {
	var media = sender.findName(this.mediaName);
	if(!this.movieStatus.stopped) {
		var cur_pos = parseFloat(media.Position.Seconds.toFixed(1));
		var send_pos = 0;
		if(parseFloat(cur_pos) >= parseFloat(this.cur_position) && parseFloat(cur_pos) <= parseFloat(this.duration)) {
			send_pos = cur_pos;
		} else if(parseFloat(cur_pos) > parseFloat(this.duration) && parseFloat(this.duration) > 0) {
			send_pos = this.duration;
		} else {
			send_pos = this.cur_position;
		}
		this.movieStatus.stopped = true;
		this.gg.ggPM(7,send_pos);
		if(this.timer) {
			clearInterval(this.timer);
		}
		if(this.playStateTimer) {
			clearInterval(this.playStateTimer);
		}
	} 
}

ggSL.prototype.onMediaUnload = function() {
	if(this.timer) {
		clearInterval(this.timer);
	}
	if(this.playStateTimer) {
		clearInterval(this.playStateTimer);
	}
	var media = this.media;
	var cur_pos = parseFloat(media.Position.Seconds.toFixed(1));
	var send_pos;
	if(parseFloat(cur_pos) >= parseFloat(this.cur_position) && parseFloat(cur_pos) <= parseFloat(this.duration)) {
		send_pos = cur_pos;
	} else if(parseFloat(cur_pos) > parseFloat(this.duration) && parseFloat(this.duration) > 0) {
		send_pos = this.duration;
	} else {
		send_pos = this.cur_position;
	}
	if(!this.movieStatus.stopped) {
		this.gg.ggPM(7,send_pos);
		this.movieStatus.stopped = true;
	}
	if(this.movieStatus.loaded && this.movieStatus.played) {
		this.gg.ggPM(4, send_pos, this.content);
		this.movieStatus.loaded = false;
	}
}

ggSL.prototype.updatePos = function(caller) {
	this.cur_position = parseFloat(this.media.Position.Seconds.toFixed(1)) > parseFloat(this.cur_position) ? parseFloat(this.media.Position.Seconds.toFixed(1)) : parseFloat(this.cur_position);
	if(caller == this.timer) {
		if(this.cur_mute != this.media.IsMuted) {
			this.cur_mute = this.media.IsMuted;
			var curMute = this.cur_mute ? 1:0;
			this.gg.ggPM(9, curMute);
		}
		if(this.cur_volume != this.media.Volume) {
			this.cur_volume = this.media.Volume;
			this.gg.ggPM(11, String(parseInt(this.cur_volume*100)));
		}
	} else {
		this.gg.ggPM(49, this.cur_position);
	}
}

ggSL.prototype.OnFullScreenChange = function(sender, args) {
		this.fullScreen = sender.getHost().content.fullScreen;
		this.gg.ggPM(10, String(this.fullScreen));
}
