/**
 * Created by IntelliJ IDEA.
 * User: frederico
 * Date: 04/04/11
 * Time: 17:15
 * To change this template use File | Settings | File Templates.
 */
ŧ = function(id) {
    return document.getElementById(id);
};

ŧ.update = function(id, nomeSubgrupo) {
    return ŧ(id).innerHTML = nomeSubgrupo;
};

clearMarked = function(el) {
    var gParent;
    if (el.parentElement) {
        gParent = el.parentElement.parentElement
    }
    if (el.parentNode) {
        gParent = el.parentNode.parentNode
    }
    var els = gParent.getElementsByClassName('marked');
    var elx;
    var value;

    var arr = new Array();
    for (i = 0; i < els.length; i++) {
        if (els.item) {
            if (els.item(i).nodeType == 1) {
                arr.push(els.item(i));
            }
        } else {
            if (!isNaN(i))
                arr.push(els[i])
        }

    }
    for (var i = 0,len = arr.length; value = arr[i],i < len; i++) {
        value.removeClassName('marked');
    }
};


changeImage = function(id, el) {
    document.getElementById(id).src = el.src;
    clearMarked(el);
    if (el.parentElement)
        el.parentElement.addClassName('marked');
    if (el.parentNode)
        el.parentNode.addClassName('marked');
};
changeStepClass = function(el) {
    var els = document.getElementsByClassName('stepSelected');
    for (var i in els) {
        if (!isNaN(i)) {
            var elx = els[i];
            elx.removeClassName('stepSelected');
            elx.addClassName('step');
        }
    }
    el.removeClassName('step');
    el.addClassName('stepSelected');
};


var window_factory = function(container, options) {
    var window_header = new Element('div', {
                className: 'window_header'
            });
    var window_title = new Element('div', {
                className: 'window_title'
            });
    window_title.update((options.title && options.title != "")?options.title:'Detalhes do produto');
    var window_close = new Element('div', {
                className: 'window_close'
            });
    var window_contents = new Element('div', {
                className: 'window_contents'
            });
    var w = new Control.Window(container, Object.extend({
                className: 'window',
                closeOnClick: window_close,
                draggable: window_header,
                insertRemoteContentAt: window_contents,
                afterOpen: function() {
                    window_title.update(container.readAttribute('title'))
                }
            }, options || {}));
    w.container.insert(window_header);
    window_header.insert(window_title);
    window_header.insert(window_close);
    w.container.insert(window_contents);
    return w;
};
