var Tooltip = function(){
    /**
     * array-like object contains keys
     * that are ids of divs, values are objects
     * with keys title, description, created, updated
     */
    var oData = {}, //
    /**
     * HTML element representing the
     * actual Tooltip
     */
    elTT, //
    /**
     * Tooltip widget object
     */
    oTT, //
    /**
     * Parent HTML DOM element of
     * all the divs that will have tooltip
     *
     */
    rootDiv = $C('albthumbs')[0], //
    /**
     * Array of all div elements
     * which will have tooltip
     */
    aDivs, //
    /**
     * Array of DOM elements for which
     * the tooltip will activate
     */
    aEls, //
 that = this, //
    /**
     * Loop over all divs inside the albthumbs div,
     * extract data, remove title, description
     * This function is also private through closure
     *
     */
    parseDivs = function(){
        $L('40 parseDivs', 'warn');
        if (!aDivs || !aDivs.length || aDivs.length === 0) {
            $L('41 going to parseDivs');
            aDivs = $C('athumb', 'div', rootDiv);
            $L('43 aDivs: ' + $LANG.dump(aDivs));
        }
        
        for (var i = 0; i < aDivs.length; i += 1) {
            var elDiv = aDivs[i];
			var aAlbdate = $C('albdate', 'div', elDiv);
			var elCreated = (aAlbdate && aAlbdate[0]) ? aAlbdate[0] : null;
			var created_time = (elCreated) ? elCreated.innerHTML : '';
			var updated_time = (elCreated && elCreated.title && elCreated !== '') ? elCreated.title : '';
            var elImg = elDiv.getElementsByTagName('img')[0];
            var o = {
                id: elDiv.id,
                title: elImg.title,
                description: elImg.alt,
				created: created_time,
				updated: updated_time
            }
            
            $L('56');
            oData[i] = o;
            
            elImg.title = '';
            elImg.alt = '';
            
        }
        
        $L('64 oData length ' + oData.length);
    }, //
    /**
     * Find the title, description
     * and other data for a given album id,
     * format data into html string
     * and return it
     * This is private function through closure
     * @param {Object} id
     */
    getText = function(id){
        $L('looking for item with id ' + id);
		var s = '', album, title, description, created, updated;
        for (var item in oData) {
            $L('66 item: ' + $LANG.dump(item), 'warn');
            if (oData[item].id == id) {
				album = oData[item];
				
				s += '<b>' + album.title + '</b>';
				s += ('' === album.description) ? '' : '<br><br>' + album.description;
				s += '<br><br>';
				s += (album.created !== '') ? album.created : '';				
				s += (album.updated !== '') ? ' - ' + album.updated : '';
				
                return s;
                break;
            }
        }
    }, //
    /**
     * Make initial tooltip widget
     * Will only make this object (widget)
     * once - on initial page load
     * 
     * @access private
     */
    makeObjectTooltip = function(){
        if (!oTT) {
            oTT = new $W.Tooltip("ttB");
            oTT.contextTriggerEvent.subscribe(function(type, args){
                $L('52 type: ' + type);
                var context = args[0];
                $L('54 context: ' + context);
                this.cfg.setProperty("text", getText(context.id));
            });
            
            
        }
        
        $L('52 make oTT');
        $L('53 id of oTT element: ' + oTT.element.id);
    }
    /**
     * Calls the tooltip widget's init() method,
     * passing array of div elements
     * to which the tooltip will be active
     * 
     * @access private
     */
    initObjectTT = function(){
        $L('58 going to init objectTT');
        if (oTT && oTT.init) {
            $L('60 objectTT already exists, going to init it');
            oTT.init('ttB', {
                context: aDivs,
                effect: {
                    effect: $W.ContainerEffect.FADE,
                    duration: 0.20
                }
            })
        }
    }
    /**
     * Public method of this object
     * will initially parse divs,
     * make tooltip object
     * and init tooltip object
     * 
     * @access public
     */
    this.init = function(){
        $L('57 starting init');
        parseDivs();
        makeObjectTooltip();
        initObjectTT();
    };
    /**
     * Getter for the private property
     * 
     * @access public
     *
     * @return object of type tooltip (Yahoo tooltip widget)
     */
    this.getObjectTT = function(){
        return that.oTT;
    }
    
    /**
     * This is for the future in case we get next page via Ajax, then we can
     * just set the received json as oData, provided it
     * will have data in format we expect.
     * 
     * @access public
     *
     * @param {Object} o
     */
    this.setObjectData = function(o){
        that.oData = o;
    }
    
    
}

$E.onDOMReady(function(){

    var oMyTooltip = new Tooltip();
    oMyTooltip.init();
});
