/**
 * @category   Camao
 * @package    Camao_Mask
 * @copyright  Copyright (c) 2010 CAMAO AG
 * @autor      Markus Zillig <markus.zillig@camao.de>
 */

function Camao_Mask()
{
    /**
     * @var object;
     */
    var infos = {
        'targetTag'   : 'a',
        'targetClass' : '',
        'targetSrc'   : 'href',
        'mask'        : ''
    }

    /**
     * __construct()
     * 
     * replace all masks from targets and store it as attributes on the DOMElement
     * creating mouseover/mouseout events
     * 
     * @params object config
     * @params string config.targetTag    Targets HMTL Tag
     * @params string config.targetSrc    Targets source attribute
     * @params string config.mask         Masking part of the target
     * 'optional'
     * @params string config.targetClass  Targets class (not required)
     * @return void;
     */
    this.__construct = function(config)
    {
        // config mask
        if (config) {
            infos.targetClass = typeof config.targetClass !== 'undefined' ? config.targetClass : infos.targetClass;
            infos.targetTag   = typeof config.targetTag   !== 'undefined' ? config.targetTag   : infos.targetTag;
            infos.targetSrc   = typeof config.targetSrc   !== 'undefined' ? config.targetSrc   : infos.targetSrc;
            infos.mask        = typeof config.mask        !== 'undefined' ? config.mask        : infos.mask;
        }

        // create Mask events
        this.createEvents();
    }

    /**
     * Create events and replace masked links 
     * 
     * @params void
     * @return void;
     */
    this.createEvents = function()
    {
        // seek targets
        var targets = $$(infos.targetTag+ (infos.targetClass ? '.'+infos.targetClass : ''));

        targets.each(function(target) {
            target.observe('mouseover', this.mouseoverEvent.bind(this, target));
            target.observe('mouseout', this.mouseoutEvent.bind(this, target));
            target.__src       = target.getAttribute(infos.targetSrc)
            target.setAttribute(infos.targetSrc, infos.mask)

        }.bind(this));
    }

    /**
     * Create mouseover event
     * add mask to target 
     * 
     * @params object target DOMElement
     * @return void;
     */
    this.mouseoverEvent = function(target)
    {
        // replace link with mask
        if (target.__src) {
            target.setAttribute(infos.targetSrc, target.__src);
        }
    }

    /**
     * Create mouseout event
     * remove mask from target 
     * 
     * @params object target DOMElement
     * @return void;
     */
    this.mouseoutEvent = function(target)
    {
        // src without mask
        target.setAttribute(infos.targetSrc, infos.mask);
    }
}
