// Copyright (C) 2005-2008 Ilya S. Lyubinskiy. All rights reserved.
// Technical support: http://www.php-development.ru/
//
// YOU MAY NOT
// (1) Remove or modify this copyright notice.
// (2) Re-distribute this code or any part of it.
//     Instead, you may link to the homepage of this code:
//     http://www.php-development.ru/javascripts/popup-window.php
//
// YOU MAY
// (1) Use this code on your website.
// (2) Use this code as part of another product.
//
// NO WARRANTY
// This code is provided "as is" without warranty of any kind.
// You expressly acknowledge and agree that use of this code is at your own risk.
// USAGE
//
// ***** Variables *************************************************************
var popup_dragging = false;
var popup_target;
var popup_mouseX;
var popup_mouseY;
var popup_mouseposX;
var popup_mouseposY;
var popup_oldfunction;
// ***** popup_mousedown *******************************************************
function popup_mousedown(e)
{
    var ie = navigator.appName == "Microsoft Internet Explorer";
    popup_mouseposX = ie ? window.event.clientX : e.clientX;
    popup_mouseposY = ie ? window.event.clientY : e.clientY;
}
// ***** popup_mousedown_window ************************************************
function popup_mousedown_window(e)
{
    var ie = navigator.appName == "Microsoft Internet Explorer";
    if (ie && window.event.button != 1)
        return;
    if (!ie && e.button != 0)
        return;
    popup_dragging = true;
    popup_target = this['target'];
    popup_mouseX = ie ? window.event.clientX : e.clientX;
    popup_mouseY = ie ? window.event.clientY : e.clientY;
    if (ie)
        popup_oldfunction = document.onselectstart;
    else
        popup_oldfunction = document.onmousedown;
    if (ie)
        document.onselectstart = new Function("return false;");
    else
        document.onmousedown = new Function("return false;");
}
// ***** popup_mousemove *******************************************************
function popup_mousemove(e)
{
    var ie = navigator.appName == "Microsoft Internet Explorer";
    var element = document.getElementById(popup_target);
    var mouseX = ie ? window.event.clientX : e.clientX;
    var mouseY = ie ? window.event.clientY : e.clientY;
    if (!popup_dragging)
        return;
    element.style.left = (element.offsetLeft + mouseX - popup_mouseX) + 'px';
    element.style.top = (element.offsetTop + mouseY - popup_mouseY) + 'px';
    popup_mouseX = ie ? window.event.clientX : e.clientX;
    popup_mouseY = ie ? window.event.clientY : e.clientY;
}
// ***** popup_mouseup *********************************************************
function popup_mouseup(e)
{
    var ie = navigator.appName == "Microsoft Internet Explorer";
    var element = document.getElementById(popup_target);
    if (!popup_dragging)
        return;
    popup_dragging = false;
    if (ie)
        document.onselectstart = popup_oldfunction;
    else
        document.onmousedown = popup_oldfunction;
}
// ***** popup_exit ************************************************************
function popup_exit(e)
{
    var ie = navigator.appName == "Microsoft Internet Explorer";
    var element = document.getElementById(popup_target);
    popup_mouseup(e);
    element.style.display = 'none';
}
// ***** popup_show_imagem************************************************************
// id          - id of a popup window;
// position    - positioning type:
//               "element", "element-right", "element-bottom", "mouse",
//               "screen-top-left", "screen-center", "screen-bottom-right"
// x, y        - offset   
// obj_id      - id da imagem (produto) a ser ampliada
// obj_nome    - nome do produto que tem sua imagem sendo ampliada
// forn_id     - id do fornecedor do produto
// forn_nome   - nome do fornecedor do produto
// prec_n      - preço do produto sem desconto
// prec_d      - preço do produto com desconto
// drag_id     - id of an element within popup window intended for dragging it
// exit_id     - id of an element within popup window intended for hiding it
// position_id - id of an element relative to which popup window will be positioned
function popup_show_imagem(id, position, x, y, obj_id, obj_nome, forn_id, forn_nome, prec_n, prec_d, esto_quant, img_sz,img_id)
{
    // Obtem o sabor se existir
    if (document.popupSabor)
    {
        var saborId = document.popupSabor.popupSaborSel.value;
    }
    else
    {
        var saborId = '';
    }
    var element = document.getElementById('ns1_' + id);
    var drag_element = document.getElementById('ns2_' + id); //drag_id
    var exit_element = document.getElementById('ns3_' + id); //exit_id
    var img_element = document.getElementById('ns4_' + id);  //obj_img
    var head_element = document.getElementById('ns6_' + id); //obj_head
    var forn_element = document.getElementById('ns7_' + id); //obj_forn
    var pn_element = document.getElementById('ns8_' + id);   //obj_pn
    var pd_element = document.getElementById('ns9_' + id);   //obj_pd
    var prod_element = document.getElementById('ns10_' + id);
    var carr_element = document.getElementById('ns11_' + id);
    //img_element.src = 'ns_images/ns_' + obj_id + '.jpg';
    // Verifica qual imagem deve ser mostrada //
    //var imgId = '';
    //if (document.getElementById('imgId')) { 
    //    imgId = document.getElementById('imgId').innerHTML;
    //    if ( imgId !='') {
    //        imgId = '_'+imgId;
    //    } 
    //}
    //img_element.src = 'ns_obj/crop.php?path=ns_' + obj_id + imgId + '.jpg&dir=ns_images&sz='+img_sz;
    jQuery('img[id$=ns4_' + id+']').attr('src', (jQuery('input[id$=hdnImagem]').attr('value').replace("sz=180","sz="+img_sz)));
    img_element.alt = obj_nome;
    img_element.title = obj_nome;
    head_element.innerHTML = '&nbsp;&nbsp;&nbsp;' + obj_nome;
    forn_element.innerHTML = 'Fabricante:&nbsp;<a href="' + monta_http(3) + '&fi=' + forn_id + '" style="color: #505050; font-weight: bold; font-size: 10px;">' + forn_nome;
    pn_element.innerHTML = 'R$ ' + prec_n;
    pd_element.innerHTML = '&nbsp;R$ ' + prec_d + '<br>';
    prod_element.href = monta_http(2) + '&pi=' + obj_id;
    carr_element.href = monta_http(8) + '&pi=' + obj_id+((saborId!='')?'&si='+saborId:'');
    if (esto_quant == 0)
    {
        document.getElementById('ns_nao_disp_'+id).style.display = 'block';
        document.getElementById('ns_disp_'+id).style.display = 'none';
    }
    else
    {
        document.getElementById('ns_nao_disp_'+id).style.display = 'none';
        document.getElementById('ns_disp_'+id).style.display = 'block';
    }
    var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;
    var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
    element.style.position = "absolute";
    element.style.display = "block";
    if (position == "element" || position == "element-right" || position == "element-bottom")
    {
        var position_element = document.getElementById('ns5_' + id); //position_id
        for (var p = position_element; p; p = p.offsetParent)
            if (p.style.position != 'absolute')
            {
                x += p.offsetLeft;
                y += p.offsetTop;
            }
        if (position == "element-right")
            x += position_element.clientWidth;
        if (position == "element-bottom")
            y += position_element.clientHeight;
        element.style.left = x + 'px';
        element.style.top = y + 'px';
    }
    if (position == "mouse")
    {
        element.style.left = (document.documentElement.scrollLeft + popup_mouseposX + x) + 'px';
        element.style.top = (document.documentElement.scrollTop + popup_mouseposY + y) + 'px';
    }
    if (position == "screen-top-left")
    {
        element.style.left = (document.documentElement.scrollLeft + x) + 'px';
        element.style.top = (document.documentElement.scrollTop + y) + 'px';
    }
    if (position == "screen-center")
    {
        element.style.left = (document.documentElement.scrollLeft + (width - element.clientWidth ) / 2 + x) + 'px';
        element.style.top = (document.documentElement.scrollTop + (height - element.clientHeight) / 2 + y) + 'px';
    }
    if (position == "screen-bottom-right")
    {
        element.style.left = (document.documentElement.scrollLeft + (width - element.clientWidth ) + x) + 'px';
        element.style.top = (document.documentElement.scrollTop + (height - element.clientHeight) + y) + 'px';
    }
    drag_element['target'] = 'ns1_' + id;
    drag_element.onmousedown = popup_mousedown_window;
    exit_element.onclick = popup_exit;
}
// ***** popup_show_texto************************************************************
// id          - id of a popup window;
// position    - positioning type:
//               "element", "element-right", "element-bottom", "mouse",
//               "screen-top-left", "screen-center", "screen-bottom-right"
// x, y        - offset   
// head_text   - text que será colocado no header da página popup
// pontoFix    - elemento de referência onde será colocada a janela com o texto (deve complementar ns_)
// drag_id     - id of an element within popup window intended for dragging it
// exit_id     - id of an element within popup window intended for hiding it
// position_id - id of an element relative to which popup window will be positioned
function popup_show_texto(id, position, x, y, head_text,pontoFix)
{
    var element = document.getElementById('ns1_' + id);
    var drag_element = document.getElementById('ns2_' + id); //drag_id
    var exit_element = document.getElementById('ns3_' + id); //exit_id
    var head_element = document.getElementById('ns6_' + id); //obj_head
    head_element.innerHTML = head_text;
    var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;
    var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
    element.style.position = "absolute";
    element.style.display = "block";
    if (position == "element" || position == "element-right" || position == "element-bottom")
    {
        var position_element = document.getElementById(pontoFix); //position_id
        for (var p = position_element; p; p = p.offsetParent)
            if (p.style.position != 'absolute')
            {
                x += p.offsetLeft;
                y += p.offsetTop;
            }
        if (position == "element-right")
            x += position_element.clientWidth;
        if (position == "element-bottom")
            y += position_element.clientHeight;
        element.style.left = x + 'px';
        element.style.top = y + 'px';
    }
    if (position == "mouse")
    {
        element.style.left = (document.documentElement.scrollLeft + popup_mouseposX + x) + 'px';
        element.style.top = (document.documentElement.scrollTop + popup_mouseposY + y) + 'px';
    }
    if (position == "screen-top-left")
    {
        element.style.left = (document.documentElement.scrollLeft + x) + 'px';
        element.style.top = (document.documentElement.scrollTop + y) + 'px';
    }
    if (position == "screen-center")
    {
        element.style.left = (document.documentElement.scrollLeft + (width - element.clientWidth ) / 2 + x) + 'px';
        element.style.top = (document.documentElement.scrollTop + (height - element.clientHeight) / 2 + y) + 'px';
    }
    if (position == "screen-bottom-right")
    {
        element.style.left = (document.documentElement.scrollLeft + (width - element.clientWidth ) + x) + 'px';
        element.style.top = (document.documentElement.scrollTop + (height - element.clientHeight) + y) + 'px';
    }
    drag_element['target'] = 'ns1_' + id;
    drag_element.onmousedown = popup_mousedown_window;
    exit_element.onclick = popup_exit;
}
// ***** popupShowFaltaProduto ************************************************************
// id          - id of a popup window;
// position    - positioning type:
//               "element", "element-right", "element-bottom", "mouse",
//               "screen-top-left", "screen-center", "screen-bottom-right"
// x, y        - offset   
// prodImg     - identificação da imagem do produto
// prodNome    - nome do produto
// pontoFix    - elemento de referência onde será colocada a janela com o texto (deve complementar ns_)
// drag_id     - id of an element within popup window intended for dragging it
// exit_id     - id of an element within popup window intended for hiding it
// position_id - id of an element relative to which popup window will be positioned
function popupShowFaltaProduto(id, position, x, y, prodImg, prodNome, pontoFix)
{
    var element = document.getElementById('ns1_' + id);
    var drag_element = document.getElementById('ns2_' + id); //drag_id
    var exit_element = document.getElementById('ns3_' + id); //exit_id
    document.getElementById('prodSemEstTxt').innerHTML = document.getElementById('rest').innerHTML;
    var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;
    var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
    element.style.position = "absolute";
    element.style.display = "block";
    if (position == "element" || position == "element-right" || position == "element-bottom")
    {
        var position_element = document.getElementById(pontoFix); //position_id
        for (var p = position_element; p; p = p.offsetParent)
            if (p.style.position != 'absolute')
            {
                x += p.offsetLeft;
                y += p.offsetTop;
            }
        if (position == "element-right")
            x += position_element.clientWidth;
        if (position == "element-bottom")
            y += position_element.clientHeight;
        element.style.left = x + 'px';
        element.style.top = y + 'px';
    }
    if (position == "mouse")
    {
        element.style.left = (document.documentElement.scrollLeft + popup_mouseposX + x) + 'px';
        element.style.top = (document.documentElement.scrollTop + popup_mouseposY + y) + 'px';
    }
    if (position == "screen-top-left")
    {
        element.style.left = (document.documentElement.scrollLeft + x) + 'px';
        element.style.top = (document.documentElement.scrollTop + y) + 'px';
    }
    if (position == "screen-center")
    {
        element.style.left = (document.documentElement.scrollLeft + (width - element.clientWidth ) / 2 + x) + 'px';
        element.style.top = (document.documentElement.scrollTop + (height - element.clientHeight) / 2 + y) + 'px';
    }
    if (position == "screen-bottom-right")
    {
        element.style.left = (document.documentElement.scrollLeft + (width - element.clientWidth ) + x) + 'px';
        element.style.top = (document.documentElement.scrollTop + (height - element.clientHeight) + y) + 'px';
    }
    drag_element['target'] = 'ns1_' + id;
    drag_element.onmousedown = popup_mousedown_window;
    exit_element.onclick = popupExitFaltaProduto;
}
/** Função de saida do popupShowFaltaProduto para que a janela seja restaurada*/
function popupExitFaltaProduto(e)
{
    var ie = navigator.appName == "Microsoft Internet Explorer";
    var element = document.getElementById(popup_target);
    popup_mouseup(e);
    var oJanelaTarget = document.getElementById('prodSemEstTxt');
    var oJanelaFonte = document.getElementById('rest');
    oJanelaTarget.innerHTML = oJanelaFonte.innerHTML;
    element.style.display = 'none';
}
// ***** Attach Events *********************************************************
if (navigator.appName == "Microsoft Internet Explorer")
    document.attachEvent('onmousedown', popup_mousedown);
else
    document.addEventListener('mousedown', popup_mousedown, false);
if (navigator.appName == "Microsoft Internet Explorer")
    document.attachEvent('onmousemove', popup_mousemove);
else
    document.addEventListener('mousemove', popup_mousemove, false);
if (navigator.appName == "Microsoft Internet Explorer")
    document.attachEvent('onmouseup', popup_mouseup);
else
    document.addEventListener('mouseup', popup_mouseup, false);
