var __$movie;
var __movie_w = 600;
var __movie_h = 400;
var __url;

var __$nav;
var __navleft;
var __navtop;
var __navbottom;

var __$main;
var __main_w = 600;

var __flash = false;

function testFlash() {
	var MM_contentVersion = 6;
	var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
	if ( plugin ) {
		var words = navigator.plugins["Shockwave Flash"].description.split(" ");
		for (var i = 0; i < words.length; ++i) {
			if (isNaN(parseInt(words[i]))) {
				continue;
			}
			var MM_PluginVersion = words[i]; 
		}
		var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
	} else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 && (navigator.appVersion.indexOf("Win") != -1)) {
		document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
		document.write('on error resume next \n');
		document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
		document.write('</SCR' + 'IPT\> \n');
	}
	if ( MM_FlashCanPlay ) {
		return true;
	} else{
		return false;
	}
}

function resizePlayer() {
	var d = new Date();
	var refresh = d.getTime();
	var fwidth = $('#movie_frame, #nomovie_msg').filter(':visible').width();

	if (fwidth < __movie_w) {
		fwidth = __movie_w;
	}
	var fheight = fwidth * __movie_h / __movie_w;
	if (__flash) {
		$('.player', '#movie_frame').css({
				width : fwidth,
				height : fheight
			});
		$('param.object_url', '#movie_frame').attr('value', __url + refresh);
		$('embed.embed_url', '#movie_frame').attr('src', __url + refresh);
		$('div.nomovie').hide();
	}  else {
		$('#movie_frame').hide();
		$('div.nomovie').show();
		$('img', '#nomovie_img').css({
				width : fwidth,
				height : fheight
		});
	}
}

function resizeMain() {
	var m_width = __$main.width();
	__$main.find('img').each(function(index, element) {
		var $img = $(element);
		var img_w = $img.width();
		var img_h;
		if (img_w > m_width) {
			img_h = m_width / img_w * $img.height();

			$img.css({
				width: m_width + 'px',
				height: img_h + 'px'
			});
		}
	});
}

function moveNav() {
	var current = $(window).scrollTop();
	var top = __$nav.offset().top;
	var scrollto = __navtop;
	if (current > __navbottom) {
		scrollto = __navbottom;
	} else if (current > __navtop) {
		scrollto = current;
	}

	__$nav.css({
		position : "absolute",
		top : scrollto + "px",
		left : __navleft + "px"
	});

}

$(document).ready(function() {
	__$movie = $('#movie_frame');
	if (__$movie.length) {
		__flash = testFlash();
		__movie_w = $('object.player', '#movie_frame').attr('width');
		__movie_h = $('object.player', '#movie_frame').attr('height');
		__url = $('param.object_url', '#movie_frame').attr('value') + '&';
		setTimeout(function() {
			resizePlayer();
		}, 500);
		$(window).bind('resize', function() {
			resizePlayer();
		});
	}

	__$main = $('#blog-content');
	if (__$main.length) {
		resizeMain();
		$(window).bind('resize', function() {
			resizeMain();
		});
	}
});

$(window).load(function() {
	__$nav = $('#primary');
	if (__$nav.length) {
		__navleft = __$nav.offset().left;
		__navtop = __$nav.offset().top;
		__navbottom = $('#doc').height() + $('#doc').offset().top - __$nav.outerHeight(true);
		$(window).bind('scroll', function() {
			moveNav();
		});
	}
});

