// JavaScript Document
$(function() {

    /*
        1.) Form Field Value Swap
    */

    swapValues = [];
    $(".swap_value").each(function(i){
        swapValues[i] = $(this).val();
        $(this).focus(function(){
            if ($(this).val() == swapValues[i]) {
                $(this).val("");
            }
        }).blur(function(){
            if ($.trim($(this).val()) == "") {
                $(this).val(swapValues[i]);
            }
        });
    });

    /*
        2.) Characters Remaining Countdown
    */

    var countdown = {
        init: function() {
            countdown.remaining = countdown.max - $(countdown.obj).val().length;
            if (countdown.remaining > countdown.max) {
                $(countdown.obj).val($(countdown.obj).val().substring(0,countdown.max));
            }
            $(countdown.obj).siblings(".remaining").html(countdown.remaining + " characters remaining.");
        },
        max: null,
        remaining: null,
        obj: null
    };
    $(".countdown").each(function() {
        $(this).focus(function() {
            var c = $(this).attr("class");
            countdown.max = parseInt(c.match(/limit_[0-9]{1,}_/)[0].match(/[0-9]{1,}/)[0]);
            countdown.obj = this;
            iCount = setInterval(countdown.init,1000);
        }).blur(function() {
            countdown.init();
            clearInterval(iCount);
        });
    });

    /*
        3.) Simply Centered Informational DIV Popup
    */

    /*
        The viewport object is mainly for the cross-browser mess
        that is unfortunately necessary to get a DIV to center.

        There is also an officially supported plugin called "Dimensions"
        that can manage some of this on your own projects. Check it out at:

        http://jquery.com/plugins/project/dimensions
    */

    var viewport = {
        o: function() {
            if (self.innerHeight) {
    			this.pageYOffset = self.pageYOffset;
    			this.pageXOffset = self.pageXOffset;
    			this.innerHeight = self.innerHeight;
    			this.innerWidth = self.innerWidth;
    		} else if (document.documentElement && document.documentElement.clientHeight) {
    			this.pageYOffset = document.documentElement.scrollTop;
    			this.pageXOffset = document.documentElement.scrollLeft;
    			this.innerHeight = document.documentElement.clientHeight;
    			this.innerWidth = document.documentElement.clientWidth;
    		} else if (document.body) {
    			this.pageYOffset = document.body.scrollTop;
    			this.pageXOffset = document.body.scrollLeft;
    			this.innerHeight = document.body.clientHeight;
    			this.innerWidth = document.body.clientWidth;
    		}
    		return this;
        },
        init: function(el) {
            $(el).css("left",Math.round(viewport.o().innerWidth/2) + viewport.o().pageXOffset - Math.round($(el).width()/2));
            $(el).css("top",Math.round(viewport.o().innerHeight/2) + viewport.o().pageYOffset - Math.round($(el).height()/2));
        }
    };
    $(".simple_popup_info").each(function(){
        $(this).css("display","none").siblings(".simple_popup").click(function(){
            $(".simple_popup_div").remove();
            var strSimple = "<div class='simple_popup_div'><div class='simple_popup_inner'>";
            strSimple += "<p class='simple_close'>[ x ] <a href='#'>Close</a></p>";
            strSimple += $(this).siblings(".simple_popup_info").html();
            strSimple += "</div></div>";
            $("body").append(strSimple);
            viewport.init(".simple_popup_div");
            $(".simple_close").click(function(){
                $(".simple_popup_div").remove();
                return false;
            });
            return false;
        });
    });

    /*
        4.) Collapsing Table Columns
    */

    $(".collapse").click(function() {
        var o = this;
        var p = $(this);
        while (!$(p).is("table")) {
            p = $(p).parent();
        }
        $(".collapse",p).each(function(i) {
            if (this == o) {
                $("tr",p).find("td:eq(" + i + ")").css("display","none");
            }
        });
        $(this).parent().css("display","none");
        $(p).siblings(".reset_collapse").html("<p><a href='#' class='show_all_collapsed'>Show All</a></p>").find(".show_all_collapsed").click(function(){
            $("th,td",p).css("display","");
            $(this).parent().remove();
            return false;
        });
        return false;
    });

    /*
        5.) The RSS Feed Reader
    */

    $.ajax({
        type: "GET",
        url: "feed.xml",
        dataType: "xml",
        success: function(rss) {
            strRSS = "<h4>" + $("/rss/channel/title",rss).text() + "</h4>";
            $("/rss/channel/item/title:lt(5)",rss).each(function(i) {
                strRSS += "<p><strong><a href='";
                strRSS += $("/rss/channel/item/link:eq(" + i + ")",rss).text();
                strRSS += "'>";
                strRSS += $(this).text();
                strRSS += "</a></strong><br />";
                strRSS += ($("/rss/channel/item/description:eq(" + i + ")",rss).text()).substring(0,200) + "...</p>";
            });
            $("#feed_me").html(strRSS);
        }
    });

});
