jQuery(document).ready(function($){



	/*---------------------------------------------------
	Passage
	---------------------------------------------------*/
	$.fn.passage = function () {
	    return this.each(function () {
	
			var $links = $(this).find('a:not(.external)'),
				$nav = $(this);
				anchor = location.hash;

			travel($links.filter('.home'), 0);
			
			if (anchor && anchor != '#home') travel($links.filter('.'+anchor.slice(1)), 1000); 
			
		
			
			$links.click( function(event) {
				event.preventDefault();
				travel(this, 1000);
			});
			
			
			function travel(target, spped) {

				var $dad = $(target).parent(),
					$current = $dad.siblings('.selected').eq(0);
			
				
				
				if( !$nav.hasClass("freezed") ) {
					$current.removeClass("selected");
					$dad.addClass("selected");
				}
				
				$nav.addClass('freezed');
				
				var where = $(target).attr("href");

				//oh well...		
				if( $.browser.msie && $.browser.version.substr(0,1)=="8" ) {

					$('#wrapper').filter(':not(:animated)').animate({
		                scrollLeft : '-=' + ( $(where).offset().left + ( -1 * ($(window).width() / 2 - 425) ) )
		            }, spped, function() { $nav.removeClass('freezed'); });
		
				} else {
				
					$('#wrapper').filter(':not(:animated)').scrollTo(where, spped, {
						axis: 'x',
						offset: { left: -1 * ($(window).width() / 2 - 425), top: 0 },
						onAfter: function (){  $nav.removeClass('freezed'); /*location.hash = where;*/}
					});
				
				} 
			
			
			}
	
	    });  
	};
	
	$("#nav").passage();
	
	
	$('.carousel_list a').append('<strong></strong>').find('strong').fadeTo(10,0);
	
	$('.carousel_list a').hover(function () {
	    $(this).find('strong').stop().fadeTo(250, 0.4);
	}, function () {
	    $(this).find('strong').stop().fadeTo(400, 0);
	});
	

	
	
	
	
	
	/*---------------------------------------------------
	Tabs
	---------------------------------------------------*/

	$.fn.tabify = function () {
	    return this.each(function () {
	
			var $tabWrapper = $(this); 
	
			//switches to the "target" tab
			function switchTab(target) {
				var targetpanel = $(target).attr("href");
		
				if ( !$(target).hasClass("selected") ) {
			
					var $old = $(target).siblings(".selected"),
						oldpanel = $old.eq(0).attr("href");
			
					//$(oldpanel).hide(); $(targetpanel).show();
					
					$tabWrapper.filter(':not(:animated)').scrollTo(targetpanel, 500, {
						axis: 'y',
						onAfter: function (){ $old.eq(0).removeClass("selected"); $(target).addClass("selected"); }
					});
					
				}
			}
	
	
			//bind to tab clicks
			var $links = $tabWrapper.siblings('.tabs').find('> a');
			
			$links.click( function(event) {
				event.preventDefault();
					switchTab(this);
			});
			
	
	    });  
	};
	$("#tabbed .container").tabify();
	

	/*---------------------------------------------------
	Carousel
	---------------------------------------------------*/
	
	$.fn.Carousel = function () {

	    function repeat(str, num) {
	        return new Array( num + 1 ).join( str );
	    }

	    return this.each(function () {
	        var $wrapper = $(this).css('overflow', 'hidden'),
	            $slider = $wrapper.find('> div.view'),
	            $items = $slider.find('> ul'),
	            $single = $items.filter(':first'),

	            singleWidth = $single.outerWidth(), 
	            visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesnt include padding or border
	            currentPage = 1,
	            pages = Math.ceil($items.length / visible);            


				$slider.css('width', (visible * singleWidth * pages + pages) + 'px');

	        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
	        if (($items.length % visible) != 0) {
	            $slider.append(repeat('<ul class="empty" />', visible - ($items.length % visible)));
	            $items = $slider.find('> ul');
	        }

				


	        // 2. Set the left position to the first 'real' item
	        $wrapper.scrollTo($items.filter(':first'), {axis:'x'});

	        // 3. paging function
	        function gotoPage(page) {
				
	            var dir = page < currentPage ? -1 : 1,
	                n = Math.abs(currentPage - page),
	                left = singleWidth * dir * visible * n;

				if (page > 0 && page <= pages ) {

					var nextEl =  '';
					if (dir == 1) nextEl = $items.eq(currentPage-1).next();
					else nextEl = $items.eq(currentPage-1).prev();
					
			
					
					//oh well
					if( $.browser.msie && $.browser.version.substr(0,1)=="8" ) { 

						$wrapper.filter(':not(:animated)').animate({
			                scrollLeft : '+=' +  singleWidth * dir
			            }, 1000, function() { currentPage = page; });

					} else {
					
		         	    $wrapper.filter(':not(:animated)').scrollTo(nextEl, 1000, {
							axis: 'x', onAfter: function (){ currentPage = page; }
						});

					}
	
	        	}

	            return false;
	        }

	        $wrapper.after('<div class="nav"><a href="#" class="back">&lt;</a><a href="#" class="forward">&gt;</a></div>');

			// bind to the forward and back buttons
	        $wrapper.siblings('.nav').find('a.back').click(function(event) {
				event.preventDefault(); 
	            return gotoPage(currentPage - 1); 

	        });

	        $wrapper.siblings('.nav').find('a.forward').click(function(event) {
				event.preventDefault(); 
	            return gotoPage(currentPage + 1);  
	        });

	        // create a public interface to move to a specific page
	        $(this).bind('goto', function (event, page) {
	            gotoPage(page);
	        });
	    });  
	};


	$('#video_list, #photo_list').Carousel();
});

