var rootPath = "";
var imagePath = "images";

var voted = false;
var lastHover = -1;

$(document).ready(function() {
	$('#submitBasicSearch').click(function() {
		$(this).parent('form').submit();
		return false;
	});

	// Advanced search
	$('#toggleAdvanced').toggle(function() {
		$('#searchAdvanced').slideDown('slow');
		$(this).find('img').attr('src', rootPath + '/' + imagePath + '/spic_minus.gif');
	}, function() {
		$('#searchAdvanced').slideUp('slow');
		$(this).find('img').attr('src', rootPath + '/' + imagePath + '/spic_plus.gif');
	});
	
	// Pagination (todo)
	$('.disabled, .selected', '.pagination').click(function() { return false; });
	
	// Tabs
	$('.tabs > .tabsTitle > a').click(function() {
		if (!$(this).hasClass('selected') && !$(this).hasClass('startSelected')) {
			$('> a.selected, > a.startSelected', '.tabs > .tabsTitle').animate({
				width: '158px',
				borderRight: '1px solid #cdcdcd'
			}, 'fast').removeClass('selected startSelected');
		
			$(this).animate({
				width: '164px',
				borderRight: '0'
			}, 'fast').addClass('selected');
			
			$('> div.selected, > div.startSelected', '.tabs > .tabsContent').removeClass('selected startSelected').show().slideUp('slow');
			$('> div:nth-child(' + ($(this).prevAll().size() + 1) + ')', '.tabs > .tabsContent').addClass('selected').slideDown('slow');
		}
		return false;
	});
	
	$('.myform .field').click(function() {
		if (!$(this).hasClass('selected')) {
			$(this).parents('.myform').find('.field.selected').removeClass('selected').children('span').animate({
				opacity: '0'
			}, 'slow');
			$(this).addClass('selected').children('span').animate({
				opacity: '1'
			}, 'slow');
		}
	});
	
	// Add recipe form
	$('.dynamicFields > *:input').live('click', function() {
		if ($(this).parent().find('input[value=]').size() < 2) {
			$(this).parent().append($(this).clone().attr('value', ''));
		}
	});

	
	$('.tabs .nextStep').click(function() {
		var temp = $(this).parents('.tabs').find('.tabsTitle > a.selected, .tabsTitle > a.startSelected').next();
		
		if (temp.size()) {
			$(temp).click();
			return false;
		}
	});
	
	// Recipe rating
	$('#recipeRating').mousemove(function(e) {
		if (!voted) {
			var rating = Math.min(5, Math.floor((e.pageX - this.offsetLeft) / 28) + 1);
			
			if (rating != lastHover) {
				lastHover = rating;
				$(this).find('.rating').animate({
					width: (rating * 20) + '%'
				}, 'fast');
			}
		}
	});
	
	$('#recipeRating').click(function(e) {
		if (!voted) {
			$.get(rootPath + "/vote", {id: $(this).find('span').text(), rating: lastHover}, function(e) {
				if (e == '1') { voted = true; }
			});
		}
	});

	// Recipe gallery
	$('#galleryLinks a').click(function(e) {
		var temp = $(this).parent().siblings(".galleryFocus").clone();

		$('.galleryFocus').slideUp('slow', function() { $(this).remove(); });
		$(temp).hide().find("img").attr("src", $(this).attr("href"));
		$(this).parent().before(temp);
		$(temp).slideDown('slow');
		
		return false;
	});
	
	// Sort by box
	$('#sort select').change(function() {
		var temp = $(this).parent("#sort").find("form");
		$(temp).attr("action", $(temp).attr("action") + $(this).val()).submit();
	});
});