$(document).ready(function() {

	if (navigator.appVersion.indexOf("Mac")!=-1) {
		$("#navigation_wrapper ul li a").each(function(index, ele){
			var _src = $(this).css('backgroundImage').replace(/\.jpg/,'_mac.jpg');
			$(this).css('backgroundImage', _src);
		});
	}
	/* 
		HomePage 
		Callout widget
	*/
	if ($("#homepage_tabbed_content").length > 0) {
		$("#homepage_tabbed_content").tabs();
		var _tabbed_timer = setInterval("next_tabbed()", 10000);
		$(".ui-tabs-nav li a").one("click", function() { clearInterval(_tabbed_timer); });
		var _strong = 'A Little Change Can Add Up';
		var _p      = 'When the vending machine is calling, help reduce your snack cravings with 5-calorie Extra&reg; gum--and a little hush money. Every time you chew Extra gum instead...';
		var _node_date   = '2009-12-01';
		var _today = [];
		var _date  = new Date();
		/* Remember to delete the following line after QA */
		/* _date.setFullYear(2009, 10, 31); */
		_today[0] 	= _date.getYear() + (navigator.appName == "Microsoft Internet Explorer" || navigator.appName == "Opera" ? 0 : 1900);
		_today[1]   = _date.getMonth() + 1;
		_today[1]   = _today[1] < 10 ? "0" + _today[1] : _today[1];
		_today[2]   = _date.getDate();
		_today[2]   = _today[2] < 10 ? "0" + _today[2] : _today[2];
		_today_date = _today.join("-");
		$.get("health_tips.xml", function(data, status) {
			$("item", data).each(function(i){
				if (i == 0) { 
					_strong = $("strong", this).text();
					_p      = $("main", this).text();
					_node_date   = $("date", this).text();
					} else {
					var _test_date = $("date", this).text()
					if ( _test_date > _node_date && _test_date < _today_date ) {
						_have_lagel = $("havelegal", this).text() == "true" ? "inline" : "none"
						_strong = $("strong", this).text();
					  _p      = $("main", this).text();
						_node_date   = $("date", this).text();
					}
				}
			});
			$("#dvalign_footer").css({ "display" : _have_lagel });
			$("#tab1 p").each(function(i){
				$("strong",this).text(_strong);
				$("a",this).attr("href", "healthy_living_tips.aspx#" + _node_date);
				if(i == 1) { $(this).html(_p); }
			});
		},'xml');
	}
	//initialize_homepage_callout();
	
	/*
		The Research
	*/
	initialize_navigation_helpers();
	
	/*
		Global Navigation
		HELPERS
	*/
	$("#navigation_wrapper ul li a").hover(function() {
		if ($(this).hasClass("active") ) { return 0; }
		var _postition = $(this).position();
		var _left      = _postition.left;
		var _top       = _postition.top;
		var _width		 = $(this).width();
		var _height		 = $(this).height();
		var _child_width  = ( $(this).parent().children("img").width() / 2);
		var _offset_left  = _child_width - ( _width / 2 );
		var _last_offset =  $(this).hasClass("last_nav") ? 60 : 0 
		$(this).parent().children("img").css({ "top": ( _top + _height ) + "px", "left" : (_left - _offset_left - _last_offset) + "px" });
	}, function() {
		$(this).parent().children("img").css({ "top": "-1000px", "left" : "-1000px" });
	}
	);
	
	if ($("#super_iframe").length > 0 && navigator.appName == "Microsoft Internet Explorer") {
		$("img.helper_top").each(function(i) {
			this.parentNode.removeChild(this)
		})
	}
	
});


function initialize_homepage_callout() {
	var _length = $("#home_bottom_content_wrapper div.active_callout_fade").length
	var _random = 0;
	var _safety = 10;
	while (_length > 3 && _safety > 0) {
		_safety-- ;
		_random = Math.floor(Math.random() * _length);
		$($("#home_bottom_content_wrapper div.active_callout_fade")[_random]).remove();
		_length = $("#home_bottom_content_wrapper div.active_callout_fade").length;
	}
	$("#home_bottom_content_wrapper div.active_callout_fade").hide();
	$("#home_bottom_content_wrapper div.active_callout_fade:first").show();
	$('#active_controller a:first').addClass("active");
	$('#active_controller a').bind("click", function(){ show_segment(this); return false; } );
	return true;
}

function show_segment(_element) {
	var _selected = $(_element).attr("rel")
	$('#active_controller a.active').removeClass("active")
	$(_element).addClass("active");
	for (var _index = 1 ; _index < 4 ; _index++ )
	{
		var _obj = $("#home_bottom_content_wrapper div.active_callout_fade")[_index - 1]
		if ( _index == _selected ) {
			$(_obj).show();
		} else {
			$(_obj).hide();
		}
	}
}


function initialize_navigation_helpers() {
	$("#the_research_content_wrapper ul.active_list li:first a").addClass("active");
	$(".tabbed_content").hide();
	$(".tabbed_content:first").show();
	$("#the_research_content_wrapper ul.active_list li a").bind("click", function() {
		$("#the_research_content_wrapper ul.active_list li a").removeClass("active");
		$(this).addClass("active")
		var _id = $(this).attr("rel");
		$(".tabbed_content").hide();
		//$(".tabbed_content:visiible").hide('slide', {direction: 'left'}, 200)
		$("#" + _id).show()
		//$("#" + _id).show('slide', {direction: 'right'}, 200)
		return false;
	});
}

/*
	Will display next tabbed item
*/
function next_tabbed() {
	var _num = $("#homepage_tabbed_content").tabs('option','selected') * 1;
  _num = _num > 1 ? 0 : _num+1;
  $("#homepage_tabbed_content").tabs('option','selected', _num);
}

var addthis_config = {
    ui_email_note: 'Enter the Extra 3PM Snackdown Challenge for your chance to win the Ultimate Biggest Loser Getaway, including a training session with Bob Harper! Plus, get tips and information to help you make better choices and live a healthier lifestyle.'
  }
