function openMenu(elementId) {
    if (elementId.target) {
        elementId = elementId.data.elID;
    }

    var element = document.getElementById(elementId);

    if (element && element.style) {
        element.style.display = 'block';
    }
}

function containsDOM(container, containee) {
    var isParent = false;
    
    do {
        if ((isParent = container == containee)) {
            break;
        }
            
        containee = containee.parentNode;
    }
    while (containee != null);
    
    return isParent;
}

function checkMouseLeave(element, evt) {
    if (element.contains && evt.toElement) {
        return !element.contains(evt.toElement);
    }
    else if (evt.relatedTarget) {
        return !containsDOM(element, evt.relatedTarget);
    }
}

function closeMenu(element, e) {
    var evt = e;
    var el = element;

    if (element &&
        element.target &&
        (arguments.length == 1)) {
        evt = element;

        if (evt.data) {
            el = document.getElementById(evt.data.menuElementID);
        }
    }

    if ((typeof (_isInterfaceLocked) == 'undefined') ||
        (_isInterfaceLocked == false) &&
        el) {
        if (checkMouseLeave(el, evt) &&
            ((typeof(_itemToEdit) == 'undefined') ||
            !_itemToEdit)) {
            el.style.display = 'none';

            if (typeof (toggleEditMode) == 'function') {
                toggleEditMode();
            }
        }
    }
}

function validateEmailAddress(fieldID) {
    var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    return filter.test($f(fieldID).val());
}

function $f(elementID) {
    if (typeof (elementID) == 'undefined') {
        return null;
    }

    var el = $('#' + elementID);
    return (el.length > 0) ? el : null;
}

if (typeof (Node) != 'undefined') {
    Node.prototype.insertAfter = function(newNode, refNode) {
        if (refNode.nextSibling) {
            return this.insertBefore(newNode, refNode.nextSibling);
        }
        else {
            return this.appendChild(newNode);
        }
    }
}

function insertAfterDOM(parent, newNode, refNode) {
    if (refNode.nextSibling) {
        return parent.insertBefore(newNode, refNode.nextSibling);
    }
    else {
        return parent.appendChild(newNode);
    }
}

jQuery.fn.swap = function(b) {
    b = jQuery(b)[0];
    var a = this[0];
    var t = a.parentNode.insertBefore(document.createTextNode(''), a);
    b.parentNode.insertBefore(a, b);
    t.parentNode.insertBefore(b, t);
    t.parentNode.removeChild(t);
    return this;
};

if (!('trim' in String.prototype)) {
    String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g, "");
    };
}

$(document).ready(function() {
    showFancyBox();
});

function showFancyBox() {
    var photoGallery = document.getElementById('photoGallery');

    if (photoGallery) {
        var g = $('a[rel=imageGalleryItems]');

        if (typeof (g.fancybox) == 'function') {
            g.fancybox({
                'transitionIn': 'none',
                'transitionOut': 'none',
                'titlePosition': 'over',
                'titleFormat': function(title,
                                    currentArray,
                                    currentIndex,
                                    currentOpts) {
                    return '<span id="fancybox-title-over">Immagine ' + (currentIndex + 1) + ' di ' + currentArray.length + '<br /><strong>' + title + '</strong></span>';
                }
            });
        }
    }
}

/// <hack target="IE7">
///
/// </hack>
function setCssClass(targetElement, className) {
    if (targetElement.getAttribute('className') != null) {
        targetElement.setAttribute('className', className)
    }
    else {
        targetElement.setAttribute('class', className)
    }
}

function isIe7OrLess() {
    var tmp = $.browser.msie + $.browser.version;

    if ((tmp.indexOf("true6") == -1) &&
        (tmp.indexOf("true7") == -1)) {
        return false;
    }

    return true;
}