// doodledo MOTION
// Global Javascript functions
// Powered by Prego

$(function()
{
	// Set up external links
	$("a[rel=external]").each(function() {this.target = "_blank"});
	
	// Fix vertical positioning of images in the right hand column
	$(".news .entry div.image-box, .portfolio-copy ul.image-list").each(function()
	{
		if(this.offsetTop > 0)
			$(this).css("top", (1-this.offsetTop)+"px");
	});
	
	// Client popups
	$("ul.clients li")
		.mouseover(function() {$(this).addClass("hover");})
		.mouseout(function() {$(this).removeClass("hover");})
		.mousemove(function(e)
		{
			var $target = $(e.target);
			if($target.hasClass("top") || $target.hasClass("copy") || $target.hasClass("bottom"))
			{
				var x = typeof(e.layerX) == "number" ? e.layerX : e.offsetX;
				var y = typeof(e.layerY) == "number" ? e.layerY : e.offsetY;
			
				if(x < 150 || $target.hasClass("copy") && y > 15 || $target.hasClass("bottom"))
					$(this).removeClass("hover");
			}
		});
		
	// Add pretty interface to file input fields
	$("input[type=file]").each(function()
	{
		$(this).wrap($("<div/>").addClass("fake-file-input"));
	});
	
	$(".fake-file-input").each(function()
	{
		var $textField = $("<input type=\"text\" class=\"fake-file\" />");
		var $button = $("<button class=\"fake-file\">Browse<"+"/button>").click(function() {return false;});
	
		$(this)
			.append($textField)
			.append($button);
	
		$("input[type=file]", this)
			.addClass("file")
			.change(function()
			{
				var path = $(this).attr("value");
				var fname = path.split("/").pop();
				$textField.attr("value", fname);
			});
	});
});
