$(document).ready(function() {	
	$(window).resize(function() {
		resizeHandler();
	}).trigger('resize');
	
	$('#video').hide();
	
	$('#video').bind('timeupdate', function() {
		$('#video').unbind('timeupdate');
		resizeHandler();
	})
	
	// auto play for firefox
	$("#video").bind('ended', function() {
		this.play();
	});
	
	// blink effect
	cursorAnimation();
	function cursorAnimation() {
		$('.blink').animate({
			opacity: 0
		}, 500, "swing").animate({
			opacity: 1
		}, 500, "swing", function() {
			cursorAnimation();
		});
	}
	
	if(jQuery.browser.msie || isiPhone()) {
	 	$('#video').remove();
	 	$('.src').remove();
	 	
	 	var image = '<img src="images/DHKY_final_loop.gif" id="video" />';
	 	$('body').prepend(image);
	 	$('#video').show();
	}
	
	function isiPhone(){
	    return (
	        (navigator.platform.indexOf("iPhone") != -1) ||
	        (navigator.platform.indexOf("iPod") != -1) ||
	        (navigator.platform.indexOf("iPad") != -1)
	    );
	}
	
	function resizeHandler() {		
		// scale the video
		var documentHeight = $(document).height();
		var documentWidth = $(document).width();
		var ratio = $('#video').width() / $('#video').height();
		
		if((documentWidth / documentHeight) < ratio) {
			$('#video').css({
				'width': 'auto',
				'height': '100%',
				'left': '0px',
				'right': '0px',
				'top': '0px',
				'bottom': '0px'
			})
			
			var marginRight = $('#video').width() - $(document).width();
			$('#video').css('left', -marginRight);
		} else {
			$('#video').css({
				'width': '100%',
				'height': 'auto',
				'left': '0px',
				'right': '0px',
				'top': '0px',
				'bottom': '0px'
			})
				
			var marginTop = $('#video').height() - $(document).height();
			$('#video').css('top', -marginTop);
		}
		
		$('#video').show();
	}
});
