// Document ready

$(document).ready(function() {
	
// Set sorting preferences by cookie
	$("label[for=store]").click(function() {
		$.cookie("sorting","store");
		location.reload();
	});

	$("label[for=item]").click(function() {
		$.cookie("sorting","item");
		location.reload();
	});
	
	$("label[for=added]").click(function() {
		$.cookie("sorting","posttime");
		location.reload();
	});	
	
	$("label[for=votes]").click(function() {
		$.cookie("sorting","votes_up");
		location.reload();
	});	

// Dropdown
	$("#sorting").hover(function() {
		$(".dropdown").show();
		$(".sortarrow").css("border-bottom","5px solid #d62800");
	},
	function() {
		$(".dropdown").hide();
		$(".sortarrow").css("border-bottom","5px solid grey");
	});
	
// Show voting controls is not voted previously
	$(".outeroffer:not(.voted)").live('mouseenter', function() {
		$(this).find(".ratebar").hide();
		$(this).find(".votebar").show();
	}).live('mouseleave', function() {
		$(this).find(".votebar").hide();
		$(this).find(".ratebar").show();
	});

// Update vote count with AJAX
	$(".votebar span").click(function() {
		offerid = $(this).closest(".outeroffer").attr("id");
		$("div#"+offerid).find(".votebar span").html("<img src=\"../img/ajax-loader.gif\" />");
		
		$.ajax({
			url: "../resources/register_voter.php",
			type: "POST",
			data: "offerid="+offerid,
			success: function() {
				$.ajax({
					url: "../resources/votes.php",
					type: "POST",
					dataType: "text",
					data: "action=vote_up&offerid="+offerid,
					success: function(votes) {
						$("div#"+offerid).find(".votebar").remove();
						$("div#"+offerid).addClass("voted");
						$("div#"+offerid).find(".ratebar span").html(votes);
						$("div#"+offerid).find(".ratebar").fadeIn();
					}
				});
			}
		});
		
		return false;
	});
	
// Show contact form
	$("select").focus(function() {
		$("#contactform_inner").show();
	});
	
// Contact form
	$("#send_message").click(function() {
		var topic = $("#message_topic").val();
		var message = $("#message").val();
		var email = $("#message_email").val();
		var dataString = 'topic='+ topic + '&message=' + message + '&email=' + email;
		
		if(message == "")  {
				$("#message").focus();
				$("#form_error").html("Har du ingen ting å si?").removeClass("lightgreytext success").addClass("error whitetext");
				return false;
		}
		
		$.ajax({
		  type: "POST",
		  url: "../resources/send_message.php",
		  data: dataString,
		  success: function(success) {
		  	$("#form_error").html(success).removeClass("lightgreytext").addClass("success whitetext").delay(3000).fadeOut();
		  		resetForm();		  	
		  }
		});
		return false;
	});
	
// Reset contact form
	function resetForm() {
		$("#message_topic #defaultOption").attr("selected","selected");
		$("#message").val("Din melding");
		$("#message_email").val("Din email");
	}


// Remove form values on focus
	$('input[type="text"], textarea').addClass("idleField");
	$('input[type="text"], textarea').focus(function() {
		$(this).removeClass("idleField").addClass("focusField");
	    if (this.value == this.defaultValue){
	    	this.value = '';
		}
	    if(this.value != this.defaultValue){
	    	this.select();
	    }
	});
	$('input[type="text"], textarea').blur(function() {
		$(this).removeClass("focusField").addClass("idleField");
	    if ($.trim(this.value == '')){
	    	this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});

}); //End document ready
