
/*
	Assorted functions and whatnot that deal with the JW player's operation and display
*/

// make it so that clicking on a playlist item plays that video
$(document).ready(function() {
	$("div.video_playlist_item").click(function() {
		jwplayer().playlistItem($("div.video_playlist_item").index(this));
	});
});


// triggered whenever jwplayer starts a new playlist item
function jw_onPlaylistItem(n) {
	// only do some stuff if we're actually displaying the playlist
	if($("#video_playlist").length) {
		// remove all playlist highlighting
		$("div.video_playlist_item").removeClass("highlighted");
		
		// selector for current playlist item
		selector = "div.video_playlist_item:eq(" + n + ")";
		
		// highlight the new one
		$(selector).addClass("highlighted");
		
		// get the playlist's current positioning
		item_top = $(selector).position().top;
		item_height = $(selector).outerHeight(true);
		current_scroll = $("#video_playlist").scrollTop();
		playlist_height = $("#video_playlist").height();
		
		// if the playlist needs to go up
		if(item_top < 0) {
			new_scroll = current_scroll + item_top;
			$("#video_playlist").scrollTop(new_scroll);
		}
		
		// if it needs to scroll down
		else if(item_top > (playlist_height - item_height)) {
			new_scroll = (current_scroll + item_top) - (playlist_height - item_height);
			$("#video_playlist").scrollTop(new_scroll);
		}
	}
	
	// remove any existing video overlay
	$("#video_overlay").remove();
	
	// see if there's any overlay code
	if(overlay_text = jwplayer().getPlaylistItem(n).description) {
		// expand the overlay code out
		overlay_text = "<div id='video_overlay'><a href='" + overlay_text + "'>View Hotel &gt;&gt;</a></div>";
		
		// figure out where it'll go
		video_offset = $("#video_player").offset();
		overlay_width = 100;
		overlay_height = 15;
		overlay_top = ((video_offset.top + $("#video_player").outerHeight()) - overlay_height) - 60;
		overlay_left = ((video_offset.left + $("#video_player").outerWidth()) - overlay_width) - 20;
		
		// slap it in
		$("#video_player").after(overlay_text);
		
		// position and size it
		$("#video_overlay").css({
			"position" : "absolute",
			"width" : overlay_width,
			"height" : overlay_height
		}).offset({
			top : overlay_top,
			left : overlay_left
		});
	}
	
	// if we've found a video_id, send off an ajax dealie to the view counter script
	if(video_id = jwplayer().getPlaylistItem(n).video_id) {
		jQuery.ajax({
			data : "id=" + video_id,
			dataType : "html",
			error : function(XHR, errmsg) {
				// errors ain't no biggie for this really
			},
			success : function(data) {
				// success ain't really anything we need do anything about either
			},
			type : "GET",
			url : "tv_statistics.php"
		});
	}
}

