﻿var locatedPanels = new Array();

function reallocate() {
    for(var i = 0; i < locatedPanels.length; i++) {
        setLyr(locatedPanels[i][0], locatedPanels[i][1], locatedPanels[i][2], locatedPanels[i][3]);
    }
}

window.onresize = function() { reallocate() }

function showPopUp(strId, strMe) {
    var popup = document.getElementById(strId);
    var helper = document.getElementById(strMe);
    setLyr(helper, strId, 25, 5);
    popup.style.display="block";
}

function hidePopUp(strId) {
    var popup = document.getElementById(strId);
    popup.style.display="none";
}

function firstSetLyr(objName,lyr,offsetX,offsetY)
{
    setLyr(objName,lyr,offsetX,offsetY);
	var panel = new Array(objName,lyr,offsetX,offsetY);
	locatedPanels.push(panel);
}

function setLyr(objName,lyr,offsetX,offsetY)
{
    var obj = document.getElementById(objName);
	var newX = findPosX(obj);
	var newY = findPosY(obj);
	var x = new getObj(lyr);
	x.style.top = newY+offsetY + 'px';
	x.style.left = newX+offsetX + 'px';
	//alert(objName + ": (" + newX + "," + newY + ")");
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
		while(1)
		{
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

function findWidth(obj)
{
	var curwidth = 0;
	if (obj.offsetWidth)
	{
		curwidth += obj.offsetWidth
	}
	else if (obj.style.width)
		curwidth += parseInt(obj.style.width);
	return curwidth;
}

function findHeight(obj)
{
	var curheight = 0;
	if (obj.offsetHeight)
	{
		curheight += obj.offsetHeight
	}
	else if (obj.style.height)
		curheight += parseInt(obj.style.height);
	return curheight;
}

function getObj(name)
{
	if(document.getElementById)
	{
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}
	else if(document.all)
	{
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}
	else if(document.layers)
	{
		if (document.layers[name])
		{
			this.obj = document.layers[name];
			this.style = document.layers[name];
		}
		else
		{
			this.obj = document.layers.testP.layers[name];
			this.style = document.layers.testP.layers[name];
		}
	}
}


function changeSize (boxName, newX, newY, lastAction) 
{
    var newstep = false
    var box = document.getElementById(boxName);
	var actX = findWidth(box);
    var actY = findHeight(box);
    //alert(actX + "/" + actY);
    var stepX, stepY;
    if(actX>newX) {
        if((actX-newX) < 30) {
            if((actX-newX) < 10) {
                stepX = -1;
            } else {
                stepX = -3;
            }
        } else {
            stepX = parseInt((newX-actX) / 10);
        }
    } else {
        if((newX-actX) < 30) {
            if((newX-actX) < 10) {
                stepX = 1;
            } else {
                stepX = 3;
            }
        } else {
            stepX = parseInt((newX-actX) / 10);
        }
    }
    if(actY>newY) {
        if((actY-newY) < 30) {
            if((actY-newY) < 10) {
                stepY = -1;
            } else {
                stepY = -3;
            }
        } else {
            stepY = parseInt((newY-actY) / 10);
        }
    } else {
        if((newY-actY) < 30) {
            if((newY-actY) < 10) {
                stepY = 1;
            } else {
                stepY = 3;
            }
        } else {
            stepY = parseInt((newY-actY) / 10);
        }
    }
    //alert(stepX + "/" + stepY)
    if (actX != newX) {
        box.style.width = (actX + stepX) +'px';
        newstep = true;
    }
    if (actY != newY) {
        box.style.height = (actY + stepY) +'px';
        newstep = true;
    }
    if(newstep) {
        setTimeout("changeSize(\"" + boxName + "\", " + newX + ", " + newY + ", \"" + lastAction + "\")", 30);
    } else {
        if(lastAction != "") {
            setTimeout(lastAction, 30);
        }
    }
}

function changeSizeAndMove(boxName, newX, newY, lastAction, motion) 
{
    var newstep = false
    var box = document.getElementById(boxName);
	var actX = findWidth(box);
    var actY = findHeight(box);
	var posX = findPosX(box);
	var posY = findPosY(box);
    //alert("Pos: ("+ posX + "," + posY +") - Size: (" + actX + "," + actY + ")");
    //return
    var stepX, stepY;
    if(actX>newX) {
        if((actX-newX) < 30) {
            if((actX-newX) < 10) {
                stepX = -1;
            } else {
                stepX = -3;
            }
        } else {
            stepX = parseInt((newX-actX) / 10);
        }
    } else {
        if((newX-actX) < 30) {
            if((newX-actX) < 10) {
                stepX = 1;
            } else {
                stepX = 3;
            }
        } else {
            stepX = parseInt((newX-actX) / 10);
        }
    }
    if(actY>newY) {
        if((actY-newY) < 30) {
            if((actY-newY) < 10) {
                stepY = -1;
            } else {
                stepY = -3;
            }
        } else {
            stepY = parseInt((newY-actY) / 10);
        }
    } else {
        if((newY-actY) < 30) {
            if((newY-actY) < 10) {
                stepY = 1;
            } else {
                stepY = 3;
            }
        } else {
            stepY = parseInt((newY-actY) / 10);
        }
    }
    //alert(stepX + "/" + stepY)
    if (actX != newX) {
        box.style.width = (actX + stepX) +'px';
        if(motion=="left") {box.style.left = (posX - stepX) + 'px';}
        if(motion=="right") {box.style.left = (posX + stepX) + 'px';}
        newstep = true;
    }
    if (actY != newY) {
        box.style.height = (actY + stepY) +'px';
        if(motion=="up") {box.style.top = (posY - stepY) + 'px';}
        if(motion=="down") {box.style.top = (posY + stepY) + 'px';}
        newstep = true;
    }
    
    if(newstep) {
        setTimeout("changeSizeAndMove(\"" + boxName + "\", " + newX + ", " + newY + ", \"" + lastAction + "\", \"" + motion + "\")", 30);
    } else {
        if(lastAction != "") {
            setTimeout(lastAction, 30);
        }
    }
}

// Visualizzazione pannelli

function panelShow(panel_name)
{
    var panel = document.getElementById(panel_name);
    panel.style.display = "block";
}

function panelHide(panel_name)
{
    var panel = document.getElementById(panel_name);
    panel.style.display = "none";
}

// Gestione dell'Immagine dell'header

function getHeaderImg(Image,height) {
    var myImageBox = document.getElementById("Image");
    myImageBox.style.height = height + 'px';
    var oldImages = document.getElementById("ImageHeader");
    if(oldImages != null) {
        myImageBox.removeChild(oldImages);
    }
    if(Image != "") {
        var myImage = document.createElement("IMG")
        myImage.src = Image
        myImage.id = "ImageHeader";
        myImage.style.border = '0px';
        myImage.style.height = height + 'px';
        myImageBox.appendChild(myImage);
    } else {
        myImageBox.style.height = '30px';
    }
}


//VEDI ANCHE
function getCambiaImgClose()
{
    panelHide("CambiaImgClose");
    panelHide("CambiaImgContent");
    changeSizeAndMove("CambiaImgBox", 167, 24, "getCambiaImgCloseEnd();", "left");
}

function getCambiaImgOpen()
{
    panelHide("CambiaImgButton");
    changeSizeAndMove("CambiaImgBox", 187, 113, "getCambiaImgEnd()", "left");

}
function getCambiaImgEnd(boxName)
{
    panelShow("CambiaImgClose");
    panelShow("CambiaImgContent");
}
function getCambiaImgCloseEnd(boxName, boxText) {
    panelShow("CambiaImgButton");
}

function getBoxClose(boxName, boxText)
{
    panelHide(boxName + "Close");
    panelHide(boxName + "Content");
    changeSizeAndMove(boxName + "Box", 200, 24, "getBoxCloseEnd(\'" + boxName + "\', \'" + boxText + "\');", "left");
}

function getBoxOpen(boxName, boxText)
{
    var tdButton = document.getElementById(boxName + "Button");
    tdButton.setAttribute("class", "btnDisactive");
    tdButton.innerHTML = boxText;
    changeSizeAndMove(boxName + "Box", 461, 437, "getBoxEnd(\'"+ boxName + "\')", "left");

    getMovieName("product").hideThumbs();

}
function getBoxEnd(boxName)
{
    panelShow(boxName + "Close");
    panelShow(boxName + "Content");
}
function getBoxCloseEnd(boxName, boxText) {
    var tdButton = document.getElementById(boxName + "Button");
    tdButton.setAttribute("class", "btnActive");
    tdButton.innerHTML = "<a href=\"#\" onclick=\"getBoxOpen(\'" + boxName + "\',\'" + boxText + "\');\">" + boxText + "</a>"
    

}

function getInfoClose(boxName)
{
    panelHide("InfoTecnicheClose");
    panelHide("InfoTecnicheContent");
    panelHide("InfoTecnicheButtons");
    panelHide("InfoTecnicheTesti");
    panelShow("CommunityMenuBut1");
    getMovieName("product").showProduct();
    changeSizeAndMove("InfoTecnicheBox", 200, 24, "getInfoCloseEnd(\'" + boxName + "\');", "left");
}

function getInfoOpen(boxName)
{
    var tdButton = document.getElementById("InfoTecnicheButton");
    tdButton.setAttribute("class", "btnDisactive");
    tdButton.innerHTML = boxName;
    changeSizeAndMove("InfoTecnicheBox", 492, 463, "getInfoEnd()", "left");

    getMovieName("product").hideProduct();

}
function getInfoEnd()
{
    panelShow("InfoTecnicheClose");
    panelShow("InfoTecnicheContent");
    panelShow("InfoTecnicheButtons");
    panelShow("InfoTecnicheTesti");
    panelHide("CommunityMenuBut1");
    
}
function getInfoCloseEnd(boxName) {
    var tdButton = document.getElementById("InfoTecnicheButton");
    tdButton.setAttribute("class", "btnActive");
    tdButton.innerHTML = "<a href=\"#\" onclick=\"getInfoOpen(\'" + boxName + "\');\">" + boxName + "</a>"
    getMovieName("product").showThumbs();

}

function PanelFadeIn(panel)
{
    panelShow(panel);
    var startMS = (new Date()).getTime();
    ChangeOpacity(panel,1000,startMS,0,70);
}
function PanelFadeOut(panel)
{
    var startMS = (new Date()).getTime();
    ChangeOpacity(panel,1000,startMS,70,0)
}

function SetOpacity(object,opacityPct)
{
    // IE.
    object.style.filter = 'alpha(opacity=' + opacityPct + ')';
    // Old mozilla and firefox
    object.style.MozOpacity = opacityPct/100;
    // Everything else.
    object.style.opacity = opacityPct/100;
}
function ChangeOpacity(id,msDuration,msStart,fromO,toO)
{
    var element=document.getElementById(id);
    //'alert(element);
    var opacity = element.style.opacity * 100;
    var msNow = (new Date()).getTime();
    opacity = fromO + (toO - fromO) * (msNow - msStart) / msDuration;
    if (opacity<0) {
        SetOpacity(element,0)
        panelHide(id);
    }
    else if (opacity>100) {
        SetOpacity(element,100)
    }
  
    if ((opacity < toO && fromO < toO) || (opacity > toO && fromO > toO)) {
        SetOpacity(element,opacity);
        element.timer = window.setTimeout("ChangeOpacity('" + id + "'," + msDuration + "," + msStart + "," + fromO + "," + toO + ")",1);
    }

}
function FadeIn(id)
{
    var element=document.getElementById(id);
    if (element.timer) window.clearTimeout(element.timer); 
    var startMS = (new Date()).getTime();
    element.timer = window.setTimeout("ChangeOpacity('" + id + "',1000," + startMS + ",0,100)",1);
}
function FadeOut(id)
{
    var element=document.getElementById(id);
    if (element.timer) window.clearTimeout(element.timer); 
    var startMS = (new Date()).getTime();
    element.timer = window.setTimeout("ChangeOpacity('" + id + "',1000," + startMS + ",100,0)",1);
}
function FadeInImage(foregroundID,newImage,backgroundID)
{
    var foreground=document.getElementById(foregroundID);
    if (backgroundID)
    {
        var background=document.getElementById(backgroundID);
        if (background)
        {
            background.style.backgroundImage = 'url(' + foreground.src + ')';
            background.style.backgroundRepeat = 'no-repeat';
        }
    }
    SetOpacity(foreground,0);
    foreground.src = newImage;
    if (foreground.timer) window.clearTimeout(foreground.timer); 
    var startMS = (new Date()).getTime();
    foreground.timer = window.setTimeout("ChangeOpacity('" + foregroundID + "',1000," + startMS + ",0,100)",10);
}

