/*
	Vodafone JS
	========================================
	Version:	0.1
	Date:		19/08/09
	Website:	http://
	Author:		Matt Ravenhall
*/

var jsSOS = window.jsSOS || {};

/*	Makes the init run at page load
============================================*/
$(window).load(function() {
	jsSOS.initialize();
});

jsSOS.initialize = function() {
	jsSOS.Common().menuSlide();
	jsSOS.Common().hideShow();
	jsSOS.Common().initGallery();

}

/*	Common functions, that can be re-called at any point.
============================================*/
jsSOS.Common = function() {
	
	// Make all the modules even height
	var menuSlide = function() {
		var position = 0;
		function move() {
			position = position + 160;
			$("#slide li").find('a').animate({backgroundPosition: '0 -' + position + 'px'}, 500) 
		}
		setInterval(move, 3500);
	}
	
	var hideShow = function() { 
	 $("#sidebar ul li a").each(function() {
		$(this).click(function(){
			var id = $(this).attr('id');
			
			$("#sidebar ul li a").each(function() {
				$(this).removeClass('selected');
				$('div#article .section').removeClass('selected');
			});
			$(this).addClass('selected');
			$('div#article .' + id).addClass('selected');
			
			
			return false;
		});
	 });
	}
	
	var initGallery = function() {
		$("#mScroll").height($("#mScroll li").filter(":first").height());
		$("#mScroll li").each(function(i) {
			if (i != 0) {
				$(this).css({display: "none"});
			} else {
				$(this).addClass("selected");
			}
		});
		$("#mNext").click(function() {
			if (($("#mScroll li").index($("#mScroll .selected")) + 1) != $("#mScroll li").length) {
				$("#mScroll .selected").animate({opacity: 'hide'}, 500, function() {
					$("#mScroll").animate({height: $(this).next("li").height()}, 250, function() {
						$("#mScroll .selected").removeClass("selected").next("li").animate({opacity: 'show'}, 500).addClass("selected");
					});
				});
			}
			return false;
		});
		$("#mPrevious").click(function() {
			if ($("#mScroll li").index($("#mScroll .selected")) != 0) {
				$("#mScroll .selected").animate({opacity: 'hide'}, 500, function() {
					$("#mScroll").animate({height: $(this).prev("li").height()}, 250, function() {
						$("#mScroll .selected").removeClass("selected").prev("li").animate({opacity: 'show'}, 500).addClass("selected");
					});
				});
			}
			return false;
		});
		var animateInterval = setInterval(function() {
			animateGallery();
		}, 3000);
	}
	
	var animateGallery = function() {
		if (($("#mScroll li").index($("#mScroll .selected")) + 1) == $("#mScroll li").length) {
			$("#mScroll .selected").animate({opacity: 'hide'}, 500, function() {
				$("#mScroll").animate({height: $("#mScroll li").filter(":first").height()}, 250, function() {
					$("#mScroll .selected").removeClass("selected");
					$("#mScroll li").filter(":first").animate({opacity: 'show'}, 500).addClass("selected");
				});
			});
		} else {
			$("#mNext").click();
		}
	}

	
	//http://api.flickr.com/services/feeds/photos_public.gne?id=50083596@N03&lang=en-us&format=atom
	
	return {
		menuSlide : menuSlide,
		hideShow : hideShow,
		initGallery : initGallery
	}
}

