var backgroundImageLocation = '/images/common/spacer.gif';
var useBackgroundDiv = false;
//backgroundIframeSrc MUST be used if the useBackgroundDiv is true
var backgroundIframeSrc = "";
var mouseX = 0;
var mouseY = 0;
var clickElement;

function hideLabel() {
    var labelBkgnd = document.getElementById("label-bkgnd");
    var labelDiv = document.getElementById("label-div");

    if (labelBkgnd != null)
    {
        document.body.removeChild(document.getElementById("label-bkgnd"));
    }
    if (labelDiv != null)
    {
        document.body.removeChild(document.getElementById("label-div"));
    }
}

function displayLabelByUPC(upc, ele, locale)
 {
    clickElement = ele;
    //background positioner image
    var backgroundImg = document.createElement("img");
    backgroundImg.src = backgroundImageLocation;
    backgroundImg.setAttribute("id", "label-pos-img");
    document.body.appendChild(backgroundImg);

    var imgPosition = $('#label-pos-img').position();


    //background
    var backgroundDiv = document.createElement("div");
    backgroundDiv.setAttribute("id", "label-bkgnd");
    backgroundDiv.style.position = "absolute";
    if (useBackgroundDiv) {
        backgroundDiv.style.height = parseInt(imgPosition.top) + 20 + "px";
        backgroundDiv.style.width = "100%";
    }
    document.body.appendChild(backgroundDiv);

    var backgroundIframe = document.createElement("iframe");
    backgroundIframe.src = backgroundIframeSrc;
    backgroundIframe.style.position = "absolute";
    backgroundIframe.setAttribute("id", "label-bkgnd-iframe");
    if (useBackgroundDiv) {
        backgroundIframe.style.height = parseInt(imgPosition.top) + 20 + "px";
        backgroundIframe.style.width = "100%";
    }
    document.getElementById("label-bkgnd").appendChild(backgroundIframe);

    var elePosition = $(ele).position();
    mouseX = elePosition.left;
    mouseY = elePosition.top;
    var leftVal = (screen.width - 320) / 2;
    var labelDiv = document.getElementById('label-div');
    if (labelDiv != null) {
        document.body.removeChild(labelDiv);
    }

    labelDiv = document.createElement("div");
    labelDiv.setAttribute("id", "label-div");
    labelDiv.style.display = "none";
    document.body.appendChild(labelDiv);

    //document.getElementById('label-div').innerHTML="Loading...";
    $('#label-div').html("<div id='label-div-loading'><p>Loading...</p></div>");


    var myWidth = 0;
    if (typeof(window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
    }

    $('#label-div').css('left', ((myWidth - 320) / 2));
    $('#label-bkgnd').css('left', ((myWidth - 320) / 2));

    var postUrl = "";
    if (locale != undefined) {
        postUrl = '/nutrition-label?upc=' + upc + '&inline=true&locale=' + locale;
    }
    else {
        postUrl = '/nutrition-label?upc=' + upc + '&inline=true&locale=english';
    }
    $.ajax({
        url: postUrl,
        success: function(data) {
            //$('.result').html(data);
            $('#label-div').html(data);
            if (useBackgroundDiv)
            {
                if ((parseInt($('#label-div').height()) + parseInt(position.top)) > parseInt($('#label-bkgnd').height()))
                {
                    $('#label-bkgnd').css("height", parseInt(position.top) + parseInt($('#label-div').height()) + "px");
                    $('#label-bkgnd-iframe').css("height", parseInt(position.top) + parseInt($('#label-div').height()) + "px");
                }
            } else {
                $('#label-bkgnd').css("height", parseInt($('#label-div').height()) + "px");
                $('#label-bkgnd-iframe').css("height", parseInt($('#label-div').height()) + "px");
            }
            window.scrollTo(0, position.top);
        }
    });

    if (mouseY >= $('#label-div').height())
    {
        //mouseY = mouseY - 400;
    }
    $('#label-div').css('top', mouseY);
    $('#label-div').css('display', 'block');
    var position = $('#label-div').position();


    if (!useBackgroundDiv) {
        $('#label-bkgnd').css("top", mouseY);
    }
    addResizeEvent(moveLabelByResize);
}

function addResizeEvent(func) {
    var oldonresize = window.onresize;
    if (typeof window.onresize != 'function') {
        window.onresize = func;
    } else {
        window.onresize = function() {
            if (oldonresize) {
                oldonresize();
            }
            func();
        }
    }
}


function moveLabelByResize() {
    var elePosition = $(clickElement).position();
    $('#label-div').css('left', elePosition.left);
}

function resizeSelf() {

    var width = document.getElementById("label-wrapper").offsetWidth;
    var height = document.getElementById("label-wrapper").offsetHeight;

    window.resizeBy(width - 320, height - 530);
}


function printLabel(upc, locale) {
    if (locale == undefined || locale == null)
    {
        window.open('/nutrition-label?upc=' + upc + '&printerFriendly=true', '', 'left=20,top=20,width=500,height=500,toolbar=0,resizable=1');
    } else
    {
        window.open('/nutrition-label?upc=' + upc + '&printerFriendly=true&locale=' + locale, '', 'left=20,top=20,width=500,height=500,toolbar=0,resizable=1');
    }

}
