﻿/*
*  Element extensions v1.01
*/

var $E = function(selector) { return document.getElement(selector); }

Element.Properties.rel = {
    get: function(){
		var rel = this.getProperty('rel') || "{}";
        return rel.test(/^{.*}$/) ? JSON.decode(rel) : rel;
    },
    set: function(value){
		var rel = this.getProperty('rel');
		return this.setProperty('rel', ($type(value) == 'object' ? JSON.encode(value) : value));
    } 
};

Element.implement({

    makeCollapsible: function(close) {
        if (!this.retrieve('safs:collapsible')) {
            this.getFirst().addEvent('click', function() {
                this.toggleClass('closed');
            } .bind(this));
            this.store('safs:collapsible', true);
        }
        return (close) ? this.addClass('closed') : this;
    },

    makeTabbed: function(show) {
        if (!this.retrieve('safs:tabs')) {
            var tabs = this.getElements('LI');
            var contents = this.getAllNext('.tabcontent').slice(0, tabs.length);
            tabs.each(function(tab, i) {
                tab.addEvent('click', function() {
                    tabs.removeClass('selected')[i].addClass('selected');
                    $$(contents).removeClass('selected')[i].addClass('selected');
                });
            });
            this.store('safs:tabs', true);
        }
        if ($defined(show)) tabs[show].fireEvent('click');
        return this;
    },

    setAsLoading: function() {
        var loadingElement = this.retrieve('safs:loading');
        if (!$defined(loadingElement)) {
            loadingElement = new Element('DIV');
            this.store('safs:loading', loadingElement);
        }
        loadingElement.setStyles(this.getCoordinates()).setStyles({
            position: 'absolute',
            opacity: 0.5
        }).addClass('loading').inject($(document.body));
        return this;
    },

    stopAsLoading: function(nohighlight) {
        var loadingElement = this.retrieve('safs:loading');
        if (loadingElement) {
            loadingElement.dispose();
            return ($defined(nohighlight)) ? this : this.set('tween', { duration: 'long' }).highlight('#FFC');
        }
    }

});


