var App = {
	height: null,
	width: null,
	_pageXOffset: 0,
	_pageYOffset: 0,
	_intMsg: 0,
	_intMsgClosed:0,

	// ... Boolen used to disable certain effects that IE6 is just too lame to handle.
	isIE6: (navigator.appVersion.match('MSIE 6')) ? true : false,
	isIE7: (navigator.appVersion.match('MSIE 7')) ? true : false,
	isIE8: (navigator.appVersion.match('MSIE 8')) ? true : false,
	isIE: (navigator.appName == 'Microsoft Internet Explorer') ? true : false,

	// ... Properties
	scrollTop: function() {
		if (self.pageYOffset) {
			return self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			return document.documentElement.scrollTop;
		} else if (document.body) {
			return document.body.scrollTop;
		} else {
			return 0;
		}
	},

	scrollLeft: function() {
		if (self.pageXOffset) {
			return self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollLeft) {
			return document.documentElement.scrollLeft;
		} else if (document.body) {
			return document.body.scrollLeft;
		} else {
			return 0;
		}
	},

	onScroll: function() {
		App._pageXOffset = App.scrollLeft();
		App._pageYOffset = App.scrollTop();

//			if ($('LOADING'))
//				$('LOADING').style.top = App._pageYOffset + 'px';
	},

	doneGoBack: function() {
		var previous = document.referrer;
		var current = window.location.href;

		if (App.stripQueryString(current) == App.stripQueryString(previous)) {
			window.location.replace(App.stripQueryString(previous));
		} else {
			//window.location.replace(previous);
			history.back();
		}
	},

	stripQueryString: function(url) {
		return url.replace(/\?.*$/, '');
	},

	wait: function() {
		$('BODY').css({cursor:'wait'});
		$('#LOADING').show('fast');
	},

	ready: function() {
		$('#LOADING').hide('fast');
		$('BODY').css({cursor:'default'});
	},

	block: function() {
		$('#WAIT_BLOCK').css('display', 'block');
	},

	unblock: function() {
		$('#WAIT_BLOCK').css('display', 'none');
	},

	setCookie: function (name, value, days) {
	  if (days) {
	    var date = new Date();
	    date.setTime(date.getTime()+(days*24*60*60*1000));
	    var expires = "; expires="+date.toGMTString();
	  }
	  else var expires = "";
	  document.cookie = name+"="+value+expires+"; path=/";
	},

	getScreenSize : function() {
		if (document.all) {
			this.width = document.body.clientWidth;
			this.height = document.body.clientHeight;
		} else {
			this.width = innerWidth;
			this.height = innerHeight;
		}
	},

	getCookie: function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
	},

	delCookie: function(name) {
        setCookie(name,"",-1);
	},

	message: function(strMessage) {
		App._intMsg++;
		$('#pnlPageMessages').show('fast');
		var strTag = '<p id="lblPageMessage'+ App._intMsg +'">'+ strMessage +'<br /><a href="javascript:App.hideMessage('+ App._intMsg +')">OK</a></p>';
		$('#pnlPageMessages').append(strTag);
	},

	hideMessage: function(intMessage) {
		App._intMsgClosed++;
		$('#lblPageMessage' + intMessage).hide('fast');
		if (App._intMsgClosed == App._intMsg) {
			$('#pnlPageMessages').hide('fast');
		}
	},
	
	toggleLink: function(ele) {
		var imgId = ele.id.replace ('Link_', '');
		var divId = imgId.replace ('Toggle_', '');

		//alert ('imgId = ' + imgId + ', divId = ' + divId);

		if ($('#'+divId)) {
			if ($('#'+divId).css('display') == "none") {
				$('#'+divId).show();
				ele.innerHTML = ele.innerHTML.replace('Show','Hide');
				$('#'+imgId).attr('class', $('#'+imgId).attr('class').replace (/\_off$/, '_on'));
			} else {
				$('#'+divId).hide();
				ele.innerHTML = ele.innerHTML.replace('Hide','Show');
				$('#'+imgId).attr('class', $('#'+imgId).attr('class').replace (/\_on$/, '_off'));
			}
		}

		ele.blur();
	}
}

$(document).ready(function() {
	$('#LOADING').hide('fast');
	App.getScreenSize();
	window.onscroll = App.onScroll;
	window.onresize = App.getScreenSize;

	// If we have any special date values selectors, create the selection box
	//    And mount event handlers
	if ($('DIV.special_date_values').length > 0) {
		$('BODY').append('<div id="special_date_selection"><ul><li><a>{now}</a></li><li><a>{one_hour_ago}</a></li><li><a>{today}</a></li><li><a>{yesterday}</a></li><li><a>{a_week_ago}</a></li><li><a>{a_week_ago_plus_one}</a></li><li><a>{last_week}</a></li><li><a>{this_week}</a></li><li><a>{a_month_ago}</a></li><li><a>{a_month_ago_plus_one}</a></li><li><a>{last_month}</a></li><li><a>{this_month}</a></li></ul></div>');

		$('DIV.special_date_values').mouseenter(function() {
			var sel = $('#special_date_selection');
			$(this).append(sel);
			var offset = $(this).offset();
			sel.offset(offset);
			sel.stop(true, true);
			sel.show('fast');
		}).mouseleave(function() {
			$('#special_date_selection').stop(true, true);
			$('#special_date_selection').hide('fast');
		});

		$('#special_date_selection').mouseleave(function() {
			$(this).stop(true, true);
			$(this).hide('fast');
		}).click(function() {
			$(this).hide('fast');
		});

		$('#special_date_selection UL LI A').click(function() {
			var value = $(this).text();
			var id = $(this).parent().parent().parent().parent().attr('id').replace(/^[^_]+_/, '');
			$('#'+id).val(value).change();
		});
	}

	/// This isn't working quite right and is broken in IE7, IE8 -- probably a simpletip bug -- 2011-08-04 kag
	if (!App.isIE6 && !App.isIE7 && !App.isIE8) {
		$('A.icoHelp IMG, .help_icon').each(function() {
			var content_id = 'Help_' + this.id.replace(/^help_(div|img)_/, '') + '_Body';
			//alert('for [' + this.id + '] the content id is [' + content_id + '] and content is [' + $('#'+content_id).html() + ']');

			$(this).simpletip({
				content: $('#'+content_id).html(),
				fixed: true,
				position: (document.body.className == 'wide') ? 'right' : 'top'
			});
		});
	}
	//*/
	
	// Toggle links
	$('A.togglePlus,A.toggleLink').click(function() {
		App.toggleLink(this);
	}).mouseout(function() {
		window.status = '';
		return true;
	});
	
	if (typeof $.datepicker != 'undefined') {
		$('INPUT.inputDateTime').datepicker({dateFormat: 'yy-mm-dd 00:00:00', buttonImage: '/_img/calendaricon.gif', showOn: 'button', buttonImageOnly: true});
	}
});

// ... For lack of a better place (for now)
Array.prototype.has = function(needle) {
	for (var i=0;i<this.length;i++) {
		if(this[i] == needle){ return true; }
	}
	return false;
}
