// JavaScript Document

var lastCat = {
	h:   '',
	c:   '',
	cid:  '',
	pid: ''
}

window.onload = function() {
	createLinkTree(this.$('PC INDUSTRIALI'), 1);
	createLinkTree(this.$('PC BOARD E SISTEMI EMBEDDED'), 2);
	createLinkTree(this.$('PANEL PC e SOLUZIONI LCD'), 3);
	createLinkTree(this.$('ETHERNET INDUSTRIALE'), 4);
	createLinkTree(this.$('VIDEOCAPTURE - DAQ - I/O'), 5);
	createLinkTree(this.$('REAL TIME HYPERVISOR'), 6);
	createLinkTree(this.$('GPS PER LOCALIZZAZIONE e TIMING'), 7);

	new Accordian('links',5,'header_highlight');

	var pageCode = 0;
	var prodCode = 0;
	
	switch(this.pageType) {
		case 'AZIENDA'  : pageCode = 1; break;
		case 'CONTATTI' : pageCode = 2; break;
		case 'PARTNERS' : pageCode = 3; break;
		case 'NEWS'     : pageCode = 4; break;
		case 'SUPPORTO' : pageCode = 5; break;
		case 'DOWNLOAD' : pageCode = 6; break;
		case 'AREA_RIS' : pageCode = 7; break;
		default: 
			try {
				prodCode = this.openTree(this.sCatUrl); 
			} catch (e) {
				//do nothing
			};
			break;
	}
	
	importFlashMovies(pageCode, prodCode);
	
	if(this.$('news'))
		initDOMnews();
}

function importFlashMovies(pageCode, prodCode) {

	var animazioneLogo = new SWFObject("/flash/common/Animazione_Logo03_common.swf?menuPath="+this.menuPath , "links", 810, 75, "0", "");
	animazioneLogo.write("animazioneLogo");

	var menuParams = pageCode != 0 ? "?menuPath="+this.menuPath+"&pageCode="+pageCode: "?menuPath="+this.menuPath;			
	var menuX = new SWFObject("/flash/common/Animazione_Menu_X.swf" + menuParams, "links", 810, 25, "0", "");
	menuX.write("animazioneMenu");
	
	var prCParam = prodCode != 0 ? "?prodCode=" + prodCode: "";
	
	if(this.$("animazioneFlash"))
	{	
		var bannerFlash = new SWFObject("/flash/prodotti/Banner_Flash.swf" + prCParam, "links", 810, 110, "0", "");
		bannerFlash.write("animazioneFlash");
	}
	
	if(pageCode == 1) {
        var dallaAallaZ = new SWFObject("/flash/azienda/Banner_DallaAallaZ.swf" , "dallaAallaZ", 400, 131, "0", "");
		dallaAallaZ.write("dallaAallaZ");
		
        var successi = new SWFObject("/flash/azienda/successi.swf" , "successi", 400, 211, "0", "");
		successi.write("successi");
		
		var eventi = new SWFObject("/flash/azienda/Banner_Eventi.swf" , "eventi", 400, 200, "0", "");
		eventi.write("eventi");
	}
}

function openTree(sCatUrl) {

	
	var localUrl = sCatUrl? sCatUrl: window.location.toString();
	
	var theHeader  = null;
	var theContent = null;	
	
	url = unescape(localUrl).toLowerCase();
		
	var divList = this.$('links').getElementsByTagName('div');
	for(var i = 0; i < divList.length; i++) {

		if(hasClass(divList[i], 'accordion_headings')) {
			var header = divList[i];

			var sctnName = header.id.substring(0,header.id.indexOf('-'))+'-content';
			var content = this.$(sctnName);

			//find if link is in header
			if(unescape(header.firstChild.href.toLowerCase()) == url) {
				theHeader  = header;
				theContent = content;
				
				this.lastCat.h = header.id;
				this.lastCat.c = content.id;
				this.lastCat.pid = '';
				this.lastCat.cid = '';
					
				break;
			} else {
				//find if link is in content
				var innerLink = findInnerLink(content.getElementsByTagName('a'), url);

				if(innerLink) {
					this.lastCat.pid = innerLink.parentNode.id.substring(3);
					this.lastCat.cid = innerLink.id;
					this.lastCat.h = header.id;
					this.lastCat.c = content.id;
					
					theHeader  = header;
				    theContent = content;
				
					break;
				}
			}
		}
	}

	if(this.lastCat.pid != '')
		oc(this.lastCat.pid, 1);
	
	if(this.lastCat.cid != '')	
		oc(this.lastCat.cid, 1);
	
	var prodCode = 0;
	if(theHeader) {
		addClass(theHeader, 'header_highlight');
		prodCode = parseInt(theHeader.id.substring(5,theHeader.id.indexOf('-'))) + 1;
	}
	
	if(theContent != null && this.lastCat.cid == '') { 
		ex(theContent);
	} else {
		ex(theContent, true);
	}
	
	return prodCode;
}

function findInnerLink(linkList, url) {
	var innerLink;
	
	for(var j = 0; j < linkList.length; j++) {
		if(unescape(linkList[j].href.toLowerCase()) == url) {
			innerLink = linkList[j];
			break;
		}
	}
	
	return innerLink;
}

function hasClass(obj, className) {
   var pattern = new RegExp('(^|\\s)' + className + '(\\s|$)'); //use this regexp
   return pattern.test(obj.className); //to check for the class
}

function addClass (obj, className) {
   if (!hasClass(obj, className)) { //if the class isn't there already
      obj.className += (' ' + className); //append it to the end of the class list
   }
}
 
function removeClass (obj, className) {
   var pattern = new RegExp('(^|\\s)' + className + '(\\s|$)'); //use this regexp
   obj.className = obj.className.replace(pattern, ' '); 
}

