

var publication = {
	busy:false,
	holder:null,
	init:function(){
		publication.holder = $('#right-col').find('.cover');
		publication.holder.find('.ir').click( function() {
			publication.navigate($(this));
			return false;
		});
	},
	navigate: function(el){
		if(publication.busy == true) return false;
		publication.busy = true;

		var direction = 'next';
		if( el.hasClass('left') )
			direction = 'previous';

		$.getJSON( '?ajax=true', { direction: direction, item: $('#right-col .cover').attr('id') }, function( data ) {
			if(data) {
				var holder = publication.holder;
				holder.find('img').attr('src', '../uploads/publications/thumb/' + data.file );
				holder.find('h1').html( data.title );
				holder.find('p.faint').html( data.subtitle );
				holder.find('a').attr('href', data.link );
				holder.attr('id', data.id );
			}
			publication.busy = false;
		})
	}
}

var slider = {
	busy:false,
	curr:1,
	max : 0,
	timer:null,
	init:function(){
		slider.max = $('#slidenav').find('a').length;
		if($('#slidenav li.act a').hasClass('content0'))
			$('#bubble').addClass('nobg');
		if(slider.max<2)
			$('#slidenav').remove();
		else {
			$('#slidenav').find('a').bind('click',function(){
				var id = $(this).attr('rel');
				if(id == slider.curr || slider.busy) return false;
				slider.busy = true;
				$('#slidenav .act').removeClass('act');
				$(this).parent().addClass('act');
				if($(this).hasClass('content0'))
					$('#bubble').addClass('nobg');
				else
					$('#bubble').removeClass('nobg');
				$('#background' + slider.curr).fadeOut(2500);
				$('#background' + id).fadeIn(2500);
				$('#slide' + slider.curr).fadeOut(2500);
				$('#slide' + id).fadeIn(2500,function(){
					slider.busy = false;
				});
				slider.curr = id;
				slider.autoslide_start();
				return false;
			});
		}
		slider.autoslide_start();
	},
	autoslide_start:function(){
		clearInterval(slider.timer);
		slider.timer = setInterval(function(){
			slider.autoslide();
		},4000);
	},
	autoslide:function(){
		if(slider.curr == slider.max) {
			$('#slidenav li:nth-child(1) a').click();
		} else {
			$('#slidenav li:nth-child(' + ( parseInt(slider.curr) + 1 ) + ') a').click();
		}
	}
}

var submenu = {
	init : function() {
		$('menu').find('ul').each(function(){
			var timer;
			submenu.create($(this),timer);
		});
	},
	create: function(el,timer){ //
		var trigger = $('menu').find('#men'+el.attr('id'));
		if(el.find('a').length<1) return;
		trigger.hover(function(){
			clearTimeout( timer );
			el.fadeIn(); //css({'display':'block'});
			trigger.addClass('over');
		},function(){
			timer = setTimeout(function(){
				el.css({'display':'none'});
				trigger.removeClass('over');
			},150);
		});
		el.find('a').hover(function(){
			clearTimeout( timer );
		},function(){
			timer = setTimeout(function(){
				el.css({'display':'none'});
				trigger.removeClass('over');
			},150);
		});
		el.find('li:even').addClass('even');
	}
}

var select = {
	init:function(){ // asd
		$('.dropdown').each(function(){
			var html = '<div><ul>';
			var selected = '';
			$(this).find('option').each(function(){
				var element = $(this);
				//if($(this).attr('selected')) selected = element.html();
				if( typeof element.val() != 'undefined' && element.val() != '' )
					html += '<li><a href="' + element.val() + '">' + element.html() + '</a></li>';
				else
					html = '<a class="opener">' + element.html().substr(0,25) + '</a>' + html;
			});
			$(this).after('<div class="select">' + html + '</ul></div></div>').remove();
			if(selected != '') $('.opener').html( selected.substr(0,25) );
		});
		$('.opener').live('click',function(){
			$(this).parent().find('div').slideToggle();
			return false;
		})
	}
}
var finder = {
	status : 0,
	init: function() {
		jQuery('#finder').bind('click',function() {
			if( !finder.status ) /* when on the result page, prevent slidetoggling */
				jQuery('#find').slideToggle();
			return false;
		})
	}
}

$(document).ready(function() {
    finder.init();
    submenu.init();
    select.init();
    slider.init();
    publication.init();
});
