if ( typeof( console) == "undefined" ) var console = {};
console.turnOff = function ()
{
        var api = [ 'log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'trace', 'group', 'groupEnd', 'time', 'timeEnd', 'profile', 'profileEnd', 'count' ];
        for ( var i = 0; i < api.length; i++ ) console[ api[i] ] = function (){};
        console.firebug = "0.00";
}
if ( typeof( console.firebug) == "undefined"  ) console.turnOff();


var $product_detail_tabs = "";

$(document).ready(function() {

	initHealthBeatSignupButtons();

	$('.hideafterload').hide();

	$product_detail_tabs = $('#product_detail > ul.tabs').tabs();

	$('.tab_trigger').click(function() {
		var idx = $(this).attr("href");
		$product_detail_tabs.tabs("select", idx);
		return false;
	});

	$('.font_adjuster').click(function() {
		var idx = $(this).attr("id");
		if(idx == "font_adjust_small") {
			$("#main").css("font-size", "13px");
		} else if(idx == "font_adjust_medium") {
			$("#main").css("font-size", "16px");
		} else if(idx == "font_adjust_large") {
			$("#main").css("font-size", "18px");
		}
	});

	$('a.embiggen').click(function() {
		var img = $(this).attr("href");
		var embiggened = $("<div id=\"embiggened\"><img id=\"embiggened_image\" width=\"395px\" height=\"500px\" src=\"" + img + "\"/><div class=\"close_modal\"><img src=\"/images/misc/button-close_modal.gif\" width=\"136\" height=\"23\" alt=\"Close Enlargement\" border=\"0\" /></div></div>").dialog({
			dialogClass: "embiggened_dialog",
			modal: true,
			overlay: { 
				opacity: 0.5, 
				background: "black" 
			},
			close: function() {
				$("#embiggened").remove();
			}
		});

		$('#embiggened, .ui-dialog-overlay, .close_modal').click(function() {
			$("#embiggened").dialog("close");
		});

		var closeBtnHeight = 49;
		var handleSize = 4;
		var h = $('#embiggened_image').height() + handleSize + closeBtnHeight + 13;
		var w = $('#embiggened_image').width() + 26;

		$("#embiggened")
			.css("height", h)
			.css("width", w);

		embiggened.data("height.dialog", h);
		embiggened.data("width.dialog", w);
		embiggened.data("position.dialog", "center");

		return false;

	});

	$("#issues").accordion({
		active: false,
		autoHeight: false,
		header: '.year_head'
	});

	$("#issues .months").accordion({
		active: false,
		autoHeight: false,
		header: '.month_head'
	});

	$(".toggle_summary").click(function() {
		$(".summary").hide();
		var id = $(this).metadata().summary_id;
		$("#summary_" + id).toggle();
	});

	$("a.video_placeholder").each(function() {
		var hrefData = $(this).attr('href');
		$(this).replaceWith('<div class="embedded_video"><embed width="320" height="255" loop="false" autoplay="false" controller="true" cache="true" src="' + $(this).attr('href') + '"/></div>');
	});

	if($('a.flash_placeholder').length > 0) {
			embedFlash();
	}

	$("#article_content table").not(".printer_friendly_button").wrap("<div class='table_wrapper'></div>");

});

function initHealthBeatSignupButtons() {
	$('.healthbeat_email_field').focus(function() {
		if($(this).val() == 'Enter E-mail Address') { $(this).val(''); }
	}).blur(function() {
		if($(this).val() == '') { $(this).val('Enter E-mail Address'); }
	});
	$('.healthbeat_name_field').focus(function() {
		if($(this).val() == 'Enter First Name (Optional)') { $(this).val(''); }
	}).blur(function() {
		if($(this).val() == '') { $(this).val('Enter First Name (Optional)'); }
	});
}

function simple_tooltip(target_items, name){
	$(target_items).each(function(i){
		$("body").append("<div class='"+name+"' id='"+name+i+"'><p>"+$(this).attr('title')+"</p></div>");
		var my_tooltip = $("#"+name+i);

		$(this).removeAttr("title").mouseover(function(){
				my_tooltip.css({opacity:0.8, display:"none"}).fadeIn(400);
		}).mousemove(function(kmouse){
				my_tooltip.css({left:kmouse.pageX+15, top:kmouse.pageY+15});
		}).mouseout(function(){
				my_tooltip.fadeOut(400);
		});
	});
}
$(document).ready(function(){
	 simple_tooltip("a.helptip","tooltip");
});



function embedFlash() {
	$('a.flash_placeholder').each(function() {
		var basePath = dirname($(this).attr('href'));
		$(this).media({
			caption: false,
			params: {
				base: basePath,
				quality:  'high',
				pluginspage:  'http://www.macromedia.com/go/getflashplayer',
				id: 'HUY',
				align:  'middle',
				play:  'true',
				loop:  'true',
				scale:  'showall',
				wmode:  'transparent',
				devicefont:  'false',
				bgcolor:  '#ffffff',
				menu:  'true',
				allowFullScreen:  'false',
				allowScriptAccess: 'always'
			}
		});
	});
	$('.flash_placeholder').css('background-color', '');
}

function trackPageview(url) {
	if(typeof pageTracker._trackPageview == 'function') {
		pageTracker._trackPageview(url);
	}
}

function dirname(path) {
    return path.match( /.*\// );
}

