﻿var xmlhttp;

function findPos(obj)
{
    var curLeft = curTop = 0;
    if (obj.offsetParent)
    {
        do
        {
            curLeft += obj.offsetLeft;
            curTop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return { x: curLeft, y: curTop };
}

function draw()
{
    var centerX = findPos(document.getElementById('x'));
    centerX.x += document.getElementById('x').style.width;
    centerX.y += document.getElementById('x').style.height;
    var centerZ = findPos(document.getElementById('z'));
    centerZ.x += document.getElementById('z').style.width;
    centerZ.y += document.getElementById('z').style.height;
    //Now you've got both centers in reference to the page
    var canvasPos = findPos(document.getElementById('canvas'));
    centerX.x -= canvasPos.x;
    centerX.y -= canvasPos.y;
    centerZ.x -= canvasPos.x;
    centerZ.y -= canvasPos.y;
    //Now both points are in reference to the canvas
    var ctx = document.getElementById('canvas').getContext('2d');
    ctx.beginPath();
    ctx.moveTo(centerX.x, centerX.y);
    ctx.lineTo(centerZ.x, centerZ.y);
    ctx.stroke();

}

function applicaOmbraTesto()
{
    $(".menuLeft ul li a").textShadow({ x: 4, y: 4, radius: 10, color: "#7F164E" });
    $(".headerTitle span").textShadow({ x: 4, y: 4, radius: 10, color: "#7F164E" });    
    
    $("h1").textShadow({ x: 4, y: 4, radius: 10, color: "#7F164E" });
    $(".newsContainer_sidebar h4").textShadow({ x: 4, y: 4, radius: 10, color: "#7F164E" });
    $(".newsContainer_sidebar p").textShadow({ x: 4, y: 4, radius: 10, color: "#7F164E" });

    $(".contentRight h2").textShadow({ x: 4, y: 4, radius: 10, color: "#7F164E" });

    $(".footerLeft span").textShadow({ x: 4, y: 4, radius: 10, color: "#7F164E" });
    $(".footerRight span").textShadow({ x: 2, y: 2, radius: 5, color: "#7F164E" });

    
}

function applicaOmbraTesto_Home()
{
    $(".menuLeft ul li a").textShadow({ x: 4, y: 4, radius: 10, color: "#7F164E" });
    $(".headerTitle span").textShadow({ x: 4, y: 4, radius: 10, color: "#7F164E" });

    $("h1").textShadow({ x: 2, y: 2, radius: 7, color: "#7F164E" });
    //$(".contentTitle h1").textShadow({ x: 4, y: 4, radius: 10, color: "#7F164E" });
    $(".contentDescription").textShadow({ x: 2, y: 2, radius: 7, color: "#7F164E" });
    
    //$(".newsContainernewsContainer_sidebar h1").textShadow({ x: 4, y: 4, radius: 10, color: "#7F164E" });
    $(".newsContainernewsContainer_sidebar h4").textShadow({ x: 4, y: 4, radius: 10, color: "#7F164E" });
    $(".newsContainernewsContainer_sidebar p").textShadow({ x: 4, y: 4, radius: 10, color: "#7F164E" });
    
    $(".footerLeft span").textShadow({ x: 4, y: 4, radius: 10, color: "#7F164E" });
    $(".footerRight span").textShadow({ x: 4, y: 4, radius: 10, color: "#7F164E" });
}

function setBackground()
{
//    var $winHeight = $(document).height();
//    document.getElementById('backgroundImg').style.height = $winHeight + 'px';
    $.backstretch("/img/bk2.jpg");
//    $.supersized({
//        slides: [{ image: '/img/bk2.jpg', title: ''}]
//    });
}

function trimToPx(str,len)
{
    var tmp = str;
    var trimmed = str;
    
    //var row = new Array();
    if (visualLength(tmp) > len)
    {
        trimmed += "...";
        while (visualLength(trimmed) > len)
        {
            tmp = tmp.substring(0, tmp.length - 1);
            trimmed = tmp + "...";
            
        }
    }

    return trimmed;
}

function diagonalText(str, totLen) 
{
    var strExit = "";
    var numInitSpace = 0;
    var tmp = " ";
    var txtCount = 0;
    while (tmp != "" && str != "" && txtCount++ < 5) 
    {
        tmp = diagonalTextPartial(str, totLen, numInitSpace);
        if (strExit != "" && strExit.substring(strExit.length - 5, strExit.length) != "<br/>") 
        {
            strExit += "<br/>";
        }
        strExit += tmp;
        str = str.replace(tmp.replace(/&nbsp;/g, ""), "");
        numInitSpace += 4;
    }

    return strExit;
}

function diagonalTextPartial(str, totLen, numInitSpace) 
{
    var tmp = str;

    if (tmp.replace(/&nbsp;/g, "") == "") 
    {
        return "";
    }

    for (var i = 0; i < numInitSpace; i++) 
    {
        tmp = "&nbsp;" + tmp;
    }

    var tmptmp = tmp;
    while (visualLength(tmptmp) > totLen) 
    {
        tmptmp = tmptmp.substring(0, tmptmp.length / 1.5)
    }

    tmptmp = tmp.substring(0, tmptmp.length);
    var lenbefore = 0;

    while (visualLength(tmptmp) <= totLen && lenbefore != visualLength(tmptmp)) 
    {
        var indexBR = tmptmp.indexOf('<br/>', 0);
        if (indexBR != -1) 
        {
            tmptmp = tmp.substring(0, indexBR + 5);
            tmp = tmp.substring(0, tmptmp.length);
            break;
        }
        else 
        {
            lenbefore = visualLength(tmptmp);
            tmptmp = tmp.substring(0, tmptmp.length + 2);
            if (tmptmp.replace(/&nbsp;/g, "") == "") 
            {
                tmp = "";
                break;
            }


            tmp = tmp.substring(0, tmptmp.length);

            while (tmp != "" && tmp.substring(tmp.length - 1, tmp.length) != " ") 
            {
                tmp = tmp.substring(0, tmp.length - 1);
                if (tmp.replace(/&nbsp;/g, "") == "") 
                {
                    break;
                }
            }
        }
    }

    return tmp;
}

function visualLength(text)
{
    var ruler = document.getElementById("ruler");
    ruler.innerHTML = text;
    return ruler.offsetWidth;
}


function XhttpCall(operazione, parametri)
{
    var xmlDoc;

    if (window.ActiveXObject)
    {
        xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
        xmlDoc.loadXML('<?xml version="1.0" encoding="iso-8859-1" ?> <ROOT/>');

        oNde = xmlDoc.createElement("OPERAZIONE");
        oNde.nodeTypedValue = operazione;
        xmlDoc.documentElement.appendChild(oNde);

        //parametri
        oNde = xmlDoc.createElement("PARAMETRI");

        oNde.nodeTypedValue = parametri; //valore parametro

        xmlDoc.documentElement.appendChild(oNde);
    }
    else if (document.implementation && document.implementation.createDocument)
    {
        xmlDoc = document.implementation.createDocument("", "ROOT", null);
        var root = xmlDoc.documentElement;

        oNde = xmlDoc.createElement("OPERAZIONE");
        root.appendChild(oNde);
        oNde.appendChild(document.createTextNode(operazione));

        //parametri
        oNde = xmlDoc.createElement("PARAMETRI");

        oNde.appendChild(document.createTextNode(parametri));

        root.appendChild(oNde);
    }
    
    //Invio    
    if (window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
        if (xmlhttp.overrideMimeType)
        {
            xmlhttp.overrideMimeType('text/xml');
        }
    }
    else
    {
        if (window.ActiveXObject)
        {
            try
            {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                try
                {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e)
                {
                }
            }
        }
        if (!xmlhttp)
        {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
    }
    xmlhttp.open("POST", "/lib/xhttpCall.aspx", false);
    xmlhttp.send(xmlDoc);

    //pacchetto vuoto
    if (xmlhttp.responseText == "")
    {
        return;
    }

    //ritorno
    if (window.ActiveXObject)
    {
        xmlDoc.loadXML(xmlhttp.responseText);
    }
    else if (document.implementation && document.implementation.createDocument)
    {
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(xmlhttp.responseText, "text/xml");
    }
   
    XhttpResponse(xmlDoc);
}

function XhttpResponse(xmlDoc)
{
    try
    {
        var nodeint = childElements(firstChildElement(firstChildElement(xmlDoc)));
        for (var i = 0; i < nodeint.length; i++)
        {
            switch (nodeint[i].nodeName)
            {
                case "home":
//                    var valueLen = 580;
//                    if (jQuery.browser.msie)
//                    {
//                        if (jQuery.browser.version >= 8.0)
//                        {
//                            valueLen = 650;
//                        }
//                        else
//                        {
//                            valueLen = 635;
//                        }
//                    }
//                    document.getElementById("contentDescription").innerHTML = firstChildElement(nodeint[i]).getAttribute("valore");
                    //document.getElementById("contentDescription").innerHTML = diagonalText(firstChildElement(nodeint[i]).getAttribute("valore"), valueLen);

                    break;
                case "prodotti":
                    $('.scrollable .items').html(getNodeCDATA(nodeint[i]));
                    $('.scrollable').data('scrollable').begin();
                    prodottiHover();
                    prodottiClick();
                    var firstProd = $(".imgThumb:first img");
                    if (firstProd.length)
                    {
                        //window.location = $(".productList li:first a").attr("href");
                        firstProd.click();
                    }
                    else
                    {
                        $('.slidetabs').data('tabs').destroy();
                        XhttpCall("5", "<PRODOTTO ID=\"-5\"/>");
                    }
                    break;
                case "immaginiprodotti":
                    $('.slidetabs').data('tabs').destroy();
                    $('.slidetabs').html(getNodeCDATA(firstChildElement(nodeint[i])));
                    $('.productSlide .images').html(getNodeCDATA(lastChildElement(nodeint[i])));
                    if ($('.slidetabs').html() != "") {
                        $(".slidetabs").tabs(".images > div", {
                            effect: 'fade',
                            fadeOutSpeed: "slow",
                            rotate: true
                        });

                        $('.images a').lightBox();
                    }
                    break;
                case "prodotto":
                    document.getElementById("divproductDetails").innerHTML = getNodeCDATA(nodeint[i]);
                    $('#scrollText').jScrollPane({ verticalGutter: 10, autoReinitialise: true });
                    $("a[rel]").overlay({ top: '15%' });
                    break;
                case "info":
                    document.getElementById("infoResponse").innerHTML = firstChildElement(nodeint[i]).getAttribute("valore");
                    break;
                case "contatti":
                    document.getElementById("infoResponse").innerHTML = firstChildElement(nodeint[i]).getAttribute("valore");
                    break;
                default:
                    break;
            }
        }
    }
    catch (e)
    {
        var log = e;
    }
}

function inviaMailContatti()
{
    var mail = document.getElementById("txtMail").value;
    var msg = document.getElementById("txtMessaggio").value;

    if (checkMail(mail))
    {
        document.getElementById("infoResponse").innerHTML = "";
        var buff = "<CONTATTI DESTINATARIO=\"" + mail + "\" MESSAGGIO= \"" + msg + "\"/>";
        XhttpCall("7", buff);
    }
    else
    {
        document.getElementById("infoResponse").innerHTML = "Indirizzo e-mail non corretto";
    }
}

//NAVIGAZIONE DOM
function getNodeCDATA(node)
{
    var value;
    //alert(node.nodeType);
    if (node.hasChildNodes)
    {
        for (var i = 0; i < node.childNodes.length; i++)
        {
            if (node.childNodes[i].nodeType == 4)
            {
                value = node.childNodes[i].nodeValue;
            }
        }
        return value;
    }
    else
    {
        return node.nodeValue
    }
}

function firstChildElement(node)
{
    var element = node.firstChild;
    while (element && element.nodeType != 1)
    {
        element = element.nextSibling;
    }
    return element;
}

function lastChildElement(node)
{
    var element = node.lastChild;
    while (element && element.nodeType != 1)
    {
        element = element.previousSibling;
    }
    return element;
}

function childElements(node)
{
    var elements = new Array();
    for (var i = 0; i < node.childNodes.length; i++)
    {
        if (node.childNodes[i].nodeType == 1)
        {
            elements.push(node.childNodes[i]);
        }
    }
    return elements;
}

function nextSiblingElement(node)
{
    var sibling = node.nextSibling;
    while (sibling && sibling.nodeType != 1)
    {
        sibling = sibling.nextSibling;
    }
    return sibling;
}

function previousSiblingElement(node)
{
    var sibling = node.previousSibling;
    while (sibling && sibling.nodeType != 1)
    {
        sibling = sibling.previousSibling;
    }
    return sibling;
}

function messaggio(val)
{
    alert(val);
}

function checkMail(elementValue)
{
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    return emailPattern.test(elementValue);
}

function backgroundImg_onclick()
{
    ;
}
