var outerLayout, innerLayout;
var lastAction, earmarkPeriod;
function init()
{
	outerLayout = $('body').layout({
			center__onresize: function(){innerLayout.resizeAll();}
		,	west__maxSize: 70
		,	east__maxSize: 70
		,	center__size: 995
		,	center__maxSize: 995
		,	center__minSize: 995
		,	spacing_open:			0	// ALL panes
		,	spacing_closed:			0	// ALL panes
		, 	resizable: false
		, 	closable: false
		});
	
	innerLayout = $('div.ui-layout-center').layout({
				north__paneSelector:	".ui-layout-north"
			,	center__paneSelector:	"#ContentPane"
			,	south__paneSelector:	".ui-layout-south"
			,	north__minSize: 80
			,	north__maxSize: 80
			,	south__minSize: 20
			,	spacing_open:			0	// ALL panes
			,	spacing_closed:			0	// ALL panes
			, resizable: false
			, closable: false
		});
	
	reDraw();
	
	/* Login */
	$("#MemberLoginForm").unbind('keypress').keypress(function(e){
		if(13 == parseInt(e.which))
		{
			e.preventDefault();
			doLogin();
		}
	});
	//removed this function because I needed to not have the user log out by clicking on their name.
	/*$("#MemberLogin").click(function(e){
		if(-1 != $(this).html().indexOf('Login'))
			var empty;
		else
			logout();
		
		return false;
	});*/
	/* Added this function to eliminate the user from logging out by clicking on their name.*/
	$("#MemberLoginOnly").click(function(e){
		if(-1 != $(this).html().indexOf('Login'))
			var empty;
		else
			logout();
		
		return false;
	});
	
	$("#frmMemberLogin button[name='submit']").click(function(){
		doLogin();
	});
	
    if('' != $.cookie('acct') && null != $.cookie('acct'))
    	$("#MemberLoginForm input[name='userid']").attr('value', $.cookie('acct')).select().focus();
}

function doLogin()
{
	var errors = $(".error-message");
	
	errors.remove();
	login("#frmMemberLogin");
}

function resizeLayout()
{
	var doc     = $(document);
	var width   = doc.width();
	var height  = doc.height();
	var content = $(".ui-layout-center");
	content.css('width', '995px');
	$("div[id='east'], div[id='west']").css('width', ((width - 995) / 2));
	alert(((width - 995) / 2));
}

function resizeResultsPane(offset)
{
	var results = $('#RequestResults');
	
	if(results.length > 0)
	{
		var offsetW = 0;
		var mWindow = $(".ui-layout-center");
		var nTab    = $("#AdminTab");
		var nNorth  = $("#north");
		var nSouth  = $("#south");
		var nMeta   = $("#PageMeta");
		var nKpi    = $("#KpiPane");
		var nFilter = $("#PreFilter");
		var nResult = $("#ResultsFilter");
		var nAdmin  = $("#AdminNotify");
		
		if(!offset)
			offset = 0;
		
		if(mWindow.length < 1)
		{
			mWindow.width = function(){return 10;};
			mWindow.height = function(){return 10;};
		}
		
		offset = (offset + 43);
		
		if($.browser.msie)
			offset += 10;
		
		if(nTab.length > 0)
			offset += nTab.height();
		
		if(nNorth.length > 0)
			offset += nNorth.height();
		
		if(nSouth.length > 0)
			offset += nSouth.height();
		
		if(nMeta.length > 0)
			offset += nMeta.height();
		
		if(nFilter.length > 0)
			offset += nFilter.eq(0).height();
		
		if(nResult.length > 0)
			offset += nResult.height();
		
		if(nAdmin.length > 0)
			offset += nAdmin.height();
		
		if(nKpi.length > 0)
		{
			offsetW += nKpi.width();
			
			if(nKpi.height() > (mWindow.height() - offset))
			{
				//offset += nKpi.height();
				//alert(mWindow.height() + " : " + nKpi.height());
			}
		}
		
		if($(".shelf", results).length > 0)
		{
			var h = (mWindow.height()) ? (mWindow.height() - offset) : offset;
			var w = ((mWindow.width() - offsetW) - 40);
			
			if(h < 200)
				h = 201;
			
			$(".shelf", results)
				.css('height', h + 'px')
				.css('width', w + 'px');
		}
	}
}

function mydiff(date1,date2,interval) {
    var second=1000, minute=second*60, hour=minute*60, day=hour*24, week=day*7;
    date1 = new Date(date1);
    date2 = new Date(date2);
    var timediff = date2 - date1;
    if (isNaN(timediff)) return NaN;
    switch (interval) {
        case "years": return date2.getFullYear() - date1.getFullYear();
        case "months": return (
            ( date2.getFullYear() * 12 + date2.getMonth() )
            -
            ( date1.getFullYear() * 12 + date1.getMonth() )
        );
        case "weeks"  : return Math.floor(timediff / week);
        case "days"   : return Math.floor(timediff / day); 
        case "hours"  : return Math.floor(timediff / hour); 
        case "minutes": return Math.floor(timediff / minute);
        case "seconds": return Math.floor(timediff / second);
        default: return undefined;
    }
}


function actionPanel(act)
{
	var actions = $(".Actions", act)
	
	if('0' == act.attr('show'))
	{
		act.prev().css('cursor', 'default');
		act.attr('show', '1');
		act.animate({height: (actions.height() + actions.prev().height()), opacity: '1', marginTop: '0px'}, 400, function(){
			actions.fadeIn('fast');
			$(this).css('height', 'auto');
		});
	}
	else
	{
		act.prev().css('cursor', 'pointer');
		act.attr('show', '0');
		actions.fadeOut('fast');
		act.animate({height: '0px', opacity: '0', marginTop: '-1px'}, 400);
	}
}

function reDraw()
{
	$(".jqtransform").jqTransform();
	$('.rounded').corners();
	$('.AutoNumCurr').attr('alt', 'p9c3p2S').autoNumeric();
	$('.AutoNum').attr('alt', 'p9c3p2S').autoNumeric();
	//JT_init();
	
	resizeResultsPane();
}

function loading(obj, mssg)
{
	var func = function(){
		doLoading(obj, mssg);
	}
	
	setTimeout(func, to);
}
var fired = false, to = 300;
function doLoading(obj, mssg)
{
	var me = $(arguments[0]);
	me.children().remove();
	
	var load = $("#LoadingPane");
	
	if(!mssg || !mssg.txt)
		var mssg = {txt: 'Loading Data ...'};
	
	if(load.length < 1)
	{
		load = $("<div id='LoadingPane'>" + mssg.txt + "<br /><img src='/img/blank.gif' alt='' /></div>");
		me.append(load);
	}
	
	fired = true;
}



function updateKpiAdmin()
{
	$.ajax({
		dataType: 'json',
		data: 'action=getKpi&c=' + earmarkPeriod.curr + '&p=' + earmarkPeriod.prev,
		async: false,
		url: '/api/frmimporter.php',
		type: 'POST',
		success: function(data, io){
			var TotAvail = $("#KpiTotAvail");
			var TotAlloc = $("#KpiTotAlloc");
			var TotRem   = $("#KpiTotRem");
			
			if(TotAvail.length > 0)
				$("div[class!='Clear']", TotAvail).html($("<span>" + data.r.data[0].PERIOD_AMT + "</span>").formatCurrency().html());
			
			if(TotAlloc.length > 0)
				$("div[class!='Clear']", TotAlloc).html($("<span>" + data.r.data[0].EARMARK_AMT + "</span>").formatCurrency().html());

			if(TotRem.length > 0)
				$("div[class!='Clear']", TotRem).html($("<span>" + data.r.data[0].TOTAL_REMAINING + "</span>").formatCurrency().html());
		},
		error: function(data, io){
			showError(data);
		}
	});
}




