$(document).ready(function(){
	
	_gaq.push(["_trackEvent", "Mail", sRefID, sRefID]);
						   
	dActDate = new Date();

	$('#counter').countdown({
		until: new Date(dActDate.getFullYear(), 12 - 1, 24, 16, 23, 15),
		compact: true, 
    	layout: $('#counter').html()
	});
	
	
	var iStartDay = 140;
	var iActDay = $('#counter').countdown('getTimes')[3];
	
	var iMinTop = 120;
	var iMaxTop = 595;
	
	var iMinTopSnow = 200;
	var iMaxTopSnow = 665;
	
	
	
	if(iActDay < iStartDay ){ 
		var iDifference = iMaxTop - ( ( (iStartDay-iActDay)/iStartDay ) * (iMaxTop - iMinTop) );
		var iDifferenceSnow = iMaxTopSnow - ( ( (iStartDay-iActDay)/iStartDay ) * (iMaxTopSnow - iMinTopSnow) );
	} else {
		var iDifference = iMaxTop;
		var iDifferenceSnow = iMaxTopSnow;
		$('#close-snow').hide();
	}
	
	$('#snow').css({
		top: iDifference   
	})
	
	snowStorm = new SnowStorm();
	snowStorm.snowStickPosition = iDifferenceSnow;
	
	var iClickCount = 0;
	
	$('#gallery-container > a').bind('click', showProject); 

	$('a.forward').bind('click', nextProject);
	$('a.back').bind('click', prevProject);

	$('#logo').bind('click', resetGalleryLogo); 
	$('a.close').bind('click', resetGallery); 
	$('#button-contact').bind('click', showContact);
	$('#button-send').bind('click', sendMail);
	
	$('#close-snow').bind('click', moveSnow);
	
	function showProject(e) {
		sItemID = e.currentTarget.id;
		
		$('#gallery div').removeClass('active');
		$('#text div').removeClass('active');
		$('#' + sItemID + '-zoom').addClass('active');
		$('#' + sItemID + '-text').addClass('active');

		$('#text-action').addClass('active');
		clickCount();
		
		loadImage(sItemID + '-zoom');
		_gaq.push(['_trackEvent', 'Gallery', 'Open', sItemID]);
	}

	function resetGallery() {
		$('#gallery div').removeClass('active');
		$('#text div').removeClass('active');
		$('#gallery-container').addClass('active');
		$('#text-start').addClass('active');
		$('#text-action').addClass('active');
		clickCount();
		_gaq.push(['_trackEvent', 'Gallery', 'Close', sItemID]);
	}
	
	function resetGalleryLogo() {
		$('#gallery div').removeClass('active');
		$('#text div').removeClass('active');
		$('#gallery-container').addClass('active');
		$('#text-start').addClass('active');
		$('#text-action').addClass('active');
		clickCount();
		_gaq.push(['_trackEvent', 'Gallery', 'Home', sItemID]);
	}


	function showContact() {
		_gaq.push(['_trackEvent', 'Text', 'Bestellung', 'Bestellung']);
		$('#gallery div').removeClass('active');
		$('#text div').removeClass('active');
		
		$('#gallery-container').addClass('active');
		$('#text-contact').addClass('active');
		$('#text-send-button').addClass('active');
		clickCount();
	}

	function nextProject(e) {
		if(e.currentTarget.parentNode.parentNode.id != 'gallery-item-20-zoom') {
			sNextZoomItemID = e.currentTarget.parentNode.parentNode.nextSibling.id;
		} else {
			sNextZoomItemID = 'gallery-item-1-zoom';
		}

		sNextTextItemID = $('#' + sNextZoomItemID).attr('lang');

		$('#gallery div').removeClass('active');
		$('#text div').removeClass('active');
		$('#' + sNextZoomItemID).addClass('active');
		$('#' + sNextTextItemID).addClass('active');
		$('#text-action').addClass('active');
		clickCount();
		loadImage(sNextZoomItemID);
		
		_gaq.push(['_trackEvent', 'Gallery', 'Next', sNextTextItemID]);
	}

	function prevProject(e) {
		if(e.currentTarget.parentNode.parentNode.id != 'gallery-item-1-zoom') {
			sPrevZoomItemID = e.currentTarget.parentNode.parentNode.previousSibling.id;
		} else {
			sPrevZoomItemID = 'gallery-item-20-zoom';
		}

		sPrevTextItemID = $('#' + sPrevZoomItemID).attr('lang');

		$('#gallery div').removeClass('active');
		$('#text div').removeClass('active');
		$('#' + sPrevZoomItemID).addClass('active');
		$('#' + sPrevTextItemID).addClass('active');
		$('#text-action').addClass('active');
		clickCount();
		loadImage(sPrevZoomItemID);
		
		_gaq.push(['_trackEvent', 'Gallery', 'Prev', sPrevTextItemID]);
	}
	
	function sendMail() {
		$.post("scripts/sendmail.php", $('#contact-form').serialize(), function(data){
			if(data == 'success'){
				_gaq.push(['_trackEvent', 'Text', 'Absenden - Erfolg', 'Absenden - Erfolg'])
				$('#contact-form input').val('');
				$('#contact-form textarea').val('');
				$('#text-send-button p span.error').remove();
				
				$('#gallery div').removeClass('active');
				$('#text div').removeClass('active');
				$('#text-contact-thanks').addClass('active');
				$('#gallery-container').addClass('active');
				clickCount();
			} else {
				_gaq.push(['_trackEvent', 'Text', 'Absenden - Fehler', 'Absenden - Fehler'])
				$('#text-send-button p span.error').remove();
				$('#text-send-button p').append('<span class="error">Bitte f&uuml;llen Sie alle Felder aus.</span>');
			}
		});	
	}
	
	function stopSnow() {
		$('div.flake').fadeOut(700, function(){snowStorm.stop();});
	}
	
	function resumeSnow() {
		snowStorm.resume();
		$('div.flake').fadeIn(700);	
	}
	
	function clickCount() {
		if(iClickCount == 0){
			preloadImages();
		}
		iClickCount++;
		if(iClickCount>10) {
			stopSnow();
		}
	}
	
	function moveSnow(){
		if( !$('#close-snow').hasClass('close') ){
			stopSnow();
			_gaq.push(['_trackEvent', 'Snow', 'Down', 'Down']);
			$('#snow').animate({
				top: iMaxTop   
			}, 700, function(){ $('#close-snow').addClass('close'); })
		} else {
			_gaq.push(['_trackEvent', 'Snow', 'Up', 'Up']);
			$('#snow').animate({
				top: iDifference   
			}, 700, function(){ $('#close-snow').removeClass('close'); resumeSnow(); })
		}
	}
	
	function loadImage(sID) {	
	
		var sSpanID = sID + '-span';

		if($('#' + sSpanID).text() != '') {
			$('#' + sSpanID).parent().prepend('<img src="' + $('#' + sSpanID).text() + '" />');
			$('#' + sSpanID).text('');
		}
	
	}
	
	function preloadImages(){
		var cache = [];
		$.each(aPreloadImages, function() {
		  var cacheImage = document.createElement('img');
		  cacheImage.src = this;
		  cache.push(cacheImage);
		});
	}
	
});

