/*
* Merrcury JQuery Functions
*/
var image = function(img){
	return imagesUrl + img;
}
 $(document).ready(function(){

   	$("a")
		.filter(".menu_group")
			.toggle(
				function(){
					$('#'+this.id+'_div').show();
				},function(){
					$('#'+this.id+'_div').hide();
				}
			)
	.end();	
	
	numberFormat = function(x, sep){
		  x += '';
		  var rgx = /(\d+)(\d{3})/;
		  while (rgx.test(x))
		    x = x.replace(rgx, '$1' + sep + '$2');
		  return x;
	}

	// Browser detection for Chrome
	if ($.browser.chrome = (navigator.userAgent.toLowerCase().indexOf('chrome') > -1)){
		$.browser.safari = false;
	}
 });
 
function responseXmltoArray(xml){
	var arr = [];
	if (xml.childNodes.length > 0){
		for (var i=0; i < xml.childNodes.length; i++){
			if(xml.childNodes[i].nodeName != '#text' && xml.childNodes[i].nodeName != '#cdata-section') {
				arr[xml.childNodes[i].nodeName] = responseXmltoArray(xml.childNodes[i]);
			} else if(xml.firstChild != null) {
				return xml.firstChild.nodeValue;
			}
		}
	}
	return arr;
}

function sendMsgToModal(divId, msg){
	container = $('#' + divId + '  div.msgFromParent p');
	container.html(msg);
	$('#' + divId + '  div.msgFromParent').show();
	
}


scrollLock = function(e) {
    var a=e.data.pos;
    window.scrollTo(a[0],a[1]);
    return false;
}

;(function($) {
	$.fn.autoSize = function(options){
		o = jQuery.extend({
			overflow_y: 'scroll',
			overflow_x: 'hidden',
			adjust: function(e){
		        pos = $(e).offset();
		        $(e).height($(window).height()-pos.top-20);
			}
		},options);
		this.each(
			function(){
				new $.AutoSize(this, o);
			}
		);
		return this;		
	};
	$.AutoSize = function(element, options){
	    $(window).resize(function(){
			$(element).trigger("adjust");
	    });
	    $(element).bind("adjust",function(){
	    	$(element).css('overflow-y',options.overflow_y);
	    	$(element).css('overflow-x',options.overflow_x);
	    	options.adjust(element); 
	    });
	    $(element).trigger("adjust");
	};	
})(jQuery);

$().ready(function() {
	var ajaxActiveRequests = 0;  
	
	$('#ajax-error-dialog').jqm({modal: true, trigger: false});
	$('#ajax-error-dialog-btn').click(function() {
		window.onbeforeunload = null;
		location.reload(true);
	});
	
	onAjaxError = function (event, XMLHttpRequest, ajaxOptions, thrownError) {
		// if current operation is in abort mode, it doesn't close the window
		if (XMLHttpRequest.status < 300) {
			return;
		}
		
		
		msg = ajaxMessages['default'];
		if (ajaxMessages[XMLHttpRequest.status]) { 
			msg = ajaxMessages[XMLHttpRequest.status];
		}
		else if (XMLHttpRequest.status >= 400) {
			msg = msg + ' (' + XMLHttpRequest.status + ')';
		}
		showAjaxError(msg);
	}
	
	showAjaxError = function(msg) {
		window.onbeforeunload = null;
		$('.popUp:visible').jqmHide();
		if (msg) {
			$('#ajax-error-dialog').find('div.ajaxAlertContent').html(msg);
		}
		$('#ajax-error-dialog').jqmShow();
	}
	
	$.ajaxSetup({cache: true});
	$().bind('ajaxError', onAjaxError);
 });