(function($){

	if ($.tplib == undefined) {
		$.tplib = {};
	}

	/**
	 * TPLib time2seconds()
	 * Utility function to take string formated as h:mm:ss and turn it into seconds.
	 */
	$.tplib.time2seconds = function(time)
	{
		var s = 0;
		$j.each(time.split(':').reverse(), function(i,num){
			s += num*Math.pow(60,i);
		})
		return s;
	}

	/**
	 * TPLib getPageScroll()
	 * Utility function to get the x & y scrolling offset of the document
	 */
	$.tplib.getPageScroll = function(){
		var o = {
			x:window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft,
			y:window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop
		};
		if (!o.x) o.x = 0;
		if (!o.y) o.y = 0;
		o.X=o.x;
		o.Y=o.y;
		return o;
	}

	/**
	 * TPLib alert()
	 * Twitter-like alerts on your page.
	 */
	$.tplib.activeAlerts = [];
	$.tplib.acitveAlertsTimeout = false;
	function alertHide()
	{
		var aa = $.tplib.activeAlerts.shift();
//		  $(aa.selector).hide(aa.effect,$.extend(aa.effectOptions,aa.effectHideOptions),aa.effectDuration);
		  $(aa.selector).hide('fast');
		
		if ($.tplib.acitveAlertsTimeout) { clearTimeout($.tplib.acitveAlertsTimeout); }
		$.tplib.acitveAlertsTimeout = false;
		if (arguments.length && arguments[0].type=="click") { arguments[0].preventDefault(); }
	}

	function alertWatch()
	{
		if (!$.tplib.activeAlerts.length) return;
		$.tplib.activeAlerts[0].timeElapsed += 100;
		if ($.tplib.activeAlerts.length>1)
		{
			if ($.tplib.activeAlerts[0].timeElapsed >= $.tplib.activeAlerts[0].minDuration)
			{
				alertHide();
				nextAlert();
			} else {
				$.tplib.acitveAlertsTimeout = setTimeout(alertWatch, 100);
			}
		} else if ($.tplib.activeAlerts[0].timeElapsed >= $.tplib.activeAlerts[0].duration) {
				alertHide();
		} else {
			$.tplib.acitveAlertsTimeout = setTimeout(alertWatch, 100);
		}
	}

	function alertOnWindowScroll(event) {
		if ($.tplib.activeAlerts.length>0)
		{
			$($.tplib.activeAlerts[0].selector).css({top:$.tplib.getPageScroll().y})
		}
	}

	function nextAlert()
	{

		if (!$($.tplib.activeAlerts[0].selector).length)
		{
			$('body').append('<div id="' + $.tplib.activeAlerts[0].alertDivId + '"></div>')
		}

		$($.tplib.activeAlerts[0].selector).html($.tplib.activeAlerts[0].msg+$.tplib.activeAlerts[0].closeHtml)
			.css({position:'absolute',top:$.tplib.getPageScroll().y})
			.removeClass()
			.addClass($.tplib.activeAlerts[0].cssClass)
//			.show($.tplib.activeAlerts[0].effect, $.extend($.tplib.activeAlerts[0].effectOptions,$.tplib.activeAlerts[0].effectShowOptions), $.tplib.activeAlerts[0].effectDuration);
      .show('fast');
		$('#' + $.tplib.activeAlerts[0].alertDivId + ' .' + $.tplib.activeAlerts[0].closeCssClass).bind('click',alertHide);
		if ($.tplib.activeAlerts[0].autoClose && $.tplib.activeAlerts[0].duration) {
			$.tplib.activeAlertsTimeout = setTimeout(alertWatch,100)
		}
		$(window).bind('scroll',alertOnWindowScroll)

	}

	$.tplib.alert = function(str)
	{
		var options = {
				alertDivId: 'tpAlertDiv',
				cssClass: 'tplib-alert-info',
				closeCssClass: 'tplib-alert-close',
				autoClose:true,
				duration:5000,
				minDuration:2500,
				effect: 'slide',
				effectDuration:500,
				effectOptions: {direction:'up'},
				effectShowOptions: {},
				effectHideOptions: {},
				closeText: 'close',
				selector: false, // do not pass this in, it will be built by the alert funciton.
				closeHtml: false, // do not pass this in, it will be built by the alert function.
				timeElapsed: 0, // do not pass this in, it will be built by the alert function.
				msg: '' // do not pass this in, it will be built by the alert function.
		};

		if (arguments.length>1) options = $.extend(options,arguments[1]);
		options.selector= '#' + options.alertDivId;
		options.closeHtml = '<a href="#" class="' + options.closeCssClass + '">' + options.closeText + '</a>';
		options.msg = str;
		options.timeElapsed = 0;
		$.tplib.activeAlerts.push(options);
		if ($.tplib.activeAlerts.length>1) {
			return;
		}
		nextAlert();

	}

	$.tplib.error = function(str) {
		var options = {
				cssClass: 'tplib-alert-error'
		};
		if (arguments.length>1) options = $.extend(options,arguments[1]);
		$.tplib.alert(str, options);

	}
	
	$.tplib.err = $.tplib.error;
})(jQuery);
