var minSize = true;

$.blockUI.defaults.overlayCSS.cursor = 'default';
$.blockUI.defaults.overlayCSS.minHeight = '768px';
$.blockUI.defaults.centerX = true;
$.blockUI.defaults.centerY = true;
$.blockUI.defaults.css.textAlign = 'left';
$.blockUI.defaults.css.padding = '0px';
$.blockUI.defaults.css.margin = '0px';

$(document).ready(function(){
	if (html5_video) {
		$('#ExternalInterfaceSWF').remove();
	}
	$('.unblock').live('click', function(){ $.unblockUI(); });
	$('#enter_button').css({width: parseInt($('#title2').find('span').width()-16,10)+'px'});
	$(window).resize(resizeHandler);
	resizeHandler();
	resizeHandler();
	rollOver('.menu_item', '#ffffff');
	rollOver('#head a', '#ffffff');
	rollOverSocial('#facebook');
	rollOverSocial('#twitter');
	rollOverSocial('#mail');
	$('#info_button').click(showInfo);
	$('.overlay_close').click(function(){
		$(this).parent().parent().fadeOut();
		if ($(this).parent().parent().attr('id') == 'overlay_video_tour') {
			$('#creation_content').fadeIn();
		}
	});
	setTimeout(function() { $('#loading').fadeOut('normal'); }, 8000);
});

function rollOver(target, over_color) {
	$(target).attr('rel', $(target).css('color'));
	$(target).mouseover(function(){
		$(this).stop().animate({color: over_color}, {duration: 1000, easing: 'easeOutQuint'});
	}).mouseout(function(){
		$(this).stop().animate({color: $(this).attr('rel')}, {duration: 600, easing: 'easeOutQuint'});
	});
}

function rollOverSocial(target) {
	$(target).attr('status', 'out');
	$(target).mouseover(function(event){
		event.stopPropagation();
		if ($(this).attr('status') == 'out') {
			$(target).attr('status', '');
			$(this).find('.social_over').fadeIn({duration: 600, easing: 'easeOutQuint', complete: function() { $(this).parent().attr('status', 'over'); }});
		}
	}).mouseout(function(event){
		event.stopPropagation();
		if ($(this).attr('status') == 'over') {
			$(target).attr('status', '');
			$(this).find('.social_over').fadeOut({duration: 600, easing: 'easeOutQuint', complete: function() { $(this).parent().attr('status', 'out'); }});
		}
	});
}

function blockPage() {
	$.blockUI.defaults.overlayCSS.opacity = 0;
	$.blockUI({ message: null });
}

function showInfo() {
	$.blockUI.defaults.overlayCSS.opacity = .9;
	$.blockUI({ 
		message: $('#info_text'), 
		css: {
			border: 'none',
			backgroundColor: '#000000',
			color: '#ffffff',
			width: '500px',
			left: '50%',
			marginLeft: '-250px',
			cursor: 'default'
		}}
	);
}

function showOverlay(title, text, legal, thumb, source, type, description) {
	$('#overlay_video_tour_title').html('<span style="color:#FFF">[</span>'+title+'<span style="color:#FFF">]</span>');
	$('#overlay_video_tour_text').html('<br>'+text);
	$('#overlay_video_tour_legal').html(legal);
	switch (type) {
		case 'video':
			$('#overlay_video_tour_thumb').html('<a href="javascript:showVideo(\''+source+'\', \''+description+'\', \''+title+'\');"><img style="border: 4px solid #515050;" src="img/'+thumb+'" width="319" height="191" border="0"></a>');
			break;
		case 'image':
			$('#overlay_video_tour_thumb').html('<a href="javascript:showImage(\''+source+'\', \''+description+'\', \''+title+'\');"><img style="border: 4px solid #515050;" src="img/'+thumb+'" width="319" height="191" border="0"></a>');
			break;
	}
	$('#overlay_video_tour').fadeIn();
}

function showVideo(id, description, title) {
	var html = '<iframe width="560" height="349" src="http://www.youtube.com/embed/'+id+'?rel=0" frameborder="0" allowfullscreen></iframe>';
	$('#overlay_media_box').html(html);
	$('#overlay_media_description').html(description);
	$('#overlay_media_title').html(title);
	$('#overlay_media').fadeIn();
}

function showImage(src, description, title) {
	var html = '<img style="border: 4px solid #515050;" src="img/'+src+'" width="552" border="0">';
	$('#overlay_media_box').html(html);
	$('#overlay_media_description').html(description);
	$('#overlay_media_title').html(title);
	$('#overlay_media').fadeIn();
}

function resizeHandler() {
	if (parseInt($(window).height(),10) < 768) {
		minSize = false;
	} else {
		minSize = true;
	}
	imgSize();
	var window_height = (minSize) ? parseInt($(window).height(),10) : 768;
	if (!isiPad()) {
		if (html5_video) {
			$('#creation video').css({width: '1024px', height: '768px'});
			var w = (1 / $('#creation video').width()) * parseInt($(window).width(),10);
			var h = (1 / $('#creation video').height()) * window_height;
			var maxx = Math.max(w, h);
			w = maxx * $('#creation video').width();
			h = maxx * $('#creation video').height();
			$('#creation video').css({width: w+'px', height: h+'px'});
			var margintop = h / 2;
			$('#creation video').css({marginTop: '-'+margintop+'px'});
			$('#creation video').parent().css({position: 'absolute', width: parseInt($(window).width(),10)+'px', height: window_height+'px', overflow: 'hidden'});
		} else {
			$('#ExternalInterfaceSWF').css({width: '1024px', height: '768px'});
			var w = (1 / $('#ExternalInterfaceSWF').width()) * parseInt($(window).width(),10);
			var h = (1 / $('#ExternalInterfaceSWF').height()) * window_height;
			var maxx = Math.max(w, h);
			w = maxx * $('#ExternalInterfaceSWF').width();
			h = maxx * $('#ExternalInterfaceSWF').height();
			$('#ExternalInterfaceSWF').css({width: w+'px', height: h+'px', position: 'absolute', overflow: 'hidden', top: '50%', marginTop: '-'+h/2+'px'});
			$('#ExternalInterfaceSWF').parent().css({position: 'absolute', width: parseInt($(window).width(),10)+'px', height: window_height+'px', overflow: 'hidden'});
		}
	} else {
		$('#creation video').css({width: '1024px', height: '768px'});
		$('#creation video').css({top: '0px'});
	}
	$('#creation_menu').css({marginLeft: '-'+parseInt($('#creation_menu').width(),10)/2+'px', top: window_height-100+'px'});
	$('#color_palette').css({marginLeft: '-'+parseInt($('#color_palette').width(),10)/2+'px', top: window_height-185+'px'});
	$('#creation').find('.line').css({marginLeft: '-'+parseInt($('#creation_menu').width(),10)/2+'px', width: parseInt($('#creation_menu').width(),10)+'px', top: window_height-116+'px'});
	$('#social').css({top: window_height-40+'px'});
	$('#disclamer').css({top: window_height-30+'px'});
	$('#info_button').css({top: window_height-30+'px'});
	if (isiPad()) {
		$('.overlay').css({height: '768px'});
	}
}

function imgSize() {
	
	var window_height = (minSize) ? parseInt($(window).height(),10) : 768;
	
	$('#home img').css({width: '1024px', height: '768px'});
	var w = (1 / $('#home img').width()) * parseInt($(window).width(),10);
	var h = (1 / $('#home img').height()) * window_height;
	var maxx = Math.max(w, h);
	w = maxx * $('#home img').width();
	h = maxx * $('#home img').height();
	var margintop = h / 2;
	$('#home img').css({position: 'absolute', width: w+'px', height: h+'px', top: '50%', marginTop: '-'+margintop+'px'});
	$('#home').css({position: 'absolute', width: parseInt($(window).width(),10)+'px', height: window_height+'px', overflow: 'hidden'});
	
	$('#color_image img').css({width: '1024px', height: '768px'});
	var w = (1 / $('#color_image img').width()) * parseInt($(window).width(),10);
	var h = (1 / $('#color_image img').height()) * window_height;
	var maxx = Math.max(w, h);
	w = maxx * $('#color_image img').width();
	h = maxx * $('#color_image img').height();
	$('#color_image img').css({position: 'absolute', width: w+'px', height: h+'px', top: '50%', marginTop: '-'+margintop+'px'});
	var margintop = h / 2;
	$('#color_image').css({position: 'absolute', width: parseInt($(window).width(),10)+'px', height: window_height+'px', overflow: 'hidden'});

}
