/* =========================================================

// jquery.homenews.js

// Date: 2009-10-02
// Author: Spiros Martzoukos
// Mail: spyros@lighthouse.gr

*
*  <ul id="news"> 
*      <li>content 1</li>
*      <li>content 2</li>
*      <li>content 3</li>
*  </ul>
*  
*  $('#news').homenews(); 
*

// ========================================================= */


(function($) {

    var beingShown = false;
    var count = 1;

    $.fn.homenews = function(options) {
        //for each element that turns into homenews...
        return this.each(function() {
            $.homenews(this, options);
        });
    };

    $.homenews = function(container, options) {
        if (beingShown) return;

        //configuration first
        var settings = {
            "animationtype": "fade",
            "speed": "normal",
            "timeout": 3000,
            "pagingopacity": 0.7,
            "prevstring": "<",
            "nextstring": ">",
            "showall": true,
            "pagingstretch": 3
        };
        if (options)
            $.extend(settings, options);

        //initiate
        var container = $(container);

        //set position:relative for paging menu to sit absolutely in it
        container.css("position", "relative");

        //hide all/show first
        container.children().hide().eq(0).show();

        //wrap all children elements in an inner container and assign them numbers
        container.children().wrapAll("<div class='homenews-container'></div>");
        var datacontainer = container.children(".homenews-container");
        datacontainer.children().each(function(i) {
            $(this).data("number", i + 1);
        });

        //create paging
        var count = datacontainer.children().size();
        var pagingHTML = "";
        for (i = 1; i <= count; i++) {
            pagingHTML += "<a href='#" + i + "'>" + i + "</a>";
            if (i < count) pagingHTML += " | ";
        }
        pagingHTML = "<a href='#1' class='prev'>" + settings.prevstring + "</a>" + pagingHTML + "<a href='#2' class='next'>" + settings.nextstring + "</a>";
        container.append("<div class='homenews-paging'>" + pagingHTML + "</div>");
        var pagingcontainer = container.children(".homenews-paging");

        //set active to 1
        datacontainer.data("active", 1);
        datacontainer.data("count", count);

        //change to the link's item onclick
        pagingcontainer.children("a").bind("click", function() {
            $.homenews.changeItem($(this), datacontainer, pagingcontainer, settings);
            return false;
        });

        //initiate automatic
        $.homenews.loop(datacontainer, pagingcontainer, settings);
    };

    $.homenews.changeItem = function(link, datacontainer, pagingcontainer, settings) {
        var targetLink = link.attr("href").split("#")[1];
        if ((targetLink != undefined) && (!beingShown)) {
            beingShown = true;
            var activeItem = datacontainer.data("active");
            var count = datacontainer.data("count");
            var targetItem;
            datacontainer.children().each(function() {
                if ($(this).data("number") == targetLink)
                    targetItem = $(this); //this is the item to be shown
            });
            //animation
            switch (settings.animationtype) {
                //fade effect            
                case "fade":
                    datacontainer.children().eq(activeItem - 1).fadeOut(settings.speed);
                    targetItem.fadeIn(settings.speed, function() {
                        beingShown = false;
                    });
                    break;
                //slide effect                                              
                case "slide":
                    //todo
                    break;
            }
            //change paging (if count>1)
            targetLink = parseInt(targetLink);
            if (count > 1) {
                //next and previous links
                if (targetLink <= 1) {
                    pagingcontainer.children(".prev").fadeOut(500);
                    pagingcontainer.children(".next").attr("href", "#2").fadeIn(500);
                } else if (targetLink >= count) {
                    pagingcontainer.children(".prev").attr("href", "#" + (count - 1)).fadeIn(500);
                    pagingcontainer.children(".next").fadeOut(500);
                } else {
                    pagingcontainer.children(".prev").attr("href", "#" + (targetLink - 1)).fadeIn(500);
                    pagingcontainer.children(".next").attr("href", "#" + (targetLink + 1)).fadeIn(500);
                }
                //enable inactive link and bind the changeitem function to it's click event
                pagingcontainer.children("a:not(.prev,.next)").each(function() {
                    $(this).attr("href", "#" + $(this).html()).unbind("click", function() {
                        $(this).bind("click", function() {
                            $.homenews.changeItem($(this), datacontainer, pagingcontainer, settings);
                            return false;
                        });
                    });
                });
                //remove href in active link
                pagingcontainer.children("a[!href]").eq(targetLink).removeAttr("href");
                //set active
                datacontainer.data("active", targetLink);
            } else {
                pagingcontainer.hide();
            }
        }
    };

    $.homenews.loop = function(datacontainer, pagingcontainer, settings) {
        var link = pagingcontainer.children("a").eq(count);
        var total = pagingcontainer.children("a:not(.prev, .next)").length;
        $.homenews.changeItem(link, datacontainer, pagingcontainer, settings); //move to next...
        count++;
        if (count > total) count = 1
        setTimeout(function() {
            $.homenews.loop(datacontainer, pagingcontainer, settings)
        }, settings.timeout);
    };

})(jQuery);
