(function($){	
	Cufon.replace('div#navigation ul li a', {hover: true, autoDetect: true});
	Cufon.replace('div#visualSmall h1', {autoDetect: true});
	
	$(function(){
		
		var currentTab = !document.location.hash ? '#chapter1' : document.location.hash;
		$('div[id^="chapter"]').hide();
		$('div'+currentTab).show();
		$('div.pageTabs ul li a[rel="'+currentTab+'"]').addClass('active');
		
		$('div.pageTabs ul li a').bind('click', function(){
			$('div.pageTabs ul li a[rel="'+currentTab+'"]').removeClass('active');
			currentTab = $(this).attr('href');
			$('div[id^="chapter"]').hide();
			$('div.pageTabs ul li a[rel="'+currentTab+'"]').addClass('active');
			$('div'+currentTab).show();
			return false;
		});
		
		$("a[rel='gallery']").colorbox({slideshow:true, slideshowSpeed: 5000, height:"95%"});
		
		$('#searchQuery').bind('focus',
			function(){
				elem = $(this);
				if(elem.val() == searchDefault){
					elem.val('');
				}
			}
		).bind('blur',
			function(){
				if(elem.val() == ''){
					elem.val(searchDefault);
				}
			}
		);
		
	});
	
	EYE.accordion = function() {
		var switchTo = function() {
			var parentEl = $(this).blur().closest('ul');
			parentEl.find('ul').hide().eq(parentEl.find('>li>a').index(this)).show();
			return false;
		};
		return {
			init: function() {
				$('ul.accordion li a.active').next().show();
				$('ul.accordion').find('>li>a').bind('click', switchTo);
			}
		};
	}();
	EYE.register(EYE.accordion.init, 'init');

	EYE.slideshow = function() {
		var current = false, newCurrent = 0, total, timer;
		var show = function() {
			if(current) {
				$('#visualBig .visualItem').eq(current).stop()
					.animate(
						{
							'opacity': 0
						},
						600,
						function(){
							$(this).css('display','none');
						});
			}
			current = newCurrent;
			$('#visualBig .visualItem').eq(current).css('display','block').stop().animate({'opacity': 1}, 600);
			$('#visualNavigation a').removeClass('active').eq(current).addClass('active');
					timer = setTimeout(goTo, slideshowSpeed*1000);
		};
		var goTo = function(ev) {
			clearTimeout(timer);
			if (ev) {
				var el;
				if ((el = $(ev.target).closest('a')).size() == 1) {
					newCurrent = $('#visualNavigation a').index(el[0]);
				}
			} else {
				newCurrent = current + 1;
			}
			if (newCurrent >= total) {
				newCurrent = 0;
			} else if (newCurrent <0 ) {
				newCurrent = total-1;
			}
			show();
		}
		return {
			init: function() {
				if (document.getElementById('visualBig')) {
					var html = [];
					total = $('#visualBig .visualItem').each(function(nr) {
						html.push('<li><a href="#" title="');
						html.push($(this).find('h3').text());
						html.push('">');
						html.push(nr+1);
						html.push('</a></li>');
					}).css({
						display: 'none',
						opacity: 0
					}).size();
					$('#visualNavigation').html(html.join('')).bind('click', goTo);
					show();
				}
			}
		};
	}();
	EYE.register(EYE.slideshow.init, 'init');
})(jQuery);
