$(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).hasClass("clicked") ) {
				
				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");
					var code = code_layer.find("strong").clone()[0];
					
					code_layer.append( code );
					code_layer.find("a").hide();
					code_layer.qtip("destroy");
				}
			}
		});
		
		$(product).find(":not(.expired) .code_layer").bind('mouseenter mouseleave', function(e) {
		
		});
		
		/*
		 * 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");
				});
			}
		});
		*/
		
	});
});
