var xmlhttp
var divDaCercare
var ALTEZZA_DIV = "650px"
var NUMERO_GIOCHI_CONSIGLIATI = 9

function loadXMLDoc2(url, divSearch)
{
url = "http://www.primavisioneweb.it/giochi-ragazze/dynamic/"+url
// codice per Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest()
  divDaCercare = divSearch
  xmlhttp.onreadystatechange=xmlhttpChange2

  xmlhttp.open("GET",url,true)
  xmlhttp.send(null)
  }
// codice per  IExplore
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    if (xmlhttp)
    {
    xmlhttp.onreadystatechange=xmlhttpChange
    xmlhttp.open("GET",url,true)
    xmlhttp.send()
    }
  }
}

function loadXMLDoc3(url, divSearch)
{
url = "http://www.giocattolibimbi.it/dynamic/"+url
// codice per Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest()
  divDaCercare = divSearch
  xmlhttp.onreadystatechange=xmlhttpChange

  xmlhttp.open("GET",url,true)
  xmlhttp.send(null)
  }
// codice per  IExplore
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    if (xmlhttp)
    {
    xmlhttp.onreadystatechange=xmlhttpChange
    xmlhttp.open("GET",url,true)
    xmlhttp.send()
    }
  }
}



function loadXMLDoc(url, divSearch)
{
url = "http://www.primavisioneweb.it/giochi-ragazze/dynamic/"+url
// codice per Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest()
  divDaCercare = divSearch
  xmlhttp.onreadystatechange=xmlhttpChange

  xmlhttp.open("GET",url,true)
  xmlhttp.send(null)
  }
// codice per  IExplore
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    if (xmlhttp)
    {
    xmlhttp.onreadystatechange=xmlhttpChange
    xmlhttp.open("GET",url,true)
    xmlhttp.send()
    }
  }
}

function xmlhttpChange()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
  {
  // if "OK"
  if (xmlhttp.status==200)
  {
	  var div = document.getElementById(divDaCercare);
	  div.innerHTML = xmlhttp.responseText;
	  var primoUl = div.firstChild;
	  if(primoUl.tagName=="UL")
	  {  	
	  	
	  	var numLink = primoUl.childElementCount; 
	  	var altez = numLink * 20 ;
	  	div.style.height = altez+"px";
	  }
	  else
	  {
	 	div.style.height= ALTEZZA_DIV ;
	  }
  }
  else
    {
    alert("Impossibile ricevere i dati")
    }
  }
}


function xmlhttpChange2()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
  {
  // if "OK"
  if (xmlhttp.status==200)
  {
  	 var div = document.createElement('div');
	  div.innerHTML = xmlhttp.responseText;
	  var primoUl = div.firstChild;
	  if(primoUl.tagName=="UL")
	  {  	
	  	
	  	  	var totalGame = document.getElementById("totalGame");
	  //	  	totalGame.innerHTML = primoUl.childElementCount;
	  	  	var grassetto = document.createElement('strong')
	  	  	   var tot = document.createTextNode(primoUl.childElementCount);

	  	  	grassetto.appendChild(tot);

	  	  	totalGame.appendChild(grassetto);
	  	  	
	  		var randomNum=new Array(); // regular array (add an optional integer
	  	var contexDiv = document.getElementById(divDaCercare);
	  	for(i = 0 ; i < NUMERO_GIOCHI_CONSIGLIATI ; i++ )
	  	{
		  	var randomnumber=1 + Math.floor(Math.random()* (primoUl.childElementCount-1) );
		  //	alert('generato '+randomnumber)
		  
		  
		  	for(j=0;j<i;j++){
		  	//		alert('confronto')
		  	//		alert(randomnumber)
		  	//		alert('con')
		  	//		alert(randomNum[j])
		  		if(randomnumber==randomNum[j]){
		  //				alert('uguali ripiglio num')
		  			randomnumber=1 + Math.floor(Math.random()* (primoUl.childElementCount-1) );
		  			j=0;
		  		}
		  			  
	  		}
		  
		  	randomNum[i]=randomnumber;
		
	  		
		 //	alert('preso '+randomnumber)
		
		  	//var divImage = document.getElementById('image1');
		  	var divImage = document.createElement('div');
		  	divImage.setAttribute('class', 'recipe-thumb');
		  	
		  	
		  	var linkA = primoUl.children[randomNum[i]].firstChild
		  	var linkRand = linkA.href
		  	
		  	var linkTitle = linkA.title
		  	var linkText = linkA.text
		 
		  	
		  	
		  	// creo link 1
		  	var linkImg = document.createElement('a');
		  	linkImg.setAttribute('href', linkRand);
		  	
		 		  	
		  	 // ricavo solo il nome della pagina
		  	var splitted = linkRand.split("/");
		  //	alert('Nome pagina');
		  //	alert(splitted[splitted.length-1]);
		  	
		  	var nomePagina = splitted[splitted.length-1] ;
		  	
		   var indice =	nomePagina.indexOf(".");
		  	
		   var nomeImg =	nomePagina.replace('.htm','.jpg'); 
		   nomeImg = 'images/'+nomeImg
		  
		 // 	alert(nomeImg);
		  	
		  	// creo immagine
		  	var img = document.createElement('img');
		 //  	img.setAttribute('src', 'images/dora-minigolf.jpg');
		 	img.setAttribute('src', nomeImg);
			img.setAttribute('alt', linkTitle);
			img.setAttribute('height', '164px');
			img.setAttribute('width', '214px');
			
			// concateno immagine a link
			linkImg.appendChild(img);
		  	
		   	// concateno link 1 a div principale	
		  	divImage.appendChild(linkImg);	  	
		 
		  	
		  	// creo h3
		  	
		  	var h3Desc = document.createElement('h3');
		  	h3Desc.setAttribute('class', 'sIFR-ignore');
		  	
		  	// creo link 2	 
		  	
		  	var linkDesc = document.createElement('a');
		  	 	
		  	linkDesc.setAttribute('href', linkRand);
		  	
		  	  var linkTextTitolo=document.createTextNode(linkText);
			linkDesc.appendChild(linkTextTitolo);
	
		  	
		  	// concateno link 2 a h3
		  	h3Desc.appendChild(linkDesc);
		  	
		  	// concateno h3 a div principale	
		  	divImage.appendChild(h3Desc);
		  	
		  	// creo p 
		  	
		  	 var pDescEstesa = document.createElement('p');
			pDescEstesa.setAttribute('class', 'autore');
	
		  	
		  	// creo link 3
		  	 var linkDescEstesa = document.createElement('a');
		  	 	
		  	linkDescEstesa.setAttribute('href', linkRand);
		  	 var linkTextEstesa=document.createTextNode(linkTitle);
			linkDescEstesa.appendChild(linkTextEstesa);
	
		  	
		  	// concateno link 3 a p
		  	pDescEstesa.appendChild(linkDescEstesa);
	
		  	
		  	// concateno p a div principale	
		  divImage.appendChild(pDescEstesa);
		  
		  // creo div stelline
		   var divRating = document.createElement('div');
		  	divRating.setAttribute('class', 'rating');
		  	
		  	var randomRating = 3 + Math.floor(Math.random()* 2 );
		  	for(z=0;z<randomRating;z++){
		  		
		  		var imgStella = document.createElement('img');
		   	imgStella.setAttribute('src', 'http://www.primavisioneweb.it/giochi-ragazze/miss-italia/image/star-100-sm-trans.png');
			imgStella.setAttribute('alt', 'Punteggio gioco');
			imgStella.setAttribute('height', '10px');
			imgStella.setAttribute('width', '10px');
			
			divRating.appendChild(imgStella);
		  	}
		 					
			
			// concateno rating a div principale
			 divImage.appendChild(divRating);
			 contexDiv.appendChild(divImage);
	  	}
	  	
	  //	var numLink = primoUl.childElementCount; 
	  //	var altez = numLink * 15 ;
	  //	div.style.height = altez+"px";
	  }
	  else
	  {
	 	div.style.height= ALTEZZA_DIV ;
	  }
  }
  else
    {
    alert("Impossibile ricevere i dati")
    }
  }
}


$(function() {
		$("#accordion").accordion({
			icons: {
    			header: "ui-icon-circle-arrow-e",
   				headerSelected: "ui-icon-circle-arrow-s"
			}
		});
	});
	$(function() {
		$("#accordion2").accordion({
			icons: {
    			header: "ui-icon-circle-arrow-e",
   				headerSelected: "ui-icon-circle-arrow-s"
			}
		});
	});
	$(function() {
		$("#accordion3").accordion({
			icons: {
    			header: "ui-icon-circle-arrow-e",
   				headerSelected: "ui-icon-circle-arrow-s"
			}
		});
	});
	
	
	

	 
	
	

