function add_blur(id) {
	$("#" + id).focus(function () {
		this.blur();
	});
}

function add_external(id) {
	$("#" + id).click(function (event) {
		event.preventDefault();
		window.open(this);
	});
}

function has_flash() {
	if (!swfobject.hasFlashPlayerVersion("9.0.115")) {
		$('#flash_movie').html(swf_get_flash);
		add_blur("get_flash");
		add_external("get_flash");
	}
}

function image_load(obj, x, type) {
	img = new Image();	
	$(img).load(function () {
		$(this).hide();
		$('#' + type + x).parent().removeClass('loading');
		$('#' + type + x).append(this);
		$(this).fadeIn();
	});
	$(img).attr('src', obj.thumbnail_url);
}

function run_image_load() {
	if (typeof(tip) == 'object') {
		for (x = 0; x < tip.length; x++) {
			image_load(tip[x], x + 1, 'tip');
		}
	}
	if (typeof(favorite) == 'object') {
		for (x = 0; x < favorite.length; x++) {
			image_load(favorite[x], x + 1, 'favorite');
		}
	}
}

function open_flashmovie(uri, vars) {
	var swf_src = uri + vars;
	var flashvars = {};
	flashvars.fullscreen_infobutton = fullscreen;	
	var params = {};
	params.play = "true";
	params.loop = "true";
	params.menu = "true";
	params.quality = "high";
	params.bgcolor = "#000000";
	params.scale = "exactfit";
	params.wmode = "transparent";
	params.devicefont = "false";
	params.allowfullscreen = "true";
	params.allowscriptaccess = "always";	
	var attributes = {};
	attributes.id = "flash_movie";
	attributes.align = "middle";
	swfobject.embedSWF(
		swf_src,
		"flash_movie",
		608,
		342,
		"9.0.115",
		swf_express,
		flashvars,
		params,
		attributes
		);
}

function open_banner(
	swf_src,
	div,
	width,
	height
) {
	var flashvars = {};	
	var params = {};
	params.play = "true";
	params.loop = "true";
	params.menu = "true";
	params.quality = "high";
	params.bgcolor = "#000000";
	params.scale = "exactfit";
	params.wmode = "transparent";
	params.devicefont = "false";
	params.allowfullscreen = "true";
	params.allowscriptaccess = "always";	
	var attributes = {};
	attributes.id = div;
	attributes.align = "middle";
	attributes.style = "margin-bottom:4px;";	
	swfobject.embedSWF(
		swf_src,
		div,
		width,
		height,
		"9.0.115",
		swf_express,
		flashvars,
		params,
		attributes
		);
}

function video_ctrl(video) {
	request_url = video.swf_url;
	request_vars = video.flashvars;
	$("#video_title").html('<h3 class="black italic">' + video.title + '</h3>');
	Cufon.replace('h3');
	$("#uploaded").html(video.last_updated);
	$("#author").html(video.author);
	
	if (video.video_src == 'canon') {
		$("#see_comments_wrapper").hide();
		$("#watch_full_film_wrapper").show();
		$("#watch_full_film").attr("href", video.webpage_url);
		$("#watch_full_arrow").attr("href", video.webpage_url);		
	} else {
		$("#watch_full_film_wrapper").hide();
		$("#see_comments_wrapper").show();
		$("#see_comments").attr("href", video.webpage_url);
		$("#see_comments_arrow").attr("href", video.webpage_url);
	}
	
	$.scrollTo(0, 700);
	open_flashmovie(video.swf_url, video.flashvars);
	pageTracker._trackPageview("/" + cc + "/" + lc + "/stage" + stage + "/" + page + "/video/" + video.id);
	
	_tag.DCSext.VideoClick=video.swf_url+video.flashvars;
	_tag.dcsCollect();
}

function check_hash() {
	var hash = self.document.location.hash.substring(1);
	var split_hash = hash.split('_');
	var i = null;
	
	if (hash.length > 0 && split_hash[0] == 'video') {
	
		if (typeof(tip) == 'object') {
			for (i in tip) {
				if (tip[i].id == split_hash[1]) {
					video_ctrl(tip[i]);
					break;
				}
			}
		}
		if (typeof(favorite) == 'object') {
			for (i in favorite) {
				if (favorite[i].id == split_hash[1]) {
					video_ctrl(favorite[i]);
					break;
				}
			}
		}
		
	} else {
		if (typeof(request_video) == 'object' && request_video !== null) {
			video_ctrl(request_video);
		}
	}
}

$(document).ready(function () {
	
	$("a[rel='external']").click(function (event) {
		event.preventDefault();
		window.open(this);
	});
	
	$("#country_selector").change(function (event) {
		var cc_lc = this.value;
		var temp = cc_lc.split('_');
		var url = '?cc=' + temp[0] + '&lc=' + temp[1];
		if (page.length > 0) {
			url += '&pg=' + page;
		}
		window.location = url;
	});
	
	has_flash();
	setTimeout("run_image_load()", 300);
	
});