﻿var HoverBoxID = "HoverBox";
var HoverBoxWidth = 340;
var HoverBoxHeight = 406;
//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, Titolo, Prezzo, PrezzoAssoluto, Famiglia, Taglie, Da, isKit, idcl) {

    classPrezzo = "TitlePrezzoProdotto_BIGRED";
    stylesconto = "position:absolute; left:210px; top:300px;";
    if (Prezzo == PrezzoAssoluto) { PrezzoAssoluto = ""; classPrezzo = "TitlePrezzoProdotto_BIG"; stylesconto = "display:none;"; }

    //AdditionalHTML = "<div style=\"position:absolute;text-align:left;padding-top:10px;left:2px;top:340px;width:335px;height:30px;\"><table style='width:316px;margin-left:10px' cellpadding=0 cellspacing=0 ><tr><td align=left class='TitleProdottoBox_BIG'>Cavaliere con armatura</td><td align=right class='TitlePrezzoProdotto_BIG'><span class='TitlePrezzoProdottoDa_BIG'>da</span> €20,00</td></tr><tr><td align=left class='TitleFamigliaProdotto_BIG'>Famiglia: Cavalieri e principesse</td><td align=right class='TitleTaglie_BIG'>taglie:</td></tr></table></div>";
    if (isKit=="False") {
        AdditionalHTML = "<div id='text_div' style='display: none; position: absolute; text-align: left; padding-top: 10px; left: 2px; top: 340px; width: 335px; height: 66px;'><table style='width:315px; height:60px; margin-left: 10px; margin-right: 15px;' cellpadding='0' cellspacing='0'><tr><td width='70%' valign='top'><table cellpadding='0' cellspacing='0' ><tr><td align='left' class='TitleProdottoBox_BIG'>" + Titolo + "</td></tr><tr><td align='left' class='TitleFamigliaProdotto_BIG'>" + Famiglia + "</td></tr><tr><td align='left' class='TitleTaglie_BIG'>" + Taglie + "<img src='/public/img/sysfile/sizes.png' /></td></tr></table></td><td width='30%' valign='bottom' align='right'><table cellpadding='0' cellspacing='0' ><tr><td align='right' class='TitlePrezzoProdotto_SMALL'><span class='TitlePrezzoProdotto_SMALL'>" + Da + " </span>" + PrezzoAssoluto + "</td></tr><tr><td align='right' class='" + classPrezzo + "'><span class='TitlePrezzoProdottoDa_BIG'>" + Da + " </span>" + Prezzo + "</td></tr></table></td></tr></table></div><div style='" + stylesconto + ";'><img src='/public/img/sysfile/bollinoscontohoverpopup_" + idcl + ".png' border='0' /></div>";
    }
    else {
        AdditionalHTML = "<div id='text_div' style=\"display:none;position:absolute;text-align:left;padding-top:10px;left:2px;top:340px;width:335px;height:30px;\"><table style='width:316px;margin-left:10px' cellpadding=0 cellspacing=0 ><tr><td colspan='2' align=left class='TitleProdottoBox_BIG'>" + Titolo + "</td><td  align=right class='TitlePrezzoProdotto_BIG'>" + Prezzo + "</td></tr><tr><td align=left class='TitleFamigliaProdotto_BIG'>" + Famiglia + "</td><td colspan='2' align=right class='TitleTaglie_BIG'></td></tr><tr><td></td><td></td><td></td></tr></table></div>";
    }
    var HoverBox = GetHoverBox(HoverBoxID, ImgUrl, AdditionalHTML);
    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 = 10;
    var margineY = 20;
    var margineY2 = 10;
    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 = getScrollY() + e.clientY - margineY - 15 - Math.max(0, (HoverBoxHeight + e.clientY - viewPortRect.height - margineY2));
            //coordinateBox.y = e.pageY - margineY - Math.max(0, (HoverBoxHeight + e.pageY - viewPortRect.height - getScrollY(), -margineY2));
           // alert("passo")
        }
        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 +15 + event.clientY + HoverBoxHeight > viewPortRect.height)
            coordinateBox.y = getScrollY() + event.clientY - margineY -15 - Math.max(0, (HoverBoxHeight + event.clientY - viewPortRect.height - margineY2));
        else
            //posizionamento sotto la linea del cursore
            coordinateBox.y = getScrollY() + event.clientY + margineY;
    
    } 
    
    SetHoverBoxStyle();    
    GetHoverBoxElement().style.left = coordinateBox.x + "px";
    GetHoverBoxElement().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 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";
        if(box.style.backgroundImage.length == 0)
        {
            box.style.backgroundImage = "url(/public/img/sysfile/popup_prodotto.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";
        document.getElementById("text_div").style.display = "";
        //box.childNodes.item(0).childNodes.item(1).style.display=""
        //box.childNodes.item(0).style.display = "";
    }

}


//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 + "\" />";  //ROBY LOADING
            
            document.getElementById(BoxID).innerHTML = "<div><img lowsrc=\"/public/img/sysfile/loading_orig.gif\"  src=\"" + ImgUrl + "\"></img>" + AdditionalHTML+ "</div>" ;
        }
        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;
}
