var nocover_path = "";
extArray = new Array(".jpg", ".jpeg", ".png", ".bmp", ".gif");
function check_Ext(file) {
	allowSubmit = false;
	if (!file) return;
	while (file.indexOf("\\") != -1)
	file = file.slice(file.indexOf("\\") + 1);
	ext = file.slice(file.indexOf(".")).toLowerCase();
	for (var i = 0; i < extArray.length; i++) {
		if (extArray[i] == ext) {
			allowSubmit = true;
			break;
		}
	}
	if (allowSubmit) return true;
	else
		alert("Il file selezionato non può essere utilizzato come immagine.\n"
			+ "Le estensioni ammesse sono:  "
			+ (extArray.join("  ")) + "\n"
			+ "Selezionare un nuovo file.");
	return false;
}

function addEvent(o, sEvent, fFunc) {
	if (window.attachEvent) {
		o.attachEvent("on" + sEvent, fFunc);
	} else if (window.addEventListener) {
		o.addEventListener(sEvent, fFunc, false);
	}
}

function addLoadEvent(fFunc) {
	addEvent(window, "load", fFunc);
}

/** * Adds skin to the <button> tags */
function initButtons() {
	var aConfAvailableButtonStyles = ["small"];
	var msieVersion = navigator.appVersion.match(/MSIE (\d+\.?\d*)/);
	var classNameRegEx = new RegExp("\\b(" + aConfAvailableButtonStyles.join("|") + ")\\b", "i");
	var d, s1, s2;
	var cButtons = document.getElementsByTagName("button");
	for (var i = 0, ii = cButtons.length; i < ii; i++) {
		if (classNameRegEx.test(cButtons[i].className) && !cButtons[i].initialized) {
			if (!cButtons[i].getAttribute("type")) cButtons[i].setAttribute("type", "button");
			d = document.createElement("div");
			d.innerHTML = cButtons[i].innerHTML;
			cButtons[i].innerHTML = "";
			s1 = document.createElement("span");
			s1.className = "left";
			s2 = document.createElement("span");
			s2.className = "right";
			if (document.all && msieVersion && msieVersion.length && msieVersion[1] && parseFloat(msieVersion[1]) < 7) {
				cButtons[i].appendChild(s1);
				cButtons[i].appendChild(s2);
				cButtons[i].appendChild(d);
			} else {
				d.appendChild(s1);
				d.appendChild(s2);
				cButtons[i].appendChild(d);
			}
			cButtons[i].onmousedown = function ()
				{
					var oDiv = this.getElementsByTagName("div")[0];
					oDiv.className = oDiv.className.replace(/\bmousedown\b/gi, "") + " mousedown";
				};
			cButtons[i].onmouseup = function ()
				{
					var oDiv = this.getElementsByTagName("div")[0];
					oDiv.className = oDiv.className.replace(/\bmousedown\b/gi, "");
					if (this.firstChild && this.firstChild.nextSibling && this.firstChild.nextSibling.nextSibling && this.firstChild.nextSibling.nextSibling.firstChild && this.firstChild.nextSibling.nextSibling.firstChild.nodeName == "A" && this.firstChild.nextSibling.nextSibling.firstChild.href) {
						location = this.firstChild.nextSibling.nextSibling.firstChild.href;
					}
				};
			cButtons[i].initialized = true;
		}
	}
}

addLoadEvent(initButtons);
var oFileUploadAttrs = {};

function fileUploader() {
	if (document.getElementById("file_input_section").value == "Reader Challange") {
		nocover_path = "";
	} else if (document.getElementById("file_input_section").value == "Link") {
		nocover_path = "";
	} else if (document.getElementById("file_input_section").value == "Comps") {
		nocover_path = "";
	} else if (document.getElementById("file_input_section").value == "Book") {
		if (location.hostname.substring(0, 9) == "localhost") {
			nocover_path = location.hostname + "/homerj.eu";
		} else {
			nocover_path = "www.homerj.eu";
		}
		nocover_path = "http://" + nocover_path + "/image/libri/_no_cover.jpg";
	} else if (document.getElementById("file_input_section").value == "User") {
		if (location.hostname.substring(0, 9) == "localhost") {
			nocover_path = location.hostname + "/homerj.eu";
		} else {
			nocover_path = "www.homerj.eu";
		}
		nocover_path = "http://" + nocover_path + "/image/avatar/nobody.gif";
	}

	var oFileUploader = document.getElementById("ImageFile");
	if (!oFileUploader) return;
	for (var i = 0, ii = oFileUploader.attributes.length; i < ii; i++) {
		if (oFileUploader.attributes[i].specified) {
			oFileUploadAttrs[oFileUploader.attributes[i].nodeName] = oFileUploader.attributes[i].nodeValue;
		}
	}
	oFileUploader.onchange = fileUploaderChangeListener;
}

addLoadEvent(fileUploader);

function fileUploaderChangeListener() {
	if (this.value.length) {
		var oDiv = document.getElementById("selectedImage");
		var pos = this.value.indexOf(":");
		if (pos == -1)  {
			oDiv.innerHTML = "<B>Per motivi di sicurezza, il browser utilizzato "
				+ "non permette il caricamento delle immagini dal computer locale<BR>"
				+ "L'immagine selezionata sarà visibile dopo il salvataggio.</B><BR><BR>"
				+ this.value;
		} else {
			oDiv.innerHTML = this.value;
		}
		if(!check_Ext(document.getElementById("ImageFile").value)) {
			document.getElementById("ImageFile").value = "";
			document.getElementById("input_image").src = nocover_path
			return;
		}
		if (pos == -1)  {
			document.getElementById ("input_image").src = nocover_path
		} else {
			document.getElementById ("input_image").src = "file:///" + document.getElementById("ImageFile").value;
		}
		document.getElementById ("image_url").value = document.getElementById("ImageFile").value;
		var obyButton = document.getElementById("SelectedUserImage");
		if (document.getElementById("NoImage")) return;
		var button = obyButton.cloneNode(true);
		button.id = "NoImage";
		var spans;
		if (obyButton.getElementsByTagName("DIV")[0].lastChild.tagName == "SPAN") {
			spans = obyButton.getElementsByTagName("span");
		}
		button.getElementsByTagName("DIV")[0].innerHTML = "Rimuovi Immagine";
		if (spans) {
			for (var j = 0, jj = spans.length; j < jj; j++) {
				button.getElementsByTagName("div")[0].appendChild(spans[j].cloneNode(true));
			}
		}
		obyButton.parentNode.insertBefore(button, obyButton.nextSibling);
		document.getElementById("NoImage").onclick = function ()
			{
				var oInputFile = document.createElement("input");
				for (var i in oFileUploadAttrs) {
					oInputFile.setAttribute(i, oFileUploadAttrs[i]);
				}
				oInputFile.onchange = fileUploaderChangeListener;
				document.getElementById("selectedImage").innerHTML = "";
				document.getElementById ("input_image").src = nocover_path;
				document.getElementById ("image_url").value = "";
				this.parentNode.removeChild(this);
			};
		if (window.isIE6) pngfix();
	}
}