f_initCenterPage = function(obj) {
	if ($(window).height() > 600) {
		obj.css({ 'top': ($(window).height() - obj.height()) / 2 });
	}
}
//center the a elemen horizontal, this is used for the video's
f_CenterElementHorizontal = function(obj) {
	if($(window).width() > 1000) {
		obj.css({ 'marginLeft': ($(window).width() - obj.width()) / 2 });
	} else {
		if (obj.width() < 1000) {
			obj.css({ 'marginLeft': (1000 - obj.width()) / 2 });
		}
	}
}

$(document).ready(function() {
	
	$('li.menu-item').children('a').empty();
	
	f_initCenterPage($('#pageWrap'));
	
	$(window).resize(function() {
		f_initCenterPage($('#pageWrap'));
	});


	//insert overlay
	$('.videoWrap').one("click", function() {
		$('<div class="overlay">').insertAfter('#pageWrap');
	});

	$('.videoWrap').click(function() {
		var 
			videoSrc = $(this).data('flash'),
			videoH = $(this).data('videoH'),
			videoW = $(this).data('videoW');
		
		//create video Container
		var crtVideo = $('.overlay').after('<div class="videoWrap active"><a href="'+videoSrc+'" style="display:block;width:'+videoW+'px;height:'+videoH+'px" id="player"> </a></div>');
		
		//init flash
		flowplayer("player", "http://3d-unit.com/wordpress/wp-content/themes/3dUnit/js/flowplayer-3.2.7.swf");
			
		$('.overlay').fadeIn('slow');
		//get height and width from the video tag to center the video in the layer
		$(crtVideo).next().css({'width': videoW, 'height': videoH });
			
		var currentVideo = (crtVideo).next();
			
		f_initCenterPage($(currentVideo));
		f_CenterElementHorizontal($(currentVideo));
		$(window).resize(function() {
			f_initCenterPage($(currentVideo));
			f_CenterElementHorizontal($(currentVideo)); 
		});
		
		//close the current layer 
		$('.overlay').click(function() { 
			$(this).fadeOut('slow');
			$($('.videoWrap.active')).remove();
		});
		
	});
})
