$(function (){
	/*
	 * iterate over all products
	 */
	$(".product").each(function() {
		
		var product = this;
		
		/*
		 * when the code button is clicked, add a css class to the
		 * product block and display the instructions, + remove href to make code selectable
		 */
		$(product).find(".product_deal a").click(function(e) {
			
			if ($(product).is(".expired")) {
				//m3Log("-- code expired");
				return false;
			}
			
			if ($(product).is(".clicked")) {
				//m3Log("-- code opened already");
				return false;
			}
			
			//e.preventDefault();
			
			/*
			 * disable codes only
			 */
			
			// only codes contain instructions
			if ($(product).is(".product_code")) {
				$(product).addClass("clicked");
				$(product).find(".instruction").slideDown("normal");
				
				
				// remove A so the user can select the code
				var code_layer = $(product).find(".code_layer");
				code_layer.find("strong").clone().appendTo(code_layer);
				code_layer.find("a").hide();
			}
		});
		
		
		$(product).find(":not(.expired) .code_layer").bind('mouseenter mouseleave', function(e) {
			var el = $(e.target).closest('.code_layer');
			
			if (e.type == 'mouseenter') {
				el.addClass('hover');	
			}
			else {
				el.removeClass('hover');	
			}
			
		});
		
		/*
		 * rating
		 */
		 
		$(product).find(".rate li").mouseover(function(e) {
			var vote_el = $(e.target).closest("li");
			
			if (!vote_el.parent("ul").hasClass("voted")) {
				vote_el.addClass("active");
			}
		});
		
		$(product).find(".rate").not("voted").mouseout(function(e) {
			var vote_el = $(e.target).closest("li");
			
			if (!vote_el.parent("ul").hasClass("voted")) {
				vote_el.removeClass("active");
			}
		});
		
		/*
		$(product).find(".rate").click(function(e) {
			
			e.preventDefault();
			
			if ($(e.target).closest("li").parent("ul").hasClass("voted")) {
				return false;
			}
			
			$(e.target).closest("ul").addClass("voted");			
			$(e.target).closest("li").addClass("active");
			
			// make an AJAX request to submit the votes. either a vote is made
			// successfully, in which case the response will contain the new
			// number of votes, or a vote has failed because the user has
			// already made a vote.
			
			// structure response object:
			// 
			//	 status : success, fail
			//	 message: string / empty
			//	 voted  : 1, 0
			//	 num_votes:
			//		votes_for: int
			//		votes_against: int
			
			var $target = $(e.target);
			var $link;
			
			if ($target.is("a")) {
				$link = $target;
			}
			else if ($target.is("li")) {
				$link = $target.find("a");
			}
			else if ($target.is("strong")) {
				$link = $target.parent("a");
			}
			
			var url = $link.attr("href");
			 
			$.getJSON(url, function(vote_response) {
				if (vote_response.status == "success") {
					
					var voted = parseInt(vote_response.voted);
					
					if (vote_response.voted === 1) {
						updateVote(
							$(product).find(".votes_for"),
							vote_response.num_votes.votes_for
						);
					}
					else if (vote_response.voted == 0) {
						updateVote(
							$(product).find(".votes_against"),
							vote_response.num_votes.votes_against
						);
					}
				}
				else if (vote_response.status == "fail") {
					// present feedback to user
				}
			});
			
			function updateVote(el, num_votes) {
				$(el).fadeOut("slow", function() {
					$(el).html(num_votes).fadeIn("slow");
				});
			}
		});
		*/
		
		/*
		 * when the user wants to share this product, toggle the share
		 * content that contains the 'tell a friend' form and the share
		 * widgets fior several sites.
		 */
		 
		 $(product).find(".share p.label").click(function(e) {
			//m3Log("-- share");
			
			e.preventDefault();
			
			var label         = this;
			var share_content = $(product).find(".share_content");
			var share_email   = $(product).find(".share_email");
			var share_uri     = $(this).children("a").attr("href");
			
			// display the share content
			if (!$(label).hasClass("expanded")) {
				$(label).addClass("expanded");
				
				$(share_content).slideDown("normal");
						
				var share_form = $(share_email).find("form");
				
				/*
				 * preview function
				 */
				$(product).find(".preview_holder a").colorbox({
					width: "50%"
				});

				$(share_form).attr("action", share_uri);
				
				$(share_form).validate({
					rules: {
						"receiver[name]" : {
							required: true
						},
						"receiver[email]" : {
							required: true,
							email: true
						},
						"sender[name]" : {
							required: true
						},
						"sender[email]" : {
							required: true,
							email: true
						},
						"message" : {
							required: true
						},
						"verificatiecode" : {
							required: true,
							minlength: 5
						}
					},
					messages: {
						"receiver[name]" : {
							required: "vul een naam in"
						},
						"receiver[email]" : {
							required: "vul een e-mailadres in",
							email: "dit e-mailadres is ongeldig"
						},
						"sender[name]" : {
							required: "vul je naam in"
						},
						"sender[email]" : {
							required: "vul je e-mailadres in",
							email: "dit e-mailadres is ongeldig"
						},
						"message" : {
							required: "vul een bericht in"
						},
						"verificatiecode" : {
							required: "verificatiecode bestaat uit 5 tekens"
						}
						
					},
					submitHandler: function(form) {
						$.ajax({
							type: 	"post",
							url:	$(form).attr('action'),
							data:	$(form).serialize(),
							success: function(data) {
								if (data == 'sent') {
									$(form).siblings('.status').html('<p>Deze actie is doorgestuurd.</p>');
									$(form).slideUp();
								}
							}
						})
					}
				});
			}
			// hide the share content
			else {
				$(label).removeClass("expanded");
				$(share_content).slideUp("normal");
			}
		});
	});
});
