//dom ready
$(document).ready(function () {
	$("body").addClass("has_js");
	addNewsImageLinks();
	alternateTableRows();
	prettyPhoto();
	searchHelpers();
});
//content loaded
$(window).load(function () {
	imageCaptions();
});

// Setup image captions
function  imageCaptions(){
	$("#content .article .editor img").each(function () {
		divWrapper = '<div class="' + $(this).attr("class") + '" style="width: ' + $(this).width() + 'px;"></div>';
		if ($(this).parent().get(0).tagName == "A") {
			$(this).parent().wrap(divWrapper);
		} else {
			$(this).wrap(divWrapper);
		};
		$(this).removeAttr("class");
		if ($(this).attr("title") != "") {
			$(this).after('<p class="caption">' + $(this).attr("title") + "</p>");
		} else if ($(this).parent("a").attr("title") != "" && $(this).parent("a").attr("title") != undefined) {
			$(this).after('<p class="caption">' + $(this).parent("a").attr("title") + "</p>");
		};
	});
}

// Set external links to open in a new window
function externalLinks() {
	$('a[href^=http://],a[href^=https://]').each(function() {
		var titleStr = $(this).attr("title");
		if (titleStr!="" && titleStr!=undefined) {
			titleStr += " (new window)";
		} else {
			titleStr = "(new window)";
		}
		$(this).addClass("externalLink");
		$(this).attr("title", titleStr);
		$(this).click(function() {
			window.open(this.href);
			return false;
		});
	});
}

// Add class to alternate table rows
function alternateTableRows() {
	$("#content .article .editor table tr:even").addClass("alt");
}

// Wrap news images with the heading link
function addNewsImageLinks() {
	$("#content .article .news ul li img").hover(
		function() {
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
		}
	);
	$("#content .article .news ul li img").click(function() {
		window.location = ($(this).parents("li").find("a").attr("href"));
	});	
}

function prettyPhoto() {
	$("a#watchvideo,a[rel=prettyPhoto]").prettyPhoto();
}

function searchHelpers() {
	$('.viewMoreBreadcrumbs').click(function () {
		$(this).find('img').attr('src', '/images/loading.gif');
		var bc = $(this).closest('.breadcrumb');
		var id = $(this).attr('href').substring($(this).attr('href').indexOf("-")+1);
		$.getJSON('/Breadcrumbs.ashx?id='+id, function (data, status) {
			bc.html($.makeArray(data).join('<br/>'));
		});
		return false;
	})
}
