$(function(){
	
	function showDetails(id) {
		if (detailsCache[id]) {
			$bottom.html(detailsCache[id]);
		} else {
			$.getJSON('/json/performance.php',{id:id},function(data){
				
				var $h5 = $('<h5/>').html('<a href="/performance.php?id='+id+'">'+data.venue+', <span class="details">'+data.town+', '+data.postcode+', '+data.date+'</span></a></h5>'),
					$ul = $('<ul/>'),
					$a;
				
				$.each(data.performers,function(key,value) {
					
					$a = $('<a/>').attr('href','/cv.php?id='+id+'&pic='+key).text(' '+value);

					$('<img/>').attr({
						src : '/img/performers/'+id+'-'+key+'-104x154.jpg',
						alt : value,
						width : 104,
						height : 154
					}).prependTo($a);
					
					$('<li/>').append($a).appendTo($ul);
					
				});
				
				$bottom.empty().append($h5).append($ul);
				detailsCache[id] = $bottom.html();
			});			
		}
	}
	
	function search(term) {
	
		if (term) {
			$search_clear.show();	
		} else {
			$search_clear.hide();	
		}
	
		var $this,
			text,
			count,
			regEx = new RegExp($.trim(term),'i');
		
		notepad_move();
		$notepad_items.removeClass('odd');
		count = 0;
		
		$notepad_items.each(function(){
		
			$this = $(this);
			text = $this.find('p').text() + $this.find('h2').text();
		
			if (text.match(regEx)) {
				$this.slideDown(200);
				count++;
				if (count % 2 === 0) {
					$this.addClass('odd');	
				}
			} else {
				$this.slideUp(200);
			}
			
		});
		
		notepad_item_count = count;
		
		setPages();
		navDown();
	}
	
	function navDown() {
		if (notepad_num_pages === notepad_current_page) {
			$nav_down.removeClass('on');
		} else {
			$nav_down.addClass('on');
		}
	}
	
	function setPages() {
		notepad_num_pages = Math.ceil(notepad_item_count / notepad_items_per_page)-1;
	}
	
	var notepad_items_per_page = 7,
		notepad_page_height = 469,
		$notepad_slider = $('#pad_slider'),
		$notepad_items = $notepad_slider.find('.pad_item').mouseenter(function(){
	
			$notepad_items.removeClass('s');
		
			var id = $(this).addClass('s').attr('class').match(/id_(\d+)/)[1];
			
			showDetails(id);
		}),
		notepad_item_count = $notepad_items.length,
		notepad_num_pages,
		notepad_current_page = 0,
		$nav_up = $('#pad_up').click(function(){
			return notepad_move('u');
		}),
		$nav_down = $('#pad_down').click(function(){
			return notepad_move('d');
		}),
		detailsCache = [],
		$bottom = $('#bottom'),
		$search_field = $('#search_field').bind('keyup',function(){
			search($search_field.val());	
		}),
		$search_clear = $('#search_clear').text('X').click(function(){
			$search_field.val('').focus();
			search();
			return false;
		}),
		$pad_date = $('#pad_date');
	
	setPages();
	
	function notepad_move(action) {
		
		if ($notepad_slider.is(':animated')) {
			return false;
		}
		
		if (action === 'd') {
			notepad_current_page++;
			
			if (notepad_current_page > notepad_num_pages) {
				notepad_current_page = notepad_num_pages;
				return false;	
			}
			
		} else if (action === 'u') {
			notepad_current_page--;
			
			if (notepad_current_page < 0) {
				notepad_current_page = 0;
				return false;	
			}
		} else {
			notepad_current_page = 0;	
		}
		
		var move_to = 0 - (notepad_current_page * notepad_page_height),
			month = $notepad_items.filter(':eq('+(notepad_current_page*notepad_items_per_page)+')').attr('class').match(/month_(\w+)/)[1];
		
		$notepad_slider.animate({top:move_to},800);
		
		navDown();

		if (notepad_current_page === 0) {
			$nav_up.removeClass('on');
		} else {
			$nav_up.addClass('on');
		}		
		
		$pad_date.text(month);
		
		return false;
		
	}
	
	navDown();

	$('#pad_content').mousewheel(function(objEvent, intDelta){
		if (intDelta > 0){
			notepad_move('u');
		}
		else if (intDelta < 0){
			notepad_move('d');
		}
		objEvent.preventDefault();
	});
	
});


