var lsw = "/images/loading_small_wait.gif";
var img_lsw = new Image();
img_lsw.src = lsw;


// Utility functions for showing loading images
$.fn.loading_layer2 = function() {
	remove_loading_layer();
    $(this).append('<div id="loading_dialog" style="display:none"></div>');
    var dims = bgSize();
	if ( navigator.userAgent.indexOf("MSIE") != -1 ) {
		dims[0] -= 22;
		dims[1] -= 4;
	}	
    $('#loading_dialog').css('width', dims[0]).css('height', dims[1]);
	if ( navigator.userAgent.indexOf("MSIE") == -1 ) {
		$('#loading_dialog').fadeIn(200)
	}
	else {
		$('#loading_dialog').show();
	}
}

$.fn.loading_layer = function() {
	remove_loading_layer();
    $(this).append('<div id="loading_dialog"></div>');
    var dims = bgSize();
	if ( navigator.userAgent.indexOf("MSIE") != -1 ) {
		dims[0] -= 22;
		dims[1] -= 4;
	}	
    $('#loading_dialog').css('width', dims[0]).css('height', dims[1]);
}

remove_loading_layer = function() {
    $('#loading_dialog').remove();
}

$.fn.show_loading_bar = function() {
	var lb = "/images/loading_bar.gif";
	var img1 = new Image();
	img1.src = lb;
	$(this).append('&#160;&#160;<img src="'+lb+'" class="indicator" id="loading_bar" />');
    return this;
}

hide_loading_bar = function() {
	$('#loading_bar').remove();
    return this;
}

$.fn.loading_small_wait = function() {
	$(this).html('<img src="/images/loading_small_wait.gif" class="indicator" />');
    return this;
}

$.fn.tiny_spinner = function() {
	$(this).html('<img src="/images/icons/iconLoadingTiny.gif" class="indicator" />');
    return this;
}

$.fn.small_spinner_on_dkgreen = function() {
	$(this).html('<img src="/images/icons/iconIndicatorDkGreenBG.gif" class="indicator" />');
    return this;
}

$.fn.small_spinner_on_blue = function() {
	$(this).html('<img src="/images/icons/iconIndicatorBlueBG.gif" class="indicator" />');
    return this;
}
$.fn.spinner_overlay = function() {
	$(this).append('<div class="loading_overlay"><img src="/images/icons/iconIndicator.gif" class="indicator" />Loading... Please do not click the back button.</div>');
	return this;
}

$.fn.large_spinner = function() {
	$(this).html('<img src="/images/icons/iconLoadingAnimation.gif" class="indicator" />');
    return this;
}

$.fn.relative_spinner = function() {
	var spin_pos = findPos( $(this).get(0) );
	spin_pos[0] += $(this).width() / 2;
	spin_pos[1] += $(this).height() / 2;
	$('#rel_spin').remove();
	$('body').append('<img src="/images/icons/iconLoadingAnimation.gif" id="rel_spin" class="no_display absolute" />');
	spin_pos[1] -= 50;//change this if the image changes ( .height() does not work when image is not displayed)
	$('#rel_spin')
			.css('left', spin_pos[0]+'px')
			.css('top', spin_pos[1]+'px')
			.fadeIn(200);
    return this;
}

$.fn.remove_relative_spinner = function() {
	$('#rel_spin').fadeOut(200, function(){
		$('#rel_spin').remove();
	})
    return this;
}

$.fn.large_blue_spinner = function() {
	$(this).html('<img src="/images/icons/iconIndicatorBlue.gif" class="indicator" />');
    return this;
}

String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};

function findWindowCenter(widthCorrect, heightCorrect){ 
    if (window.innerHeight || window.innerWidth) {      
        yScroll = window.innerHeight/2 + window.scrollY;
        xScroll = window.innerWidth/2 + window.scrollX;
    } else if (document.body.offsetHeight || document.body.offsetWidth){ // all but Explorer Mac
        yScroll = document.body.offsetHeight/2 + document.body.scrollTop;
		xScroll = document.body.offsetWidth/2
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        yScroll = document.body.offsetHeight/2;
        xScroll = document.body.offsetWidth/2;
    }
    yScroll += heightCorrect;
    xScroll += widthCorrect;
    return [xScroll, yScroll];
}

function findYScroll(){ 
    if (window.innerHeight || window.innerWidth) {      
        yScroll = window.scrollY;
    } else if (document.body.offsetHeight || document.body.offsetWidth){ // all but Explorer Mac
        yScroll = document.body.scrollTop;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        yScroll = document.body.offsetHeight/2;
    }
    return yScroll;
}



Array.prototype.has = function(value) {
	var i;
	for (var i = 0, loopCnt = this.length; i < loopCnt; i++) {
		if (this[i] == value) {
			return true;
		}
	}
	return false;
};

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function bgSize(){
    if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {      
        yScroll = window.innerHeight + window.scrollMaxY;
        xScroll = window.innerWidth + window.scrollMaxX;
        var deff = document.documentElement;
        var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
        xScroll -= (window.innerWidth - wff);        
        /*
        var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
        yScroll -= (window.innerHeight - hff);
        */
    } else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
        yScroll = document.body.scrollHeight;
        if ( navigator.userAgent.indexOf("MSIE") != -1 ) {
            xScroll = document.body.offsetWidth;       
        }
        else {
            xScroll = document.body.scrollWidth;       
        }
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        yScroll = document.body.offsetHeight;
        xScroll = document.body.offsetWidth;
    }
    return [xScroll, yScroll];
}
