var zoomDisplayed  = false;
var curtainVisible = false;

function getElementPosition (e)
{
    var p = {};
    p.x = e.offsetLeft;
    p.y = e.offsetTop;

    var c = e.offsetParent;
    while (c)
    {
        p.x += c.offsetLeft;
        p.y += c.offsetTop;

        c = c.offsetParent;
    }
    
    return p;
}

function getGlobalMousePosition (e)
{
    var p = { x: e.clientX, y: e.clientY };
    var d = document.documentElement;
    var b = document.body;
    var w = window;

    p.x += (w.scrollX) ? w.scrollX : (d.scrollLeft + b.scrollLeft);
    p.y += (w.scrollY) ? w.scrollY : (d.scrollTop + b.scrollTop);

    return p;
}

function toogleZoom(event, id)
{
    if (id == null)
        id = "zoomproduit";

    var el = document.getElementById(id);
    var dy = (zoomDisplayed) ? "none" : "block";
   
    el.style.display=dy;
    
    zoomDisplayed = !zoomDisplayed;

    toggleCurtain(event);
}

function moveZoom (event, element)
{
    var p = getElementPosition (element);
    var d = document.documentElement;
	var b = document.body;
	var w = window;

    p.x -= (w.scrollX) ? w.scrollX : (d.scrollLeft + b.scrollLeft);
	p.y -= (w.scrollY) ? w.scrollY : (d.scrollTop + b.scrollTop);

    var x = Math.round (((event.clientX - p.x) / 950) * 100);
    var y = Math.round (((event.clientY - p.y) / 650) * 100);

    element.style.backgroundPosition = x + "% " + y + "%";
}

function moveNav (event, element)
{
    var p = getElementPosition (element);
    var u = document.getElementById ("navunivers");
    var n;
    
    for (var i = 1; i < 5; i++)
    {
        n = document.getElementById ("lescategories" + i);
        if (n) break;
    }

    var w = n.offsetWidth - u.offsetWidth;
    var x = Math.round(((event.clientX - p.x) / u.offsetWidth) * w);

    n.style.marginLeft = -x + "px";
}

function showCurtain (event)
{
    var curtain = document.getElementById ("curtain");
    if (curtain == null)
    {
        var bodyHeight = 960;
        var bodyWidth  = document.width;

        if (typeof bodyWidth == 'undefined')
        {
            bodyWidth  = document.body.offsetWidth;
        }
        
        curtain    = document.createElement ("div");
        curtain.id = "curtain";

        with (curtain.style)
        {
            width           = bodyWidth + "px";
            height          = bodyHeight + "px";
            display         = "none";
            backgroundColor = "black";
            position        = "absolute";
            top             = 0;
            left            = 0;
            opacity         = 0.8;
            MozOpacity      = 0.8;
            filter          = "alpha(opacity=80)";
            zIndex          = 999;
        }

        document.body.appendChild (curtain);
    }

    curtainVisible = true;
    curtain.style.display = "block";
    Valraiso.CSS.events.hideSelect();

    var el = document.getElementById("zoomproduit");

    moveZoom(event, el);
}

function hideCurtain (event)
{
    var curtain = document.getElementById ("curtain");
    if (curtain != null)
    {
        curtain.style.display = "none";
        curtainVisible = false;
        Valraiso.CSS.events.showSelect();
    }
}

function toggleCurtain (event)
{
    if (curtainVisible) hideCurtain(event);
    else showCurtain(event);
}

function swapFlv (flv)
{
    var flash = document.getElementById("flash");
    var str;

    str  = '<object type="application/x-shockwave-flash" data="http://flv-player.net/medias/player_flv_maxi.swf?'+ new Date().getTime()  +'" width="435" height="250">';
    str += '<param name="movie" value="http://flv-player.net/medias/player_flv_maxi.swf" />';
    str += '<param name="allowFullScreen" value="true" />';
    str += '<param name="FlashVars" value="flv=http%3A//www.valraiso.biz/videos/lapierre/' + flv + '.flv&amp;width=435&amp;height=250&amp;autoplay=1&amp;autoload=1&amp;margin=0&amp;showfullscreen=1&amp;playertimeout=1000&amp;playeralpha=100&amp;loadingcolor=c4cdb5&amp;buttoncolor=3d5a09&amp;buttonovercolor=c4cdb5&amp;slidercolor1=3d5a09&amp;slidercolor2=3d5a09&amp;sliderovercolor=c4cdb5&amp;buffermessage=_n_&amp;buffercolor=999999&amp;buffershowbg=0&amp;showmouse=autohide" />';
    str += '</object>';

    flash.innerHTML = str;
}

function showArchive()
{
    var archive = EL('archive');
    var value   = Valraiso.form.getFieldValue (archive);
    
    if (value != '')
    {
        document.location = urlArchives + '/' + value;
    }
}