$(function(){
	
	$(window).resize(resize).trigger('resize');
	$('#imprint').hover(function(e){
		$(this).find('div').addClass('hover');
	}, function(){
		$(this).find('div').removeClass('hover');		
	});
	
	$('#overlay img').click(function(){
		$('body').data('state', 'ready');
		$('body').find('#overlay').fadeOut(750);
		$('body').find('#paper').fadeIn();
		return false;
	});
	
	$('body').data('state', 'loading').attr('class','loading').find('#overlay img').attr('src', '/images/overlayc.jpg').load(function(){
		$('#overlay img').unbind().attr('src', '/images/blank.gif');
		$('#overlay').fadeIn(1000, function(){
			countDown(9, 4);
			window.setTimeout(function(){
				$('#header').css('left', '-560px').animate({left: '0px'}, 750, 'bounceout');
				$('body').data('state', 'ready');
				$('body').find('#overlay').fadeOut(750);
				$('body').find('#paper').fadeIn();	
				load();
			}, 1000);
	
		});
	});
	
	/* NEWSLETTER */
	
	/*$('#subscribeNewsletterForm').submit(function(){
		var email = $(this).find('input').val();
		if((email != 'Your E-Mail') && (email != '')){
			$.post("/ajax/subscribe.php", {email: email});			
		}
		return false;
	});
	*/
    
    
    
	/* POPUP */
	
	$('.popup').click(function(){
		var info = ($(this).attr('alt')).split('|');
		window.open('popup.php?src=/images/news/'+info[0]+'.jpg','DC4', 'height='+info[1]+',left='+((screen.availWidth / 2)-280)+',top='+((screen.availHeight / 2)-250)+',width=560');
	});
	
    
	/* FORMS */
	
	$('form').formUI({submit: function(form){
		switch($(form).attr('id')){
			case 'newsletterSubscribeForm':
				$('#badge').trigger('animate');
			break;
			case 'pressLoginForm':
				$('#pressLogin').trigger('login', {username:$('#username').val(), password:$('#password').val()});
			break;
		}
		return false;
	}});
   
		
	/* NAVIGATION */
	
	$('#navigation li a').click(function(){
		if(!$(this).is('.external')){
			//var link = $(this).parent();
			var link = $(this);
			$('body').fadeOut(500, function(){
				//document.location.href = '#'+$(link).attr('class')+'/';
				document.location.hash = $(link).attr('href');
				load();
				$('body').fadeIn(500, function(){
					$('body').data('state', 'ready').attr('class', 'ready');
				});
			});
			return false;			
		}
		return true;
	});
	
	$('.hoverable').hover(function(){
		var hover = $(this).find('.hover');
		$(hover).fadeIn('slow');
	}, function(){
		var hover = $(this).find('.hover');
		$(hover).fadeOut('slow');
	}).find('.hover').css('display', 'none');
		
	/* NEWS */
	
	var next = $('#newsroll').data('display', 0).parent().find('.next').click(function(){
		if($('#newsroll').data('display')+1 >= $('#newsroll li').length){
			var n = 0;
		}else{
			var n = $('#newsroll').data('display')+1;
		}
		$('#newsroll li').addClass('hidden').eq(n).removeClass('hidden').parent().data('display', n);
		return false;
	});
	
	var prev = $('#newsroll').data('display', 0).parent().find('.previous').click(function(){
		if($('#newsroll').data('display')-1 <= -1){
			var n = ($('#newsroll li').length-1);
		}else{
			var n = $('#newsroll').data('display')-1;
		}
		$('#newsroll li').addClass('hidden').eq(n).removeClass('hidden').parent().data('display', n);
		return false;
	});
	
	/* PRESS LOGIN */
	
	$('#pressLogin').bind('login', function(e, credentials){
		$.post("/ajax/press.php", {username: credentials.username, password: credentials.password}, function(response){
			if(response != 0){				
				$('#pressLogin, .overlay').fadeOut(500, function(){
					$('#content ol li a').each(function(i, link){
						if(($(link).attr('href')).indexOf('press') < 0){
							$(link).attr('href', 'http://press.dc4.de/'+$(link).attr('href')+'').attr('target', '_blank');							
						}
					});	
				});				
			}
		}, 'json');
	});
	
	/* BADGE */
	
	$('#badge').data('state', 'minimized').bind('maximize', function(){
		if($('#badge').data('state') == 'minimized'){
			$('#badge ol li').addClass('hidden').eq(1).removeClass('hidden');
			$('#badge').animate({height: '270px', left: '490px', top: '-70px', width: '270px'}, 500, 'elasout', function(){
				$('#badge ol li').addClass('hidden').eq(2).removeClass('hidden');
				$('#badge').data('state', 'maximized');
			});						
		}
	}).bind('minimize', function(){
		if($('#badge').data('state') == 'maximized'){
			$('#badge ol li').addClass('hidden').eq(1).removeClass('hidden');
			$('#badge').animate({height: '126px', left: '590px', top: '27px', width: '126px'}, 500, 'elasout', function(){
				$('#badge ol li').addClass('hidden').eq(0).removeClass('hidden');
				$('#badge').data('state', 'minimized');	
			});
		}
	});
	
	$('#badge .maximize').click(function(){
		$('#badge').trigger('maximize');
		return false;
	});

	$('#badge .minimize').click(function(){
		$('#badge').trigger('minimize');
		return false;
	});
    
    $('#badge .minimize_img').click(function(){
		$('#badge').trigger('minimize');
		return false;
	});
	
	$('#badge .subscribe').click(function(){
		$('#badge').trigger('minimize');
		return false;
	});

});

function countDown(from, to){
	if(from-to > 0){
		$('#overlay img').css('background-position', '0px '+((from-to)*28)+'px');
		window.setTimeout(function(){
			countDown(from-1, to);
		}, 250);
	}
}

function load(){
	/* Extract Location Anchor */
	var anchor = ((document.location.hash).replace('#!', '')).replace(/\//g, '');
	/* Assign Body ID & Class */
	$('body').attr('class', $('body').data('state')).attr('id', (anchor ? anchor : 'home'));
	background();
	resize();
}

function resize(){
	var w = new Array($(window).width(), $(window).height());
	$('#overlay, #paper, #main').each(function(i,e){
		if($(e).attr('id') == 'main'){
			$(e).height(($('#paper').height()-200));
			$('#contentBrands, #contentContact, #contentPress').each(function(i, c){
				var offset = new Array(130,170,170);
				$(c).css({'margin-top': ($('#main').height()/2 - offset[i])});
			});
		}else{
			$(e).height(w[1]);			
		}
	});
	$('#overlay img').css('top', ((w[1]/2) -  14) + 'px').css('left', ((w[0]/2) - 40) +'px');
	$('#imprint').css('top', ((w[1]/2) - 125)+'px');

}

function background(){
	$('body #paper').css('background-image', 'none');
	if($(window).width() >= 1440){
	    $('body#store #paper').css('background-image', 'url(/images/store/1_1440.jpg)');
	}
	if($(window).width() >= 1600){
	    $('body#store #paper').css('background-image', 'url(/images/store/1_1600.jpg)');
	}
	if($(window).width() < 1440){
	    $('body#store #paper').css('background-image', 'url(/images/store/1_1440.jpg)');
	}
	if($(window).width() <= 1024){
	    $('body#store #paper').css('background-image', 'url(/images/store/1_1024.jpg)');
	}
}