

function getNewSubmitForm(){
    var submitForm = document.createElement("FORM");
    document.body.appendChild(submitForm);
    submitForm.method = "POST";
    return submitForm;
}

function createNewFormElement(inputForm, elementName, elementValue){
    var newElement = document.createElement('input');
    newElement.setAttribute("name",elementName);
    newElement.setAttribute("type","hidden");
    inputForm.appendChild(newElement);
    newElement.value = elementValue;
    return newElement;
}

function createFormAndSubmit(action,name_p,value_p)
{
    var submitForm = getNewSubmitForm();
    createNewFormElement(submitForm, name_p, value_p);
    submitForm.action= action;
    submitForm.submit();
}

function showDiv(event, id, innerHTML, width, height)
{
    var newdiv;
    var pos_x;
    var pos_y;
    var myWidth = 0, myHeight = 0;
    var pos_xr = 0, pos_yr = 0;
    var IE = document.all?true:false;

    if (!document.getElementById(id))
    {
        newdiv = document.createElement('div');
        newdiv.setAttribute('id', id);
        newdiv.style.position = "absolute";
        document.body.appendChild(newdiv);
    }

    document.getElementById(id).style.width = width;
    document.getElementById(id).style.height = height;
    document.getElementById(id).style.border = "1";
    document.getElementById(id).innerHTML = innerHTML;

    if (window.Event) {document.captureEvents(Event.MOUSEMOVE);}

    pos_x = (window.Event) ? event.pageX : event.clientX + (document.documentElement.scrollLeft ?
        document.documentElement.scrollLeft : document.body.scrollLeft);

    pos_y = (window.Event) ? event.pageY : event.clientY + (document.documentElement.scrollTop ?
        document.documentElement.scrollTop : document.body.scrollTop);

    if (typeof(window.innerWidth) == 'number')
    {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    }
    else
    if (document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
    {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    }
    else
    if (document.body && ( document.body.clientWidth || document.body.clientHeight) )
    {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }

    if (event.clientY+height+5>myHeight){pos_y = pos_y-(event.clientY+height-myHeight+50);}

    myHeight=0;

    if (pos_x < 0){pos_x = 0;}
    if (pos_y < 0){pos_y = 0;}

    document.getElementById(id).style.top = pos_y+0+'px';
    document.getElementById(id).style.left = pos_x+15+'px';

    document.getElementById(id).style.visibility = 'visible';
}

function mostraAnteprima(event, nameImage, width, height, desc)
{
    showDiv(event, 'icofoto',
    '<div class ="anteprima" style="width:'+width+'px;"><div><img src="'+nameImage+'"></div><div>'+desc+'</div></div>', width, height);
}

function nascondiAnteprima()
{
	document.getElementById('icofoto').innerHTML = '';
	document.getElementById('icofoto').style.visibility = 'hidden';
}

function ResizeImm()
{
    var myWidth = 0, myHeight = 0;
    if (typeof(window.innerWidth) == 'number')
    {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else
    if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight) )
    {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else
    if (document.body && (document.body.clientWidth || document.body.clientHeight) )
    {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }

    myHeight = myHeight-35;
    myWidth = myWidth-30;

    //var fotowidth = document.getElementById("foto").width;

    var theImg = document.getElementById("foto");
    var theImg_height = theImg.height;
    var theImg_width = theImg.width;
    
    var theImg_ratio = theImg_height/theImg_width;
    var scale;

    if (theImg.height*myWidth>myHeight*theImg.width)
    {
        scale = myHeight/theImg_height;
	}
	else
	{
        scale = myWidth/theImg_width;
	}
	
	if (myHeight > 100 && myWidth > 100)
	{
        document.getElementById("foto").setAttribute("height",Math.floor(theImg_height*scale));
        document.getElementById("foto").setAttribute("width",Math.floor(theImg_width*scale));
	}
}

// da qui overlay;

function loadimg(nameimg,desc,imgWidth_p,imgHeight_p)
{
    var overlay;
    var box;
    var imgdiv;
    var imgdesc;
    var foto_img;
    var foto_img_obj;
    var foto_id = 'foto';
    var src = 'images/'+nameimg;
    var windowHeight;
    var windowWidth;
    var newImg;
    var imgHeight=imgHeight_p;
    var imgWidth=imgWidth_p;
    var pos_y;
    var pos_x;
    var scale;
    var box_id = 'box';

    if(!document.getElementById("overlay"))
    {
        overlay = document.createElement('div');
        overlay.setAttribute('id', 'overlay');
        document.body.appendChild(overlay);
        overlay.setAttribute('class', 'overlay');

        box = document.createElement('div');
        box.setAttribute('id', box_id);
        document.body.appendChild(box);

        imgdiv = document.createElement('div');
        imgdiv.setAttribute('id', 'imgdiv');
        box.appendChild(imgdiv);

        foto_img=document.createElement("img");
        foto_img.setAttribute('id', foto_id);
        imgdiv.appendChild(foto_img);

        imgdesc = document.createElement('div');
        imgdesc.setAttribute('id', 'imgdesc');
        box.appendChild(imgdesc);

        overlay.onclick = function() {
            $(this).fadeOut('fast');
            $('#box').hide();
        };

        box.onclick = function() {
            $('#overlay').fadeOut('fast');
            $('#box').hide();
        };
    }

    windowHeight = getWindowHeight();
    windowWidth = getWindowWidth();

    document.getElementById(foto_id).setAttribute("src", src);
    scale = ((imgHeight*(windowWidth-30)>(windowHeight-35)*imgWidth) ? (windowHeight-35)/imgHeight : (windowWidth-30)/imgWidth);
    imgHeight = Math.floor(imgHeight*scale);
    imgWidth = Math.floor(imgWidth*scale);

    document.getElementById(foto_id).setAttribute('height',String(imgHeight));
    document.getElementById(foto_id).setAttribute('width',String(imgWidth));
    document.getElementById("imgdesc").innerHTML = desc;

    pos_y = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    if (pos_y < 0){pos_y = 0;}
    pos_x = ((getWindowWidth()-parseInt(imgWidth))/2)-20;

    document.getElementById(box_id).style.top = String(pos_y)+'px';
    document.getElementById(box_id).style.left = String(pos_x)+'px';
    document.getElementById(box_id).style.height = String(imgHeight+30)+'px';
    document.getElementById(box_id).style.width = String(imgWidth+40)+'px';

    $('#overlay').fadeIn('fast');
    $('#box').fadeIn('slow');
}

function resizeloadimg()
{
    var pos_y;
    var pos_x;
    var foto_id = 'foto';
    var box_id = 'box';
    var windowHeight;
    var windowWidth;
    var scale;
    var imgHeight=document.getElementById(foto_id).getAttribute('height');
    var imgWidth=document.getElementById(foto_id).getAttribute('width');

    windowHeight = getWindowHeight();
    windowWidth = getWindowWidth();
    scale = ((imgHeight*(windowWidth-30)>(windowHeight-35)*imgWidth) ? (windowHeight-35)/imgHeight : (windowWidth-30)/imgWidth);
    imgHeight = Math.floor(imgHeight*scale);
    imgWidth = Math.floor(imgWidth*scale);

    document.getElementById(foto_id).setAttribute('height',String(imgHeight));
    document.getElementById(foto_id).setAttribute('width',String(imgWidth));

    pos_y = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    if (pos_y < 0){pos_y = 0;}
    pos_x = ((getWindowWidth()-parseInt(imgWidth))/2)-20;

    document.getElementById(box_id).style.top = String(pos_y)+'px';
    document.getElementById(box_id).style.left = String(pos_x)+'px';
    document.getElementById(box_id).style.height = String(imgHeight+30)+'px';
    document.getElementById(box_id).style.width = String(imgWidth+40)+'px';
}

// fine overlay

function onrezizeDo()
{
    RePos();
    resizeloadimg();
}

function getWindowHeight()
{
    var windowHeight = 0;
    if (typeof(window.innerHeight) == 'number')
    {
        windowHeight = window.innerHeight;
    }
    else
    {
        if (document.documentElement && document.documentElement.clientHeight)
        {
            windowHeight = document.documentElement.clientHeight;
        }
        else
        {
            if (document.body && document.body.clientHeight)
            {
                windowHeight = document.body.clientHeight;
            }
        }
    }
    return windowHeight;
}

function getWindowWidth()
{
    var windowWidth = 0;
    if (typeof(window.innerWidth) == 'number')
    {
        windowWidth = window.innerWidth;
    }
    else
    {
        if (document.documentElement && document.documentElement.clientWidth)
        {
            windowWidth = document.documentElement.clientWidth;
        }
        else
        {
            if (document.body && document.body.clientWidth)
            {
                windowWidth = document.body.clientWidth;
            }
        }
    }
    return windowWidth;
}

function encode(string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	}

function lookup(inputString,lang)
{
    inputString = inputString.replace(String.fromCharCode(224), "&agrave;");
    inputString = inputString.replace(String.fromCharCode(232), "&egrave;");
    inputString = inputString.replace(String.fromCharCode(236), "&igrave;");
    inputString = inputString.replace(String.fromCharCode(242), "&ograve;");
    inputString = inputString.replace(String.fromCharCode(249), "&ugrave;");

    //alert(inputString.charCodeAt(0));

    if(inputString.length == 0)
    {
        $('#suggestions').hide();
    }
    else
    { 
        $.post("mulino.php?pg=elajax&lg="+lang, {queryString: ""+inputString+""},
            function(data)
            {
                if(data.length >0)
                {
                $('#suggestions').show();
                $('#autoSuggestionsList').html(data);
                }
            });
    }
} // lookup

function fill(thisValue) {
$('#input_search').val(thisValue);
$('#suggestions').hide();
document.getElementById("input_search").focus();
}


