﻿var HoverBoxID = "HoverBox";
var HoverBoxWidth = 340;
var HoverBoxHeight = 340;
//Modalità Flash o HTML: se OnlyHTMLMode==false utilizza flash per l'hoverbox
var OnlyHTMLMode = true;

//Fa comparire l'hover box
function HoverBoxOn(parent, ImgUrl, AdditionalHTML) {
    var HoverBox = GetHoverBox(HoverBoxID, ImgUrl, AdditionalHTML);
    //posizionamento fuori dallo schermo
    HoverBox.style.visibility = "visible";
    document.onmousemove = followmouse;
}

//fa scomparire l'hover box
function HoverBoxOff() {
    if (GetHoverBoxElement() != null) {
        SetHoverBoxStyle();
        GetHoverBoxElement().style.visibility = "hidden";
        document.onmousemove = null;
    }
}

function followmouse(e) {
    var viewPortRect = getCurrentViewport();
    var margineX = 30;
    var margineX2 = 30;
    var margineY = 30;
    var margineY2 = 30;
    var coordinateBox = new coordinate();

    if (typeof e != "undefined") 
    {
        //coordinata X
        if (margineX +e.pageX + HoverBoxWidth > viewPortRect.width) 
            //posizionamento a sinistra del cursore
            coordinateBox.x = e.pageX - margineX + document.body.scrollLeft - HoverBoxWidth ;
        else
            //posizionamento a destra del cursore
            coordinateBox.x = e.pageX + margineX + document.body.scrollLeft  ;

        //coordinate y
        if (margineY + e.pageY + HoverBoxHeight > viewPortRect.height) 
            coordinateBox.y = e.pageY - margineY - Math.max(0, (HoverBoxHeight + e.pageY - viewPortRect.height - document.body.parentNode.scrollTop - margineY2));
        else
            //posizionamento sotto la linea del cursore
            coordinateBox.y = e.pageY + margineY;
    }
    else if (typeof window.event !="undefined")
    {
        //coordinata X
        if(event.clientX + margineX + HoverBoxWidth > viewPortRect.width)
            //posizionamento a sinistra del cursore
            coordinateBox.x =event.clientX - margineX + document.body.scrollLeft - HoverBoxWidth;
        else
            //posizionamento a destra del cursore
            coordinateBox.x=event.clientX + margineX + document.body.scrollLeft;
            
        //coordinate y
        if(margineY + event.clientY + HoverBoxHeight > viewPortRect.height)
            coordinateBox.y = document.body.parentNode.scrollTop + event.clientY - margineY - Math.max(0, (HoverBoxHeight + event.clientY - viewPortRect.height - margineY2));
        else
            //posizionamento sotto la linea del cursore
            coordinateBox.y = document.body.parentNode.scrollTop + event.clientY + margineY;
    
    } 
    
    SetHoverBoxStyle();    
    GetHoverBoxElement().style.left = coordinateBox.x + "px";
    GetHoverBoxElement().style.top = coordinateBox.y + "px";
} 

//Imposta lo stile dell'hoverbox
function SetHoverBoxStyle()
{
    if(GetHoverBoxElement()!=null)
    {
        var box=GetHoverBoxElement();
        /*box.style.display="table-cell";*/
        box.style.position="absolute";
        box.style.width=HoverBoxWidth+"px";
        box.style.height=HoverBoxHeight+"px";
        box.style.zindex="1000";
        box.style.backgroundImage = "url(/controlli/eCommerce/res/imgs/Back_Anteprima_Sticker.png)";
        box.style.lineHeight = 100;
        box.style.backgroundPosition="center center";
        box.style.backgroundRepeat="no-repeat";
        //posizionamento iniziale fuori schermo
        box.style.left="-500px";
        box.style.top = "-500px";
        box.style.paddingTop = "20px";
        //box.style.paddingLeft = "20px";
        box.style.textAlign = "center";
        box.style.verticalAlign = "middle";
    }

}


//Inizializza il box che esegue l'hover
function initHoverBox(BoxID, ImgUrl, AdditionalHTML) {
    if (document.getElementById(BoxID) == null) {
        //box non esistente: lo inizializzo
        var newdiv = document.createElement('div');
        newdiv.setAttribute('id', BoxID);
        //newdiv.innerHTML = "<img src=\"" + ImgUrl + "\" style=\"margin-top:10px; margin-left:10px;\" />";
        document.body.appendChild(newdiv);
    }
}

//riempie la hoverbox
function FillHoverBox(BoxID, ImgUrl, AdditionalHTML) {
    if (document.getElementById(BoxID) != null) {
        if (OnlyHTMLMode)
        {
            document.getElementById(BoxID).innerHTML = "<img src=\"" + ImgUrl + "\" />";
        }
        else {
            document.getElementById(BoxID).innerHTML =
            "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#                   version=8,0,0,0\" width=\"280\" height=\"320\" id=\"HoverPreview\" align=\"middle\"> " +
                "<param name=\"allowScriptAccess\" value=\"sameDomain\" /> " +
                "<param name=\"movie\" value=\"/public/img/HoverPreview.swf?imgFileName=" + ImgUrl + "\" /> " +
                "<param name=\"quality\" value=\"high\" /> " +
                "<param name=\"wmode\" value=\"trasparent\" /> " +
                "<param name=\"bgcolor\" value=\"#ffffff\" /> " +
                  "<embed src=\"/public/img/HoverPreview.swf\" quality=\"high\" bgcolor=\"#ffffff\" width=\"280\" height=\"320\" name=\"HoverPreview\" align=\"middle\" allowScriptAccess=\"sameDomain\" FlashVars=\"imgFileName=" + ImgUrl + "\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" /> " +
            "</object>";
        }
    }
}

//Restituisce l'elemento DIV floating
function GetHoverBoxElement() {
    return document.getElementById(HoverBoxID);
}


//restituisce il box utilizzato per fare l'hover
function GetHoverBox(BoxID, ImgUrl, AdditionalHTML) {
    if (document.getElementById(BoxID) == null) {
        initHoverBox(HoverBoxID, ImgUrl, AdditionalHTML);
        FillHoverBox(HoverBoxID, ImgUrl, AdditionalHTML);
    }
    else {
        FillHoverBox(HoverBoxID, ImgUrl, AdditionalHTML);
    }
    return document.getElementById(BoxID);
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        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;
}


///Utilities

//ADTs----------------------------------------
///ADT rettangolo
function rectangle(rect_width, rect_height) {
    this.width = rect_width;
    this.height = rect_height;
}

//ADT rettangolo posizionato
function positioned_rectangle(rect_width, rect_height, rect_x ,rect_y) {
    this.width = rect_width;
    this.height = rect_height;
    this.x = rect_x;
    this.y = rect_y;
}

//ADT coordinate
function coordinate(coord_X, coord_Y) {
    this.x = coord_X;
    this.y = coord_Y;
}
//Fine ADTs----------------------------------------

//restituisce il viewport attuale
//non include la barra di scorrimento verticale
function getCurrentViewport() {
    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
    }

    var currentViewport = new rectangle(viewportwidth, viewportheight);
    return currentViewport;
}