/**
 * The custom JavaScript used for the added functionality.
 *
 * @author Dan Hensby <dan@betterbrief.co.uk>
 * @author Josh Holloway <josh@betterbrief.co.uk>
 * @version 1
 * @copyright 2010 Better Brief LLP
 *
 */

//Load jQuery
google.load("jquery", "1.5");
google.load("jqueryui", "1.8");

// effectively 'on document ready'
google.setOnLoadCallback(function() {

	(function($) {


		//Set variables for fade speeds of the hover background colours
		var inSpeed = 550,
			outSpeed = 550,
			$gameForm = null;

		//add a class to body so we can disble standard css
		$('body').addClass('js');


		$('#menu1').addClass('js').find('li:not(.current,.section)').hover(function() {
			$(this).children().stop().animate({
				backgroundColor: '#63669B'
			});
		}, function() {
			$(this).children(':not(.clicked)').stop().animate({
				backgroundColor: '#1076BC'
			});
		}).click(function() {
			var $this = $(this);
			$this.children().addClass('clicked').stop().animate({
				backgroundColor: '#63669B'
			}).parent().siblings().children('.clicked').animate({
				backgroundColor: '#1076BC'
			}).removeClass('clicked');
			$('#menu1 .current,#menu1 .section').children().animate({
				backgroundColor: '#1076BC'
			}).removeClass('current').removeClass('section');
			window.location = $this.children('a').attr('href');
		});

		/**
		 * Home Page
		 */
		if ($('#HomePage').length) {

			//SlideShow

			var $slideShow = $('#SlideShow'),
				$slides = $slideShow.find('li'),
				$currentSlide = $slides.first(),
				ssWidth = $slideShow.width(),
				inSpeed = 1000,
				outSpeed = 1000,
				inParams = {
					left: 0
				},
				outParams = {
					left: ssWidth
				},
				outCallback = function() {
					$(this).css('left', -ssWidth)
				};

		  $slides.show();

			//hide all of the slides that arent the current one
			$slides.not($currentSlide).each(outCallback);

			$slides.click(function(ev) {
				var $this = $(this);
				//dont do the click effect if we are in the current item
				//if (!$this.hasClass('current')){
					//get this items slide
					var $thisSlide = $this;
					$currentSlide.stop().animate(outParams, outSpeed, outCallback);
					$thisSlide.stop().animate(inParams, inSpeed);
					//set the new current slide and item
					$currentSlide = $thisSlide;
				//}
				//supress default action if not clicking on div.slide
				//return ( !!$(ev.target).closest('div.slide', ev.currentTarget).length );
			});

			//this function is called to start the automatic slide show
			function startRotator() {
				return setInterval(function() {
					var $next = $currentSlide.next();
					if (!$next.length) {
						$next = $slides.first();
					}
					$next.click();
				},6000);
			}

			//start the slideshow and record it's ID so we can disable it later
			var intervalID = startRotator();

			//when we hover on a slide we want to stop the auto rotation
			//$slides.hover(function() {
			//	//stop the auto rotation
			//	clearInterval(intervalID);
			//}, function() {
			//	//start the auto animation
			//	intervalID = startRotator();
			//	var $this = $(this);
			//});


			var highlightColours = {
				'lmm-awards': 'la-hover',
				'bridge-project': 'bp-hover'
			};

			function getHighlight(cls) {
				if(cls.indexOf('lmm-awards') !== -1) {
					return highlightColours['lmm-awards'];
				}
				else {
					return highlightColours['bridge-project'];
				}
			}

			$('div.lmm-awards, div.bridge-project').bind({
				mouseenter: function() {
					var color = getHighlight(this.className);
					$(this).stop(true, true).addClass(color, 250);
				},
				mouseleave: function() {
					var color = getHighlight(this.className);
					$(this).stop(true, true).removeClass(color, 250);
				}
			});

		}
		else if (false && document.body.id == 'GamePage' && ($gameForm = $('form.CurrentLevel')) && $gameForm.length) {
			var $replace = $('#replace');
			function doAJAXForm($form,$replaceEl,extraData) {
				if (extraData) {
					extraData = '&' + extraData;
				}
				else {
					extraData = '';
				}
				$.ajax({
					beforeSend: function(jqXHR,settings) {
						if ($form.data('isSending')) {
							return false;
						}
						$form.data('isSending',true);
					},
					complete: function(jqXHR,textStatus) {
						$form.data('isSending',false);
					},
					contentType: $form.attr('enctype'),
					data: $form.serialize() + extraData,
					error: function(jqXHR,textStatus,errorThrown) {
						console.log(jqXHR,textStatus,errorThrown);
						window.location = window.location;
					},
					success: function(data,textStatus,jqXHR) {
						//if (history && history.pushState) {
						//	history.pushState({},'',);
						//}
						$replaceEl.html(data);
					},
					type: $form.attr('method'),
					url: $form.attr('action')
				});
			}
			$('button',$gameForm).live('click',function() {
				var $this = $(this);
				doAJAXForm($gameForm,$replace,$this.attr('name') + '=' + $this.val());
				return false;
			});
			$('#Form_NextQForm').live('submit',function() {
				doAJAXForm($(this),$replace);
				return false;
			});
			$('#Form_FinishForm').live('submit',function() {
				var $this = $(this);
				doAJAXForm($this,$this);
				return false;
			});
		}
	})(jQuery);

});

