/**
* Functions
*******************************************************************/

function getMinutesAgo(timestamp) {
	time_diff = request_time - timestamp;
	minutes_ago = Math.floor(time_diff / 60);
		
	if (minutes_ago <= 1) {
		return '1 minute ago';
	} else {
		return minutes_ago+' minutes ago';
	}
}

function navOver() {
	if (!$(this).hasClass('top-level')) {
		$(this).next('div.box').slideDown();
	}
}
function navOut(eventObj) {
	if (!$(eventObj.relatedTarget).hasClass('content') && !$(eventObj.relatedTarget).hasClass('box')) {
		$(this).next('div.box').slideUp('fast');
	}
}
function navBoxOver() {
	return;
}
function navBoxOut(eventObj) {
	if ($(eventObj.relatedTarget).attr('id') != $(this).siblings('a').attr('id')) {
		$(this).slideUp('fast');
	}
}

/**
* On DOM Load
*******************************************************************/

$(document).ready(function(){
	
	// Timestamps
	$('span[time]').each(function(){
		time_diff = request_time - $(this).attr('time');
		minutes_ago = Math.floor(time_diff / 60);
		
		if (minutes_ago <= 1) {
			$(this).text('1 minute ago');
		} else {
			$(this).text(minutes_ago+' minutes ago');
		}
	});
	
	// Top Navigation
// 	if ($('div.navigation').length > 0) {
// 		var hoverIntentConfig = {
// 			over: navOver,
// 			timeout: 500,
// 			out: navOut
// 		};
// 		var hoverOutIntentConfig = {
// 			over: navBoxOver,
// 			timeout: 500,
// 			out: navBoxOut
// 		};
// 		$('div.navigation li a').hoverIntent(hoverIntentConfig);
// 		$('div.navigation div.box').hoverIntent(hoverOutIntentConfig);
// 	}
	// Fancy Box
	if ($('a.fancybox').length > 0) {
		$("a.fancybox").fancybox({
			'titleShow' : false
		});	
	}
	// Text Size
	if ($('#articleCSS').length > 0) {
		$('#articleCSS li').hover(
			function(){
				if ($(this).hasClass('active')) $(this).children('a').css('cursor', 'default');
			},
			function(){
				if ($(this).hasClass('active')) $(this).children('a').css('cursor', 'pointer');
			}
		);
		$('#articleCSS li').click(function(){
			if ($(this).hasClass('active')) return;
			var id_split = $(this).attr('id').split('-');
			$.cookie('storyCSSToggle', id_split[1]);
			jQuery.storyCSSToggle();
		});
		jQuery.storyCSSToggle = function() {
			if ($.cookie('storyCSSToggle')) {
				var type = $.cookie('storyCSSToggle');
				$('ul#articleCSS li').each(function(){
					if ($(this).attr('id') == 'articleCSS-'+type) {
						$('div.articleContainer').addClass(type+'Text');
						$(this).removeClass('inactive').addClass('active');
						$img = $(this).find('img');
						if ($img.attr('src').indexOf('_active') == -1) {
							var current_img_src_split = $img.attr('src').split('.');
							$img.attr('src', current_img_src_split[0]+'_active.'+current_img_src_split[1]);
						}
					} else {
						var this_type = $(this).attr('id').replace('articleCSS-', '');
						$('div.articleContainer').removeClass(this_type+'Text');
						$(this).removeClass('active').addClass('inactive').find('img').attr('src', '/media/ui/textsize_'+this_type+'.gif');
					}
				});
			}
		}
		if (typeof jQuery.cookie == 'undefined') {
			$.getScript('/custom/js/jquery.cookie.js', function(){
				jQuery.storyCSSToggle();
			});
		} else {
			jQuery.storyCSSToggle();	
		}
	}
});
