////////////////////////// 
//Inizializzazioni globali
//////////////////////////
var idMenu = 3;
var idLayout_L = 1;
var pageNumber = 1;
var pageSize = 1000;

// Array documenti
var v = new CDocuments();
v.bodyText=false;
var documents;


/////////////////////////////////////////////////////////////////////////////////////////////////////////
// se stato pagina false quando si clicca sul prodotto si apre la scheda
// se stato pagina true quando si clicca sul prodotto si navigano i prodotti correlati all'articolo scelto
/////////////////////////////////////////////////////////////////////////////////////////////////////////
var pageStatus = false;

var lenNews=0;


function init()
{
	// leggo e impagino menu e su ogni voce appiccico la menuclick() 
	// chiamo la funz menu della prima voce che voglio
	var SoR = new CSoapRequest(WebServiceUrlPath+"ws/std/WsMenus.asmx", "GetMenuItems", "requestMsg");
	var theQueryObject = new Object();
	theQueryObject.parentNodeId=255;

	var objResponse = new Object();
	if (SoR.sendRequestObject(theQueryObject) == true)
	{
		CXmlDeserializer.Deserialize(objResponse, SoR.responseNode.node());
		var len = objResponse.table.rows.length;			
		var HTML = '<table border=0 cellpadding=0 cellspacing=0 class="Menu-Table"><tr>';
			for(var i=0; i<len; i++){
				var r = objResponse.table.rows[i];
				var txt = r["HTMLTEXT"];
				var idmenu = r["NODEID"];
				var idlayout = parseInt(r["IDKEY"]);

				var menuArray = "javascript:MenuClick("+idmenu+","+idlayout+")";
				
				if(idmenu==10)
					HTML += '<td class="td-Menu"><a href="Login.aspx" target="_blank" class="a-Menu" id="idmenu'+idmenu+'">&nbsp;' + txt + '</a></td>';
				else
					HTML += '<td class="td-Menu"><a href="'+menuArray+'" class="a-Menu" id="idmenu'+idmenu+'">&nbsp;' + txt + '</a></td>';
								
			}
			HTML += '<td class="td-Menu"><a href="mailto:info@leopizzo.com" class="a-Menu">&nbsp;@</a></td>';			
			HTML += '</tr></table>';
	} else {return;}

	document.getElementById("div-menu").innerHTML = HTML;

	MenuClick(idMenu,idLayout_L);

	//initScroll();
	//setTimeout('scroll()', 35);

}

function MenuClick(idmenu,idlayout)
{
	document.getElementById("idmenu"+idMenu).className="a-Menu";
	document.getElementById("idmenu"+idmenu).className="a-MenuRed";
	idMenu=idmenu;
	idLayout_L=idlayout;
	
	//if(idLayout_L==2)
	//	pageStatus = true; // i prodotti linkano a prodotti correlati
	//else
	//	pageStatus = false;// i prodotti linkano alla propria scheda
	
	documents = v.GetDocs(idMenu,-1, pageNumber, pageSize);

	ImpaginateDocuments();
}

function ImpaginateDocuments(arg)
{

	switch (idLayout_L) {
		case 1: // Attivo una pagina che prevede una img statiche e sotto le news
			if(arg!=null && arg==3)
			{
				HTML = DoWithRol("-WithRol",true,6);			
				document.getElementById("div-home").style.display="none";
				document.getElementById("div-WithRol").style.display="block";			
			}
			else
			{
				HTML = DoWithRol("-News",false,0);
				document.getElementById("div-home").style.display="block";
				document.getElementById("div-WithRol").style.display="none";
			}
			document.getElementById("div-WithOutRol").style.display="none";			
			document.getElementById("div-Rol").style.display="block";
			break;
		case 2: // Attivo una pagina che prevede le prime 6 img grandi e le altre sotto
			HTML = DoWithRol("-WithRol",true,6);			
			document.getElementById("div-home").style.display ="none";
			document.getElementById("div-WithRol").style.display="block";
			document.getElementById("div-WithOutRol").style.display="none";			
			document.getElementById("div-Rol").style.display ="block";
			break;
		case 3: // Attivo una pagina che prevede le prime 6 img grandi
			HTML = DoWithoutRol("withOutRol");			
			document.getElementById("div-home").style.display="none";
			document.getElementById("div-WithRol").style.display="none";
			document.getElementById("div-WithOutRol").style.display="block";			
			document.getElementById("div-Rol").style.display="none";
			break;
	}
}

function DoWithRol(cls,arg,start){
	// annulla timer precedente
	if(timerId!=null) clearTimeout(timerId);
	document.getElementById("div-Rol").innerHTML="";
	lenNews=documents.rows.length;
	// se ci sonopił di 6 prodotti attiva il timer
	if(lenNews>6){
		document.getElementById("div-Rol").innerHTML=ImpaginateRol(cls,start);
		initScroll();
		setTimeout('scroll()', 35);
	}	
	// i primi 6 li impagina alcentro
	if(arg)
		document.getElementById("div-WithRol").innerHTML=ImpaginatePageCenter("withRol");
	///////////////////////
	/// rivedere questa parte ilrollante deve essere attivato solo per pił disei prodotti
	//////////////////////
}

function DoWithoutRol(cls){
	// annulla timer precedente
	if(timerId!=null) clearTimeout(timerId);
	// azzera html del div rollante
	document.getElementById("div-Rol").innerHTML="";
	// impagina parte centrale

	document.getElementById("div-WithOutRol").innerHTML=ImpaginatePageCenter(cls);	
}

function ImpaginateRol(cls,start){
	
	var HTML = '<table cellpadding=0 cellspacing=0 border=0 style="height:134px;"><tr><td class="td-Main-LineaRollante"><img src="images/quadratino.jpg"></td></tr>'
		HTML += '<tr><td align="center">'

	HTML+='<div id="container" class="container'+cls+'">';
	HTML+=' <div id="internal" class="internal'+cls+'">';

	HTML += '<table cellpadding="0" cellspacing="0" border=0 class="table'+cls+'"><tr>';			
	var documentsTemp = [];
		for(var j=start;j<documents.rows.length;j++){
			documentsTemp[documentsTemp.length]=documents.rows[j];
		}

		lenNews=documentsTemp.length;
		for(var i=0;i<lenNews;i++){
			var img = documentsTemp[i]["Images"];
			var imgtype = 0;

				if(img!=null && img[0]!=null){
					imgtype=parseInt(img[0]["IMAGETYPE"]);
					img=img[0]["SRC"];
				} else {
					img='empty1.jpg';
				}
			if(documentsTemp[i]["RelatedDoc"]!=null && documentsTemp[i]["RelatedDoc"]!='')
				pageStatus = true; // i prodotti linkano a prodotti correlati
			else
				pageStatus = false;// i prodotti linkano alla propria scheda
				
			var js = SchedaClick(documentsTemp[i]["IDDOC"],documentsTemp[i]["HASDETAIL"]);
	
			//if(i%2==0)
			//	HTML+= 	'<td id="c'+i+'" class="td-internal'+cls+'">';
			//else
			//	HTML+= 	'<td id="c'+i+'" class="td-internal'+cls+'">';
			
			switch (imgtype) {
				case 1:
					HTML+= 	'<td id="c'+i+'" class="td-internal'+cls+'">';
					//HTML += '<img src="productimages/'+img+'" class="a-Image'+cls+'" onclick="'+js+'" style=" cursor:pointer;"/>';
					HTML += '<img src="productimages/'+img+'" class="a-Image'+cls+'" '+js+'/>';
					break;
				case 2:
					HTML+= 	'<td id="c'+i+'" class="td-internal'+cls+'-2">';
					HTML += '<img src="productimages/'+img+'" class="a-Image'+cls+'"/>';
					break;
				case 3:
					HTML+= 	'<td id="c'+i+'" class="td-internal'+cls+'-3">';
					HTML += '<img src="productimages/'+img+'" class="a-Image'+cls+'"/>';
					break;
				default:
					HTML+= 	'<td id="c'+i+'" class="td-internal'+cls+'">';
					HTML += '<img src="productimages/'+img+'" class="a-Image'+cls+'"/>';
					break;					
			}
			HTML+= 	'</td>';
		}			
		HTML+= 	'</tr></table></div></div></td></tr><tr><td class="td-Main-LineaRollante"><img src="images/quadratino.jpg"></td></tr></table>';

	return HTML;
}

function ImpaginatePageCenter(cls){
	
		var img1='empty1.jpg';
		var img2='empty2.jpg';
		var img3='empty3.jpg';
		var img4='empty2.jpg';
		var img5='empty3.jpg';
		var img6='empty1.jpg';
		var title1,title2,title3,title4,title5,title6;
		var link1,link2,link3,link4,link5,link6;
		
		if(documents.rows[0]){
			img1 = documents.rows[0]["Images"][0]["SRC"];
			title1=documents.rows[0]["IDDOC"];//documents.rows[0]["Images"][0]["IMGDESCRIPTION"];

			if(documents.rows[0]["RelatedDoc"]!=null && documents.rows[0]["RelatedDoc"]!='')
				pageStatus = true; // i prodotti linkano a prodotti correlati
			else
				pageStatus = false;// i prodotti linkano alla propria scheda

			//alert(documents.rows[0]["HASDETAIL"])

			link1 = SchedaClick(documents.rows[0]["IDDOC"],documents.rows[0]["HASDETAIL"]);

		}
		if(documents.rows[1]){
			img2 = documents.rows[1]["Images"][0]["SRC"];
			title2=documents.rows[1]["IDDOC"];//title2=documents.rows[1]["Images"][0]["IMGDESCRIPTION"];

			if(documents.rows[1]["RelatedDoc"]!=null && documents.rows[1]["RelatedDoc"]!='')
				pageStatus = true; // i prodotti linkano a prodotti correlati
			else
				pageStatus = false;// i prodotti linkano alla propria scheda

			link2 = SchedaClick(documents.rows[1]["IDDOC"],documents.rows[1]["HASDETAIL"]);
		}

		if(documents.rows[2]){
			img3 = documents.rows[2]["Images"][0]["SRC"];
			title3=documents.rows[2]["IDDOC"];//title3=documents.rows[2]["Images"][0]["IMGDESCRIPTION"];

			if(documents.rows[2]["RelatedDoc"]!=null && documents.rows[2]["RelatedDoc"]!='')
				pageStatus = true; // i prodotti linkano a prodotti correlati
			else
				pageStatus = false;// i prodotti linkano alla propria scheda

			link3 = SchedaClick(documents.rows[2]["IDDOC"],documents.rows[2]["HASDETAIL"]);

		}

		if(documents.rows[3]){
			img4 = documents.rows[3]["Images"][0]["SRC"];
			title4=documents.rows[3]["IDDOC"];//title4=documents.rows[3]["Images"][0]["IMGDESCRIPTION"];

			if(documents.rows[3]["RelatedDoc"]!=null && documents.rows[3]["RelatedDoc"]!='')
				pageStatus = true; // i prodotti linkano a prodotti correlati
			else
				pageStatus = false;// i prodotti linkano alla propria scheda

			link4 = SchedaClick(documents.rows[3]["IDDOC"],documents.rows[3]["HASDETAIL"]);
		}

		if(documents.rows[4]){
			img5 = documents.rows[4]["Images"][0]["SRC"];
			title5=documents.rows[4]["IDDOC"];//title5=documents.rows[4]["Images"][0]["IMGDESCRIPTION"];

			if(documents.rows[4]["RelatedDoc"]!=null && documents.rows[4]["RelatedDoc"]!='')
				pageStatus = true; // i prodotti linkano a prodotti correlati
			else
				pageStatus = false;// i prodotti linkano alla propria scheda

			link5 = SchedaClick(documents.rows[4]["IDDOC"],documents.rows[4]["HASDETAIL"]);
		}

		if(documents.rows[5]){
			img6 = documents.rows[5]["Images"][0]["SRC"];
			title6=documents.rows[5]["IDDOC"];//title6=documents.rows[5]["Images"][0]["IMGDESCRIPTION"];

			if(documents.rows[5]["RelatedDoc"]!=null && documents.rows[5]["RelatedDoc"]!='')
				pageStatus = true; // i prodotti linkano a prodotti correlati
			else
				pageStatus = false;// i prodotti linkano alla propria scheda

			link6 = SchedaClick(documents.rows[5]["IDDOC"],documents.rows[5]["HASDETAIL"]);
		}
		
	
	var HTML = 	'<table border=0 cellpadding=0 cellspacing=0 class="'+cls+'-MainTable">';
		HTML +=	'<tr>';
		HTML +=		'<td>';
		HTML +=			'<table border=0 cellpadding=0 cellspacing=0 class="'+cls+'-Table">';			
		HTML +=			'<tr>';
		HTML +=			'<td class="'+cls+'-td-Immagine"><img class="'+cls+'-img1" src="productimages/'+img1+'" title="'+title1+'" '+link1+'></td>';
		HTML +=			'<td class="'+cls+'-td-Linea"></td>';
		HTML +=			'<td class="'+cls+'-td-Spazio"></td>';
		HTML +=			'<td class="'+cls+'-td-Linea"></td>';
		HTML +=			'<td class="'+cls+'-td-Immagine" valign=top><table border=0 cellpadding=0 cellspacing=0><tr><td><img class="'+cls+'-img2" src="productimages/'+img2+'" title="'+title2+'" '+link2+'></td></tr><tr><td style="height:40px;"></td></tr><tr><td><img class="'+cls+'-img3" src="productimages/'+img3+'" title="'+title3+'" '+link3+'></td></tr></table></td>';
		HTML +=			'<td class="'+cls+'-td-Linea"></td>';
		HTML +=			'<td class="'+cls+'-td-Spazio"></td>';
		HTML +=			'<td class="'+cls+'-td-Linea"></td>';
		HTML +=			'<td class="'+cls+'-td-Immagine" valign=top><table border=0 cellpadding=0 cellspacing=0><tr><td><img class="'+cls+'-img4" src="productimages/'+img4+'" title="'+title4+'" '+link4+'></td></tr><tr><td style="height:40px;"></td></tr><tr><td><img class="'+cls+'-img5" src="productimages/'+img5+'" title="'+title5+'" '+link5+'></td></tr></table></td>';
		HTML +=			'<td class="'+cls+'-td-Linea"></td>';
		HTML +=			'<td class="'+cls+'-td-Spazio"></td>';
		HTML +=			'<td class="'+cls+'-td-Linea"></td>';
		if(idMenu==10)
			HTML +=			'<td class="'+cls+'-td-Immagine" style="padding-top:40px;"><table border=0 cellpadding=0 cellspacing=0><tr><td><img class="'+cls+'-img6" src="productimages/'+img6+'" title="'+title6+'" '+link6+'></td></tr><tr><td style="padding-top:20px;"><a class="a-Menu" href="Languages.aspx">&nbsp;e-commerce ita</a></td></tr><tr><td><a class="a-Menu" href="Languages.aspx?languageId=2">&nbsp;e-commerce eng</a></td></tr></table></td>';
		else
			HTML +=			'<td class="'+cls+'-td-Immagine"><img class="'+cls+'-img6" src="productimages/'+img6+'" title="'+title6+'" '+link6+'></td>';		
		HTML +=			'</tr>';
		HTML+=			'</table>';		
		HTML+=		'</td>';
		HTML+=	'</tr>';
		HTML+=	'</table>';

	return HTML;
}

function SchedaClick(iddoc,hasdetail)
{
	if(pageStatus){ 
		return "onclick=\"javascript:RelatedClick("+iddoc+")\" style=\" cursor:pointer;\"";

	} else {
		// navigo le schede
		if(hasdetail)
			return "onclick=\"javascript:OpenDialog2('Stampascheda.aspx?idScheda="+iddoc+"',700,1000,'print')\" style=\" cursor:pointer;\"";
		else
			return ""
	}
}

function RelatedClick(iddoc)
{
	// navigo i prodotti correlati
	pageStatus=false;
	documents = v.GetDocsRelated(iddoc, pageNumber, pageSize);

	ImpaginateDocuments(idMenu);
}

function OpenDialog2(url, height, width, targetName)
{
	var features = "directories=no,location=no,scrollbars=yes,resizable=yes,top=0,left=0";
	if ( arguments.length == 2 )
		features += ",height=" + height;
	else if ( arguments.length >= 2 )
		features += ",height=" + height + ",width=" + width;

	var hwnd = window.open(url, targetName, features);
	hwnd.focus();
}