/* IE support of Array.indexOf... non-existent lol */
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}


$(document).ready(function(){

	/* Lock widths to their auto generated sizes before we hide second level menus */
	$("li.top").each(function(){
		$(this).css('width', $(this).width());
	});
	
	/* Hide second level menus */
	$("ul.second").each(function(){
		$(this).data('height', $(this).height());

		$(this).css({
			height: 0,
			marginBottom: 0,
			paddingTop: 0,
		});
	});

//	$("ul.second").hide();

/*
	$("li.top a").hover(
		function(){
			$(this).parent().parent().find("ul.second").show(250);
		},
		function(){
			$(this).parent().parent().find("ul.second").hide(250);
		}
	);

*/
/*
	$("li.top").mouseenter(function(){
		$(this).addClass('selected').find("ul.second").show();
	});

	$("li.top").mouseleave(function(){
		$(this).removeClass('selected').find("ul.second").hide();
	});
*/

	$("li.top").mouseenter(function(){
		$(this).addClass('selected').find("ul.second").each(function(){
			$(this).stop().animate({
				height: $(this).data('height'),
				marginBottom: 5,
				paddingTop: 5,
			}, 100);
		});
	});

	$("li.top").mouseleave(function(){
		$subnav = $(this).find("ul.second");
		
		if ($subnav.length > 0) {
			$subnav.stop().animate({
				height: 0,
				marginBottom: 0,
				paddingTop: 0,
			}, 100, function(){
				$(this).parent().removeClass('selected');
			});
		} else {
			$(this).removeClass('selected');
		}
	});


/* Gallery */

	if (($("#gallery").length > 0) && (typeof(images) != "undefined")) {
	
		// Set inital image
		$("img#image").attr('src', images[0]);
		
		// Hide arrows if only one image
		if (images.length == 1) {
			$("#left-arrow").hide();
			$("#right-arrow").hide();
		}
			
		if($("ul#thumblist").length > 0) {
		
			// Generate thumbnails
			for (var i=0; i<images.length; i++) {
				$li = $('<li><img class="thumb" src="' + images[i].replace('.jpg','-tn.jpg') + '" alt="thumb-' + i + '"></li>');
				if (i == 0) { $li.addClass('selected'); }
				$("ul#thumblist").append($li);
			}
		}
		
		// Timer event
		function nextImage() {
			var imageSrc = $("img#image").attr('src');
			var index = images.indexOf(imageSrc);
			index++;
			changeImage(index);
		}		
		
		// Kick off timer
		var	timer = setInterval(nextImage, 5000);	
		
		/* Thumbnail is clicked */
		$("img.thumb").click(function(){
			/* Cancel the timer */
			clearTimeout(timer);

			var imageSrc = $(this).attr('src').replace('-tn','');
			var index = images.indexOf(imageSrc);
			changeImage(index);
		});
		
		/* Left arrow clicked */
		$("#left-arrow").click(function(){
			/* Cancel the timer */
			clearTimeout(timer);

			var imageSrc = $("img#image").attr('src');
			var index = images.indexOf(imageSrc);
			index--;
			changeImage(index);
		
		});

		/* Right arrow clicked */
		$("#right-arrow").click(function(){
			/* Cancel the timer */
			clearTimeout(timer);

			var imageSrc = $("img#image").attr('src');
			var index = images.indexOf(imageSrc);
			index++;
			changeImage(index);
		
		});
	
		/* Keydown next/previous Image */
		$(document).keydown(function(event){
			/* Cancel the timer */
			clearTimeout(timer);

			/* Next */
			if ((event.which == 39) || (event.keyCode == 39)) {
				var imageSrc = $("img#image").attr('src');
				var index = images.indexOf(imageSrc);
				index++;
				changeImage(index);
			}
			/* Previous */
			if ((event.which == 37) || (event.keyCode == 37)) {
				var imageSrc = $("img#image").attr('src');
				var index = images.indexOf(imageSrc);
				index--;
				changeImage(index);
			}
		});

		/* Left thumb-arrow clicked */
		$("#left-scroll-arrow").click(function(){
			/* Cancel the timer */
			clearTimeout(timer);


			var leftLimit = Math.floor(images.length / 6) * -960;
			var newThumbLeft = thumbLeft + 960;
			if (newThumbLeft > 0) { newThumbLeft = leftLimit; }
				
			thumbLeft = newThumbLeft;
			$("ul#thumblist").stop().animate({
				'left': (thumbLeft + 20) + 'px'
			}, 500);
		});

		/* Right thumb-arrow clicked */
		$("#right-scroll-arrow").click(function(){
			/* Cancel the timer */
			clearTimeout(timer);

			var leftLimit = Math.floor(images.length / 6) * -960;
			var newThumbLeft = thumbLeft - 960;
			if (newThumbLeft < leftLimit) { newThumbLeft = 0; }
			
			thumbLeft = newThumbLeft;
			$("ul#thumblist").stop().animate({
				'left': (thumbLeft + 20) + 'px'
			}, 500);
		});

		var thumbLeft = 0;
		
		/* Change Image function */
		function changeImage(index) {

			if (index < 0) { index = images.length -1;	}
			if (index >= images.length) { index = 0; }
			
			var imageSrc = images[index];
			var thumbSrc = images[index].replace('.jpg','-tn.jpg');
			
			$("img.thumb").parent().removeClass('selected');
			
			$('img.thumb[src="' + thumbSrc + '"]').parent().addClass('selected');
				

			$("img#image").attr('src', imageSrc);
/*
			$("img#image").fadeOut('fast', function() {
				$(this).attr('src', imageSrc).fadeIn('fast');
			});
*/		

			/* Find new thumb position */
			var newThumbLeft = Math.floor(index / 6) * -960;

			// Already heading to the right position so return
			if (newThumbLeft == thumbLeft) {
				return;

			// Stop everything and start moving to new position
			} else {
				thumbLeft = newThumbLeft;
				$("ul#thumblist").stop().animate({
					'left': (thumbLeft + 20) + 'px'
				}, 500);
			}
		}
	}





/* Misrepresentations act */
	
//	$("footer#act").hide();
	$("#act").toggle();

	$("a#show-act").click(function(event){
		event.preventDefault();
		$("#act").slideToggle(250);
		$("html, body").animate({ scrollTop: 10000 }, 'slow'); 
	});
});
