$(document).ready(function(){

	initForms();
	initRating();
	initFaq();
	initSorting();
	
});

// Make forms submittable
function initForms(){
	if($('form').length){
		$('form input.permit').val(scramble($('form input.permit').eq(0).val()));
		
		$('form a.submit').click(function(){
			$(this).parents('form').eq(0).submit();
		});
		
		$('form input:visible, form textarea').focus(function(){
			$(this).addClass('focus');
		});
		
		$('form input:visible, form textarea').blur(function(){
			$(this).removeClass('focus');
		});
	}
}

function initRating(){

	// Animate the hovering nicely!
	$('.rating a').hoverIntent(function(){
		var thisItem = $(this);
		var rating = thisItem.parent();
		var wdth = (parseInt(thisItem.css('margin-left'))+parseInt(thisItem.width()))+'px';
		//alert($('.votes'. rating).val());
		thisItem.attr('title', $('.votes', rating).val()+' votes');
		thisItem.siblings('.score').animate({"width": wdth}, 200);
		rating.addClass('hovering');
	},function(){});
	
	$('.rating').hoverIntent(function(){},function(){
		var thisItem = $(this);
		var wdth = $('.wdth', thisItem).val()+'px';
		$('.score', thisItem).animate({"width": wdth}, 400);
		thisItem.removeClass('hovering');
	});
	
	// Submit a rating!
	$('.rating a').click(function(){
		var rating = $(this).parent();
		var score = $(this).attr('rel');
		var blurb_id = $('.blurb_id', rating).val();
		$.ajax({
			type: "POST",
			url: $('form', rating).attr('action'),
			data: "blurb_id="+blurb_id+"&score="+score,
			success: function(msg){
				if(parseInt(msg) == msg){
					$('.wdth', rating).val(msg);
					$('.score', rating).animate({"width": msg+'px'}, 400);
					$('.votes', rating).val(parseInt($('.votes', rating).val())+1);
				}else{
					alert(msg);
				}
			}
		});
	});
}

// Initialise the FAQ sliding
function initFaq(){
	if($('#faq').length){
		$('#faq h4 a').click(function(){
			thisItem = $('.expander', this);
			if(thisItem.is('.expanded')){
				thisItem.parent().parent().next().slideUp(200);
				thisItem.removeClass('expanded');
			}else{
				thisItem.parent().parent().next().slideDown(200);
				thisItem.addClass('expanded');
			}
		});
	}
}

// Initialise sorting hover for older browsers
function initSorting(){
	if($('#sortoptions').length){
	
		$('#sortoptions').hoverIntent(function(){
			$('ul', this).addClass('hovering');
		},function(){
			$('ul', this).removeClass('hovering');
		});
	
	}
}

// Scramble!
function scramble(t){
	if(t){
		var s = new Array('Z','U','2','h','s','X','t',')','O','w','R','l','\'','J','#','I','z','6','7','!','o','K','<','q','g','3','a','8','G','-','A','?','Q',' ','M','T','(','m','9','>','H',':','V','u','S','5','=','p','x','d','Y','P',';','j','F','b','D','L','.','1','N','k','c','/','\\','W','i','n','y','"','C','4','f',',','v','E','B','r','@','e');
		var n = '';
		var x = t.split('');
		
		$.each(x, function(i, c){
			var p = $.inArray(c, s);
			if(p < 0){
				n = n + c;
			}else if(p == 24){
				n = n + '\\';
			}else{
				p = p + 40;
				if(p >= 80){ p = p - 80; }
				n = n + s[p];
			}
		});
		return n;
	}
	return '';
}