﻿// Injects read more links to reveal the remainder of an article.
// TO IMPROVE:  
// 	- Calculate snippet height by injecting some text for 
// 	  a specified number of lines and store height.
var ReadMore = Class.create({
    initialize: function(elements, options) {
        this.options = Object.extend({
            snippetHeight: 100, 				// height, make dynamic if possible
            duration: 0.25, 					// duration of revealing effect
            useIcon: true,                  // this is if you want to use a graphic
            icon: '_ui/img/readmore.gif', 	// image source for anchor triggers.
            triggerClass: 'readmore', 	// anchor css class for triggers.
            readMoreText: "Read More",       // this is the text for the read more anchor
            insertLocation: "after",         // choices are "after", "top", "before", "bottom"
            useTrigger: true
        }, options || {});

        var boundClick = this.__lnkClick.bindAsEventListener(this);

        for (var i = 0, len = elements.size(); i < len; i++) {
            var ht = elements[i].getHeight();
            if (ht > this.options.snippetHeight) {
                var element = $(elements[i]);
                if (this.options.useTrigger == true) {
                    element._originalHeight = ht;
                    var readMore = new Element('a', {
                        'class': this.options.triggerClass,
                        'rel': element.identify(),
                        'href': '#readmore'
                    })

                    if (this.options.useIcon == true) {
                        readMore.insert(new Element('img', { 'src': this.options.icon, alt: this.options.readMoreText }));
                    } else {
                        readMore.update(this.options.readMoreText);
                    }


                    //  element.insert(readMore);
                    var opts = {};
                    opts[this.options.insertLocation] = readMore;
                    element.insert(opts);

                    readMore.observe('click', boundClick);
                }
                this.snipHeight(element);
            }
        }
    },
    __lnkClick: function(e) {
        e.stop();
        var lnk = (e.element().tagName !== "A") ? e.element().up("a") : e.element();
        //lnk.hide();
        new Effect.Fade(lnk, { duration: this.options.duration });

        // adjust height
        var target = $(lnk.readAttribute("rel"));
        this.unSnipHeight(target);
    },

    snipHeight: function(el) {
        el.setStyle({ height: this.options.snippetHeight + "px", overflow: 'hidden' });
    },

    unSnipHeight: function(el) {
        //el.setStyle({height: "auto", overflow: "visible"});
        new Effect.Morph(el, {
            duration: this.options.duration,
            style: {
                height: el._originalHeight + 'px'
            }
        });
    }

});

