// AJAX FORM SUBMISSION GLOBALS
var gsResponse = '';
var gsSendingForm = '';

function detectTimezone(){

	var dtDate = new Date();
	return dtDate.getTimezoneOffset();

}

// SEE 'TZ' VAR NOTES IN bootstrap.php FILE WHERE THIS IS PICKED BACK UP.
gsTZ = detectTimezone();
document.cookie = 'TZ=' + escape(String(gsTZ)) + ';path=/';

function pauseTime(millis) {
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while(curDate-date < millis);
}

// handleResponse, hideBusy, getServerResponse, and showBusy are functions I repeatedly use for AJAX form submits

function handleResponse(sResponse) {
	if (sResponse.indexOf('SUCCESS: ') != -1) {
		try {
			handleSuccess(sResponse);
		} catch(e1) {}
	} else {
		sResponse = sResponse.split('ERROR: <br />').join('');
		sResponse = sResponse.split('ERROR: ').join('');
		$('#errorbox').html(sResponse).fadeIn('slow');
		try {
			handleError(sResponse);
		} catch(e2) {}
	}
}

function hideBusy() {
	$('#progress').hide();
	$('#' + gsSendingForm + ' FIELDSET').css('visibility','visible');
	handleResponse(gsResponse);
	gsSendingForm = '';
	gsResponse = '';
}

function getServerResponse(sResponse, sStatus) {
	gsResponse = sResponse;
	setTimeout('hideBusy();clearTimeout();',900);
}

function showBusy(asFormData, oForm, asOptions) {
	$('#errorbox').hide();
	$('#infobox').hide();	
	gsSendingForm = oForm[0].id;
	$('#' + gsSendingForm + ' FIELDSET').css('visibility','hidden');
	$('#progress').show();
}

// give us the ajaxify function for a form
(function($) {
	$.fn.ajaxify = function() {
	
		$(this).ajaxForm({beforeSubmit: showBusy, success: getServerResponse, resetForm: false, clearForm: false});	
	
	};
})(jQuery);

function bookmarksite(title, url){
	if (document.all)
	window.external.AddFavorite(url, title);
	else if (window.sidebar)
	window.sidebar.addPanel(title, url, "")
}


$().ready(function(){ //when page has fully loaded

    $.ajaxSetup ({
        // Disable caching of AJAX responses */
        cache: false
    });

	try {$(':text:visible:enabled:first').focus();}catch(e3){}

	$('A,.button,.nodrag')
		.css('MozUserSelect','none')
        	.bind('selectstart', function() {
            		return false;
        	})
		.bind('draggesture', function() {
        		return false;
        	});
        	
	$(':text').bind('focus', function() {
	  try{$(this).caret(this.value.length);}catch(e4){}
	});	
	
	$("a.ctip").cluetip({
		splitTitle: '|', // use the invoking element's title attribute to populate the clueTip...
	                     // ...and split the contents into separate divs where there is a "|"
		showTitle: false // hide the clueTip's heading
	});	
	
	$('#search-results TD A').click(function(e) {
		var s = $(this).contents('IMG').attr('alt');
		if (s == 'Comment') {
			var sLink = $(this).attr('href');
			e.stopPropagation();
			location.href = sLink;
			return false;			
		}
		e.stopPropagation();
		return true;	
	});

	$('#search-results TD').click(function() {
		var s = $(this).siblings(':first-child').contents('a').attr('href');
		window.open(s);
	});
	
	$('#paginator TD').click(function() {
		var sWhich = $(this).html();
		sWhich = escape(sWhich);
		sWhich = (sWhich.indexOf('Next') != -1) ? 'Next' : sWhich;
		sWhich = (sWhich.indexOf('Prev') != -1) ? 'Prev' : sWhich;
		sWhich = sWhich.toLowerCase();
		sWhich = (sWhich == 'first') ? 1 : sWhich;
		sWhich = (sWhich == 'prev') ? gnPrev : sWhich;
		sWhich = (sWhich == 'next') ? gnNext : sWhich;
		sWhich = (sWhich == 'last') ? gnLast : sWhich;
		$('#p').val(sWhich);
		$('#search-alt FORM').submit();
	});
	
	$('textarea.resizable:not(.processed)').TextAreaResizer();
	$('textarea.limit-comment').textlimit('span.counter',2000);	
	
});

function resortRecords(sWhich) {
	var s = $('#' + sWhich).html();
	s = escape(s);
	s = s.split('%20').join(' ');
	s = s.split('%u25B4').join('');
	s = s.split('%u25BE').join('');		
	s = $.trim(s);
	var sSort = s;
	var sDir = $('#d').val();
	if (sDir == 'desc') {
		s = s + ' &#9652;';
		sDir = 'asc';
	} else {
		s = s + ' &#9662;';
		sDir = 'desc';
	}
	sSort = sSort.toLowerCase();
	$('#' + sWhich).html(s);
	$('#d').val(sDir);
	$('#s').val(sSort);
	$('#p').val(1);
	$('#search-alt FORM').submit();
}


