function errh(msg,ex) {
	  if(ex.javaClassName=="it.lottomatica.club.web.authentication.AuthenticationException"){
		  location.href="/arearicevitori/common/accessoNegato.html";
	  }
}

dwr.engine.setErrorHandler(errh);
jQuery.noConflict();
getSection();

function getUrlVars(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
}
function getUrlVar(name){
    return getUrlVars()[name];
}

function creaSottoMenu(sottoMenu){
	document.getElementById('menuL2').innerHTML=sottoMenu;
}
function creaMenu(menu){
	if(menu!=null && menu!=''){
		document.getElementById('menuPrincipale').innerHTML=menu;
		document.getElementById('menuPrincipale').style.display='';
	}
	paintMenu();
} 

function getSection(){
	function initFromSection(section){
		if(section == null || section == '')
			_section=getParam("sezione");
		else
			_section=section;
		if(_section == null || _section == '')
			_section='pub';
		loadAllcss();
	}
	if(window.authenticationProxy != null){
		authenticationProxy.getUserSection(initFromSection);
	}else{
		_section=getParam("sezione");
	}
}

function loadMenu(){
	if(_section!='' && _section!='pub'){
		var sottosezione = unescape(getUrlVar('sottosezione'));
		var nome_sottosezione = unescape(getUrlVar('nome_sottosezione'));
		dwr.engine.beginBatch();
			menuProxy.getSottoMenu(sottosezione,creaSottoMenu);
			if(document.getElementById('menuPrincipale')){
				document.getElementById('menuPrincipale').style.display='none';
				menuProxy.getMenu(sottosezione,nome_sottosezione,creaMenu);
			}
		dwr.engine.endBatch();	
	}
	if(_section == 'pub'){
		document.getElementById('menuPrincipale').style.display='';
		paintMenu();
	}
	if(_section == null || _section == ''){
		window.setTimeout("loadMenu()", 1000);
	}else{
		loadHeaderFooterLink();
	}
}

function loadHeaderFooterLink(){
	if(_section=='club' || _section=='ricevitori')
		document.getElementById('Homepage').href='/arearicevitori/privato/index.html?sottosezione=home';
	else if(_section=='gevonline')
		document.getElementById('Homepage').href='/arearicevitori/privato/gevonline/index.html?sottosezione=home';
	else if(_section=='pub'||_section=='')
		document.getElementById('Homepage').href='/arearicevitori/pub/index.html';
	for(i=1;i<5;i++){
		document.getElementById('testata'+i).href=document.getElementById('testata'+i).href.replace('@sezione@' , _section)+'?sezione='+_section;
	}
	if(_section=='gevonline'){
		document.getElementById('footer1').href='https://www.grattaevincionline.it/chisiamo/chi_siamo.do';
		document.getElementById('footer1').target="_black"; 
		document.getElementById('footer2').href='https://www.grattaevincionline.it/alfresco/terminicg/notelegali.html';
		document.getElementById('footer2').target="_black";
		document.getElementById('footer3').style.display='none';
	}else{
		for(i=1;i<3;i++)
			document.getElementById('footer'+i).href=document.getElementById('footer'+i).href.replace('@sezione@',_section)+'?sezione='+_section;
	}
}

function displayDefaultSubmenu(newStyle) {
	if (_section) {
		var defaultSub = document.getElementById('sub_' + _subsection);
		if(defaultSub != undefined) {
			defaultSub.style.display = newStyle;
		}
	}
} 

function paintMenu(){
	
	var subsection = getParam("sottosezione");
	var subsection_name= getParam("nome_sottosezione");
	var my_page_name =  getParam("p");
	var sottomenu_page_id = 'sottomenu_' + my_page_name;
	var sottomenu_page_orizzontale_id = 'sottomenu_foglia_' + subsection_name;
	
	var my_channel_li = document.getElementById(subsection);
	
	if( my_channel_li )
	{ 
		my_channel_li.className = "on"; 
	}
	
	displayDefaultSubmenu("block");

	//var my_page_name =  getParam("p");
	var my_page_li = document.getElementById( my_page_name );
	if( my_page_li )
	{
		my_page_li.className= "on";
	}

	//var sottomenu_page_id = 'sottomenu_' + my_page_name;
	var my_page_sottomenu = document.getElementById( sottomenu_page_id );
	if( my_page_sottomenu )
	{
		my_page_sottomenu.className= "on";
	}

	//var sottomenu_page_orizzontale_id = 'sottomenu_foglia_' + _sottosezione_privato;
	var my_page_sottomenu = document.getElementById( sottomenu_page_orizzontale_id );
	if( my_page_sottomenu )
	{
		my_page_sottomenu.className= "on";
	}

	//var sottomenu_page_gold_id = _sottosezione_privato;
	//var my_page_sottomenu_gold = document.getElementById( sottomenu_page_gold_id );
	var my_page_sottomenu_gold = document.getElementById( subsection_name );
	if( my_page_sottomenu_gold )
	{
		my_page_sottomenu_gold.style.display= "block";
	}

}

jQuery(document).ready(function() {
	loadMenu();
});
