/**
 * @fileoverview sueddeutsche.de ORM JavaScript library.
 *
 * @author      Murat Purc m.purc@edelweiss72.de
 * @version     0.1
 */


/**
 * szOrm, used for namespacing
 *
 * @namespace  szOrm
 */
var szOrm = {};



/**
 * Simple storage container class to store data using keys.
 * 
 * Usage:<br />
 * <code>
 * register = new szOrm.Registry();<br />
 * register.set("browser", "msie");<br />
 * var text = register .get("browser");<br />
 * register.remove("browser");<br />
 * </code>
 * 
 * @class szOrm.Registry
 */
szOrm.Registry = Class.create({

    /**
     * Constructor
     */
    initialize: function() {

        /**
         * Arrray to hold the data
         * @type  array
         */
        this.container = new Array();
    
    },
    

    /**
     * Stores a value using key
     *
     * @param   {string}  key     Key of value to store
     * @param   {mixed}   value   The value
     * @return  {bool}    Allways true
     */
    set: function(key, value) {
        this.container[key] = value;
        return true;
    },
    

    /**
     * Returns the value by key
     *
     * @param   {string}  key       Key of value to get
     * @param   {mixed}   defValue  Default value to return, if no data exists in storage container
     * @param   {bool}    remove    Flag to remove the entry from storage container
     * @return  {mixed}   The value
     */
    get: function(key, defValue, remove) {
        var value = null;
        if (typeof(this.container[key]) !== "undefined") {
            value = this.container[key];
            if (typeof(remove) !== "undefined") {
                this.remove(key);
            }
        } else if (typeof(defValue) !== "undefined") {
            value = defValue;
        }
        return value;
    },
    

    /**
     * Remove the entry from storage container by key.
     *
     * @param   {string}  key  Key of value to remove.
     * @return  {bool}    Allways true
     */
    remove: function(key) {
        return this.set(key, null);
    }
    
});

/**
 * Instance of szOrm.Registry class
 * @type  szOrm.Registry
 */
var szOrmRegistry = new szOrm.Registry();


/**
 * Base class used to fold elements.
 *
 * @class szOrm.BaseFold
 */
szOrm.BaseFold = Class.create({

    /**
     * Constructor
     * 
     * @param  {string}  id  Id of initial active element, optional
     */
    initialize: function(id) {

        /**
         * Id of actual item id
         * @type  string
         */
        this.actId = (typeof(id) == "undefined") ? null : id;
        
        /**
         * Object list to cache items state
         * @type  array
         */
        this.aItems = new Object();
    
    },
    
    /**
     * Toggles the status of actual element by replacing the classname.
     * 
     * @param  {string}  find     Classname to find
     * @param  {string}  replace  New classname to set
     */
    toggleStatus: function(find, replace) {
        if (Element.hasClassName(this.actId, find)) {
            Element.removeClassName(this.actId, find);
            Element.addClassName(this.actId, replace);
        }
    }

});



/**
 * Class to fold offer navigation. Extends szOrm.BaseFold.
 * 
 * @class   szOrm.OfferNaviFold
 * @extends szOrm.BaseFold
 */
szOrm.OfferNaviFold = Class.create(szOrm.BaseFold, {

    /**
     * Toggles the state of fold item
     * 
     * @param  {string}  id  Id of item
     */
    toggle: function(id) {
        if (!$(id)) {
            return;
        }
        
        if (this.actId != null && this.actId != id) {
            // close previous opened item
            this.toggleStatus('opened', 'closed');
        }
        
        // handle new item which ist to open
        this.actId = id;
        this.toggleStatus('closed', 'opened');
    }

});


// #################################################################################################
// Methods

/**
 * Function to initialize the offer navigation folding. Is to call on onLoad-Event.
 * Works with 2 modes:
 * 1. Mode: Loops through listitems and try top catch an allready opened item
 * 2. Mode: Random opening of an item
 *
 * @param   {bool}    random  Flag to random opening of an navigation item. Setting of this flag will be 
 *                            overwrite actual "opened" classname in listitems
 * @param   {string}  naviId  Identifier (id-attribute) of navigation element
 * @return  {szOrm.OfferNaviFold}  New instance of an szOrm.OfferNaviFold class
 */
szOrm.initOffernavi = function(random, naviId) {

    var activeItem = null;
    
    var list = $(naviId).select('div[title="offernaviitem"]');
    if (list) {

        if (random) {
            random = Math.round(Math.random() * (list.length-1));
        }
        
        for (var i=0; i<list.length; i++) {
            var s = list[i];
            
            if (random) {
                // handle random position mode
                if (random == i) {
                    activeItem = s.id;
                    Element.removeClassName(s.id, 'closed');
                    Element.addClassName(s.id, 'opened');
                } else if (Element.hasClassName(s, 'opened')) {
                    Element.removeClassName(s.id, 'opened');
                    Element.addClassName(s.id, 'closed');
                }
            } else if (Element.hasClassName(s, 'opened')) {
                // set id of actual opened item
                activeItem = s.id;
            }
        }
/*        
        list.each(function(s, pos) {
            if (szOrmRegistry.get('offernavi_randompos') !== null) {
                // handle random position mode
                if (szOrmRegistry.get('offernavi_randompos') == pos) {
                    szOrmRegistry.set('offernavi_activeitem', s.id);
                    Element.removeClassName(s.id, 'closed');
                    Element.addClassName(s.id, 'opened');
                } else if (Element.hasClassName(s, 'opened')) {
                    Element.removeClassName(s.id, 'opened');
                    Element.addClassName(s.id, 'closed');
                }
            } else if (Element.hasClassName(s, 'opened')) {
                // set id of actual opened item
                szOrmRegistry.set('offernavi_activeitem', s.id);
            }
        });
*/
        
    }

    return new szOrm.OfferNaviFold(activeItem);

/*    
    offerNavi = new szOrm.OfferNaviFold(
        szOrmRegistry.get('offernavi_activeitem')
    );
*/
    
}


// #################################################################################################
// Prototype related


/**
 * Extend prototype Element, if necessary.
 */
if (typeof(Element.replaceClassName) !== "function") {

    /**
     * Extend prototype Element, add method replaceClassName() to replace classnames.
     *
     * @extends Element
     */
    Element.addMethods({
        replaceClassName: function(element, className, replacement) {
            if (!(element = $(element))) return;
            return element.removeClassName(className).addClassName(replacement);
        }
    });

}


// #################################################################################################
// Firebug

/**
 * Firebug emulation, to prevent errors if firebug is not available
 * TODO: is to remove after finishing development
 */
if (!("console" in window) || !("firebug" in console)) {
(function(){
	window.console = {
		log:   function(){},
		debug: function(){},
		info:  function(){},
		warn:  function(){},
		error: function(){}
	}
})();
}