//time formating
var monthMapping = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
var dayMapping = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
function formatData(date) {
    return (undefined == monthMapping[date.getMonth()]) ? '' : dayMapping[date.getDay()]+' '+monthMapping[date.getMonth()] +' '+ date.getDate()+' '+date.getFullYear();
}

//feedUrl: the url of feed
//reader_link: url to link to page-blog in reader page
//container: could be element-Id or element
//entryNum: 顯示幾則
function YusDynamicFeedControl(feedUrl, reader_link, container, autoplay, entryNum, showEntryList, isNoImage) {
    this.feedUrl = feedUrl;     //feed url string
    this.reader_link = reader_link; //reader link for this feed
    this.container = null;      //id or element
    this.feedObj = null;        // the feed contain object
    this.currIndex = 0;         //目前focus的index
    this.displayTimer = null;   //timer handler
    this.isAutoplay = autoplay; //是否自動撥放，true/false
    this.displayTime = 6000;    //多久換下一篇
    this.showEntryList = showEntryList; //是否顯示文章列表
    this.isNoImage = isNoImage //是否顯示圖示
    this.entryNum = entryNum; //一次顯示幾則
    this.currContentEl = document.createElement('div'); //目前顯示文章的container
    this.itemTitleDiv = null; //目前顯示文章的Title
    this.dateDiv = null; //目前顯示文章的日期作者
    this.itemArr = new Array(); //

    if (typeof container == 'string') {
        this.container = document.getElementById(container);
    } else {
        this.container = container;
    }

    //get feed's articals
    var feed = new google.feeds.Feed(feedUrl);
    feed.setResultFormat(google.feeds.Feed.JSON_FORMAT);
    feed.includeHistoricalEntries();
    feed.setNumEntries(entryNum);
    
    //for object scope issue
    var yusCtl = this;
    feed.load(function(result) {
        yusCtl.start(result);
    });
}

//start to show feed data
YusDynamicFeedControl.prototype.start = function(result) {
    var yusCtl = this;
    if (!result.error) {
        this.feedObj = result.feed;
 
        //blog title
        var titleEl = this.container.appendChild(document.createElement('div'));
        //titleEl.appendChild(this.createLink_(this.reader_link, this.feedObj.title));
        this.createLink_(titleEl, this.reader_link, this.feedObj.title);
        $(titleEl).attr('class', 'txtmode_blog_title');

        //current focus contain
        this.container.appendChild(this.currContentEl);
        $(this.currContentEl).attr('class','txtmode_blog_txt');

        var titleImgDiv = this.currContentEl.appendChild(document.createElement('div'));
        $(titleImgDiv).attr('class','blog_titleImgDiv');
        
        //Blog縮圖
        if (this.isNoImage) {
            //不顯示圖式
            $(this.currContentEl).css('height','119px');
            $(titleImgDiv).css('height','40px');
        } else {
            //顯示圖示
            var imgDiv = titleImgDiv.appendChild(document.createElement('div'));
            var imgEl = imgDiv.appendChild(document.createElement('img'));
            var imgUrl = './getpic.php?FEED='+this.feedObj.link+'&SCREEN_PATH='+this.feedObj.entries[0].link;
            $(imgDiv).css('position', 'absolute');
            $(imgDiv).css('z-index', '1');
            $(imgDiv).css('width', '100%');
            $(imgEl).attr('class','txtmode_blog_img');
            $(imgEl).attr('src', imgUrl);
        }
    
        //文章標題
        this.itemTitleDiv = titleImgDiv.appendChild(document.createElement('div'));
        $(this.itemTitleDiv).attr('class','blog_itemTitleDiv');
        
        //日期作者
        this.dateDiv = this.currContentEl.appendChild(document.createElement('div'));
        $(this.dateDiv).attr('class','blog_dateDiv');
        
        //內容摘要
        this.itemDescDiv = this.currContentEl.appendChild(document.createElement('div'));
        $(this.itemDescDiv).attr('class','blog_itemDescDiv');

        var clearEl = this.container.appendChild(document.createElement('div'));
        $(clearEl).css('clear','both');
        
        //entry list
        var itemsDivEl = this.container.appendChild(document.createElement('div'));
        $(itemsDivEl).css('overflow','hidden');
        if (this.showEntryList) {
            for (var i=0; i<this.feedObj.entries.length; i++) {
                var item = this.feedObj.entries[i];
                var itemsEl = itemsDivEl.appendChild(document.createElement('div')); //每則文章Dom元件
                var itemShareEl = itemsEl.appendChild(document.createElement('img')); //推文按鈕
                $(itemShareEl).attr('src', 'http://b.static.ak.fbcdn.net/rsrc.php/zAB5S/hash/4273uaqa.gif');
                $(itemShareEl).data('item', item);
                $(itemShareEl).click(function(){
                    var item = $(this).data('item');
                    var aLink = yusCtl.reader_link+'_'+item.link
                    sendToFriendWall(null, '', item.title, aLink, item.contentSnippet, null, null);
                });
                //itemsEl.appendChild(this.createLink_(this.reader_link+'_'+item.link, item.title));
                this.createLink_(itemsEl, this.reader_link+'_'+item.link, item.title); //標題及開啟連結
                itemsEl.index = i;
                $(itemsEl).mouseover(function() {
                    yusCtl.autoplay(false);
                    yusCtl.currIndex = this.index;
                    yusCtl.updateCurrentItem();
                })
                $(itemsEl).mouseout(function() {
                    yusCtl.autoplay(true);
                })
                this.itemArr[i] = itemsEl;
            }
        }
        $(this.container).css('margin-bottom','5px');
        
        //default current content
        this.updateCurrentItem();
        this.autoplay(true);
    }
}

//a helper method
YusDynamicFeedControl.prototype.createLink_ = function(containerEl, href, text, opt_target) {
    var link = containerEl.appendChild(document.createElement('a'));
    //link.href = href;
    link.href = '#';
    $(link).click( function(){
         window.open(href);
    });
    link.innerHTML = text;
    if (opt_target) {
        link.target = opt_target;
    }  
    return link;
};

//update focus item to current item
YusDynamicFeedControl.prototype.updateCurrentItem = function() {
    var currEntry = this.feedObj.entries[this.currIndex];
    var currItem = this.itemArr[this.currIndex];

    //改變List的CSS表現
    for (var i=0; i<this.feedObj.entries.length; i++) {
        var e = this.itemArr[i];
        if (i == this.currIndex) {
            $(e).attr('class','item_title_on');
            
        } else {
            $(e).attr('class','item_title');
        }
    }

    //文章標題
    $(this.itemTitleDiv).html('<h2><a href="'+this.reader_link+'_'+currEntry.link+'">'+currEntry.title+'</a><h2>');

    //日期作者
    var txt = '<div style="color:#777;overflow:hidden">'+formatData(new Date(currEntry.publishedDate))+' - '+currEntry.author+'</div>';
    $(this.dateDiv).html(txt);

    //內容簡介
    $(this.itemDescDiv).html('&nbsp;&nbsp;&nbsp;&nbsp;'+currEntry.contentSnippet);
}
/*
//文繞圖
//update focus item to current item
YusDynamicFeedControl.prototype.updateCurrentItem = function() {
    var currEntry = this.feedObj.entries[this.currIndex];
    var currItem = this.itemArr[this.currIndex];
    
    for (var i=0; i<this.feedObj.entries.length; i++) {
        var e = this.itemArr[i];
        if (i == this.currIndex) {
            $(e).attr('class','item_title_on');
            
        } else {
            $(e).attr('class','item_title');
        }
    }
    var txt = '<div><a href="'+this.reader_link+'_'+currEntry.link+'">'+currEntry.title+'</a></div>';
    txt += '<div style="color:#777">author:'+currEntry.author+'-'+formatData(new Date(currEntry.publishedDate))+'</div>';
    txt += '<div style="color:#111">'+currEntry.contentSnippet+'</div>';
    
    $(this.currContentEl).html('');
    
    var imgEl = this.currContentEl.appendChild(document.createElement('img'));
    var imgUrl = './getpic.php?FEED='+this.feedObj.link+'&SCREEN_PATH='+this.feedObj.entries[0].link;
    $(imgEl).attr('class','txtmode_blog_img');
    $(imgEl).attr('src', imgUrl);
    
    $(this.currContentEl).attr('class','txtmode_blog_txt');
    $(this.currContentEl).append(txt);
}
*/
    
//play to next one
YusDynamicFeedControl.prototype.next = function() {
    var yusCtl = this;
    this.currIndex++;
    if (this.currIndex >= this.feedObj.entries.length) {
        this.currIndex = 0;
    }
    yusCtl.updateCurrentItem();
}

//start or stop autoplay
YusDynamicFeedControl.prototype.autoplay = function(isAuto) {
    var yusCtl = this;
    if (this.displayTimer) {
        clearInterval(this.displayTimer);
        this.displayTimer = null;
    }
    
    if (isAuto && this.isAutoplay) {
        this.displayTimer = setInterval(function() {
            yusCtl.next();
        }, this.displayTime);
    }
}
