$(document).ready(function() {
	
	//When the page loads...
	$(".tab-content").css("left", "-9999px"); // Hide all the content off to left of screen.
	
	// Address handler
    $.address.init(function(event) {
		
		var pathNames = event.pathNames;
		
		if (event.value != '/') {
			if(pathNames.length > 1) {
				// Do Nothing
			} else {
				var selection = $('a[rel=address:' + event.path + ']');
				var tabToSelect = $("li.tab a[href='" + selection.attr('href') + "']").parent();
				tabToSelect.addClass('active');
				var activeTab = selection.attr('href');
				$(activeTab).css({zIndex: 10, left: 0, opacity: 0});
				$(activeTab).fadeTo("slow", "1");
			}
		} else {
			$("ul.tabs li:first").click(); // Activate the first tab
		}

    }).change(function(event) {
		
		var selection = $('a[rel=address:' + event.path + ']');
		var tabToSelect = $("li.tab a[href='" + selection.attr('href') + "']").parent();
		$(tabToSelect).trigger('click');
		
		// update addthis path, use instant timeout to allow page to update first
		setTimeout(function(){appendToSharePath("#"+ event.value)}, 0); 
		
        if (event.value == '/') $("ul.tabs li:first").click();
    });
	
	//On Click Event
	$("li.tab").click(function(evt) {
		
		evt.preventDefault();
		evt.stopPropagation();
		
		if ($(this).hasClass('active')) { // if the clicked tab is already active, ignore the click
			return false;
		}
		
		var activeSubTabs = $("li.tab.active ul.subtabs"); // hide inactive subtabs

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content

		// hide all tab-dependent content
		$('.tab-dependent').addClass('none');
		// show relevant tab-dependent content
		var tabDependentElements = $('.tab-dependent[rel=' + activeTab + ']');
		var randomTabDependentIndex = Math.floor(Math.random() * tabDependentElements.length);
		$(tabDependentElements.get(randomTabDependentIndex)).removeClass('none');

		$("li.tab.active").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab-content").css("left", "-9999px"); // Hide all the content off to left of screen.
		$("object").each(function(){ 
			var obj = swfobject.getObjectById($(this).attr("id"));
			try {
				pauseAllVideos();
			} catch(err) {
				//console.dir(err)
			}
			
		});
		
		
		$(activeTab).css({zIndex: 10, left: 0, opacity: 0});
		$(activeTab).fadeTo("slow", "1"); //Fade in the active ID content, delay that a bit in order to let the movie load in
		
		var parentTabs = $(this).parents("li.tab");
		if (parentTabs.length) {
			parentTabs.addClass("active");
		}
		
		activeSubTabs.each(function() {
			if ($(this).parent('.active').length == 0) {
				var $this = $(this);
				$this.animate(
					{
						height: 0
					}, 
					'slow', 
					'swing', 
					function() {
						$(this).removeAttr('style');
					}
				);
			}
		});
		
		var subTabs = $(this).find('ul.subtabs');
		if (subTabs.length) {			
			subTabs.each(function() {
				var $this = $(this);
				var targetHeight = $this.height();
				$this.css({
					height: 0,
					overflow: 'hidden'
				});
				$this.animate({
					height: targetHeight
				}, 'slow');
			});
		}
		
		var childTabs = $(this).find('li.tab');
		//console.log('childTabs', childTabs);
		if (childTabs.length) {
			$(childTabs[0]).click();
		}
		
	});
	
	var defaultActiveTab = $('li.tab.active'); // find a tab marked with class 'active' in markup
	if (defaultActiveTab.length) {
		defaultActiveTab = defaultActiveTab.get(0);
		var defaultActiveTabActiveChildren = $(defaultActiveTab).find('li.tab.active')
		if (defaultActiveTabActiveChildren.length) {
			defaultActiveTab = defaultActiveTabActiveChildren.get(0);
		}
		
		$(defaultActiveTab).removeClass('active').click();
	}
	
});

