function objExists(objTag, objId) {
	if ($(objTag+"#"+objId).size() > 0) {
	//if ($(objTag+"[id='"+objId+"']").size() > 0) {
		return true;
	} else {
		return false;
	}
}




function preloadLoadingBg() {
	var preload = ["/images/loading/loadingBg.png", "/images/loading/loadingCircle_999999.png"];
	$.each(preload, function(i){
		$("<img>").attr("src", $(this));
	});
}
preloadLoadingBg();


var checkIfLoading = false;
function createLoadingPage() {
	var loadingId = "gLoad_IMG";
	if (!objExists("div", loadingId)) {
		var bg = $("<div id='"+ loadingId +"'>");
		bg.css("width", "200px");
		bg.css("height", "200px");
		bg.css("position", "fixed");
		bg.css("top", (parseInt($(window).height()/2)-100).toString()+"px");
		bg.css("left", (parseInt($(window).width()/2)-100).toString()+"px");
		bg.css("overflow", "hidden");
		bg.css("background", "url(/images/loading/loadingBg.png) 0px 0px no-repeat");

		var circle = $("<div>");
		circle.css("width", "100px");
		circle.css("height", "100px");
		circle.css("position", "relative");
		circle.css("top", "50px");
		circle.css("left", "50px");
		circle.css("overflow", "hidden");

		var loadimg = $("<img width='100' height='800' align='absmiddle' border='0' src='/images/loading/loadingCircle_999999.png'>");
		loadimg.css("position", "absolute");
		loadimg.css("top", "0px");
		loadimg.css("left", "0px");
		if (!checkIfLoading) {
			loadimg.load(function(){
				loadingAnimation(100, loadingId);
			});
		}
		circle.append(loadimg);
		bg.append(circle);
		$(document.body).append(bg);
	}
}

function loadingAnimation(topPosition, loadingId) {
	checkIfLoading = true;
	if (topPosition > 700) {
		topPosition = 0;
	}
	$("#"+loadingId).contents().eq(0).contents().eq(0).css("top", "-"+topPosition+"px");
	topPosition += 100;
	setTimeout("loadingAnimation("+topPosition+", '"+loadingId+"')", 50);
}











function createLoadingPage2() {
	var imgId = "gLoad_IMG";
	if (!objExists("div", imgId)) {
		if (location.href.toLowerCase().indexOf("/pl.asp?m=") > -1) {
			$(createAlphaBody(null, null)).fadeIn(300, function(){});
			var msg = $("<div>");
			$(msg).css("color", "#ffffff");
			$(msg).css("font-size", "16px");
			$(msg).css("line-height", "24px");
			$(msg).css("font-weight", "bold");
			$(msg).css("position", "absolute");
			$(msg).css("top", "50px");
			$(msg).css("margin", "auto");
			$(msg).css("overflow", "hidden");
			$(msg).css("font-family", "verdana");
			$(msg).css("text-align", "center");
			$(msg).css("width", $(window).width().toString()+"px");
			$(msg).html("<strong style='font-size:36px'>PER STAMPARE IL DOCUMENTO</strong><br />QUANDO PRONTO UTILIZZA L'ICONA STAMPANTE IN ALTO A SINISTRA");
			$(msg).appendTo($("#createAlpha_DIV"));
		}


		var div = $("<div id='"+imgId+"'>");
		$(div).css("position", "fixed");
		$(div).css("z-index", "3");
		$(div).css("left", Math.round($(window).width()-200).toString()+"px");
		$(div).css("top", Math.round($(window).height()-50).toString()+"px");
		$(div).css("overflow", "hidden");
		$(div).css("width", "200px");
		$(div).css("height", "50px");
		$(div).css("background-image", "url(/images/template/loadingQuad200x50.gif)");
		$(div).appendTo(document.body);


	}
}



function closeOverlayDiv() {
	var arr = ["gpic_DIV", "alphaDivCloseButton_DIV", "createOverlay_DIV", "notes_DIV", "absoluteCenterContainer_DIV", "gLoad_IMG", "openOverTextarea_DIV", "awOverMask"];
	$.each(arr, function(){
		$("#"+this).remove();
	});
	closeAlphaBody();
}



function closeAlphaBody() {
	$("#createAlpha_DIV").fadeOut(200, function(){
		$("#createAlpha_DIV").remove();
	});
}

function createAlphaBody(c, a) {
	var divId = "createAlpha_DIV";
	if (!objExists("div", divId)) {
		$("input").blur();
		var divW = $(window).width();
		var divH = $(window).height();
		var alphaDiv = $("<div id='"+divId+"'>");
		$(alphaDiv).css("width", divW);
		$(alphaDiv).css("height", divH);
		$(alphaDiv).css("margin", "0px 0px 0px 0px");
		$(alphaDiv).css("padding", "0px 0px 0px 0px");
		$(alphaDiv).css("position", "fixed");
		$(alphaDiv).css("left", "0");
		$(alphaDiv).css("top", "0");
		if (a == null) {
			a = "0.9";
		}
		$(alphaDiv).css("opacity", a);
		$(alphaDiv).css("z-index", "2");
		if (c == null) {
			c = "#000000";
		}
		$(alphaDiv).css("background-color", c);
		//$(alphaDiv).hide();
		$(alphaDiv).appendTo(document.body);
		return $(alphaDiv);
	}
}


function slideLoading(imgId, yPos, t, picWidth, step) {
	var timing = 50;
	/*
	if (t >= parseInt(10000/timing)) {
		closeOverlayDiv();
	}
	*/
	if (objExists("img", imgId)) {
		t++;
	 	yPos -= picWidth;
	 	$("#"+imgId).css("top", yPos);
	 	if (yPos == parseInt(-picWidth*(step-1))) {yPos = 0;}
		loadTimeout = setTimeout("slideLoading('" + imgId + "', " + yPos + ", " + t + ", " + picWidth + ", " + step + ")", timing);
	}
}
