var currentTab, newHash, safariCurrentTab;
var seen = false;

var slideWidth = 895;
var transitionDuration = 500;
var currentSlide = -1;
var numSlides;
var slideNumNav;

var currentVideoId;

jQuery().ready(function() {
	
	numSlides = jQuery("#slider").children().length;
	
	jQuery("#slideBox").css("overflow", "hidden");
	
	slideNumNav = jQuery("#slideNumberNav a");
	
	// Initialize history plugin.
	// The callback is called at once by present location.hash.
	if ($.browser.safari) {
		// Safari is stubborn - so am I
		currentSlide = 1;
		slideContent("position", 1);
	} else {
		$.history.init(updateState);
	}
	
	jQuery("#slideRight").click(function() {
		slideContent("right", slideWidth);
		return false;
	});

	jQuery("#slideLeft").click(function() {
		slideContent("left", slideWidth);
		return false;
	});

	jQuery("#slideNumberNav a").click(function() {
		if (!jQuery(this).hasClass("selected")) {
			jQuery("#slideNumberNav a").removeClass("selected");
			jQuery(this).addClass("selected");
			currentSlide = jQuery(this).attr("rel");
			slideContent("position", jQuery(this).attr("rel"));
		}
	});
	
	updateNavArrows();
	
	enableVideoHotspots();
	
	$("area[rel*='external']").click(function() {
		pageTracker._trackPageview('/outbound/' + $(this).attr('href'));
	});
});

function slideContent(direction, distance) {
	if (direction == "right") {
		jQuery("#slider").animate({
			"right": "+=" + distance + "px"
		}, transitionDuration);
		currentSlide++;
	} else if (direction == "left") {
		jQuery("#slider").animate({
			"right": "-=" + distance + "px"
		}, transitionDuration);
		currentSlide--;
	} else if (direction == "position") {
		if (currentSlide != distance) {
			currentSlide = distance;
		}
		// convert distance into pixel distance
		distance = (distance - 1) * slideWidth;
		jQuery("#slider").animate({
			"right": distance
		}, transitionDuration);
	}
	updateSlideNavNumber();
	updateNavArrows();
}

function updateSlideNavNumber() {
	jQuery("#slideNumberNav a").removeClass("selected");
	jQuery(slideNumNav[currentSlide - 1]).addClass("selected");
	
	if (location.hash != "#" + jQuery(slideNumNav[currentSlide - 1]).attr("rev")) {
		location.hash = "#" + jQuery(slideNumNav[currentSlide - 1]).attr("rev");
	}
}

function updateNavArrows() {
	if (currentSlide == numSlides) {
		jQuery("#slideLeft").show();
		jQuery("#slideRight").hide();
	} else if (currentSlide == 1) {
		jQuery("#slideLeft").hide();
		jQuery("#slideRight").show();
	} else {
		jQuery("#slideLeft").show();
		jQuery("#slideRight").show();
	}
}


/*** History and location hash ***/

// updateState function
// This function is called when:
// 1. after calling $.historyInit();
// 2. after calling $.historyLoad();
// 3. after pushing "Go Back" button of a browser
function updateState(hash) {
	if(!hash && !seen) {
		// force first category, first time only*
		$.history.load($("#slideNumberNav a:first").attr("rev"));
		seen = true;
	}
	
	setCurrentTab(hash);
	trackView(hash);
	
	updateSlideNavNumber();
}

function trackView(hash) {
	if (hash) {
		hash = hash.replace("#", "");
		locationPath = document.location.pathname;
		if (locationPath.lastIndexOf("/") != (locationPath.length - 1)) {
			locationPath = locationPath + "/";
		}
		pageTracker._trackPageview(locationPath + hash);
	}
}


function setCurrentTab(t) {
	t = t.replace('#', '');
	currentTab = t;
	
	// show requested tab
	if (t.length) {
		slideNumNav = jQuery("#slideNumberNav a");
		for (var x = 0; x < slideNumNav.length; x++) {
			if (jQuery(slideNumNav[x]).attr("rev") == currentTab) {
				currentSlide = x+1;
				slideContent("position", x+1);
				break;
			}
		}
	}
}

function enableVideoHotspots() {
	jQuery('#watchVideo').click(function() {
		showVideoPlayer(jQuery(this).attr("rel"));
		return false;
	});
	
}

function showVideoPlayer(videoId)
{
	
	currentVideoId = videoId;
	
	var overlay = document.createElement('div');
	overlay.setAttribute('id', 'overlay');
	jQuery(document.body).append(overlay);
	jQuery(overlay).click(closeVideoPlayer);
	
	jQuery('#videoPlayer').show();
	jQuery('#videoPlayer a.close').click(closeVideoPlayer);
	
	pageTracker._trackPageview('/jaclyn_smith/video/' + currentVideoId);
	
	return false;
}

function closeVideoPlayer()
{
	jQuery('#videoPlayer').hide();
	jQuery('#overlay').remove();
	
	return false;
}