(function(TemporaHelper, $, undefined) {
    var PopupWinDefaults = {
        width: "800",
        height: "600"
    };
    
    $.fn.PopupWin = function (o) {
        return this.each(function () {
            new $.PopupWin(this, o);
        });
    };
    
    $.PopupWin = function (el, o) {
	if($(el).attr("expand")!=null)
        {
            $(el).attr("title", "Click to show image in a popup window");
            $(el).css({
                "cursor": "pointer" 
            });
            $(el).click(function(e, ev){
                this.o = $.extend({}, PopupWinDefaults, o || {});
                var bigUrl = ($(el).attr("expandurl")==null) ? null : $(el).attr("expandurl");
                var src = (bigUrl==null) ? $(el).attr('src') : bigUrl;
                window.open(src,'Show','width=' + this.o.width + 'px,height=' + this.o.height + 'px');
                e.preventDefault();
                return false;
            });
        }
    };
    
    var overlayCount=0;
    
    // Defaults for text overlay.
    var defaults = {
        fontColour: "black",        // Font colour.
        fontSize: "1.2em",
        img_visible: true,          // Show/hide the image.
        txt_visible: true,          // Show/hide the text overlay.
        overrideLeft: -1,           // Override left position.
        overrideTop: -1,            // Override right position.
        delayOverall: 5000,         // Overall delay.
        fadeDelay: 4000             // Delay before fade.
    };
    
    // Overlay functions.
    $.fn.Overlay = function (o) {
        return this.each(function () {
            new $.Overlay(this, o);
        });
    };
    
    $.Overlay = function (el, o) {
        overlayCount++;
                
        var _self = this;
        this.o = $.extend({}, defaults, o || {});
    
        _self.$div = $(el);
        
        if (!this.o.img_visible)
            _self.$div.hide();
        if (this.o.txt_visible) {
            var w = _self.$div.width();
            var posTop = (this.o.overrideTop>0) ? this.o.overrideTop : 0;
            var posLeft = (this.o.overrideLeft>0) ? this.o.overrideLeft : 40;
            $(el,"ul").prepend('<div id="txt-overlay-' + overlayCount + '"></div>');
            $('#txt-overlay-' + overlayCount).css({
                'width': (w-posLeft) + 'px',
                'position': 'relative',
                'z-index': 0,
                'top': posTop + 'px',
                'left': posLeft + 'px',
                'font-size': this.o.fontSize,
                'color': this.o.fontColour,
                'height': '0px'
            });
            
            _self.$div.children('ul').hide();
        
            // Get list
            var lst = [];
            var i = 0;
            _self.$div.children("ul").children().each(function (index) {
                lst[i] = $(this).html();
                i++;
            });
        
            var x = 0;
            var fd = this.o.fadeDelay;
            var elmt = $("#txt-overlay-" + overlayCount);
            elmt.html(lst[x]);
            elmt.html(lst[x]).delay(fd).fadeOut(500);
            x++;
            setInterval(function() {
                elmt.html(lst[x]).fadeIn(250);
                elmt.html(lst[x]).delay(fd).fadeOut(500);
                x++;
                if (x == lst.length) { x = 0; }
            }, _self.o.delayOverall);
        }
    }
    
    var RssDefaults = {
        url: '',
        maxItems: 2,
        onError: 'Temporary unavailable'
    };

    $.fn.RssReader = function (o) {
        return this.each(function () {
            new $.RssReader(this, o);
        });
    };

    $.RssReader = function (el, o) {
        this.o = $.extend({}, RssDefaults, o || {});

        $.get(this.o.url, function (data) {
            this.o = $.extend({}, RssDefaults, o || {});
            var $xml = $(data);
            var rss = "<table id='RssTable'>";
            var maxCount = this.o.maxItems;
            var index = 0;
            $xml.find("item").each(function () {
                var $this = $(this),
                    item = {
                        title: $this.find("title").text(),
                        link: $this.find("link").text(),
                        description: $this.find("description").text(),
                        pubDate: $this.find("pubDate").text(),
                        author: $this.find("author").text()
                    }

                // Show top feeds.
                $.each($this, function () {
                    if (index == maxCount)
                        return false;
                    rss = rss + "<tr><td><a href='" + item.link + "' title='" + item.title + "'>"  + item.title + "</a></td></tr>";
                    index++;
                });
            });
            $(el).html(rss + "</table>");
        })
        .error(function () { $(el).html(this.o.onError); })
    };    
}(window.TemporaHelper = window.TemporaHelper || {}, jQuery));
