var $ = YAHOO.util.Dom.get;
var STATUS_TAGS_UNLOAD = 1;
var STATUS_TAGS_INIT_LOAD = 2;
var STATUS_TAGS_FULL_LOAD = 4;

function a() {};

function applet() {
	this.initLoad = true;
	this.list = null;
	
	this.lastSearchObj = new Object();
	this.lastSearchObj.type = "";
	this.lastSearchObj.id = "";
	this.lastSearchObj.ids = "";
	this.lastSearchObj.name = "";
	
	this.videoDefaultImage = ROOTDIR+"img/icon_tube.gif";
	
	this.barlength = 137;
	this.bardelta = (this.barlength / 10 / 2);	
	this.sliderLocked = false;		
	this.listNullVar = "";
	
	// IDs, FIELDs, DIVs INIT
	this.ID_TableFlashVideo = "TABLE_FLASHVIDEO_";
	this.ID_TAG_IMG = "";
	this.ID_TAG_POS = "TAGID_POS";	
	this.ID_TAG_ARROW = "TAGID_ARROW";
	this.ID_RATING = "ID_RATING_";
	this.ID_FOCUSVIDEO = "FOCUSVIDEO_";
	this.ID_FOCUSVIDEO_IMG = "FOCUSVIDEO_IMG_";
	
	this.DIVID_RECENT_TAGS = $("VIDEO_RECENT_TAGS");	
	this.FLDID_VIDEO_TAGIT = $("VIDEO_TAGIT");
	
	// FLASH PLAYER INIT
	this.playerState = FP_STATE_NOTLOADED;
	this.tagsState = STATUS_TAGS_UNLOAD;
	this.playlistState = PLAYLIST_UNLOAD;	
	
	var params = new Object();
	params.swfObj = new flashPlayer("fpvalve", ROOTDIR+"swf/valve_player.swf");
	params.callback = this;
	params.callback_function = "playFlashVideo";
	this.playlistObj = new playlist(params);
		
	
	 // TOOLTIP INIT
	this.tooltipObj = new YAHOO.widget.Panel("TOOLTIP_TAGS", { 
		zIndex: 998,
	    close:false,  
	    visible:false,
		draggable:false} );
		
	this.tooltipArrowObj = new YAHOO.widget.Panel("TOOLTIP_ARROW", {
		zIndex:999,
	    close:false,  
	    visible:false,
		draggable:false} );
		
	 // TOOLTIP MAIN INIT
	this.tooltipMainObj = new YAHOO.widget.Panel("TOOLTIP_TAGS_MAIN", { 
		zIndex: 998,
	    close:false,  
	    visible:false,
		draggable:false} );
		
	this.tooltipMainArrowObj = new YAHOO.widget.Panel("TOOLTIP_ARROW_MAIN", {
		zIndex:999,
	    close:false,  
	    visible:false,
		draggable:false} );		
};

applet.prototype.getLastSearchObj = function() {
	return this.lastSearchObj;
};

applet.prototype.getList = function(pobj, playvideo) {
	this.playvideo = playvideo;
	var callback = {success: this.getListSuccess, failure: this.getListFailure, scope: this};
	var sUrl = ROOTDIR+"services/mediaunbound/videos_json.jhtml";
	
	
	if( _startUp.initCheck == false) //start up initial artist selections
	{		
		_startUp.getArtists();
		if(_startUp.artists != null && _startUp.artists != "")
		{			
			pobj.ids = _startUp.artists;
			pobj.type = "artist_seed";
			
			_startUp.artists = null;
		}
	}
	
	var userObj = $USER.getUserObj();
	var type = ((pobj) && (pobj.type)) ? pobj.type : "";
	var ids = ((pobj) && (pobj.ids)) ? pobj.ids.toString().replace(/ /gi, "") : "";	
	
	switch(type) {
		case "director_videos":
			var name = ((pobj) && (pobj.name)) ? pobj.name.toString().replace(/ /gi, "") : this.lastSearchObj.name.toString().replace(/ /gi, "");	
			break;
		default:
			var id = ((pobj) && (pobj.id)) ? pobj.id.toString().replace(/ /gi, "") : this.lastSearchObj.id.toString().replace(/ /gi, "");		
			break;
	};
	
	//console.log("applet MU params user id: "+userObj.id);
	//console.log("applet MU params user id2: "+userObj.sessionid);
	
	this.lastSearchObj.type = type;
	this.lastSearchObj.id = id;
	this.lastSearchObj.ids = ids;
	this.lastSearchObj.name = name;
		
	var params = ""
		+ "user="+((userObj.id) ? userObj.id : userObj.sessionid)
		+ ((pobj.tag) ? "&tag="+pobj.tag : "")
		+ ((id) ? "&id="+id : "")
		+ ((ids) ? "&ids="+ids : "")
		+ ((name) ? "&name="+name : "")
		+ ((type) ? "&type="+type : "");		
		
	//console.log("MU params: "+params);		
	
	$("DIV_VIDEOS_LIST").innerHTML = "<center><b>Loading videos playlist...</b></center>";
	YAHOO.util.Connect.asyncRequest("POST", sUrl, callback, params); 
};

applet.prototype.getListSuccess = function (o) {
	try {	
		var obj = eval(o.responseText);		
		obj = obj[0];
		if (obj.status == true) {
			var playlist = obj.list;
			if (this.playlistObj.getCurrentVideo()) playlist = this.populateNewList(playlist);
			this.setPlaylist(playlist);
			var html = "";
			var length = playlist.length;
			for (var x=0; x<length; x++) {
				html += this.formatVideo(playlist[x], x, length-1);				
			};
			
			html += "<table style='width:90%'><tr><td align='right'><a href='javascript:a();' onclick='$I.getMore()' class='link11'><b>Click to get more >></b></a></td></tr></table>";
			
			$("DIV_VIDEOS_LIST").innerHTML = html;
			this.playlistObj.playlistManager("setfirst");
			this.tagsState = STATUS_TAGS_INIT_LOAD;
			this.getTagsForVideos(playlist[0].id);
			var currentVideo = this.playlistObj.getCurrentVideo();	
			
			if (this.initLoad) {					
				this.initLoad = false;
				this.playlistObj.setPlaylistState(PLAYLIST_LOADED);
				this.delayPlayVideo();
			} else {
				if (currentVideo) {
					this.playlistObj.playlistManager("setfirst");
					this.highlightVideo(currentVideo);
					this.playlistObj.setPlaylistState(PLAYLIST_LOADED);
				} else {
					this.playlistObj.setPlaylistState(PLAYLIST_RELOADED);
				};
			};
		} else throw "F";	
	} catch(e) {		
		// CAPTURE ERROR
		$("DIV_VIDEOS_LIST").innerHTML = "<center><b>There is an error loading your Valve playlist</b></center>";
	};
};

applet.prototype.getListFailure = function (o) {
	$("DIV_VIDEOS_LIST").innerHTML = "<center><b>There is an error loading your Valve playlist</b></center>";
};

applet.prototype.populateNewList = function(playlist) {
	var length = playlist.length;
	var currentVideo = this.playlistObj.getCurrentVideo();	
	if (currentVideo) {
		for (var x=0; x<length; x++) {
			var video = playlist[x];
			if (video.id == currentVideo.id) {
				playlist.splice(x, 1);
				break;
			};
		};
		playlist.unshift(currentVideo);
	}; 
	return playlist;
};

applet.prototype.delayPlayVideo = function() {
	if (this.getPlayerState() == FP_STATE_LOADED) this.playFlashVideo(); // LOAD EVERY SECOND TO CHECK IF PLAYER IS INITIALIZE
	else self.setTimeout("$I.delayPlayVideo()", 1000);
};

applet.prototype.formatVideo = function(item, index, length) {
	var html = "";
	
	if (item.id != "error") {
		var _image = ((item.flvimage == this.listNullVar) ? ((item.image == this.listNullVar) ? this.videoDefaultImage : item.image) : item.flvimage);
		var _director = (item.director) ? item.director.toString().replace(/&amp;/gi, "") : item.director;
		var _yearDate = "";
		if (item.premierDate != this.listNullVar) {
			//var data = item.premierDate.split("-");
			//var _yearDate = "("+data[0]+")";
		} 
		
		html = ""
			+ "<a name='"+this.ID_FOCUSVIDEO+item.id+"'></a>"
			+ "<table cellpadding='1' cellspacing='1' border='0' style='width:260px;' id='"+this.ID_TableFlashVideo+item.id+"' onmouseover='$I.onVideoOver("+index+")' onmouseout='$I.onVideoOut("+index+")'>"
			+ ((index < length) ? "<tr><td colspan='2'><div style='width:100%;border-top:dotted 1px #CFCFCF;height:1px;font-size:1px;'>&nbsp;</div></td></tr>" : "")
			+ "<tr valign='top'>"
				+ "<td style='width:70px' onclick='$I.playFlashVideo("+index+", false, true)' onmouseover='$I.showPlayIcon("+index+")'>"
					+ "<div style='position:relative;cursor:pointer;'>"
						+ "<div id='"+this.ID_FOCUSVIDEO_IMG+item.id+"' style='position:absolute;display:none;cursor:pointer;'><img src='"+ROOTDIR+"img/1.gif' style='height:47px;width:68px;'></div>"
						+ "<img src='"+_image+"' style='z-index:10;width:64px;border: 2px black solid; margin: 2px;' onError=\"this.src='" + this.videoDefaultImage + "'\">"
					+ "</div>"
				+ "</td>"
				+ "<td style='width:190px;position:relative;'>"				
					+ "<table cellpadding='1' cellspacing='1' border='0' style='width:100%'>"
					+ "<tr valign='top'>"
						+ "<td valign='bottom'><img src='"+ROOTDIR+"img/1.gif' id='"+this.ID_TAG_POS+item.id+"' style='width:1px;'></td>"
						+ "<td>"
							+ "<div style='overflow:none;width:160px;#width:155px;'>"
								+ "<a href='javascript:a();' onclick='$I.playFlashVideo("+index+", false, true)' onmouseover='$I.showPlayIcon("+index+")' class='link' style='text-decoration:none;border-bottom:0px;'>"+this.parseString(item.title)+" "
								+ _yearDate +"</a> "
								+ "<span style='position:relative;z-Index:201;'>"					
									+ "<img src='"+ROOTDIR+"img/icon_tag_off.gif' id='"+this.ID_TAG_ARROW+item.id+"' style='cursor:pointer;border:0px;' onmouseover='event.cancelBubble = true;$I.videoTagOver(this, "+index+")'/>"
								+ "</span>"
								+ "<div style='margin-bottom:3px;'>"
									+ "<a href='javascript:a();' class='linkblueB11' onclick='$I.getList({type: \"artist_videos\", id:\""+item.artistId+"\"})'>"+this.parseString(item.artist)+"</a>"
								+ "</div>"
								+ "<div style='margin-bottom:3px;'>"
									+ "<b>Director: </b>"+((_director != this.listNullVar) ? "<a href='javascript:a();' class='link11' onclick='$I.getList({type: \"director_videos\", name:\""+_director+"\"})'>"+this.parseString(item.director)+"</a>" : "N/A")
								+ "</div>"
							+ "</div> "
						+ "</td>"
						+ "<td><div onclick='$I.videoBoxClose("+index+")' style='cursor:pointer;'><img src='"+ROOTDIR+"img/icon_close.gif'>&nbsp;&nbsp;</div></td>"
					+ "</tr>"
					+ "</table>"
				+ "</td>" 
			+ "</tr>" 
			+ "</table>";	
	};

	return html;
};

applet.prototype.parseString = function(str) {
	str = str.toString().replace(/&amp;/gi, "&");
	str = str.toString().replace(/&quot;/gi, "&#34;");
	str = str.toString().replace(/'/gi, "&#39;");
	return str;
};

applet.prototype.refresh = function() {
	this.getList(this.lastSearchObj);
};

applet.prototype.cancelBubble = function(e) {
	e = e || window.event;
	e.cancelBubble = true;
};

applet.prototype.getMore = function() {
	switch(this.lastSearchObj.type) {
		case "artist":
			this.lastSearchObj.type = "";
			this.lastSearchObj.id = "";
			break;
	};
	this.getList(this.lastSearchObj);
};

applet.prototype.getSimilarLinks = function(type, value) {
	var obj = new Object();
	obj.type = type;
	var currentVideo = this.playlistObj.getCurrentVideo();	
	switch(type) {
		case "video": obj.id = ((currentVideo) ? currentVideo.id : ""); break;
		case "artist": obj.id = ((currentVideo) ? currentVideo.artistId : ""); break;
		case "director": obj.id = ((currentVideo) ? currentVideo.director : ""); break;
		case "tagged_videos": obj.id = null; obj.tag = value; break;
	};
	this.getList(obj);
};

applet.prototype.loadSliders = function() {	
	this.sliderFreshneshObj = YAHOO.widget.Slider.getHorizSlider("sliderFreshnessBG", "sliderFreshnessThumb", 0, this.barlength); 
	this.sliderFreshneshObj.onSlideEnd = function() {
		if ($I.getSliderLock()) {
			var obj = $I.getLastSearchObj();
			obj.pop = $I.getSliderValue("popular");
			obj.cur = $I.getSliderValue("fresh");
			$I.getList(obj);
		};
	};
	
	this.sliderPopularObj = YAHOO.widget.Slider.getHorizSlider("sliderPopularBG", "sliderPopularThumb", 0, this.barlength);
	this.sliderPopularObj.onSlideEnd = function() {
		if ($I.getSliderLock()) {
			var obj = $I.getLastSearchObj();
			obj.pop = $I.getSliderValue("popular");
			obj.cur = $I.getSliderValue("fresh");
			$I.getList(obj);
		};
	};
	
	self.setTimeout("$I.setSliders("+(this.barlength / 2)+")", 500);
	var obj = new Object();
	obj.cur = "0";
	obj.pop = "0";
	this.getList(obj, true);
};

applet.prototype.setSliders = function(value) {
	this.sliderFreshneshObj.setValue(value);
	this.sliderPopularObj.setValue(value);
	self.setTimeout("$I.setSliderLock(true)", 1000);
};

applet.prototype.getSliderValue = function(type) {
	switch(type) {
		case "fresh": var value = this.sliderFreshneshObj.getValue(); break;
		case "popular": var value = this.sliderPopularObj.getValue(); break;		
	}	
	value = Math.ceil(parseInt(value) / this.bardelta) - 10;
	return value;
}

applet.prototype.getSliderLock = function() {
	return this.sliderLocked;
};

applet.prototype.setSliderLock = function(value) {
	this.sliderLocked = value;
};

// PLAYLIST CSS FUNCTIONS
applet.prototype.videoBoxClose = function(index) {
	var video = this.getPlaylistVideo(index);
	video.closed = true;
	this.getVideoObject(video.id).style.display = "none";
	this.processEvent("remove", video);
	var currentVideo = this.playlistObj.getCurrentVideo();	
	if (video.id == currentVideo.id) this.playlistObj.playNextVideo();
};

applet.prototype.onVideoOver = function(index) {
	var video = this.getPlaylistVideo(index);
	this.getVideoObject(video.id).style.backgroundColor = "#f0f0f0";
};

applet.prototype.onVideoOut = function(index) {	
	var playing = (this.getPlaylistVideo(index).id == this.getPlaylistVideo(this.playlistObj.getSelectedCounter()).id) ? true : false;
	this.getVideoObject(this.getPlaylistVideo(index).id).style.backgroundColor = (playing) ? "#fffae5" : "#ffffff";
	var video = this.getPlaylistVideo(index);
	this.hidePlayIcon(index);
};

applet.prototype.showPlayIcon = function(index) {	
	var video = this.getPlaylistVideo(index);
	var obj = $(this.ID_FOCUSVIDEO_IMG+video.id);
	obj.style.display = "";
	obj.className = "playOff";
};

applet.prototype.hidePlayIcon = function(index) {
	var playing = (this.getPlaylistVideo(index).id == this.getPlaylistVideo(this.playlistObj.getSelectedCounter()).id) ? true : false;
	var video = this.getPlaylistVideo(index);
	var obj = $(this.ID_FOCUSVIDEO_IMG+video.id);
	obj.style.display = (playing) ? "" : "none";
	obj.className = (playing) ? "playOn" : "playOff";
};

applet.prototype.videoTagOver = function(el, index) {
	this.lastIndex = index;
	var video = this.getPlaylistVideo(index);
	var tags = (video.detailObj) ? this.getTopTags(video.detailObj.tags) : "None.";	
	var _rows = Math.ceil((tags.length+14) / 35)-1;	
	this.tooltipObj.setBody("<b>Recent Tags: </b>"+tags);	
	var _y = YAHOO.util.Dom.getY(this.ID_TAG_ARROW+video.id)-10;		 		
	var _x_arrow = YAHOO.util.Dom.getX(this.ID_TAG_ARROW+video.id);
	var _x_box = YAHOO.util.Dom.getX(this.ID_TAG_POS+video.id);
	
	$(this.tooltipObj.id).style.display = "";	
	this.tooltipObj.cfg.setProperty("xy", [_x_box, _y-22-(_rows*14)]);	
	this.tooltipObj.render();
	this.tooltipObj.show();
	
	$(this.tooltipArrowObj.id).style.display = "";
	this.tooltipArrowObj.cfg.setProperty("xy", [_x_arrow, _y]);	
	this.tooltipArrowObj.render();
	this.tooltipArrowObj.show();

	el.src = ROOTDIR+"img/icon_tag_on.gif";
};

applet.prototype.videoTagOut = function() {
	if (this.lastIndex || (this.lastIndex == 0)) {		
		var video = this.getPlaylistVideo(this.lastIndex);		
		if (video && $(this.ID_TAG_ARROW+video.id)) $(this.ID_TAG_ARROW+video.id).src = ROOTDIR+"img/icon_tag_off.gif";
	};
	this.tooltipObj.hide();
	this.tooltipArrowObj.hide();
};

applet.prototype.videoTagMainOver = function(el) {
	var video = this.playlistObj.getCurrentVideo();	
	if (video) {
		var tags = (video.detailObj) ? this.getTopTags(video.detailObj.tags) : "None.";
		var _rows = Math.ceil((tags.length+14) / 48)-1;	
		this.tooltipMainObj.setBody("<b>Recent Tags: </b>"+tags);
		
		var _y = YAHOO.util.Dom.getY(el.id)-14;		 		
		var _x = YAHOO.util.Dom.getX(el.id);
		
		$(this.tooltipMainObj.id).style.display = "";	
		this.tooltipMainObj.cfg.setProperty("xy", [_x-100, _y-22-(_rows*14)]);	
		this.tooltipMainObj.render();
		this.tooltipMainObj.show();
		
		$(this.tooltipMainArrowObj.id).style.display = "";
		this.tooltipMainArrowObj.cfg.setProperty("xy", [_x, _y]);	
		this.tooltipMainArrowObj.render();
		this.tooltipMainArrowObj.show();
	};
	el.src = ROOTDIR+"img/icon_tag_large_on.gif";
};

applet.prototype.videoTagMainOut = function(el) {
	el.src = ROOTDIR+"img/icon_tag_large_off.gif";
	this.tooltipMainObj.hide();
	this.tooltipMainArrowObj.hide();
};

applet.prototype.highlightVideo = function(video, doNotJump) {
	var obj = $(this.ID_FOCUSVIDEO_IMG+video.id);
	if (obj) {
		obj.style.display = "";
		obj.className = "playOn";
	};

	this.getVideoObject(video.id).style.backgroundColor = "#fffae5";
	this.loadVideoDetail(video);
	if (this.playlistObj.getSelectedCounter() > 0) {
		if (!doNotJump) window.location = "#"+this.ID_FOCUSVIDEO+video.id;	// SKIP TO PLAYLIST VIDEO
	};
};

applet.prototype.loadVideoDetail = function(video) {
	var _yearDate = "";
	if (video.premierDate != this.listNullVar) {
		//var data = video.premierDate.split("-");
		//var _yearDate = "("+data[0]+")";
	}
	
	var _director = (video.director && (video.director != this.listNullVar)) ? "<a href='javascript:a();' class='link11' onclick='$I.getList({type: \"director_videos\", name:\""+video.director.toString().replace(/&amp;/gi, "")+"\"})'>"+this.parseString(video.director)+"</a>" : "N/A";
	var html = "<div style='width:200px'>"
		+ "<b>"+this.parseString(video.title)+"</b> "+_yearDate+"<br>"
		+ "<a href='javascript:a();' class='linkblueB11' onclick='$I.getList({type: \"artist_videos\", id:\""+video.artistId+"\"})'>"+this.parseString(video.artist)+"</a><br>"
		+ "<b>Director: </b>"+_director
		+ "</div>";
	$("VIDEO_DETAIL").innerHTML = html;
	
	var image = (video.flvimage != this.listNullVar) ? video.flvimage : ((video.image != this.listNullVar) ? video.image : ROOTDIR+"img/icon_tube_large.gif");
	var html = "<img style='margin: 2px;border:2px black solid;width:86px;height:65px;' src='"+image+"' onError=\"this.src='"+ROOTDIR+"img/icon_tube_large.gif'\"/>";
	$("VIDEO_IMAGE").innerHTML = html;
	
	if (video.detailObj) this.populateTagDetail(video.detailObj); // POPULATE DETAIL INFO
};

applet.prototype.getVideoObject = function(id) {
	return $(this.ID_TableFlashVideo+id);
};

// PLAYLIST FUNCTIONS
applet.prototype.setPlayerState = function(value) {
	this.playlistObj.setPlayerState(value);
};

applet.prototype.getPlayerState = function() {
	return this.playlistObj.getPlayerState();
};

applet.prototype.setPlaylist = function(list) {
	this.playlistObj.setPlaylist(list);
};

applet.prototype.getPlaylist = function() {
	return this.playlistObj.getPlaylist();
};

applet.prototype.getPlaylistVideo = function(index) {
	return this.playlistObj.getPlaylistVideo(index);
};

applet.prototype.playFlashVideo = function(index, skip, doNotJump) {
	try {
		var tmpVideo = this.playlistObj.getCurrentVideo();
		var video = this.playlistObj.playFlashVideo(index, 0, skip);		
		if (video) {	
			this.processEvent("play", video);		
			if (tmpVideo) {	
				if (this.getVideoObject(tmpVideo.id)) {
					this.getVideoObject(tmpVideo.id).style.backgroundColor = "#ffffff";
					$(this.ID_FOCUSVIDEO_IMG+tmpVideo.id).style.display = "none";
				};
				if (skip) this.processEvent("skip");	// LOGGING CURRENT SKIP VIDEO
			};
			this.highlightVideo(video, doNotJump);	
		} else throw "PLAYLIST CANNOT PLAY FLASH VIDEO";
	} catch(e) {
	// CAPTURE ERROR
	
	};
};

applet.prototype.playPreviousVideo = function(skip) {
	this.playlistObj.playPreviousVideo(skip);
};

applet.prototype.playNextVideo = function(skip) {
	this.playlistObj.playNextVideo(skip);
};

applet.prototype.getCurrentVideo = function() {
	var obj = new Object();
	obj.video = this.playlistObj.getCurrentVideo();
	obj.index = this.playlistObj.getSelectedCounter();
	obj.position = 	this.playlistObj.getVideoPosition();
	return obj;
};

applet.prototype.playerOnPause = function(value) {
	this.playlistObj.setVideoPosition(value);
};

// RATING FUNCTIONS
applet.prototype.selectRating = function(rating) {
	this.processEvent(rating);
};

applet.prototype.onmouseoverRating = function(rating) {
	for (var x=0; x<=5; x++) {
		if (x != 0) {
			var obj = $(this.ID_RATING+x);
			if (obj) {
				if (x <= rating) obj.src = ROOTDIR+"img/icon_rating_on.gif";
				else obj.src = ROOTDIR+"img/icon_rating_off.gif";
			};
		};
	};
};

applet.prototype.onmouseoutRating = function() {
	var currentVideo = this.playlistObj.getCurrentVideo();	
	var rating = (currentVideo.detailObj) ? ((currentVideo.detailObj.rating.indexOf("none") > -1) ? 0 : currentVideo.detailObj.rating) : 0;
	for (var x=0; x<=5; x++) {
		if (x != 0) {
			var obj = $(this.ID_RATING+x);
			if (obj) {
				if (x <= rating) obj.src = ROOTDIR+"img/icon_rating_on.gif";
				else obj.src = ROOTDIR+"img/icon_rating_off.gif";
			};
		};
	};
};

applet.prototype.setRating = function(rating) {	
	for (var x=1; x<=5; x++) {
		var obj = $(this.ID_RATING+x);
		if (obj) {
			if (x <= rating) obj.src = ROOTDIR+"img/icon_rating_on.gif";
			else obj.src = ROOTDIR+"img/icon_rating_off.gif";
		};
	};
};

// EVENTS: ("votePlus", "voteMinus", "skip", "replay", "remove")
applet.prototype.processEvent = function(event, video) {
	var callback = {success: this.processEventSuccess, failure: this.processEventFailure, scope: this};
	var sUrl = ROOTDIR+"services/mediaunbound/sendevent.jhtml";
	var userObj = $USER.getUserObj();
	var video = video || this.playlistObj.getCurrentVideo();
	var params = ""
		+ "userId="+((userObj.id) ? userObj.id : userObj.sessionid)
		+ "&id="+video.id
		+ "&event="+event
	YAHOO.util.Connect.asyncRequest("POST", sUrl, callback, params); 
};

applet.prototype.processEventSuccess = function(o) {
	try {
		var obj = eval(o.responseText);
		if (obj[0].status == true) {
			if (obj[0].event.indexOf("none") > -1) {
				var currentVideo = this.playlistObj.getCurrentVideo();	
				currentVideo.detailObj.rating = obj[0].event;
				var rating = (obj[0].event.indexOf("none") > -1) ? 0 : obj[0];
				this.setRating(parseInt(rating));
			};
		};
	} catch(e) {
	// CAUGHT ERRORS
	
	};
};

applet.prototype.processEventFailure = function(o) {

};

// TAGS FUNCTIONS
applet.prototype.tagVideo = function(index, tag) {
	var tagObj = $("VIDEO_TAGIT");
	if (tagObj.value) {
		var callback = {success: this.tagVideoSuccess, failure: this.tagVideoFailure, scope: this};
		var sUrl = ROOTDIR+"services/mediaunbound/tagVideo.jhtml";
		var userObj = $USER.getUserObj();
		var video = video || this.playlistObj.getCurrentVideo();
		var params = ""
			+ "userId="+((userObj.id) ? userObj.id : userObj.sessionid)
			+ "&id="+video.id
			+ "&tag="+tagObj.value.toString().replace(/&/gi, "amp;");
		YAHOO.util.Connect.asyncRequest("POST", sUrl, callback, params); 
	};
	return false;
};

applet.prototype.tagVideoSuccess = function(o) {
	try {
		var obj = eval(o.responseText);
		if (obj[0].status == true) {
			this.FLDID_VIDEO_TAGIT.value = "";
			var currentVideo = this.playlistObj.getCurrentVideo();
			currentVideo.detailObj.tags.push(obj[0].tag);			
			this.populateTagDetail(currentVideo.detailObj);			
		} else throw "F";
	} catch(e) {
	// CAUGHT ERRORS
	};
};

applet.prototype.tagVideoFailure = function(o) {

};

applet.prototype.getTagsForVideos = function(ids) {
	var callback = {success: this.getTagForVideoSuccess, failure: this.getTagForVideoFailure, scope: this};
	var sUrl = ROOTDIR+"services/mediaunbound/getTags.jhtml";
	var userObj = $USER.getUserObj();
	var video = video || this.playlistObj.getCurrentVideo();
	var params = ""
		+ "userId="+((userObj.id) ? userObj.id : userObj.sessionid)
		+ "&ids="+ids;
	YAHOO.util.Connect.asyncRequest("POST", sUrl, callback, params); 
};

applet.prototype.getTagForVideoSuccess = function(o) {
	try {
		this.processTagsForVideos(o.responseText);
		switch (this.tagsState) {
			case STATUS_TAGS_INIT_LOAD:
				this.tagsState = STATUS_TAGS_FULL_LOAD;
				var playlist = this.getPlaylist();								
				this.populateTagDetail(playlist[0].detailObj);
				if (playlist.length > 1) {
					var ids = "";
					for (var x=1; x<playlist.length; x++) {
						ids += (ids) ? "," : "";
						ids += playlist[x].id;
					};					
					this.getTagsForVideos(ids);
				};
				break;
			case STATUS_TAGS_FULL_LOAD: this.tagsState = STATUS_TAGS_UNLOAD; break;
		};
	} catch(e) {
	// CAUGHT ERRORS

	};
};

applet.prototype.getTagForVideoFailure = function(o) {
	
};

applet.prototype.processTagsForVideos = function(xml) {
	// CONVERT CURRENT XML STRING TO PROPER TAGS
	xml = xml.toString().replace(/&gt;/gi, ">");
	xml = xml.toString().replace(/&lt;/gi, "<");
	xml = xml.toString().replace(/&quot;/gi, "\"");
	
	// SETUP CROSS-BROWSER XML PARSER
	if (window.ActiveXObject) {
		var parser=new ActiveXObject("microsoft.xmldom");
		parser.async="false";
		parser.loadXML(xml);
		var xmlData = parser;		
		var type = "ie";
	} else if (document.implementation && document.implementation.createDocument) {
		//var parser=document.implementation.createDocument("","",null);
		//parser.load(xml);	
		var parser=new DOMParser();
		var xmlData = parser.parseFromString(xml,"text/xml");
		var type = "ns";
	};
	
	// SETUP TAGS OBJECT
	var rootNode = xmlData.documentElement;		
	if (rootNode) {
		if (rootNode.nodeName == "video-data") {
			var nodesList = rootNode.childNodes;
			for (var x=0; x<nodesList.length; x++) {
				if (nodesList.item(x).nodeName == "video") {				
					var video = nodesList.item(x).childNodes;
					var obj = new Object();
					for (var y=0; y<video.length; y++) {
						if (video.item(y).nodeName == "tags") {
							obj.tags = new Array();
							var tags = video.item(y).childNodes;
							for (var z=0; z<tags.length; z++) {
								if (tags.item(z).nodeName == "tag") obj.tags.push(this.getXMLNodeValue(type, tags.item(z)));
							};
						} else {
							var value = this.getXMLNodeValue(type, video.item(y));
							switch(video.item(y).nodeName) {
								case "id": obj.id = value; break;
								case "rating": obj.rating = value; break;
							};
						};
					};
					this.attachTagToVideo(obj);	// ATTACH TO PLAYLIST
				};
			};
		} else if (rootNode.nodeName == "error") {
		// CAUGHT ERROR

		} else {
		// CAUGHT UNEXPECTED ERROR
		
		};
	};
};

applet.prototype.attachTagToVideo = function(obj) {
	var list = this.getPlaylist();
	for (var x=0; x<list.length; x++) {
		if (parseInt(list[x].id) == parseInt(obj.id)) {
			list[x].detailObj = obj;
			return 0; // ATTACH SUCCESSFULL
		};
	};
};

applet.prototype.populateTagDetail = function(obj) {
	var rating = (obj.rating.indexOf("none") > -1) ? 0 : obj.rating;
	this.setRating(rating);
	this.DIVID_RECENT_TAGS.innerHTML = (obj.tags.length > 0) ? this.getTopTags(obj.tags, true, 5) : "None.";
	$("IMG_VIDEO_RECENT_TAGS").style.display = (obj.tags.length > 5) ? "" : "none";
};

applet.prototype.getTopTags = function(tags, href, length) {
	var value = "";
	var tag = "";
	var length = (length) ? length : tags.length;
	for (var x=0; x<length; x++) {
		if (tags[x]) {
			value += (value) ? ", " : "";
			tag = tags[x].toString().replace(/'/gi, "&#39;");
			value += (!href) ? tags[x] : "<a href='javascript:a();' class='link11' onclick='$I.getSimilarLinks(\"tagged_videos\", \""+encodeURI(tag)+"\")'>"+tags[x]+"</a>";
		};
	};	
	return (value == "") ? "None." : value;
};

applet.prototype.getXMLNodeValue = function(type, obj) {
	return (type == "ie") ? obj.text : obj.textContent;
};

applet.prototype.addToSite = function() {
	var obj = $("ID_TAGTO");
	var value = obj[obj.selectedIndex].value;
	var url = "";
	switch(value) {
		case "del.icio.us": 
			url = "http://del.icio.us/post/?"
				+ encodeURI("url=http://www.videovalve.tv&title=Video Valve - Your Personalized Music Video Channel"); 
			break;
		case "digg.com": 
			url = "http://digg.com/submit?phase=2"
				+ encodeURI("&url=http://www.videovalve.tv&title=Video Valve&bodytext=Your Personalized Music Video Channel");			
			break;
		case "reddit.com":
			url = "http://reddit.com/submit?"
				+ encodeURI("url=http://www.videovalve.tv&title=Video Valve - Your Personalized Music Video Channel");
			break;
	};
	if (url) window.open(url); 
	obj.selectedIndex = 0;
};

applet.prototype.loadMiniPlayer = function() {
	var options = ""
		+ "toolbar=0"
		+ ",location=0"
		+ ",directories=0"
		+ ",scrollbars=0"
		+ ",resizeable=0"
		+ ",status=0"
		+ ",width=425"
		+ ",height=165";
	this.playlistObj.pauseVideo();
	var miniplayer = window.open("/sites/videovalve/index.jhtml?loc=miniplayer", "miniplayer", options);
};