var glitter, gold = 1.618;



(function($)
{

	var b = $( 'body' ), s = $('#stage'), l = $( '#logo' ), q = $( '#quote' );

	// !Control
	$('#right-arrow').click(function() {s.flickDragClick('next')});
	$('#left-arrow').click(function()  {s.flickDragClick('prev')});



	s.mousewheel(function( e, d )
	{
		e.preventDefault();
		vel = Math.abs( d );

		d = ( d > 0 ) ? 'next' : 'prev';
		s.flickDragClick( d );
	});



	// !Listeners
	s.flickDragClick( 'addListener', 'init', 	onInit );
	s.flickDragClick( 'addListener', 'resize', 	onResize );
	s.flickDragClick( 'addListener', 'newView',	onNewView );

	function onInit( w, h )
	{
		resizeSlides( w, h );
		setLogo( w, h );
		setQuote();
	}

	function onResize( w, h )
	{
		resizeSlides( w, h );
		setLogo( w, h );
	}

	function onNewView( w, h )
	{
		resizeSlides( w, h );
		setQuote();
	}



	function resizeSlides( w, h )
	{
		$( '.show, .prev, .next' ).each(function( i, v )
		{
			v = $( v );

			var
				w = s.width(),
				h = s.height();

			if( w != v.width() || h != v.height() )
			{
				var newSize = { 'width'	 : w, 'height' : h };

				v.css( newSize );
				setSize( v, w, h );
			}
		});
	}

	function setLogo( w, h )
	{
		var
			w = s.width(),
			h = s.height();


		var
			x = ( w - 309 ) / 2,
			y = ( ( h - 315 ) / 2 ) + 114 - 30;	//	30 = mod

		l.css({ 'left' : x, 'top' : y });
	}

	function setQuote()
	{
		t = $( '.show' ).children( 'img' ).attr( 'alt' );

		q.fadeOut( 200, function()
		{
			q.html( t );
			q.css( 'margin-top', 12 + ( ( 75 - q.height() ) /2 ) );
					
			q.fadeIn( 500 );
		});
	}



	// !Sizing
	function setPosition(a,b,c,d){if(a=="x"&&b||a=="y"&&!b){a=0}else{switch("center"){case"top":a=0;break;case"left":a=0;break;case"bottom":a=c-d;break;case"right":a=c-d;break;default:a=(c-d)/2}}return a}function setSize(a,b,c){a.each(function(a,d){var e=$(d).children("img"),f=e.attr("width"),g=e.attr("height"),i=b/f*g>c?true:false,j=Math.ceil(i?b:c/g*f),k=Math.ceil(i?b/f*g:c),l=setPosition("x",i,b,j),m=setPosition("y",i,c,k);e.css({width:j,height:k,left:l,top:m})})}



	// !Order button
	$( '#order .serif' )	.click( openORDER );
	$( '#order .sub-txt' )	.click( openORDER );
	
	function openORDER() { window.open( 'order.pdf' ); }
	
	$( '.contactdiv' ).click ( function() { window.location = 'mailto:info@cremedelacreme.be'; });
	
	// !flickdrag
	s.flickDragClick(
	{
		loop   : true,

		slideSpeed : 600,
		resetSpeed : 300,

		swipeDistance : .4,

		autoplay : true,
		autoplayAfterUse : true,
		autoplayInterval : 10000,
		autoplayInitWait : 20000,
		autoplayWaitAfterUse : 20000
	});
	
	
	
	// !Let it snow
	/*
	s.snowfall(
	{
		collection : null,
		
		minSize : 3,       
	    maxSize : 6,
		
		round : true,
		
		flakeCount : 200,
		maxSpeed : 10
	});
	
	// !Let it frost
	b.mousemove( function() 
	{
		s.stop( true ).fadeTo( 500, 1, function() 
		{
			s.fadeTo( 30000, .2 );
		});
	});
	b.trigger( 'mousemove' );
	*/


})(jQuery);
