/* Autor: Danny Toth - www.schnitzraum.de */

//-----------------------------------------------
// PREDPARE DOCUMENT & FUNCTIONS	
//-----------------------------------------------
var bgsrc = 'img/sitebg.jpg';
var logosrc = 'img/logo-holz-persp.png';
var btnisrc = 'img/btn-i.png';

function resizeBg() {
	var bg = $('#bg');
	if( ($(window).width() / $(window).height()) < (bg.width() / bg.height()) ) {
		bg.removeClass().addClass('bgheight');
		bg.css({'left': '50%' , 'margin-left': (bg.width()/2)*(-1)});
    }else{
    	bg.removeClass().addClass('bgwidth');
    	bg.css({'left': '50%' , 'margin-left': (bg.width()/2)*(-1)});
    };
    return true;
};
$(window).resize(function() {
    resizeBg();
});


//-----------------------------------------------
// DOCUMENT READY	
//-----------------------------------------------

$(document).ready(function(){

	
	//-----------------------------------------------
	// LOADEREVENT
	//-----------------------------------------------
	$('#loader').bind('load', function() {
		$(this).delay(500).fadeIn(2000);
		$(this).animate({
			rotate: '359',
		}, 2000, 'easeOutElastic');
	});
	
	$('#loader').bind('loaded', function() {
		$(this).stop(true).fadeOut(200);
	});

	//-----------------------------------------------
	// BACKGROUND
	//-----------------------------------------------
	$.preLoadImages(bgsrc,
		function() {
//			$(window).load(function() {
				var bg = '<img style="display:none;" src="'+bgsrc+'" id="bg" alt="" />';
				if($('body').append(bg)){
					if(resizeBg()){
						$('#bg').fadeIn(1000);
						//console.log('bgfadeIn');
						resizeBg();
					}
				}
//			});
	});

	//-----------------------------------------------
	// FANCYBOX
	//-----------------------------------------------
	function initFancy(){
		$('a.btn').fancybox({
			'href'				: $(this).attr('href'),
			'overlayShow'		: false,
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'speedIn'			: 500,
			'speedOut'			: 400,
			'easingIn'			: 'easeOutBack',
			'easingOut'			: 'easeInBack',
			'width'				: '80%',
			'height'			: '90%',
			'autoScale'			: false,
			'titleShow'			: false,
			'type'				: 'iframe'
		});
	};

	//-----------------------------------------------
	// NAVIEVENTS
	//-----------------------------------------------
	function initKeyboard(){
		//console.log('initKeyboard');
		$(document).bind('keydown', 'i', function(){
			//console.log('i');
			$('#btn-i').fancybox({
				'overlayShow'		: false,
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'elastic',
				'speedIn'			: 500,
				'speedOut'			: 400,
				'easingIn'			: 'easeOutBack',
				'easingOut'			: 'easeInBack',
				'width'				: '80%',
				'height'			: '90%',
				'autoScale'			: false,
				'titleShow'			: false,
				'type'				: 'iframe',
				'enableEscapeButton': true,
				'onClosed'			: function(){initKeyboard()}
			}).trigger('click');
			$(this).unbind('keydown');
		});
	};

	//-----------------------------------------------
	// LOGOANIMATION
	//-----------------------------------------------
	var logo = '<div id="logoholz" class="relative"><img src="'+logosrc+'" id="logoholzimg" alt="" /><a class="absolute btn" id="btn-i" href="impressum.html" title="Impressum"><img src="'+btnisrc+'" alt="" /></a></div>';
	$.preLoadImages([logosrc,btnisrc],
			function() {
				if($('body').append(logo)){
					//console.log('append');
						$('a.btn').fadeTo(10, 0.5);
						$('#logoholz').css('rotate', -250);
						$('#logoholz').delay(1500).animate({
							top: '50%',
							left: '50%',
							rotate: '0'
						}, 1300, 'easeOutElastic');
						$('a.btn').live('mouseenter', function(){
							$(this).fadeTo(200, 1);
						});
						$('a.btn').live('mouseleave', function(){
							$(this).fadeTo(2000, 0.5);
						});
						initKeyboard();
						initFancy();
				};
			});

}); // END OF $(document).ready...




