/*
	Smooth Feed
	Author: <Denon Studio>
	Published: 200905 :: Denon Studio via ThemeForest.com
*/

var SmoothFeed = new Class({
    
    defaultUrl   : "/php/get_feed.php",
    feedData     : null,
    container    : null,
    periodHandle : null,
    titles       : [],
    center       : null,
    leftArrow    : null,
    rightArrow   : null,
    INTERVAL     : 3000,   
    
    currentTitle   : null,
    currentIndex   : 0,
    stopIteration  : false,
    
    initialize : function (container, interval) {
        this.container = $(container);
        if (this.container) {
            this.center     = this.container.getElementById("center"     );
            this.leftArrow  = this.container.getElementById("left_arrow" );
            this.rightArrow = this.container.getElementById("right_arrow");
            
            this.rightArrow.addEvent("click", this.onRightArrowClick.bind(this));
            this.leftArrow .addEvent("click", this.onLeftArrowClick .bind(this));

            if ($defined(interval)) 
                this.INTERVAL = interval;
            
            this.downloadData();
        }
    },
    
    downloadData : function() {
        var request = new Request.JSON({url : this.defaultUrl, onSuccess : this.onDownloadSuccess.bind(this)});
        request.send();
    },
    
    onDownloadSuccess : function(responseJSON, responseText){
        if (responseJSON) {
            this.feedData = responseJSON;
            this.render();
            this.periodHandle = this.showNext.periodical(this.INTERVAL, this);
        }
    }, 

    generateElements : function() {
        var item;
        for (var i = 0; i < this.feedData.length; i++){
            item = this.feedData[i];
            this.titles.push(new Element("a",{"class":"smooth_feed_title"  ,"html": item["title"], "href" : item["link"], "target" : "_blank"}));
        }
    },

    onLeftArrowClick : function() {
        this.showNext(this.currentIndex - 1, true);
    },

    onRightArrowClick : function() {
        this.showNext(this.currentIndex + 1, true);
    },

    onMouseOver : function() {
        this.stopIteration = true;
        this.rightArrow.morph({"opacity":"0.9"});
        this.leftArrow .morph({"opacity":"0.9"});
    },
    
    onMouseOut : function() {
        this.stopIteration = false;
        this.rightArrow.morph({"opacity":"0.6"});
        this.leftArrow .morph({"opacity":"0.6"});
    },
    
    render : function() {
        this.container.addEvent("mouseover", this.onMouseOver.bind(this));
        this.container.addEvent("mouseout" , this.onMouseOut .bind(this));
        this.container.set     ("class"    ,"smooth_feed"               );
        
        this.generateElements();
        this.showNext(0);
    },

    showNext : function(currentIndex, force) {
        if (!force && this.stopIteration) 
            return

        if (this.currentTitle)
            this.center.removeChild(this.currentTitle);

        this.currentIndex = ($defined(currentIndex)) ? currentIndex : this.currentIndex + 1;
        if (this.currentIndex < 0) this.currentIndex = this.feedData.length - 1;
        if (this.currentIndex >= this.feedData.length) this.currentIndex = 0;
        
        this.currentTitle = this.titles [this.currentIndex];
        this.center.adopt(this.currentTitle);

        this.rightArrow.setStyles({"height": this.container.offsetHeight + "px", "opacity" : (this.stopIteration) ? "0.9" : "0.7"});
        this.leftArrow .setStyles({"height": this.container.offsetHeight + "px", "opacity" : (this.stopIteration) ? "0.9" : "0.7"});
    }
});
