﻿$(document).ready(function() {
	mediaNav();
	loadVideo("http://blip.tv/play/hYgdjMEDn519%2Em4v");
	loadFeatureVideo("http://blip.tv/play/hYgdgcqdBAA%2Em4v");
	loadTwitterWidget();
	tooltips("li strong", "tooltip");
	if (getParameterByName("view") == "episodes") $("#icon_watch a").click();
});

// Handle tabbed view in Media box
function mediaNav() {
	$("#ut_nav a").click(function() {
		$("#ut_nav li").removeClass('selected');
		$(this).parent().addClass('selected');
		loadMediaTab($(this).parent().attr('id').replace('icon_', ''));
	});

	$("#content_watch a").click(function() {
		$("#content_watch li").removeClass('selected'); 
		$(this).parent().addClass('selected');
	});
}

// Show tab in Media box
function loadMediaTab(tab) {
	$("#ut_content div[id^='content']").hide();
	$("#content_" + tab).show();
}

// Load main video player
function loadVideo(vid) {
	var flashvars = false;
	var params = {};
	params.allowscriptaccess = "always";
	params.allowfullscreen = "true";
	params.wmode = "transparent";
	var attributes = false;
	swfobject.embedSWF(vid, "video_player", "446", "324", "9.0.0", "false", flashvars, params, attributes);
}

// Load feature wall video player
function loadFeatureVideo(vid) {
	var flashvars = false;
	var params = {};
	params.allowscriptaccess = "always";
	params.allowfullscreen = "true";
	params.wmode = "transparent";
	var attributes = false;
	swfobject.embedSWF(vid, "feature_player", "463", "300", "9.0.0", "false", flashvars, params, attributes);
}

// Convert title attributes to nice tooltips
function tooltips(target_items, name) {
	$(target_items).each(function(i) {
		$("body").append("<div class='" + name + "' id='" + name + i + "'>" + $(this).attr('title') + "</div>");
		var my_tooltip = $("#" + name + i);

		if ($(this).attr('title') != "") {
			$(this).removeAttr("title").mouseover(function() {
				my_tooltip.css({opacity:1, display:"none"}).fadeIn(200);
			}).mousemove(function(kmouse) {
				my_tooltip.css({left:kmouse.pageX - 125, top:kmouse.pageY + 15});
			}).mouseout(function() {
				my_tooltip.fadeOut(200);
			});
		}
	});
}

// Twitter widget specification
function loadTwitterWidget() {
	new TWTR.Widget({
		profile: true,
		id: 'twitter',
		loop: true,
		width: 160,
		height: 198,
		theme: {
			shell: {
				background: '#00ADEF',
				color: '#FFFFFF'
			},
			tweets: {
				background: '#FFFFFF',
				color: '#3C3B37',
				links: '#00ADEF'
			}
		}
	}).render().setProfile('DrRobi_S').start();
}

// Extract Querystring parameter
function getParameterByName(name) {
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if (results == null)
		return "";
	else
		return results[1];
}
