var menuAttivo = false;
var statusMenuNews = 0;

function showSchedaTecnica(nomeProdotto, nomeDesigner, urlFoto)
{
    var element = document.createElement('DIV');
    element.id = "popup";
    
    var html = '<div id="popup"><div class="scheda">';
    html += '<div id="top">';
    html += '<a href="#" onclick="closeLightBox();return false" title="close"><img src="images/close_black.gif" alt="close" class="close" /></a>';
    html += 'la scheda tecnica';
    html += '</div>';
    html += '<div id="intestazione">';
    html += '<h1>'+nomeProdotto+'</h1>';
    html += '<h2>'+nomeDesigner+'</h2>';
    html += '</div>';
    html += '<img src="'+urlFoto+'" alt="scheda tecnica" />';
    html += '</div></div>';
    showLightBox(html);
    //element.innerHTML = html
    //document.getElementsByTagName("body")[0].appendChild(element);
    
}

function closeSchedaTecnica()
{
    var scheda = document.getElementById("popup");
    scheda.parentNode.removeChild(scheda);
}

function showHideElement(id)
{
    var element = document.getElementById(id);
    if(element.style.display == '')
        element.style.display = 'none';
    else
        element.style.display = '';
}

function showHideMenu(id)
{
    var element = document.getElementById(id);
    if(element.className == 'sel')
    {
        element.className='';
        menuAttivo = false;
    }
    else
    {
        if(menuAttivo)
        {
            document.getElementById(menuAttivo).className = "";
        }
        element.className='sel';
        menuAttivo = id;
    }
}

function showThumbProdotto(urlFoto, container, cont)
{
    var element = document.createElement('DIV');
    element.id = "thumbProdotto";
    element.className = "thumbmenu";
    element.innerHTML = '<img src="'+urlFoto+'" alt="" />';
    element.style.top = Math.round(cont*13.5)+"px";
    document.getElementById(container).appendChild(element);
}

function hideThumbProdotto()
{
    var element = document.getElementById("thumbProdotto");
    if(!element)
        return;
    element.parentNode.removeChild(element);
}

function makeRequest(url) 
{
	if (window.XMLHttpRequest) // Mozilla, Safari,...
	{ 
		http_request = new XMLHttpRequest();
	} 
	else if (window.ActiveXObject) // IE
	{ 
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(!http_request)
	{
		return;
	}
	http_request.onreadystatechange = alertContents;
	http_request.open('GET', url, true);
	http_request.send(null);
}

function openPopUp(url,width,height)
{
	leftValue = (screen.width - width)/2;
	topValue = (screen.height - height)/2;
	window.open(url,'emoticons','width='+width+',height='+height+',top='+topValue+',left='+leftValue+',scrollbars=1');
}

function showZoomProdotto(urlImmagine)
{    
    document.getElementById("mycustomscroll").style.display = "none";
    document.getElementById("fotoZoomProdotto").style.display = "none";
    document.getElementById("loading").style.display = "";
    document.getElementById("zoomProdotto").style.display = "";
    
    var imageObj = new Image();
    imageObj.onload = function()
                      {
                          if(document.getElementById("fotoZoomProdotto"))
                          {
                              document.getElementById("fotoZoomProdotto").src = imageObj.src
                              document.getElementById("fotoZoomProdotto").style.display = "";
                          }
                          
                          if(document.getElementById("loading"))
                            document.getElementById("loading").style.display = "none";
                      }
    imageObj.src=urlImmagine+"?"+Math.rand;
}


function hideZoomProdotto()
{
    document.getElementById("mycustomscroll").style.display = "";
    document.getElementById("zoomProdotto").style.display = "none";
}

function scrollNews(incremento)
{
    statusMenuNews += incremento;
    if(statusMenuNews<0)
    {
        statusMenuNews = 0;
        return;
    }
    var base = 10;
    var start = statusMenuNews
    var numeroNews = 2;
    
    url = "index.php?fase=getJsonNews&startLimit="+(base+(start*numeroNews))+"&stopLimit="+numeroNews;
    var http_request = "";
    if (window.XMLHttpRequest) 
        http_request = new XMLHttpRequest();
    else if (window.ActiveXObject) 
        http_request = new ActiveXObject("Microsoft.XMLHTTP");

    http_request.open("GET", url, true);
    http_request.onreadystatechange = function () 
    {
        if (http_request.readyState == 4) 
        {
        if (http_request.status == 200) 
        {
            var news = eval("(" + http_request.responseText + ")");
            var container = document.getElementById("elencoNews");
            
            var html = "";
            var cont = 0;
            for (i in news)
            {
                cont++;
                html += "<li>";
                html += "<span class=\"data\">"+news[i].dataInserimentoStringa+"</span>";
                html += "<a href=\"/index.php?fase=elencoNews&idNews="+i+"\" title=\"\">"+(news[i].titolo.substr(0, 30))+"</a>";
                html += "</li>";
            }
            if(!cont)
            {
                statusMenuNews -= incremento;
                return;
            }
            container.innerHTML = "";
            container.innerHTML = html;
        }
        }
    }
    http_request.send(null);
}

function changeStatusValue(value)
{
    document.getElementById("statusValue").value = value;
}

function getViewportSize()
{
    var viewportwidth;
    var viewportheight;
    
    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    
    if (typeof window.innerWidth != 'undefined')
    {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
    }
    
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
    
    else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
    {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
    }
    
    // older versions of IE
    
    else
    {
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
    }
    return [viewportwidth, viewportheight];
}

function showLightBox(contenuto)
{
    closeLightBox();
    var altezza = document.documentElement.clientHeight;
    var container = document.createElement('DIV');
    container.style.width = "100%";
    container.style.height = altezza+"px";
    
    container.style.position = "absolute";
    container.style.top = "0px";
    container.style.left = "0px";
    
    var viewportSize = getViewportSize();
    var xPos = Math.round((viewportSize[0] - SlideShow.slideSize[0])/2);
    var yPos = Math.round((viewportSize[1] - SlideShow.slideSize[1])/2);
    container.id = "lightBox";
    var html = '<div style="position:relative;z-index:100000;filter:alpha(opacity=80);opacity:0.8;width:100%;height:'+altezza+'px;background:#000"></div>';
    html     +='<div onclick="closeLightBox()" style="z-index:100000;position:absolute;top:0px;width:100%;left:0px;border:0px solid #f00;height:'+altezza+'px"><table align="center" style="z-index:1000;height:100%;border:0px solid #f00"><tr><td valign="middle">'+contenuto+'</td></tr></table></div>';

    container.innerHTML = html;
    document.getElementsByTagName("body")[0].appendChild(container);
}

function closeLightBox()
{
    var container = document.getElementById("lightBox");
    if(container)
        container.parentNode.removeChild(container);
}

function addImmagine(text)
{
    var contatore = document.getElementById("numeroFoto");
    var par = document.createElement('p');
    par.innerHTML = "<label>inserisci immagine</label>";
    par.innerHTML += "<input style=\"width:200px\" name='foto_"+(contatore.value*1+1)+"' type=\"file\" size=\"20\" />";
    if(text)
        par.innerHTML += "<input style=\"width:350px\" name='testo_"+(contatore.value*1+1)+"' type=\"text\" size=\"20\" />";
    document.getElementById("immagini").appendChild(par);
    contatore.value++;
    
}