var modalintervalid = null;
var showpriceintervalid = null;
var showloggedinintervalid = null;

function positionModalPopup(positionTo, modalbehaviourid, offsetX, offsetY)
{
    // Find Modal Position
    var button = $get(positionTo);
    if (!button) return;
    var bounds = Sys.UI.DomElement.getBounds(button);
    if (!bounds) return;

    // Find Modal
    var modal = $find(modalbehaviourid);
    if (!modal) return;
    modal.show();
    
    // Check offsets
    if (!offsetX) offsetX = 0;
    if (!offsetY) offsetY = 0;
    
    // Set Position
    moveModalPopup(modalbehaviourid, bounds.x + offsetX, bounds.y + offsetY)
}
    
function moveModalPopup(modalbehaviourid, x, y)
{
    var pnl = $find(modalbehaviourid);
    if (pnl && pnl._popupElement) 
    {
        pnl._popupElement.style.left = x + 'px'; 
        pnl._popupElement.style.top = y + 'px';
        pnl._popupElement.style.position = 'absolute'
        modalintervalid = setTimeout('moveModalPopup(\'' + modalbehaviourid + '\',' + x + ',' + y + ');', 100);
    }
}

function clearModalPopupInterval()
{
    if (!modalintervalid)
        return;
    clearTimeout(modalintervalid);
}

function setClearInputField(fieldid, text, clear)
{
    var tb = $get(fieldid)
    if (!tb) return;
    if (clear && tb.value==text) tb.value = '';
    if (!clear && tb.value=='') tb.value = text;
}

function hideAddedToShoppingCart(lightboximageid)
{
    var message = $get('divImageAddedToShoppingCart');
    message.style.display = 'none';
}

function doShowAddedToShoppingCart(lightboximageid)
{
    var message = $get('divImageAddedToShoppingCart');
    if (!message) return;
    
    var imagebutton = $get(lightboximageid);
    if (!imagebutton) return;
    
    var bounds = Sys.UI.DomElement.getBounds(imagebutton);
    message.style.left = bounds.x + 'px';
    message.style.top = bounds.y + 'px';
    message.style.display = 'block';
    setTimeout("hideAddedToShoppingCart('" + lightboximageid + "')", 2000);
}

function showAddedToShoppingCart(lightboximageid)
{
    setTimeout("doShowAddedToShoppingCart('" + lightboximageid + "')", 500);
}

function hideDivContainer(imContainer)
{
    var pContainer = $get(imContainer);
    if (pContainer)
        pContainer.style.display = 'none';
}

function showImagePrice(imPrice, divContainer, xDif, yDif)
{
    if (!divContainer) divContainer = 'divPriceContainer';
    if (!xDif) xDif = -8;
    if (!yDif) yDif = -20;
        
    if (showpriceintervalid != null)
        clearTimeout(showpriceintervalid);
        
    var price = $get(divContainer);
    if (!price) return;
    
    var priceText = $get(divContainer + 'Text');
    if (!priceText) return;
    priceText.innerHTML = imPrice.getAttribute('price');
    
    var bounds = Sys.UI.DomElement.getBounds(imPrice);
    
    price.style.left = bounds.x + xDif + 'px';
    price.style.top = bounds.y + yDif + 'px';
    price.style.display = 'block';
    showpriceintervalid = setTimeout("hideDivContainer('" + divContainer + "')", 2000);
}

function showLoggedInToUse(imControl)
{
    var div = $get('divLoggedInToUse');
    if (!div) return;
    
    if (showloggedinintervalid != null)
        clearTimeout(showloggedinintervalid);
        
    var bounds = Sys.UI.DomElement.getBounds(imControl);
    //if (!bounds) return;
    
    div.style.left = bounds.x + bounds.width + 'px';
    div.style.top = bounds.y + bounds.height + 'px';
    div.style.display = 'block';
    showloggedinintervalid = setTimeout("hideDivContainer('divLoggedInToUse')", 5000);
}