/**
 * 
 * @projectDescription jQuery plugin library for Perform Group networks
 * 
 * @author Developed by Adam coogan
 * @version 1.2
 * 
 * Built on top of the jQuery library
 * http://jquery.com 
 * 
**/

(function($) {

// global element
var e;

/**
 * 
 * @projectDescription jQuery Tabs plugin for composite tabbed master page elements with matched class name
 * 
 * @author Developed by Adam Coogan
 * @version 1.0
 * @since 1.0
 * 
 * @example $(".compositetabs").cTabs();
 *  
 * @type jQuery 
 * 
 */
	
$.fn.cTabs = function() {               
    return this.each(function() { 
		new $ct(this); 
	});      
};

$.cTabs = function(e){	
	 
	var $master = $(e).attr("id");				
	var $tabwrapper = $("#" + $master + " div.tabbedMasterPageElement");		
	var $ul = $tabwrapper.find('> .ul ul');						 																			
	var $panels = $tabwrapper.find('> div:gt(0)');	
	var $tabs = $ul.find('> li > a');		
			
	$tabs.each(function(i){
		$(this).attr("href","#" + $($panels[i]).attr("id"));
	});
			
	$tabs.click(function(){				
		$tabs.parent().removeClass('selected');					
		$(this).parent().addClass('selected');									
		$panels
			.hide()
			.filter(this.hash)
			.show();												
		return false;					
	}).filter(window.location.hash ? '[hash=' + window.location.hash + ']' : ':first').click();
			
}

// Create shortcuts for internal use
var $ct = $.cTabs;

/** end **/

/**
 * 
 * @projectDescription jQuery Tabs plugin for composite tabbed master page elements with matched class name
 * 
 * @author Developed by Adam Coogan
 * @version 1.0
 * @since 1.2
 * 
 * @example $(".compositetabs").cTabs();
 *  
 * @type jQuery 
 * 
 */
	
$.fn.tabs = function() {               
    return this.each(function() { 
		new $t(this); 
	});      
};

$.tabs = function(e){	
		
	var $tabwrapper = $("." + $(e).attr("class"));					
	var $panels = $tabwrapper.find('> div');	
	var $tabs = $tabwrapper.find('> ul > li > a');		
	var onLoad = $tabs.parent().filter(".selected").length != 0 ? true : false;
		
	$tabs.click(function(){
		if (!onLoad) {
			$tabs.parent().removeClass('selected');
			$(this).parent().addClass('selected');
			$panels.hide().filter(this.hash).show();
			return false;
		}
		else {
			$tabs.each(function(){
				if ($(this).parent().hasClass("selected")!=true) {
					$panels.filter($(this).attr("href")).hide();
				}
			});
			onLoad = false;	
		}
	}).filter(window.location.hash ? '[hash=' + window.location.hash + ']' : ':first').click();	

}

// Create shortcuts for internal use
var $t = $.tabs;

/** end **/

/**
 * 
 * @projectDescription change all the elements that have the matched class name to convert to days, hours, minutes ago
 * 
 * @author Developed by Adam Coogan
 * @version 1.0
 * @since 1.1
 * 
 * @example $(".timestamp").ago();
 *  
 * @type jQuery 
 * 
 */
	
$.fn.ago = function(o) {               
    return this.each(function() { 
		new $ago(this, o); 
	});      
};

// Default configuration properties.
var defaults = {
	serverTime:"/page/serverTime/time.xml",
	lang_minute_plural: "[NUM] minutes ago",	
	lang_minute_singular: "[NUM] minute ago",
	lang_hour_plural: "[NUM] hours ago",
	lang_hour_singular: "[NUM] hour ago",
	lang_day_plural: "[NUM] days ago",	
	lang_day_singular: "[NUM] day ago",
	display_inline:true
};

$.ago = function(e, o){
	
	this.config = $.extend({}, defaults, o || {});
    c = this.config;
	
	$.ajax({
		url:c.serverTime,
		success: function(xml) {
			$("date", xml).each(function(){
				//get server time  
				var tickServerTime = parseInt($(this).attr("timestamp"));
				
				doAgo(new Date(tickServerTime));
			});
		},
		error: function() { 
			//do nothing, just let the dates display as they are
		}
	});						
	
	function doAgo(serverTime){
		mssec = 1000;
		msmin = 60 * mssec;
		mshrs = 60 * msmin;
		msday = 24 * mshrs;
		
		$("." + $(e).attr("class")).each(function(){
			var ts = serverTime.getTime() - new Date($(this).text());
			tsday = (ts / msday);
			tshrs = ((ts / mshrs) % 24);
			tsmin = ((ts / msmin) % (60));
			
			//TODO: Add months ago support												
			if (Math.floor(tsday) > 0) {
				$(this).html(Math.floor(tsday) == 1 ? c.lang_day_singular.replace("[NUM]", Math.floor(tsday)) : c.lang_day_plural.replace("[NUM]", Math.floor(tsday)));
			}
			else 
				if (Math.floor(tshrs) > 0) {
					$(this).html(Math.floor(tshrs) == 1 ? c.lang_hour_singular.replace("[NUM]", Math.floor(tshrs)) : c.lang_hour_plural.replace("[NUM]", Math.floor(tshrs)));
				}
				else 
					if (Math.floor(tsmin) > 0) {
						$(this).html(Math.floor(tsmin) == 1 ? c.lang_minute_singular.replace("[NUM]", Math.floor(tsmin)) : c.lang_minute_plural.replace("[NUM]", Math.floor(tsmin)));
					}
			
			if (c.display_inline) {
				$(this).css("display", "inline");
			}
		});
	}
}

// Create shortcuts for internal use
var $ago = $.ago;

/** end **/
	
})(jQuery);
