$(document).ready(function(){
	
	// Enables bookmarking through URL anchors
	var bookmark = window.location.hash;
	bookmark = bookmark.match(/[^#]+/);
	Hash.value = bookmark;
	if (bookmark != null) {
		$.ajax({
			type: 'GET',
			url: bookmark + '.php',
			dataType: 'html',
			data: { "async": 1 },
			success: function(data) {
				$('#content').html(data);
				Stats.hit('http://tribecasmiles.com/' + bookmark + '.php');
			}
		});
	}
	else {
		Stats.hit('http://tribecasmiles.com/home.php');
	}
	
	// TODO document.onclick should close menu as well as clicking a link (for iPads)
	
	// Drop down navigation menu
	function swapImage(menu) {
		var image = $('img', menu);
		var currentImg = $(image).attr('src');
		$(image).attr('src', $(image).attr('hover'));
		$(image).attr('hover', currentImg);
	}
	$("ul#nav_container li").hover(
		function(){
			swapImage(this);
			$('ul:first',this).show();
		}, 
		function(){
			swapImage(this);
			$('ul:first',this).hide();
		}
	);
	
	// Converts all links to AJAX requests
	$('a, area').click(function(e) {
		if ($(this).attr('class') != "nofollow") {
		  e.preventDefault();
		  // So that the menu specific ajax call doesn't pick this up
		  e.stopPropagation();
		  var href = $(this).attr('href');
		  $.ajax({
			  type: 'GET',
			  url: href,
			  dataType: 'html',
			  data: { "async": 1 },
			  success: function(data) {
				  Hash.value = remExt(href);
				  window.location.hash = remExt(href);
				  $('#content').html(data);
				  var url = 'http://tribecasmiles.com/' + href;
				  Stats.hit(url);
			  }
		  });
		}
	});
	
	// AJAX page requests (for drop down menu)
	$('#nav_container .sub_menu li').click(function(e) {
		e.preventDefault();
		var href = $('a', this).attr('href');
		$.ajax({
			type: 'GET',
			url: href,
			dataType: 'html',
			data: { 'async': 1 },
			success: function(data) {
				Hash.value = remExt(href);
				window.location.hash = remExt(href);
				$('#content').html(data);
				var url = 'http://tribecasmiles.com/' + href;
				Stats.hit(url);
			}
		});
	});
	
	function remExt(filename) {
		return filename.split('.').reverse().slice(1).reverse().join('.');
	}
	
});

Stats = {
	hit : function(url) {
		try {
			var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
			piwikTracker.trackLink(url, 'link');
		} catch(err) {}
	}
}

Hash = {
	timer : null,
	value : "",
	poll : function() {
		var hash = window.location.hash.match(/[^#]+/);
		// Javascript doesn't seem to be able to compare two different type object strings correctly
		// Hard to explain, but basically we need to cast 'hash' as a string for it to compare correctly.
		// Very weird. The next line of code converts 'hash' to a string.
		hash += "";
		if (hash != Hash.value && hash != 'null') {
			$.ajax({
				type: 'GET',
				url: hash + ".php",
				dataType: 'html',
				data: { "async": 1 },
				success: function(data) {
					$('#content').html(data);
				}
			});
			Hash.value = hash;
		}
	}
}


var DELAY = 6500; // Delay in milliseconds between image swaps
$(document).ready(function() {
	var rotateImagesTimer = setInterval(rotateImages, DELAY);
	
	function rotateImages() {
		var currentImage = $('#slideshow_container div.current');
		var nextImage = currentImage.next();
		if (nextImage.length === 0) {
			nextImage = $('#slideshow_container div:first');
			clearInterval(rotateImagesTimer);
			return null;
		}
			
		currentImage.removeClass('current').addClass('previous');
		
		if ($(nextImage).hasClass('video')) {
			$('#video_container').delay(1000).show();
		}
		else {
			$('#video_container').hide();	
		}
		
		nextImage.css({ opacity: 0.0 }).addClass('current').animate({ opacity: 1.0 }, 1000,
			function() { currentImage.removeClass('previous'); }
		);
	}
	
	$('#video_container').click(function() {
		clearInterval(rotateImagesTimer);
	});
	
	// Sets up polling for URL hashes
	Hash.timer = setInterval(function() { Hash.poll(); }, 60);
		
});

