$(document).ready(function() {
	
	var max_height = 0;
	$("#your-competition-entries .quarter .inner h3").each(function() {
		var this_height = $(this).height();
		if (this_height > max_height) {
			max_height = this_height;
		}
	}).height(max_height + "px");
	
	var max_height = 0;
	$("#hunt-20th .inner h3").each(function() {
		var this_height = $(this).height();
		if (this_height > max_height) {
			max_height = this_height;
		}
	}).height(max_height + "px");
	
	$("#mentors .quarter, #mentors li, .campaigns-featured .picks, body.nav-catch-up .quarter-portrait, #get-involved-iwant .inner-no-top, body.nav-campaigners .latest-blog-1, body.nav-campaigners .latest-blog-2, body.nav-campaigners .latest-blog-3, body.nav-campaigners .latest-blog-4, body.nav-campaigners .bebo-extract, #news-archive-extracts .margin, #your-competition-entries .quarter, #hunt-20th .quarter, .height-home-block .margin, #home-iwant .margin, #home-competition-banner .inner, #your-competition-entries li, body.nav-campaigners .full-width-1 .campaign-ind-width-text").each(function() {
		var href = $("a:first", this).attr("href");
		var cta = $(".more:first a", this);
		
		if (cta.length == 0) {
			cta = $(".cont:first a", this);
		}
		
		if (cta.length == 0) {
			cta = $("a:first", this);
		}
		
		$(this)
		.css("cursor", "pointer")
		.hover(function() {
			cta.addClass("hover");
		}, function() {
			cta.removeClass("hover");
		})
		.click(function(e) {
			e.preventDefault();
			//if (cta.hasClass("external")) {
				//window.open(href);
			//} else {
				window.location.href = href;
			//}
		});
	});
	
	$("#get-involved-iwant").each(function() {
		var left = $(".left", this);
		var right = $(".right", this);
		if (left.height() > right.height()) {
			right.height(left.height() + "px");
		}
		if (right.height() > left.height()) {
			left.height(right.height() + "px");
			left.css("background-color", "#000");
		}
	});
	
	$("a").each(function() {
		var a = $(this);
		var href = a.attr("href");
		if (href && href.substring(0,10) == "/Link.jsp?") {
			a.attr("href", href.substring(14));
		}
	});
	
	var image_left = new Image();
	image_left.src = "/assets/images/carousel-left-hover.gif";
	var image_right = new Image();
	image_right.src = "/assets/images/carousel-right-hover.gif";
	
	$(".campaigners-carousel .left").hover(function() {
		$(this).attr("src", "/assets/images/carousel-left-hover.gif");
	}, function() {
		$(this).attr("src", "/assets/images/carousel-left.gif");
	});
	
	$(".campaigners-carousel .right").hover(function() {
		$(this).attr("src", "/assets/images/carousel-right-hover.gif");
	}, function() {
		$(this).attr("src", "/assets/images/carousel-right.gif");
	});
	
	$(".home-carousel img.left, .home-carousel img.right").hover(function() {
		$(this).css("opacity", "0.75");
	}, function() {
		$(this).css("opacity", "1");
	});
	
	$("div.comment-block").each(function() {
		var comment = $(this);
		var date = $("p.comment-date", this);
		date.append(" <a href='#' class='report-comment'>Report this comment</a>");		
		$("a.report-comment", this).one("click", function(e) {
			e.preventDefault();
			$(this).fadeOut("fast", function() {
				$("input[type='submit']", comment).click(function(e) {
					e.preventDefault();
				})
				$("input[type='submit']", comment).one("click", function() {					
					$(this).fadeTo("fast", 0.5);
					var form = $("form", comment);
					$.ajax({
						type: "POST",
						url: form.attr("action"),
						data: "MAX_FILE_SIZE=104857600&action[report-comment]=Submit&fields[flagged]=true&id="+$("input[name='id']", form).val()+"&fields[reported-reason]="+URLEncode($("textarea", form).val())+"&url=" + URLEncode(window.location.href),
						success: function() {
							form.append("<p style='display:none;'>This comment has been reported to a moderator. Thank you.</p>");
							form.fadeOut("fast", function() {
								$("label, input, textarea", this).hide();
								$(this).show();
								$("p", this).fadeIn("fast");
							});
						}
					});
				});
				$("form", comment).fadeIn("fast");
			});			
		});		
	});
	
});

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function trackVideo(section, object, type, title) {
	if (section == "") { section == "null"; }
	if (object == "") { object == "null"; }
	if (type == "") { type == "null"; }
	if (title == "") { title == "null"; }	
	var url = "/video/" + section + "/" + object + "/" + type + "/";
	if (title != "") {
		url += title + "/"
	}	
	pageTracker._trackPageview(url);

	if (type == "vimeo") {
		setTimeout(function() {
			var id = $("span#video_id").html();
			$.ajax({
				type: "POST",
				url: "/video-tracking/",
				data: "id=" + id + "&action[video-tracking]=Submit"
			});
		}, 20000);
	};
	
}