$(document).ready(function() {
	
$sbnContainer = $('#sidebar div.container ul');
$(function(){

      var $container = $('#itemContainer');
	
	/* Function to check for Firefox 5 since css-animations stopped working
		if firefox 5 then use jquery animationEngine instead of css.  */
	var ua = $.browser;
	if ( ua.mozilla && ua.version.slice(0,1) == "5" ) {
		var useAnimationType = 'jquery';	
	}else{
		var useAnimationType = 'best-available';
	}
	// Check for # in URL and see if it's an actual Target
	var hash=location.hash;
	hashClass = hash.replace('#','.');
	hashClass = hashClass.replace(/'/g, '');
	
	hash = hash.replace('#', '');
	hash = hash.replace(/-/g, ' ');
	
	
		if($(hashClass).size() > 0){ 
			//If target does exist then…
			
			$container.isotope({
				itemSelector: '.item',
				filter: hashClass,
				animationEngine: useAnimationType
			  });
		  
	
			$("li.cat-item a:contains('"+hash+"')").addClass('active');

	
			var highlighter = $('#sidebar .container');
			var index = $("li.cat-item a:contains('"+hash+"')").parent().index();
			var basePosition = 70; // The distance, in pixels, from the top to the first category link, 
			
			/* 'offset' = is the distance between the middle of two category navigation li's. 
			If the height of the button changes and the highlight starts acting weird look here! */
			var offset = 38; 
			var newPosition = (offset * index)+basePosition+"px";
			
			highlighter.css({background: "#262626 url('img/nav-highlight-bg.jpg') repeat-x 0px "+newPosition});

		}else if($('.item').size() > 0 ){ 
		//If target doesn't but there are items on page exist then highlight and activate the first icon! :D
			$('#sidebar .container').css({background: "#262626 url('img/nav-highlight-bg.jpg') repeat-x 0px 70px"});
			$("#sidebar li.cat-item:first a").addClass('active');
			$container.isotope({
				itemSelector: '.item',
				animationEngine: useAnimationType
		  	});	
		}else{
			
			$('#sidebar .container').css({background: "#262626 url('img/nav-highlight-bg.jpg') repeat-x 0px 3px"});
		}

 });
 
	 
	//Function when clicking the 
	$('.cat-item a').click(function(){	
	
	//If there are ".items" then it's the right page so disable the links.
	if($('.item').size() > 0 ){
		//There are items on the page... 
		
		//Check for target from other pages
		
	
		var selector = $(this).text();
		
		selector = selector.replace(/ /g, '-');
		selector = "."+selector.replace(/'/g, '');
		$('#itemContainer').isotope({ filter: selector });
		$(this).parent().siblings().children('a').removeClass('active', 'slow');
		$(this).addClass('active', 'slow');
		
		// Animate the position of the orange highlight.
		//Get the clicked elements hierarchy
		var highlighter = $('#sidebar .container');
		var index = $("li.cat-item").index($(this).parent("li"));
		var basePosition = 70; // The distance, in pixels, from the top to the first category link, 
		
		/* 'offset' = is the distance between the middle of two category navigation li's. 
		If the height of the button changes and the highlight starts acting weird look here! */
		var offset = 38; 
		var newPosition = (offset * index)+basePosition+"px";
		highlighter.stop().delay(100).animate({backgroundPosition: "0px "+newPosition},{duration:500});
		return false;
	}else{
		//There aren't any items on the page, override links to go back to homepage
		var selector = $(this).text();
		selector = selector.replace(/ /g, '-');
		
		//forward to page with new hash
		window.location = "../#"+selector;
		return false;
	}
	
		
	});
	
	
	$('.item a').click(function(){
		var myTarget = $(this).attr('href');
		var myTargetName = $(this).attr('title');
		var url = $(this).attr('name');
	 	videoTag = $(myTarget).parent().html();
		$.fancybox({
			'autoDimensions': false,
			'width': 637,
			'height': 357,
			'content' : '<iframe src="http://player.vimeo.com/video/' + url + '?title=0&amp;byline=0&amp;portrait=0&amp;color=ff9933&amp;autoplay=1;" width="640" height="360" frameborder="0" style="background:#000;"></iframe>',
			'padding': 0,
			'showCloseButton' : true,
			'enableEscapeButton': true,
			'scrolling': false,
			'title' : myTargetName,
			'showNavArrows' : true,
			'overlayShow':true,
			'titlePosition' : 'outside',
			'onComplete': function () {
                   $("#fancybox-inner").css({ 'overflow': 'hidden' });
                  // VideoJS.setupAllWhenReady();
                },
                'onClosed': function () { $("#fancybox-inner").empty(); }
			});
			return false;
		});
		
		
	
	
});
