/* House of Dagmar main JS functions */

jQuery(document).ready(function($) {

$(window).load( function() {
	
	if($('#collection').length > 0) {
	
		$('.collection-text').jScrollPane( { showArrows: true, scrollbarMargin: 0 } );
		$('.collection-items').jScrollPane( { showArrows: true, scrollbarMargin: 0 } );
		
		//Settings
		var curItem = 0;
		var centerMargin = 0;
		var windowWidth = $(window).width();
		if($('#wpbody-content').length > 0) {
			windowWidth = $('#wpbody-content').width();
		}
		var windowHeight = $('#collections').height();
		var speed = 1100;
		var items = $.makeArray($('li.item-link'));
			
		//Function to scale collections to fit window
		function scaleCollection() {
		
			windowWidth = $(window).width();
			if($('#wpbody-content').length > 0) {
				windowWidth = $('#wpbody-content').width();
			}
			var totalBoxes = $('.collection-box').length;
			windowHeight = $('#collections').height();
			//Center navigation and position buttons
			$('#navigation').css('width',($('#navigation ul').innerWidth()+10)+'px');
			var footPosition = $('#navigation').position();
			$('#navi-next').css('right',(footPosition.left-20)+'px');
			$('#navi-previous').css('left',(footPosition.left-25)+'px');
			//Set total width to wrapper
			$('#collections-size-holder').css('width',(windowWidth*totalBoxes)+'px');
			//Center single images and videos
			$('.collection-video').each(function() {
				if($(this).height() < 530) {
					$(this).css('margin-top',((windowHeight-$(this).height())/2)+'px');
				}
			});
			//Update scroller position
			$('#collections').trigger( 'goto', [ curItem ] );
			//Adjust marker position
			var position = $(items[curItem]).position();
			$('#navigation-marker').css('left',position.left);
		
		}
		
		//Resize now and bind event
		scaleCollection();
		$(window).resize(function() { scaleCollection() });
		
		//Init collection scroll
		$('#collection').serialScroll({
			target:'#collections',
			items:'.collection-box',
			next:'#navi-next',
			prev:'#navi-previous',
			axis:'x',
			cycle:true,
			lock:false,
			stop:true,
			navigation:'#navigation li a',
			duration:speed,
			force:true,
			lazy:false,
			constant: false,
			easing:'easeOut',
			jump:true,
			onBefore:function( e, elem, $pane, $items, pos ) {
				curItem = pos;
				if(pos == 0) {
					this.offset = 0;
				} else {
					centerMargin = ((windowWidth-$(elem).width())/2);
					this.offset = -centerMargin;
				}
				$('li.active').removeClass('active');
				$(items[pos]).addClass('active');
				var position = $(items[curItem]).position();
				$("#navigation-marker").stop().animate({ left: position.left, width: $(items[curItem]).outerWidth({margin: false}) }, speed, 'easeOut' );
			}
		});
		
		$(document).keyup(function (e) {
			if(e.which == 37) {
				$('#collections').trigger('prev');
			}
			if(e.which == 39) {
				$('#collections').trigger('next');
			}
		});
		
		$('#startlogo').stop().animate({opacity: 1.0}, 500).fadeOut(1000);
		
	}
});

//$('#startlogo').stop().animate({opacity: 1.0}, 500).fadeOut(1000);

});