﻿var PreviewHoverBoxID = "HoverBoxPreview";
var PreviewHoverBoxWidth = 340;
var PreviewHoverBoxHeight = 206;
//Modalità Flash o HTML: se OnlyHTMLMode==false utilizza flash per l'hoverbox
var PreviewOnlyHTMLMode = true;

//Fa comparire l'hover box
function PreviewHoverBoxOn(parent, ImgUrl, AdditionalHTML) {
    var HoverBox = PreviewGetHoverBox(PreviewHoverBoxID, ImgUrl, AdditionalHTML);
    //posizionamento fuori dallo schermo
    HoverBox.style.visibility = "visible";
    document.onmousemove = Previewfollowmouse;
}

//fa scomparire l'hover box
function PreviewHoverBoxOff() {
    if (PreviewGetHoverBoxElement() != null) {
        PreviewSetHoverBoxStyle();
        PreviewGetHoverBoxElement().style.visibility = "hidden";
        document.onmousemove = null;
    }
}

function Previewfollowmouse(e) {
    var viewPortRect = PreviewgetCurrentViewport();
    var margineX = 30;
    var margineX2 = 30;
    var margineY = 30;
    var margineY2 = 30;
    var coordinateBox = new Previewcoordinate();

    if (typeof e != "undefined") 
    {
        //coordinata X
        if (margineX +e.pageX + PreviewHoverBoxWidth > viewPortRect.width) 
            //posizionamento a sinistra del cursore
            coordinateBox.x = e.pageX - margineX + document.body.scrollLeft - PreviewHoverBoxWidth ;
        else
            //posizionamento a destra del cursore
            coordinateBox.x = e.pageX + margineX + document.body.scrollLeft  ;

        //coordinate y
        if (margineY + e.pageY + PreviewHoverBoxHeight > viewPortRect.height)
            coordinateBox.y = e.pageY - margineY - Math.max(0, (PreviewHoverBoxHeight + e.pageY - viewPortRect.height - getScrollY() - 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 + PreviewHoverBoxWidth > viewPortRect.width)
            //posizionamento a sinistra del cursore
            coordinateBox.x =event.clientX - margineX + document.body.scrollLeft - PreviewHoverBoxWidth;
        else
            //posizionamento a destra del cursore
            coordinateBox.x=event.clientX + margineX + document.body.scrollLeft;
            
        //coordinate y
        if(margineY + event.clientY + PreviewHoverBoxHeight > viewPortRect.height)
            coordinateBox.y = getScrollY() + event.clientY - margineY - Math.max(0, (PreviewHoverBoxHeight + event.clientY - viewPortRect.height - margineY2));
        else
            //posizionamento sotto la linea del cursore
            coordinateBox.y = getScrollY() + event.clientY + margineY;
    
    } 
    
    PreviewSetHoverBoxStyle();    
    PreviewGetHoverBoxElement().style.left = coordinateBox.x + "px";
    PreviewGetHoverBoxElement().style.top = coordinateBox.y + "px";
}


function getScrollY() {
    var scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return scrOfY;
}


//Imposta lo stile dell'hoverbox
function PreviewSetHoverBoxStyle()
{
    if(PreviewGetHoverBoxElement()!=null)
    {
        var box=PreviewGetHoverBoxElement();
        /*box.style.display="table-cell";*/
        box.style.position="absolute";
        box.style.width=PreviewHoverBoxWidth+"px";
        box.style.height=PreviewHoverBoxHeight+"px";
        box.style.zindex="1000";
        //box.style.backgroundImage = "url(/controlli/eCommerce/res/imgs/Back_Anteprima_Dimensioni.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 PreviewinitHoverBox(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 PreviewFillHoverBox(BoxID, ImgUrl, AdditionalHTML) {
    if (document.getElementById(BoxID) != null) {
        if (PreviewOnlyHTMLMode)
        {
            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 PreviewGetHoverBoxElement() {
    return document.getElementById(PreviewHoverBoxID);
}


//restituisce il box utilizzato per fare l'hover
function PreviewGetHoverBox(BoxID, ImgUrl, AdditionalHTML) {
    if (document.getElementById(BoxID) == null) {
        PreviewinitHoverBox(PreviewHoverBoxID, ImgUrl, AdditionalHTML);
        PreviewFillHoverBox(PreviewHoverBoxID, ImgUrl, AdditionalHTML);
    }
    else {
        PreviewFillHoverBox(PreviewHoverBoxID, ImgUrl, AdditionalHTML);
    }
    return document.getElementById(BoxID);
}

function PreviewfindPosX(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 PreviewfindPosY(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 Previewrectangle(rect_width, rect_height) {
    this.width = rect_width;
    this.height = rect_height;
}

//ADT rettangolo posizionato
function Previewpositioned_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 Previewcoordinate(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 PreviewgetCurrentViewport() {
    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 Previewrectangle(viewportwidth, viewportheight);
    return currentViewport;
}
