//reads the cookies into javascript
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}


// clear interval (if one has been set) & update hint box value using helper function
function show_hint( p_hint_text ) {
	window.clearInterval( document.getElementById( 'hint_box' ).interval );
	display_hint( p_hint_text );
}

// set new interval
function set_timeout() {
	document.getElementById( 'hint_box' ).interval = window.setInterval( 'reset_hint()', 2000 );
}

// call to external Javascript/PHP file and pass Parent ID
function update_child( p_parent_option, what_update ) {

	// create new script element and set its relative URL (including ID argument)
	script = document.createElement( 'script' );
	script.src = 'external.php?cosa=' + what_update + '&parent_id=' + p_parent_option.value;

	// attach (load) script element to document head
	document.getElementsByTagName( 'head' )[0].appendChild( script );
}
// Allarga il menù a tendina dei select
function allarga(tendina){
	document.getElementById(tendina).style.width="400px";
}
// Stringe i menù a tendina dei select
function stringi(tendina){
	document.getElementById(tendina).style.width="130px";
}
var ritardo;

  var gamelist = new Array();
  var gametitle = new Array();
function avvia(secondi) {
  if (secondi >0) {
    intervallo=secondi*1000;
    window.clearInterval(ritardo);
    ritardo = window.setInterval("autoslide()", intervallo);
    document.cookie='autoslide='+secondi;
  }
  else {
    ferma();
    
  }
}
function ferma() {
  window.clearInterval(ritardo);
  document.a_play.secondi.selectedIndex="s0";
}
function slide(val) {
	var prossimo = val-1+2;
	var precedente = val-1;
	if ((prossimo<totale) && (prossimo > 2) ) {
	var collegamento = "<a href=\"javascript: slide(\'"+precedente+"\')\"><<</a> &nbsp; <a href=\"javascript: slide(\'"+prossimo+"\')\">>></a>";
	document.getElementById("successivo").innerHTML = collegamento;
	}
	else if (prossimo ==2) {
        var collegamento = "<< &nbsp; <a href=\"javascript: slide(\'"+prossimo+"\')\">>></a>";
	document.getElementById("successivo").innerHTML = collegamento;
	}
	else {
        var collegamento = "<a href=\"javascript: slide(\'"+precedente+"\')\"><<</a> &nbsp; >>";
	document.getElementById("successivo").innerHTML = collegamento;
	}


   var alldata = gamelist[val];
   var dati = alldata.split("|");
	var rom = dati[0];
	var snap = dati[1];
	var emu = dati[2];
	var sound = dati[3];
	var graphic = dati[4];
	var cocktail = dati[5];
	var protection = dati[6];
	var manu = dati[8];
	var anno = dati[9];
	var work = dati[10];
	var source = dati[11];
	var titolo = gametitle[val];
	titolo="<img src=\"img/"+work+".gif\"> <img src=\"img/"+source+".gif\"> <a href=\"gioco.php?&game="+rom+"\">"+titolo+"</a>";
        var immagine = "<center><img src=\""+snap+"\" height=\"250px\" id=\"icogallery\"></center>";
	var response = "<b>"+titolo+"</b><br><i>"+manu+", anno "+anno+"</i><br>Emulazione: "+emu+"<br>Colore: "+graphic+"<br>Sonoro: "+sound+"<br>Modalità Cocktail: "+cocktail+"<br>Protezione: "+protection;
	document.getElementById('immagine').innerHTML = immagine;
	document.getElementById('contenuto').innerHTML = response;
}
var primo = 1;
function autoslide() {
	var prossimo = primo-1+2;
	var precedente = primo-1;
	if ((prossimo<totale) && (prossimo > 2) ) {
	var collegamento = "<a href=\"javascript: slide(\'"+precedente+"\')\"><<</a> &nbsp; <a href=\"javascript: slide(\'"+prossimo+"\')\">>></a>";
	document.getElementById("successivo").innerHTML = collegamento;
	}
	else if (prossimo==2) {
        var collegamento = "<< &nbsp; <a href=\"javascript: slide(\'"+prossimo+"\')\">>></a>";
	document.getElementById("successivo").innerHTML = collegamento;
	}
	else {
        var collegamento = "<a href=\"javascript: slide(\'"+precedente+"\')\"><<</a> &nbsp; >>";
	document.getElementById("successivo").innerHTML = collegamento;
	}

   var alldata = gamelist[primo];
   //alert (alldata); /debug
	var dati = alldata.split("|");

	var rom = dati[0];
	var snap = dati[1];
	var emu = dati[2];
	var sound = dati[3];
	var graphic = dati[4];
	var cocktail = dati[5];
	var protection = dati[6];
	var manu = dati[8];
	var anno = dati[9];
	var work = dati[10];
	var source = dati[11];
	var titolo = gametitle[primo];
	titolo="<img src=\"img/"+work+".gif\"> <img src=\"img/"+source+".gif\"> <a href=\"gioco.php?&game="+rom+"\">"+titolo+"</a>";
        var immagine = "<center><img src=\""+snap+"\" height=\"250px\" id=\"icogallery\"></center>";
	var response = "<b>"+titolo+"</b><br><i>"+manu+", anno "+anno+"</i><br>Emulazione: "+emu+"<br>Colore: "+graphic+"<br>Sonoro: "+sound+"<br>Modalità Cocktail: "+cocktail+"<br>Protezione: "+protection;
	document.getElementById('immagine').innerHTML = immagine;
	document.getElementById('contenuto').innerHTML = response;
	if (primo<(totale-1)) {
	   primo++;
         }
        else {
          if (totpage>1) {
              ferma();
              setTimeout('gotopage()', intervallo);
          }
          else {
            primo=1;
                 var timer = getCookie("autoslide");
                 avvia(timer);
          }
        }

}
// esegue il passaggio alla pagina successiva durante lo slideshow
function gotopage() {

            if (confirm("Passo alla pagina successiva?"))
            {
            document.cookie="startslide=1";
            location.href=nextpage;
            }
            else{
                 primo=1;
                 var timer = getCookie("autoslide");
                 avvia(timer);
            };
}
// avvia lo slide automaticamente dalla pagina prima
function checkslide(){
var started=getCookie("startslide");
if (started==1) {
  var timer = getCookie("autoslide");
  avvia (timer);
  document.cookie="startslide=0";
}
}


// Funzioni di Popup, cambia la dimensione della finestra aperta
function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=350');");
}
function popUp2(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=200');");
}

function popUp5(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=200,height=400');");
}
function popUp6(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=400');");
}
function popUp3(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=460,height=370');");
}
function popUp4(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=250,height=250');");
}

// Funzione per inviare il form premendo invio
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}

/***********************************************
* Dynamic Ajax Content- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var loadedobjects=""
var rootdomain="http://"+window.location.hostname

function checkform(which, what, where){
var get_text=""
for (i=0;i<which.length;i++){
var tempobj=which.elements[i]
if (((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&tempobj.selectedIndex==-1)){
get_text=get_text;
}
else {
  get_text=get_text+ "&" +tempobj.id +"=" +tempobj.value;
}
}
var newlink=what+get_text;
// alert (newlink); //debug
ajaxpage(newlink, where);
}

function ajaxpage(url, containerid){
// alert(url); // debug
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
page_request.open('GET', url, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

// Funzione per il caricamento dinamico dei CSS ed altro
function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref);
loadedobjects+=file+" "; //Remember this object as being already added to page
}
}
}

// Funzione per cambiare le immagini
function movepic(img_name,img_src,img_alt) {
document[img_name].src=img_src;
document[img_name].alt=img_alt;
}
// Funziona per visualizzare le immagini full size
function picsize(img_name) {
document[img_name].style.height='auto';
}
// Funzione per ridurre le snap a 190 di altezza
function smallsize(img_name) {
document[img_name].style.height='190px';
}

// Funzione per mostrare/nascondere un layer
function toggleLayer(whichLayer)
{
if (document.getElementById)
{
 // this is the way the standards work
var style2 = document.getElementById(whichLayer).style;
style2.display = style2.display? "":"block";
}
else if (document.all)
{
 // this is the way old msie versions work
var style2 = document.all[whichLayer].style;
style2.display = style2.display? "":"block";
}
else if (document.layers)
{
 // this is the way nn4 works
var style2 = document.layers[whichLayer].style;
style2.display = style2.display? "":"block";
}
}


//styleswitcher

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}


function show(t, id) {
var tags = document.getElementsByTagName(t);
for (var i = 0; i < tags.length; i++) {
if (tags[i].id == id) {
  if (tags[i].style.display == 'none') {
tags[i].style.display = '';
  }
  else {
       tags[i].style.display = 'none';
  }

}
}
}

function setFramesStyleSheet(title) {
  parent.frames['titolo'].setActiveStyleSheet(title);
  parent.frames['sinistra'].setActiveStyleSheet(title);
  parent.frames['corpo'].setActiveStyleSheet(title);
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

function nospam() {
  document.write('<input type="hidden" name="palla" value="MAME">');
}

