$(document).ready( function() {

	//target blank
	$('A[class="blank"]').click( function() {
		window.open( $(this).attr('href') );
		return false;
	});

	$( function() {
		if( !navigator.userAgent.match( /(iPhone|iPod|iPad)/i ) ) {
			$("#form").validator();
		}
	});

});

var videoWidth = 853;
var videoRatio = 4/3; // 4/3 // 16/9

jQuery.fn.tubular = function(videoId,wrapperId) {
	t = setTimeout("resizePlayer()",1000);
	jQuery('#video').css('height','200px');
	jQuery('#video').prepend('<div id="yt-container" style="overflow: hidden; position: absolute; z-index: 1;"><div id="ytapiplayer">You need Flash player 8+ and JavaScript enabled to view this video.</div></div>');
	jQuery('#' + wrapperId).css({position: 'absolute', 'z-index': 1});

	var ytplayer = 0;
	var pageWidth = 0;
	var pageHeight = 0;
	var videoHeight = videoWidth / videoRatio;
	var duration;

	var params = { allowScriptAccess: "always", wmode: "transparent" };
	var atts = { id: "myytplayer" };
	swfobject.embedSWF("http://www.youtube.com/v/" + videoId + "?enablejsapi=1&playerapiid=ytplayer&loop=1", "ytapiplayer", videoWidth, videoHeight, "8", null, null, params, atts);

	jQuery(window).resize(function() {
		resizePlayer();
	});

	return this;
}

function onYouTubePlayerReady(playerId) {
	ytplayer = document.getElementById("myytplayer");
	ytplayer.setPlaybackQuality('medium');
	ytplayer.mute();
}

function resizePlayer() {
	var newWidth = jQuery(window).width();
	var newHeight = jQuery(window).height();
	jQuery('#yt-container, #video-cover').width(newWidth).height(newHeight);
	if (newHeight > newWidth / videoRatio) {
		newWidth = newHeight * videoRatio;
	}
	jQuery('#myytplayer').width(newWidth).height(newWidth/videoRatio);
}

$().ready(function() {
	$('body').tubular('9owdjqiMw2s','video');

	$('#videoPause').click(function() {
		if ($(this).hasClass('videoPaused')) {
			ytplayer.playVideo();
			$(this).removeClass('videoPaused');
		} else {
			ytplayer.pauseVideo();
			$(this).addClass('videoPaused');
		}
		return false;
	});

	$('#videoMute').click(function() {
		if ($(this).hasClass('videoMute')) {
			ytplayer.mute();
			$(this).removeClass('videoMute');
		} else {
			ytplayer.unMute();
			$(this).addClass('videoMute');
		}
		return false;
	});

	$('#videoStop').click(function() {
		ytplayer.stopVideo();
		ytplayer.clearVideo();
		$('#yt-container').hide();
		return false;
	});
});
