// Script ad uso del sito Parlamento

if(document.all && !document.getElementById) {
    document.getElementById = function(id) {
         return document.all[id];
    }
}


function keyPopUp(objLink, objEvent) {
	var iKeyCode;
    if (objEvent.keyCode)
		iKeyCode = objEvent.keyCode;
	else if (objEvent.which)
		iKeyCode = objEvent.which;
       // se il tasto non è Enter o Tab, 
       // ridà il controllo al browser
    if (iKeyCode != 13 && iKeyCode != 32) {
		return true;
	} else {
		window.open(objLink.href);
		return false; 
	}
}
var subArea = "";
var baseMargin = 0;
function setPopup()
{
    if (document.getElementsByTagName) {
		var hrefColl = document.getElementsByTagName("a");
	} else if (document.all) { 
		var hrefColl = document.all.tags("a");
	} 
	if (hrefColl != null) {	
		var i;
		for (i=0; i<hrefColl.length; i++) {
			if (hrefColl[i].getAttribute("rel") != null && hrefColl[i].getAttribute("rel") != '' && (hrefColl[i].getAttribute("rel") == '_blank' || hrefColl[i].getAttribute("rel") == 'schedaweb' || hrefColl[i].getAttribute("rel") == 'popup') ) {
				hrefColl[i].onclick = function(){
					window.open(this.href);
					return false;
				}
				hrefColl[i].onkeypress = function(event){return keyPopUp(this, event);}
				hrefColl[i].removeAttribute("rel");
				if (!hrefColl[i].title){
					hrefColl[i].title="Il link apre una nuova finestra";
				}
			}
		}
	}
	for (i=0; i<document.styleSheets.length - 1; i++) {
		if (document.styleSheets[i].href.indexOf("apem") > 0)  {
			var rules=document.styleSheets[i].cssRules? document.styleSheets[i].cssRules: document.styleSheets[i].rules;
			for (i=0; i<rules.length; i++) {
				if (rules[i].selectorText.toLowerCase()=="body")  {
					var bm = rules[i].style.marginLeft;
					baseMargin = bm.slice(0, bm.indexOf("px"));
				}
			}
		}
	}
	// richiamo una funzione di frameBuster perché alle volte le pagine Camera ritornano alla home rimanendo dentro al loro "chiosco" (frameset)
	buster();
	// aggiungo questa funzione qui perché così viene eseguita anch'essa all'onLoad
	// la funzione è ritenuta utile solo per la grafica del sito APEM
	if (subArea == "apem") {
		setMargin();  
	}
}

function WinWidth()	{
    var width = -1;
    if (window.innerWidth)
    {
        width = window.innerWidth;
    }
    else if (document.getElementsByTagName)
    {
        var html = document.getElementsByTagName('html');
        if (html[0].offsetWidth)
        {
            width = html[0].offsetWidth;
        }
    }
    return width;
}
function setMargin() {
	// per evitare che il disegno adattabile si scomponga per finestre troppo estese, imposta il margine in modo da non superare i 1280px 
	if (document.getElementsByTagName) {
		var body = document.getElementsByTagName('body');
	} else if (document.all) {
		var body = document.all.tags('body');
	}
	var margin = WinWidth() - 1280;
	if (margin > baseMargin) {
		body[0].style.marginRight = margin + 'px';
	} else {
		body[0].style.marginRight = baseMargin + 'px';
	}
}

function buster() {
	if (parent.frames.length > 0) top.location.replace(document.location); 
}
window.onresize = function() {if (subArea == 'apem') {setMargin();}}

