jQuery(document).ready(function ()
{
	jQuery("a").each(function ()
	{
		var a = jQuery(this);
		var rel = a.attr("rel");
		
		if (a.attr("href") && rel && rel.toLowerCase() == "external")
		{
			a.attr("target", "_blank");
		}
	});
	
	jQuery("input.watermarked").each(function (index)
	{
		var current = jQuery(this);
		var title = current.attr("title");
		
		if (title && title != "")
		{
			current.removeClass("watermarked");
		}
		else
		{
			current.attr("title", current.val());
		}
		
		current.bind("focus", function ()
		{
			if (current.attr("title") == current.val() && current.hasClass("watermarked"))
			{
				current.val("").removeClass("watermarked");
			}
		})
		.bind("blur", function ()
		{
			if (current.val() == "" || current.val() == current.attr("title"))
			{
				current.val(current.attr("title")).addClass("watermarked");
			}
		});
	});
	
	setupContentWrapper(4, 1000);
	
	jQuery(window).bind("resize", function ()
	{
		setupContentWrapper(0, 0);
	});
	
	jQuery(document).bind("click", function ()
	{
		setupContentWrapper(4, 100);
	});
});

function setupContentWrapper(resize, delay)
{
	var mainWrapper = jQuery("#wrapper");
	var leftBar = jQuery("#left-bar");
	
	if (mainWrapper != null && leftBar != null)
	{
		var height = mainWrapper.height();
		var padding = leftBar.css("padding-bottom");
		
		if (padding != null && padding != "")
		{
			height = height - parseInt(padding);
		}
		
		var visible = jQuery(window).height();
		
		if (height < visible)
		{
			leftBar.css("padding-bottom", (visible - height) + "px");
		}
		else
		{
			leftBar.css("padding-bottom", "0");
		}
	}
	
	if (resize > 0)
	{
		var timeout = setTimeout("setupContentWrapper(" + (resize - 1) + ", " + delay + ");", delay);
	}
}

function setupSearchField()
{
	var searchField = jQuery("#search-field");
	var keywords = jQuery.jget["s"];
	
	if (searchField != null && keywords != null && keywords != "")
	{
		searchField.attr("title", searchField.val()).val(keywords);
	}
}

function executeSearch()
{
	var searchField = jQuery("#search-field");
	
	if (searchField != null)
	{
		window.location = "/?s=" + escape(searchField.val());
	}
	
	return false;
}

function fixImages()
{
	jQuery("#main-content img").each(function ()
	{
		var oldWidth = jQuery(this).width();
		var newWidth = 612;
		
		if (oldWidth > newWidth)
		{
			var oldHeight = jQuery(this).height();
			var newHeight = Math.floor((newWidth * oldHeight) / oldWidth);
			
			jQuery(this).attr("height", "").attr("width", "")
			.css(
			{
				height: newHeight + "px",
				width: newWidth + "px"
			});
		}
	});
}

function prepareLeftBar()
{
	jQuery("div.item").each(function ()
	{
		var img = jQuery(this).find("img").first();
		
		if (img.height() < jQuery(this).height() - 15)
		{
			img.css("padding-bottom", (jQuery(this).height() - img.height() - 15) + "px");
		}
	});
}

function prepareRightBar()
{
	var tabs = jQuery("h2.tab");
	var layers = jQuery("div.tab");
	
	tabs.each(function (index)
	{
		if (layers.length >= index + 1)
		{
			var children = layers.eq(index).find("ul, ol");
			
			if (children.length > 0)
			{
				var layer = layers.eq(index);
				
				layer.addClass("slider");
				
				if (index == 3)
				{
					layer.addClass("open").css("height", "auto");
				}
				
				jQuery(this).css("cursor", "pointer")
				.bind("click", function ()
				{
					openTab(index);
				});
			}
			else
			{
				layers.eq(index).hide();
			}
		}
		
		var header = jQuery(this).find("span").first();
		var image = iconsFolder + "right-bar-video.gif";
		
		if (header.text() != "mrisTV.com")
		{
			image = iconsFolder + "right-bar-" + index + ".gif";
		}
		
		header.before(jQuery("<img src=\"" + image + "\" height=\"33\" width=\"50\" alt=\"\" class=\"right\" />"));
	});
	
	jQuery("#right-bar").show();
}

var rightBarDuration = 333;

function openTab(show)
{
	jQuery("div.tab").each(function (index)
	{
		var children = jQuery(this).find("ul, ol");
		
		children.find("img").parent().hide();
		
		if (show == index && !jQuery(this).hasClass("open"))
		{
			jQuery(this).addClass("open")
			.animate(
			{
				"height": (33 + children.outerHeight()) + "px"
			},
			{
				"duration": rightBarDuration,
				"queue": false
			});
		}
		else if (jQuery(this).hasClass("open"))
		{
			var layer = jQuery(this);
			
			layer.animate(
			{
				"height": "33px"
			},
			{
				"duration": rightBarDuration,
				"queue": false,
				
				"complete": function ()
				{
					layer.removeClass("open");
				}
			})
		}
	});
}

