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

	$('#subposts ul li.post').bind('mouseenter', function(){
		$('.detail', this).animate({'top': 0}, {duration: 150, queue: false});
	}).bind('mouseleave', function(){
		$('.detail', this).animate({'top': -126}, {duration: 150, queue: false});
	});
	
	var index = 0;
	var prev = $('#subposts a#prev');
	var next = $('#subposts a#next');
	var posts = $('#subposts li.post');
	var limit = posts.length - 3;
	prev.addClass('disabled');
	prev.css('opacity', 0.25);
	if (index == limit) {
		next.addClass('disabled');
		next.css('opacity', 0.25);
	}
	
	prev.click(function(){
		if(index > 0) {
			posts.each(function(i){
				$(this).animate({'left': '+=226'}, {'duration': 300, 'queue': false});
			});
			index--;
			if (index == 0) prev.addClass('disabled').fadeTo(150, 0.25);
			if (index == limit-1) next.removeClass('disabled').fadeTo(150, 1);
		}
		return false;
	});
	next.click(function(){
		if(index < limit) {
			posts.each(function(i){
				$(this).animate({'left': '-=226'}, {'duration': 300, 'queue': false});
			});
			index++;
			if (index == limit) next.addClass('disabled').fadeTo(150, 0.25);
			if (index == 1) prev.removeClass('disabled').fadeTo(150, 1);
		}
		return false;
	});
	
	var switcher = $('#category-switcher');
	var link = $('.expand a', switcher);
	var h1 = switcher.height();
	$('.category-block', switcher).hide();
	var h2 = switcher.height();
	link.toggle(
		function(){
			switcher.animate({'height': h1}, {duration: 200, queue: false, complete: function(){
				$('#countries', switcher).fadeIn(150, function(){
					$('#issues', switcher).fadeIn(150);
					$('span', link).css('background-position', 'bottom');
				});
			}});
		},
		function(){
			$('#issues', switcher).fadeOut(150, function(){
				$('#countries', switcher).fadeOut(150, function(){
					switcher.animate({'height': h2}, {duration: 200, queue: false});
					$('span', link).css('background-position', 'top');
				});
			});
		}
	);
	
	function setFieldBehaviour(obj, val) {
		if (obj.val() == '') obj.val(val);
		obj.focus(function(){
			$(this).removeClass('valid');
			$(this).removeClass('error');
			if ($(this).val() == val) $(this).val('');
		}).blur(function(){
			if ($(this).val() == '') {
				$(this).val(val);
				$(this).removeClass('prefilled');
			}
		});
		obj.parents('form').submit(function(){
			if (obj.val() == val) obj.val('');
		});
	}

	setFieldBehaviour($('input#s'), $('input#searchsubmit').val());
	$('.signupform label').addClass('hidden');
	$('.signupform input.text').each(function(i, obj){
		setFieldBehaviour($(obj), $(obj).prev('label').html());
	});
	
	$('#searchform').validate({
		rules: {
			s: {
				required: true,
				minlength: 3
			}
		}
	});

	$('#quicksignupform').validate();
	$('#longsignupform').validate();

	var reports = $('#reports-feed ul');
	var speed = 1000;
	var pause = 5000;
	
	function tick() {
		var first = $('li:first', reports);
		reports.animate({'top': -first.outerHeight()}, speed, 'swing', function(){
			reports.css('top', 0);
			reports.append(first.remove());
			first.hide().fadeIn(speed/2);
			registerFancybox($('li:last a.ushahidi', reports));
		});
	}
	
	interval = setInterval(tick, pause);
	
	$('.home #countries ul').before('<div id="country-map-overlay"></div>');
	var slugs = new Array();
	var countries = $('.home #countries a');
	countries.each(function(){
		var segs = $(this).attr('href').split('/');
		var slug = segs[segs.length-2];
		slugs.push(slug);
	}).hover(function(){
		$('#country-map-overlay').addClass(slugs[countries.index(this)]);
	}, function(){
		$('#country-map-overlay').attr('class', '');
	});
	
	function registerFancybox(obj) {
		obj.fancybox({
			height         : 2000,
			width          : 745,
			transitionIn   : 'elastic',
			transitionOut  : 'elastic',
			speedIn        : 300,
			speedOut       : 300,
			opacity        : true,
			overlayColor   : '#000',
			overlayOpacity : 0.8,
			showNavArrows  : false,
			onComplete     : function(){
				$('#fancybox-title').html($('#fancybox-title-main').html());
				Cufon.replace('#fancybox-title', { fontFamily: 'Min Rounded', hover: true });
			}
		});
	}

	registerFancybox($('a.ushahidi'));
	
	
});
