var gScrollTextScrollActive = false;



function gScroll_dragSelect(s) {
	if (document.all) {
		document.documentElement.onselectstart = function() {
			if (s == "none") {
				return false;
			} else {
				return true;
			}
		}
	} else {
		document.documentElement.style.MozUserSelect = s;
	}
}



function gScroll_readFile(file, obj) {
	//file = "privacy_eu.xml";
	$(document).ready(function(){
		var fileType = file.substring(file.length-3, file.length);
		switch (fileType) {
			case "xml":
				fileType = "xml";
				break;
			case "txt":
				fileType = "text";
				break;
		}
		$.ajax({
			type: "GET"
			, url: file
			, dataType: fileType
			, cache: false
			, success: function(returnValue) {
				switch (fileType) {
					case "xml":
						$(returnValue).find("xmlText").each(function(){
							$(obj).append($(this).text());
						});
						break;
					case "text":
						$(obj).append(returnValue);
						break;
				}
			}
			, error: function(request, error, tipo_errore) {
				alert(error);
			}
		});
	});
}




function gScroll_moveText(scrollContainer, pos) {
	pos = Math.floor( ( ($(scrollContainer).height()-$(scrollContainer).parent().height()) / 100) * pos );
	$(scrollContainer).css("top", -pos);
}


function gScroll_fixHeight(scrollDiv, scrollContainer, scrollBar, scrollButton, scrollInfo) {
	if ($(scrollContainer).height() <= $(scrollDiv).height()) {
		$(scrollBar).hide();
		$(scrollButton).hide();
		$(scrollInfo).hide();
		$(scrollDiv).css("height", $(scrollContainer).height()+"px");
		$(scrollDiv).parent().css("height", $(scrollContainer).height()+"px");
	}
}



function gScroll_openText(id, e) {
	var obj = $("#"+id);
	var url = obj.attr("rel");
	var title = obj.attr("title");

	var scrollDiv = $("<div>");
	$(scrollDiv).css("width", obj.width().toString()+"px");
	$(scrollDiv).css("height", String(obj.height()-16)+"px");
	$(scrollDiv).css("clear", "both");
	$(scrollDiv).css("position", "relative");
	$(scrollDiv).css("overflow", "hidden");
	$(scrollDiv).appendTo(obj);

	var scrollInfo = $("<div>");
	$(scrollInfo).css("width", obj.width().toString()+"px");
	$(scrollInfo).css("height", "16px");
	$(scrollInfo).css("clear", "both");
	$(scrollInfo).css("text-align", "right");
	$(scrollInfo).css("line-height", "16px");
	$(scrollInfo).css("overflow", "hidden");
	$(scrollInfo).text("Posizione: 0%");
	$(scrollInfo).appendTo(obj);

	var scrollContainer = $("<div id='gScroll_container_DIV'>");
	$(scrollContainer).css("width", String($(scrollDiv).width()-30)+"px");
	$(scrollContainer).css("height", "auto");
	$(scrollContainer).css("line-height", "16px");
	$(scrollContainer).css("font-size", "10px");
	$(scrollContainer).css("clear", "both");
	$(scrollContainer).css("position", "absolute");
	$(scrollContainer).css("top", "0px");
	$(scrollContainer).css("left", "0px");
	$(scrollContainer).css("overflow", "hidden");
	$(scrollContainer).css("background", "#ffffff url(/images/template/textareaBg.gif) 0% 0% repeat-x-y");
	$(scrollContainer).appendTo(scrollDiv);
	gScroll_readFile(url, $(scrollContainer));


	var scrollBar = $("<div>");
	$(scrollBar).css("position", "absolute");
	$(scrollBar).css("top", "0px");
	$(scrollBar).css("left", String($(scrollContainer).width()+11)+"px");
	$(scrollBar).css("width", "8px");
	$(scrollBar).css("height", $(scrollDiv).height().toString()+"px");
	$(scrollBar).css("background-color", "#eeeeee");
	$(scrollBar).appendTo(scrollDiv);

	var scrollButton = $("<div>");
	$(scrollButton).css("position", "absolute");
	$(scrollButton).css("top", "0px");
	$(scrollButton).css("left", String($(scrollContainer).width()+8)+"px");
	$(scrollButton).css("width", "12px");
	$(scrollButton).css("height", "28px");
	$(scrollButton).css("border", "1px solid #047b99");
	$(scrollButton).css("background-color", "#b1b2e9");
	$(scrollButton).css("cursor", "move");
	$(scrollButton).css("opacity", "0.5");
	$(scrollButton).bind("mousedown", function() {
		gScroll_dragSelect("none");
		gScrollTextScrollActive = true;
		$(this).css("opacity", "1");
		$(document).bind("mousemove", function(e){
			if (gScrollTextScrollActive) {
				topPosition = e.pageY-$(scrollDiv).offset().top-15;
				if (topPosition < 0) {topPosition = 0;}
				if (topPosition > $(scrollBar).height()-30) {topPosition = $(scrollBar).height()-30;}
				$(scrollButton).css("top", topPosition+"px");
				topPosition = Math.floor( (topPosition*100) / ($(scrollBar).height()-30) );
				$(scrollInfo).text("Posizione "+topPosition.toString()+"%")
				gScroll_moveText($(scrollContainer), topPosition);
			}
		});
	});
	$(document).bind("mouseup", function() {
		gScroll_dragSelect("");
		gScrollTextScrollActive = false;
		$(scrollButton).css("opacity", "0.5");
	});
	$(scrollButton).appendTo(scrollDiv);
	setTimeout(function(){gScroll_fixHeight(scrollDiv, scrollContainer, scrollBar, scrollButton, scrollInfo);}, 1000);
}

$(document).ready(function(e){
	if (objExists("div", "textScrollDiv")) {
		gScroll_openText("textScrollDiv", e);
	}
});

