/**
 * Butlins Big Weekends
 *
 * Contains:
 * - Gallery animation script
 * - In-page smooth scroller
 *
 * @author Chris Blackburn <chrisb@de-construct.com>
 * @version $Id: bigweekends.js 17368 2008-08-27 18:58:58Z CBlackburn $;
 */

var BigWeekends = function() {
	/**
	 * Flag identifying whether there is currently a gallery animation 
	 * in progress.
	 * @var boolean
	 */
	var _gallery_animating = false;
	
	/**
	 * URL for ajax script to provide image ratings.
	 * @var string
	 */	
	var _image_vote_url = '/photo/vote/';



	/**
	 * Function that initialises event listeners on the gallery back and 
	 * forwards handlers.
	 *
	 * @return boolean
	 */
	function initGallery()
	{	
		$('a.gallery-right').click(function() {
				
			BigWeekends.moveGallery(this, 'forward');
			
			return false;
		});
		
		$('a.gallery-left').click(function() {
		
			BigWeekends.moveGallery(this, 'backward');
		
			return false;
		});		
	}
	
	
	
	/**
	 * Function that moves through the gallery.
	 *
	 * @param element element
	 * @param string direction
	 */	
	function moveGallery(element, direction)
	{
		/**
		 * Default duration in milliseconds for the movement animation to 
		 * take.
		 * @var int
		 */
		var defaultSpeed = 900;
		
		/**
		 * Default width in pixels which the gallery will move.
		 * @var int
		 */		
		var defaultWidth = 543;
		
		/**
		 * Total width of all the images in the gallery.
		 * @var int
		 */		
		var width = 0;	
		
		
		$(element).parent().find('.gallery-content-list ul').each(function() {
		    //alert("width:" + $(this).innerWidth())
			width += $(this).innerWidth() +1;
		});		
	
		if(!BigWeekends._gallery_animating) {
			
			BigWeekends._gallery_animating = true;
			
			var imageList = $(element).parent().find('.gallery-content');		
			var currentLeft = imageList.css('left') == 'auto' ? 0 : parseInt(imageList.css('left').replace('px', ''));
				
			switch(direction) {
			
				case 'forward':
												
					var leftVal = currentLeft - defaultWidth;
					var leftPane = currentLeft - (defaultWidth*2);
					//alert("leftVal:" + leftVal);
					//alert("leftVal:" + leftVal);
					//alert("currentLeft:" + currentLeft);
					//alert("width:" + width);
					/* 
					 * If the next movement would throw us off the end of 
					 * the gallery peg the movement back down to the maximum.
					 */
					if(Math.abs(leftPane) > width) {
									
						leftVal = parseInt('-' + (width - defaultWidth));
					}
					//alert("leftPane:" + leftPane + "-leftVal:" + leftVal+ "-width:" + width + leftVal+ "-defaultWidth:" + defaultWidth)		
					//alert(Math.abs(leftPane));
					break;
				case 'backward':
				
					var leftVal = currentLeft + defaultWidth;
					var leftPane = Math.abs(currentLeft) - defaultWidth;				
				
					/* 
					 * If the next movement would throw us off the end of 
					 * the gallery peg the movement back down to the minimum.
					 */				
					if(leftPane < 0) {
					
						leftVal = 0;
					}	
								
					break;	
			}

			/* 
			 * Adjust the amount of time the animation should take based on 
			 * the width of the movement.
			 */
			var time = Math.abs(defaultSpeed * ((currentLeft - leftVal) / defaultWidth));
						//alert("left:" + leftVal)	
			imageList.animate({
				left: leftVal + 'px'
			},
			time,
			'swing',
			function() {
			
				BigWeekends._gallery_animating = false;
			});
		}	
	}
	
	
	
	/**
	 * Function that grabs each in-page link and replaces the onclick
	 * handler with a script-based smooth scroller.
	 */
	function initInPageLinks()
	{
		$('a.in-page').click(function() {
		
			$('html, body').animate({scrollTop: 0}, 'slow'); 
			
			return false;
		});
	}
	
	
	
	/**
	 * Function that grabs each add-element link and replaces the onclick
	 * handler which clones an element to add.
	 */
	function initAddElements()
	{
		$('a.add-element').click(function() {
			
			var sourceElement = $(this).parents('.add-element-container').find('.adding-element:first');
			
			sourceElement.after(sourceElement.clone());
									
			return false;
		});
	}
	


	/**
	 * Function that adds event listeners to the image ratings lists. 
	 * Provides anonymous functions for event handling.
	 */	
	function initRatings()
	{
		$('ul.rating').find('li').each(function() {
		
			$(this).mouseover(function() {
			
				$(this).prevAll('li').andSelf().addClass('mouseover');
				
				return false;
			});
			
			$(this).mouseout(function() {
			
				$(this).prevAll('li').andSelf().removeClass('mouseover');
				
				return false;
			});	
			
			$(this).click(function() {
			
				/* needs to get correct image identifier from CS. */			
				var vote = $(this).prevAll('li').andSelf().length;
				$.post(
					BigWeekends.imageVoteUrl,
					{rating: vote, image: 123}
				);
			});
		});
	}



	return {
		initGallery: initGallery,
		moveGallery: moveGallery,
		initInPageLinks: initInPageLinks,
		initAddElements: initAddElements,
		imageVoteUrl: _image_vote_url,
		initRatings: initRatings
	};
}();


$(document).ready(function() { 
	BigWeekends.initGallery(); 
	BigWeekends.initInPageLinks();
	BigWeekends.initAddElements();
	BigWeekends.initRatings();
	if (jQuery.fn.pngFix) $(document).pngFix();
});