$(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");
			}
		});
	});
});
