wsm.include("event.js");
wsm.include("dom.js");

var mnuItem = new Array();
var td = new Array();
var rowNumber = 8;
var colNumber = 5;
var lastClickedControl = null;
var mnuStr;

if (document.all) {
	var cursHand = "hand";
	var cursNormal = "default";
} else {
	var cursHand = "pointer";
	var cursNormal = "normal";
}

var typo = new Image();
var blackArrow = new Image();
var greyArrow = new Image();
var greyArrowDown = new Image();

function readPageParameters() {
	// interpreta la stringa di URL 
	if (parent.document.URL) {
		callingURL = parent.document.URL;
		if (callingURL.indexOf('?') != -1) {
			callingURL = callingURL.substring(callingURL.indexOf('?')+1,callingURL.length);
			if (callingURL.indexOf("mnu=")!=-1) {
				start = callingURL.indexOf("mnu=")+4; 
				end = (callingURL.indexOf(";",start)!=-1 ? callingURL.indexOf(";",start) : callingURL.length);
				return callingURL.substring(start,end)
			}			
		}
	}
	return null
}

function mnu_Item(label, linkUrl, targetUrl, isASubMenuFlag, subMenuFlag, overStyle, outStyle, clickStyle) {
	this.label = label;
	this.linkUrl = linkUrl;
	this.targetUrl = targetUrl;
	this.isASubMenuFlag = isASubMenuFlag;
	this.subMenuFlag = subMenuFlag;
	this.overStyle = overStyle;
	this.outStyle = outStyle;
	this.clickStyle = clickStyle;
}

function td_Menu(level, objName, seqStart, overStyle, outStyle, clickStyle) {
	// --- Properties ---//
	this.name = objName;
	this.obj = document.getElementById(objName);
	this.css = document.getElementById(objName).style;
	this.overStyle = overStyle;
	this.outStyle = outStyle;
	this.clickStyle = clickStyle;
	this.empty = (mnuItem[seqStart] == "");
	this.seq = seqStart;
	this.level = level;
	this.clicked = false;
	this.opened = false;

	// --- Methods ---//
	this.out = td_out;
	this.over = td_over;
	this.setText = td_setText;
	this.setStyle = td_setStyle;
	this.clickOn = td_clickOn;
	this.showSubMenu = td_showSubMenu;
	this.hideSubMenu = td_hideSubMenu;

	// --- Initialization --- //
	this.setText(seqStart);
	this.out();

	return this
}

function td_setText(index) {
	this.obj.innerHTML = '<img src="' + typo.src + '" width="5">'
			+ mnuItem[index].label;
	this.empty = (mnuItem[index].label == "");
	this.seq = index;
}

function td_over() {
	if (!this.empty) {
		if (mnuItem[this.seq].overStyle)
			this.setStyle(mnuItem[this.seq].overStyle);
		else
			this.setStyle(this.overStyle);
	}
}

function td_out() {
	if (!this.clicked) {
		if (mnuItem[this.seq].outStyle)
			this.setStyle(mnuItem[this.seq].outStyle);
		else
			this.setStyle(this.outStyle);
	} else {
		if (mnuItem[this.seq].clickStyle)
			this.setStyle(mnuItem[this.seq].clickStyle);
		else
			this.setStyle(this.clickStyle);
	}
}

function td_clickOn(skipCallingUrl) {
	if (!this.empty) {
		hideAllDivs();
		var nl, i = 1;

		for (nl = 1; nl <= colNumber; nl++) {
			var i = 1;
			while (td[nl + "_" + i] && !td[nl + "_" + i].empty) {
				if (nl >= this.level || td[nl + "_" + i].opened) {
					td[nl + "_" + i].opened = false;
					td[nl + "_" + i].clicked = false;
					td[nl + "_" + i].out();
				}
				i++;
			}
		}

		if (mnuItem[this.seq].clickStyle)
			this.setStyle(mnuItem[this.seq].clickStyle);
		else
			this.setStyle(this.clickStyle);

		if (mnuItem[this.seq].subMenuFlag) {
			this.hideSubMenu();
			this.showSubMenu();
		}

		this.clicked = true;

		var skip = wsm_defined(skipCallingUrl) && skipCallingUrl;
		if (!skip) {
			if (!mnuItem[this.seq].isASubMenuFlag) {
				// Normale voce di menu
				if (!skipCallingUrl && mnuItem[this.seq].linkUrl != "") {
					this.opened = true;
					var LinkUrl = mnuItem[this.seq].linkUrl;
					var Target = mnuItem[this.seq].targetUrl;
					if (mnuItem[this.seq].linkUrl.indexOf("javascript:") == -1
							&& mnuItem[this.seq].linkUrl.indexOf("http://") == -1)
						LinkUrl = "http://www.tecnospa.com/IT" + LinkUrl;
					// ***
					// top.bottomFrame.document.write("");
					top.document.getElementById("bottomFrame").style.height = "476px";
					// **
					if (Target == "" || Target == "_self")
						top.document.location.href = LinkUrl;
					else {
						if (Target != "_blank" && top.frames[Target])
							eval("top." + Target + ".document").location.href = LinkUrl;
						else
							window.open(LinkUrl, "", "");
					}
				}
			} else {
				// Voce di menu che e' un menu collegato
				if (mnuItem[this.seq].linkUrl != "") {
					this.opened = true;
					var LinkUrl = mnuItem[this.seq].linkUrl;
					var Target = mnuItem[this.seq].targetUrl;
					if (mnuItem[this.seq].linkUrl.indexOf("javascript:") == -1
							&& mnuItem[this.seq].linkUrl.indexOf("http://") == -1)
						LinkUrl = "http://www.tecnospa.com" + LinkUrl;
					// ***
					// top.bottomFrame.document.write("");
					top.document.getElementById("bottomFrame").style.height = "476px";
					// **
					if (mnuStr != null)
						LinkUrl = LinkUrl + "?mnu=" + mnuStr;
	
					if (Target == "" || Target == "_self")
						top.document.location.href = LinkUrl;
					else {
						if (Target != "_blank" && top.frames[Target])
							eval("top." + Target + ".document").location.href = LinkUrl;
						else
							window.open(LinkUrl, "", "");
					}
				}
			}
		}
	}
	mnuStr = null;
}

function td_showSubMenu() {
	var i = 1, r = 1;
	var nl = (this.level + 1);
	while (mnuItem[this.seq + '-' + i]) {
		td[nl + "_" + r].setText(this.seq + '-' + i);
		td[nl + "_" + r].out();
		i++;
		r++;
		if (r > rowNumber) {
			nl++;
			r = 1;
		}
	}
}

function td_hideSubMenu() {
	var nl = (this.level + 1);
	for (nl; nl <= colNumber; nl++) {
		var i = 1;
		while (td[nl + "_" + i] && !td[nl + "_" + i].empty) {
			td[nl + "_" + i].setText("0");
			td[nl + "_" + i].clicked = false;
			td[nl + "_" + i].out();
			i++;
		}
	}
}

function td_setStyle(styleString) {
	switch (styleString) {
		case "outFullBlack" :
			this.css.cursor = cursNormal;
			this.css.color = "white";
			this.css.background = "black";
			if ("img_" + this.name)
				document.getElementById("img_" + this.name).src = typo.src;
			break;
		case "out1" :
			this.css.cursor = cursNormal;
			this.css.color = "black";
			if ("img_" + this.name)
				document.getElementById("img_" + this.name).src = typo.src;
			break;
		case "out" :
			this.css.cursor = cursNormal;
			this.css.color = "#333333";
			this.css.background = "#EFECE8";
			if (document.getElementById("img_" + this.name))
				document.getElementById("img_" + this.name).src = typo.src;
			break;
		case "overFullRed" :
			this.css.cursor = cursHand;
			this.css.color = "white";
			this.css.background = "red";
			break;
		case "over1" :
			this.css.cursor = cursHand;
			this.css.color = "red";
			break;
		case "over" :
			this.css.cursor = cursHand;
			this.css.color = "red";
			this.css.background = "#CCCCCC";
			break;
		case "clickFullRed" :
			this.css.cursor = cursHand;
			this.css.color = "white";
			this.css.background = "red";
			if ("img_" + this.name)
				document.getElementById("img_" + this.name).src = blackArrow.src;
			break;
		case "click1" :
			this.css.cursor = cursHand;
			this.css.color = "red";
			if (document.getElementById("img_" + this.name)) {
				if (mnuItem[this.seq].subMenuFlag)
					document.getElementById("img_" + this.name).src = blackArrow.src;
				else {
					if (mnuItem[this.seq].linkUrl != ""
							&& mnuItem[this.seq].targetUrl == "")
						document.getElementById("img_" + this.name).src = greyArrowDown.src;
					else
						document.getElementById("img_" + this.name).src = typo.src;
				}
			}
			break;
		case "click" :
			this.css.cursor = cursHand;
			this.css.color = "red";

			this.css.background = "#CCCCCC";
			if (document.getElementById("img_" + this.name)) {
				if (mnuItem[this.seq].subMenuFlag)
					document.getElementById("img_" + this.name).src = greyArrow.src;
				else {
					if (mnuItem[this.seq].linkUrl != ""
							&& mnuItem[this.seq].targetUrl == "")
						document.getElementById("img_" + this.name).src = greyArrowDown.src;
					else
						document.getElementById("img_" + this.name).src = typo.src;
				}
			}
			break;
		default :
			break;
	}
}

function clickMenuItem(seq, skipCallingUrl) {
	var skip = wsm_defined(skipCallingUrl) && skipCallingUrl;
	var mnuSeq = seq.split("-");
	for (i = 0; i < mnuSeq.length; i++) {
		if (mnuSeq[i] > rowNumber) {
			var x = Math.floor(parseInt(mnuSeq[i]) / rowNumber);
			var y = parseInt(mnuSeq[i]) % rowNumber;
			if (y == 0)
				td[(i + x) + "_" + rowNumber].clickOn(skip);
			else	
				td[(i + x + 1) + "_" + y].clickOn(skip);
		} else
			td[(i + 1) + "_" + mnuSeq[i]].clickOn(skip);
	}
}

function hideAllDivs() {
	var divCerca = document.getElementById('DivCerca');
	if (divCerca)
		divCerca.style.visibility = 'hidden';
}

var divCercaInited = false;
function showSearchDiv(areaId) {
	var searchForm = $('wsm_siteSearch_form');
	if (searchForm) {
		$('wsm_siteSearch_mainFieldName').value = "content, short-content";
		$('wsm_siteSearch_searchAreaId').value = areaId;
	}
	var divCerca = $('DivCerca');
	if (divCerca) {
		if (!divCercaInited) {	
			positionDivCerca();
			Event.add(window, 'resize', function(e) {
				positionDivCerca()
			});
			divCercaInited = true;
		}
		divCerca.style.visibility = 'visible';
	}
}

function positionDivCerca() {
	var divCerca = $('DivCerca');
	var p = DOM.getAbsPosition($('img_3_5'));
	divCerca.style.left = p.absLeft + "px";
	divCerca.style.top = p.absTop + "px";		
}

function downloadObject(societa, folder, subpath) {
	top.location.href = "/Magic94Scripts/mgrqispi94.dll?APPNAME=eSiMa05&PRGNAME=DownloadObject&ARGUMENTS=-A"
			+ societa + ",-A" + folder + ",-A" + subpath
}

function menuInit(typoSrc, blackArrowSrc, greyArrowSrc, greyArrowDownSrc) {
	typo.src = typoSrc;  
	blackArrow.src = blackArrowSrc;   
	greyArrow.src = greyArrowSrc;  
	greyArrowDown.src = greyArrowDownSrc;
	
	mnuStr = readPageParameters();
	
	for (i = 1; i <= rowNumber; i++) {
		if (mnuItem[i])
			td["1_" + i] = new td_Menu(1, "1_" + i, i, "over1", "out1","click1");
		else
			td["1_" + i] = new td_Menu(1, "1_" + i, 0, "over1", "out1", "click1");
	}

	for (i = 1; i <= rowNumber; i++)
		td["2_" + i] = new td_Menu(2, "2_" + i, "0", "over", "out", "click");
	for (i = 1; i <= rowNumber; i++)
		td["3_" + i] = new td_Menu(3, "3_" + i, "0", "over", "out", "click");
	for (i = 1; i <= rowNumber; i++)
		td["4_" + i] = new td_Menu(4, "4_" + i, "0", "over", "out", "click");
	for (i = 1; i <= rowNumber; i++)
		td["5_" + i] = new td_Menu(5, "5_" + i, "0", "over", "out", "click");

	if (mnuStr != null) {
		var mnu = mnuStr.split(",")[0];
		if (mnu != null)
			clickMenuItem(mnu);
	} else {
		var pageURL = document.location.href.replace(/%20/gi, " ");
		for (var s in mnuItem) {
			var l = mnuItem[s].linkUrl;
			if (l != "" && compareURLs(pageURL, l)) {
				clickMenuItem(s, true);
				break;
			}
		}
	}
}

function compareURLs(url1, url2) {
	var url1Split = url1.split("?");
	var url2Split = url2.split("?");
	
	if (url1Split[0] != url2Split[0])
		return false;
	if (url1Split[1] == null && url2Split[1] == null)
		return true;
		
	var paramsUrl1 = [];
	var paramsUrl2 = [];
	if (url1Split[1] != null) {
		var paramsUrl1 = url1Split[1].split("&");
		paramsUrl1.sort();
	}
	if (url2Split[1] != null) {
		var paramsUrl2 = url2Split[1].split("&");
		paramsUrl2.sort();
	}
	for (var i=0; i<paramsUrl1.length; i++)
		if (paramsUrl1[i] != paramsUrl2[i])
			return false;
	
	return true;
}