SWFAddress.addEventListener(SWFAddressEvent.CHANGE, swfAddressSwap);
_timer = null;
_project = null;
_sideNavTween = null;

// ---------------------------------------------------------------------------

function filterNav (className) {
	// reset content
	$('#sideNavMenu ul').css({top:0});
	// Dont animate IE or Safari, box model troubles
	if($.browser.msie || $.browser.safari) {
		if (className != 'all') {
			var activeLinks = $('#sideNavMenu ul li.'+className);
			$('#sideNavMenu ul li.'+className).show();
			$('#sideNavMenu ul li:not(.'+className+')').hide();														 
		}
		else {
			var activeLinks = $('#sideNavMenu ul li');
			$('#sideNavMenu ul li').show();		
		}
	}
	//Animate only for Firefox
	else {
		if (className != 'all') {
			var activeLinks = $('#sideNavMenu ul li.'+className);
			$('#sideNavMenu ul li.'+className).slideDown(400);
			$('#sideNavMenu ul li:not(.'+className+')').slideUp(400);														 
		}
		else {
			var activeLinks = $('#sideNavMenu ul li');
			$('#sideNavMenu ul li').slideDown(400);		
		}
	}
	//$('#sideNavMenu/ul').fadeIn('normal'); <- this breaks the filter results in Safari
	// build pagination
	var totalPages = Math.ceil(activeLinks.size() / 15);
	$('#sideNavPager').empty();
	if (totalPages > 1) {
		var result = 'Page: ';
		for (var i = 0; i < totalPages; i++) {
			var dist = -(315*i);
			var id = (i == 0) ? 'id="sideNavActivePager"' : '';
			result += '<a href="#" '+id+' onclick="sideNavScroll(this, '+dist+');return false;">'+(i+1)+'</a>';
			if (result.length > 0 && i < totalPages - 1) result += ' | ';
		}
		$('#sideNavPager').html(result);
	}
}
function loadPage ($anchor, category) {
	resetScroll();
	// get project name
	var href = $anchor.attr('href');
	var text = $anchor.text();
	//urchin time
	if (window.urchinTracker) urchinTracker(href);
	var parts = cleanSplit(href, '/');
	_project = parts[parts.length-1];
	// deactivate old active project
	$('#sideNavActiveLink').removeAttr('id');
	$('#sideNavActiveProjArrow').remove();
	// activate current project link
	$anchor.attr('id', 'sideNavActiveLink');
	$anchor.append('');
	// clear content
	$('#mainContent').css({backgroundImage:'url(_img/projects/'+_project+'/bg.jpg)'});
	$('#sideNavLegend').attr('className', category);
	$('#backToTop').css('display', 'none');
	$('#content').html('<p id="loadingPage"><img src="_img/shared/spinner.gif" width="14" height="14" /> Loading Page</p>');
	// load page
	clearTimeout(_timer);
	_timer = setTimeout('doLoadPage()', 100);
}
function doLoadPage(){
	var pg = 'projects/'+_project+'.php';
	$('#content').load(pg, null, function(){
		$.getScript("http://www.kurtnoble.com/test/_lib/sifractivate.js", function(){ 
			loadCallback();						
		});
	});
}
function loadCallback(){
	$('#backToTop').css('display', 'none');
	if($('#moviePlayer').size() > 0) {		
		var $thumbs =  $('#movieThumbs li');	
		var $default = $thumbs.filter('.default');
		
		var folder = $('#moviePlayer').find('.folder').html();
        var flashVars = { folder: folder};
		
		if($default.size() > 0) {
			$default.addClass('down');
			var flv = $default.find('a').attr('rel'); 
			var caption = $default.find('a img').attr('alt');
			jQuery.extend(flashVars, { defaultFLV: flv, defaultCaption: caption });
		}
		
        var params = { wmode: "transparent", allowscriptaccess: "always" };
        var attributes = { id: "moviePlayer" };
        swfobject.embedSWF("../_swf/featurettePlayer.swf", "moviePlayer", "679", "340", "9.0.0", false, flashVars, params, attributes);
				
		$thumbs.click(function() {
			$(this).addClass('down').siblings().removeClass('down');
			var flv = $(this).find('a').attr('rel'); 
			var caption = $(this).find('a img').attr('alt');
			// call externalInterface on flv player
			try {
				document.getElementById('moviePlayer').init(flv,caption);
			}
			catch(e) {
				// function not available
			}
			
			return false;
		});
	}	
	
	if($('#flvPlayer').size() > 0) {		
		var width = 679;
		var height = 420;
		
		if($('#flvPlayer').find('.width').size() > 0) {
			width = $('#flvPlayer').find('.width').html();
		}
		if($('#flvPlayer').find('.height').size() > 0) {
			height = $('#flvPlayer').find('.height').html();						   
		}
	
		var video = $('#flvPlayer').find('.video').html();
		var hasAudio = $('#flvPlayer').find('.hasAudio').html();
        var flashVars = { video: video, hasAudio: hasAudio};		
        var params = { wmode: "transparent", allowscriptaccess: "always" };
        var attributes = { id: "flvPlayer" };
        swfobject.embedSWF("../_swf/portfolioFLVPlayer.swf", "flvPlayer", width, height, "9.0.0", false, flashVars, params, attributes);
	}	
	
	if($('#swfPlayer').size() > 0) {		
		var width = 640;
		var height = 480;
		
		if($('#swfPlayer').find('.width').size() > 0) {
			width = $('#swfPlayer').find('.width').html();
		}
		if($('#swfPlayer').find('.height').size() > 0) {
			height = $('#swfPlayer').find('.height').html();						   
		}
	
		var file = $('#swfPlayer').find('.file').html();
        var flashVars = { };		
        var params = { wmode: "transparent", allowscriptaccess: "always" };
        var attributes = { id: "swfPlayerembed" };
        swfobject.embedSWF(file, "theSWF", width, height, "9.0.0", false, flashVars, params, attributes);
	}	
	
	
}
function sideNavScroll (anchor, y) {
	$('#sideNavActivePager').removeAttr('id');
	$(anchor).attr('id', 'sideNavActivePager');
	if (_sideNavTween != null) _sideNavTween.stop();
	
	$('#sideNavMenu ul').animate({ 
		top: y
	}, 600, "easeOutCirc" );
	_sideNavTween = new Tween($('#sideNavMenu ul').get(0), 'top');
	//_sideNavTween.start(null, y, 15, 'easeout');
	//$('#sideNavMenu/ul').animate({top:y}, 800, 'easeout', function(){
		//$(this).css('top', y);
	//});
}
function historyScroll ($anchor, y) {
	$('#sideNavActivePager').removeAttr('id');
	$anchor.attr('id', 'sideNavActivePager');
	if (_sideNavTween != null) _sideNavTween.stop();
	$('#sideNavMenu ul').animate({ top:y }, 600, "easeOutCirc" );
	_sideNavTween = new Tween($('#sideNavMenu ul').get(0), 'top');
	//_sideNavTween.start(null, y, 15, 'easeout');
	//$('#sideNavMenu/ul').animate({top:y}, 800, 'easeout', function(){
		//$(this).css('top', y);
	//});
}
// ---------------------------------------------------------------------------
// Old School Nav Scroller

function initScroll(){
	_nav = document.getElementById('sideNav');
	_scrolltimer = null;
	_scrolling = false;
	if (document.all) {
		if (document.documentElement && document.documentElement.clientHeight) {
			var doc = document.documentElement;
		} else {
			var doc = document.body;
		}
	} else {
		var doc = document;
	}
	doc.onscroll = startScroll;
	window.onresize = startScroll;
}
function resetScroll(){
	stopScroll();
	_nav.style.top = '0px';
	window.scrollTo(0,0);
}
function startScroll(){
	if ( ! _scrolling) {
		_scrolling = true;
		doScroll();
	}
}
function stopScroll(){
	_scrolling = false;	
	clearTimeout(_scrolltimer);
}
function doScroll(){
	if (document.documentElement && document.documentElement.clientHeight) {
		var windowHeight = document.documentElement.clientHeight;
	} else if (document.body && document.body.clientHeight) {
		var windowHeight = document.body.clientHeight;
	} else {
		var windowHeight = window.innerHeight;
    }
	if (window.pageYOffset) {
		var scrollTop = window.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		var scrollTop = document.documentElement.scrollTop;
	} else if (document.body && document.body.scrollTop) {
		var scrollTop = document.body.scrollTop;
	} else {
		scrollTop = 0;
	}
	if (windowHeight >= 550) {
		var yTarget = scrollTop;
		var yCurrent = parseInt(_nav.style.top) || 0;
		var y = yCurrent + (yTarget - yCurrent) / 2;
		_nav.style.top = y + 'px';
		if (Math.abs(yTarget - y) <= 2) {
			stopScroll();
			_nav.style.top = yTarget + 'px';
		} else {
			_scrolltimer = setTimeout('doScroll()', 50);
		}
	} else {
		stopScroll();
		_nav.style.top = '0px';
	}
}

function swfAddressSwap(event) {
	var seourl = 'work'+event.value;
	var $project = $("a[href='"+seourl+"']");
	var $links = $("#sideNavMenu a[href*='work/']:visible");
	if(seourl == 'work/') {
		$firstLink = $links.eq(0);
		loadPage($firstLink, $firstLink.parent().attr('class'));
	}
	else {
		// reset scrollbar if all are showing
		if($('#sideNavFilter select').val() == 'all') {
			var pageDistance = -315;
			var projectSpot = $links.index($project)+1;
			var pageNumber = Math.floor(projectSpot / 16);
			var scrollDistance = pageDistance * pageNumber;
			var $scrollLink = $('#sideNavPager a').eq(pageNumber);
			sideNavScroll($scrollLink, scrollDistance);
		}
		loadPage($project, $project.parent().attr('class'));
	}
	return false;
}

// ---------------------------------------------------------------------------

$(document).ready(function(){
	initScroll();
	// activate nav filter
	filterNav('all');
	$('#sideNavFilter select').change(function(){
		filterNav($(this).val());
	})
	// activate nav menu
	$('#sideNavMenu a').click(function(){
		var href = $(this).attr('href').substr(4);
		var text = $(this).text();
		SWFAddress.setValue(href);
		SWFAddress.setTitle('KNI - '+text+' - Work');
		//loadPage(this, this.parentNode.className);
		return false;
	});
	
	$('#sideNavMenu li a').hover(function(){
		$(this).prev().stop();
		$(this).prev().attr("style","opacity: 1;");
	}, function(){
		$(this).prev().animate({ 
        	opacity: "0.01"
      	}, 100);
	});
	
	$('#sideNavPager a').click(function() {
		return false;										
	});
	
	// back to top
	$('#backToTop').click(function(){//a[@href=#top]
		resetScroll();
		return false;
	});

});

// ---------------------------------------------------------------------------
