﻿//-- Interface animations ----------------------------

// Show / Hide Page header
function ShowHideHeader(imageID, headerContentID, headerCollapsedID)
{
    var collapsed = false;
    var image = $(imageID);
    var headerCollapsed = $(headerCollapsedID);

    collapsed = (null != headerCollapsed.value && "true" == headerCollapsed.value);
    new Effect.toggle(headerContentID, 'blind', { duration: 0.5 });

    if (collapsed)
    {
        image.writeAttribute('src', '/CdcPoint/images/commons/iconize_up.gif');
    } 
    else
    {
        image.writeAttribute('src', '/CdcPoint/images/commons/iconize_down.gif');
    }

    headerCollapsed.value = !collapsed;
    PageMethods.SetHeaderCollapseStatus(!collapsed);
}

// Show hide an interface container element (Like the ones used for site tree menu, products cloud, brands...)
function ShowHideElement(sender, target)
{
    var mustLoad = false;
    var element = $(sender);

    new Effect.toggle($(target), 'blind', { duration: 0.2 });

    if (element.hasClassName('closed'))
    {
        element.writeAttribute('title', element.readAttribute('title').sub('Nascondi', 'Mostra'));
        
        element.removeClassName('closed');
        element.addClassName('open');

        // If the element has just been loaded, not load it anymore
        if (!element.hasAttribute('loaded'))
        {
            element.writeAttribute('loaded', 'true');
            mustLoad = true;
        }
    } 
    else
    {
        element.writeAttribute('title', element.readAttribute('title').sub('Mostra', 'Nascondi'));
        
        element.removeClassName('open');
        element.addClassName('closed');
    }

    return mustLoad;
}

/* -- Drop down menu Management -- */

var DDSPEED = 10;
var DDTIMER = 15;

// main function to handle the mouse events //
function ddMenu(headerID, contentID, direction)
{
    var header = document.getElementById(headerID);
    var content = document.getElementById(contentID);
    clearInterval(content.timer);
    if (direction == 1)
    {
        clearTimeout(header.timer);
        if (content.maxh && content.maxh <= content.offsetHeight) { return }
        else if (!content.maxh)
        {
            content.style.display = 'block';
            content.style.height = 'auto';
            content.maxh = content.offsetHeight;
            content.style.height = '0px';
        }
        content.timer = setInterval(function() { ddSlide(content, 1) }, DDTIMER);
    } else
    {
        header.timer = setTimeout(function() { ddCollapse(content) }, 50);
    }
}

// collapse the menu //
function ddCollapse(content)
{
    content.timer = setInterval(function() { ddSlide(content, -1) }, DDTIMER);
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(headerID, contentID)
{
    var header = document.getElementById(headerID);
    var content = document.getElementById(contentID);
    clearTimeout(header.timer);
    clearInterval(content.timer);
    if (content.offsetHeight < content.maxh)
    {
        content.timer = setInterval(function() { ddSlide(content, 1) }, DDTIMER);
    }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(content, direction)
{
    var currh = content.offsetHeight;
    var dist;
    if (direction == 1)
    {
        dist = (Math.round((content.maxh - currh) / DDSPEED));
    } else
    {
        dist = (Math.round(currh / DDSPEED));
    }
    if (dist <= 1 && direction == 1)
    {
        dist = 1;
    }
    content.style.height = currh + (dist * direction) + 'px';
    content.style.opacity = currh / content.maxh;
    content.style.filter = 'alpha(opacity=' + (currh * 100 / content.maxh) + ')';
    if ((currh < 2 && direction != 1) || (currh > (content.maxh - 2) && direction == 1))
    {
        clearInterval(content.timer);
    }
}

//-- Product images ---------------------------

function image_onload(imageID, anchorID) {
    var image = $(imageID);
    var anchor = $(anchorID);

    // Il controllo sulla dimensione è un workaround per gestire le immagini da 1px
    // che il picserver ritorna invece di un HTTP 404 quando l'immagine è assente
    // ciò causa il trigger dell'evento load in ogni caso
    if (image != null) {
        // ie
        if ((image.fileSize != undefined) && (image.fileSize != 1167)) {
        
            if(image.style.visibility == 'hidden')
                image.style.visibility = 'visible';
            
            if(image.style.display == 'none')
                image.style.display = 'block';

            if (anchor != null) {
                anchor.rel = 'lightbox[gallery]';                
            }
        }
        // firefox
        else if (image.width > 1) {

            if (image.style.visibility == 'hidden')
                image.style.visibility = 'visible';

            if (image.style.display == 'none')
                image.style.display = 'block';

            if (anchor != null) {
                anchor.rel = 'lightbox[gallery]';               
            }
        }
    }

    image = null;
    anchor = null;
}

//-- Notify script laoded to the script manager
if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

