/*
* jQuery Plugin: peepShow
* http://www.roydukkey.com/
*
* Copyright (c) 2009 Rory Dueck
*
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* Date: 2009-08-28 (Fri, 27 Aug 2009)
* Version: 0.0.3
*
* NOTE: Please report any improvements to roydukkey-at-gmail-dot-com.
*       There are still many improvements that can me made to this
*       script. Thanks to all in the open community.
*
*       Currently only work for one instance
*
*/
(function($)
{
    // Do CAST all VARS

    $.fn.peepShow = function(s)
    {
        var o = {
            width: 0,
            height: 0,
            order: "numeric",
            orderBy: "",
            fadeSpeed: 2000,
            delay: 5000,
            controls: 0,
            addNext: false,
            addPlay: false,
            addStop: false,
            addPrev: false
        }

        $.extend(o, s);

        // Set Width and Height
        $(this).css({
            width: o.width,
            height: o.height,
            position: "relative",
            overflow: "hidden"
        });

        return this.each(function()
        {
            var ps, s, uB, aS = 0, i, g;
            ps = $.fn.peepShow;
            uB = (s = (g = $(this)).find(".slide")).length;

            // Add Class
            this.className += (this.className ? " " : "") + "peepShow pShw" + ps.luid;

            // Create Index of Slides
            for (i = 0; i < uB; i++) ps.index[ps.luid][0].push(i);

            // Set Order of Slides in Index
            switch (o.order)
            {
                // Radomize  
                case "random":
                    ps.index[ps.luid][0].sort(function() { return 0.5 - Math.random() }); break;
                // Specific Order  
                case "specific":
                    for (i in o.orderBy = o.orderBy.replace(" ", "").split(",")) ps.index[ps.luid][0][i] = o.orderBy[i] - 1; break;
            }

            // Add CSS to Slides
            s.css({
                position: "absolute",
                top: 0,
                left: 0
            })
            // Show First Slide
		.eq(ps.index[ps.luid][0][aS++]).show();

            if (s.length > 1)
            {
                // Set all other Index values
                ps.index[ps.luid][1].push(
			        o.fadeSpeed, 																																		// 0
			        o.delay, 																																				// 1
			        o.controls > uB ? uB : o.controls, 																								// 2
			        o.addNext, 																																			// 3
			        o.addPlay, 																																			// 4
			        o.addStop, 																																			// 5
			        o.addPrev, 																																			// 6
			        uB, 																																							// 7
			        setTimeout("$.fn.peepShow.step(" + ps.luid + "," + aS + "," + (aS + 1) + ")", o.delay)					// 8
		        );
            }

            // Create Button Actions
            if (
			ps.index[ps.luid][1][2] > 0 ||
			ps.index[ps.luid][1][3] ||
			ps.index[ps.luid][1][4] ||
			ps.index[ps.luid][1][5] ||
			ps.index[ps.luid][1][6]
		)
            {
                g.append('<div class="controls nav"><ul></ul><span class="bkgd"><img src="/images/ui/sldshw-controls-bg.png" border="0" /></span><div class="clr"></div></div>');
                ps.updateBtn(ps.luid, aS);

                /*$(o.btnPrev+","+o.btnPlay+","+o.btnStop+","+o.btnNext).hover(function(){
                $(this).addClass("hover");
                },function(){
                $(this).removeClass("hover");
                });*/
            }

            // Increment for next peepShow
            ps.luid++;
        });

    }

    $.extend($.fn.peepShow, {
        luid: 0,
        index: [[[], [], []], [[], [], []]], // [ luid ][ arrayIndex | timers ][ position ]
        step: function(i, a, t)
        {
            var ps = $.fn.peepShow, s = $(".pShw" + i + " .slide");
            clearTimeout(ps.index[i][1][8]);

            s.eq(ps.index[i][0][a - 1]).fadeOut(ps.index[i][1][0]);
            s.eq(ps.index[i][0][t - 1]).fadeIn(ps.index[i][1][0]);

            ps.updateBtn(i, t)
            ps.index[i][1][8] = setTimeout("$.fn.peepShow.step(" + i + "," + t + "," + (t == ps.index[i][1][7] ? 1 : t + 1) + ")", ps.index[i][1][1]);
        },
        stop: function(i)
        {
            clearTimeout($.fn.peepShow.index[i][1][8])
        },
        updateBtn: function(i, a)
        {
            var ps = $.fn.peepShow.index, o = "<li><span>More Projects:</span></li>", x, u;

            // Prev
            if (ps[i][1][6]) o += '<li class="btnPrev"><a href="javascript:$.fn.peepShow.step(' + i + ',' + a + ',' + (a - 1 == 0 ? ps[i][1][7] : a - 1) + ')">&laquo;</a></li>';
            // Play
            if (ps[i][1][4]) o += '<li class="btnPlay"><a href="javascript:$.fn.peepShow.step(' + i + ',' + a + ',' + (a == ps[i][1][7] ? 1 : a + 1) + ')">&#9658;</a></li>';
            // Stop
            if (ps[i][1][5]) o += '<li class="btnNext"><a href="javascript:$.fn.peepShow.stop(' + i + ')">&#9632;</a></li>';
            // Numeric
            if (ps[i][1][2] > 0)
            {

                if (a - Math.round(ps[i][1][2] / 2) < 0)
                {
                    x = 0;
                    u = ps[i][1][2];
                } else
                {
                    if (a + Math.round(ps[i][1][2] / 2) > ps[i][1][7])
                    {
                        x = ps[i][1][7] - ps[i][1][2];
                        u = ps[i][1][7];
                    } else
                    {
                        x = a - Math.round(ps[i][1][2] / 2);
                        u = a + Math.round(ps[i][1][2] / 2) + (Math.round(ps[i][1][2] / 2) != ps[i][1][2] / 2 ? -1 : -0);
                    }
                }
                for (++x; x <= u; x++)
                {
                    o += '<li';
                    if (x == a)
                    {
                        o += ' class="current"><a>' + x + '</a></li>';
                    } else
                    {
                        o += '><a href="javascript:$.fn.peepShow.step(' + i + ',' + a + ',' + x + ')">' + x + '</a></li>';
                    }
                }

            }
            // Next
            if (ps[i][1][3]) o += '<li class="btnNext"><a href="javascript:$.fn.peepShow.step(' + i + ',' + a + ',' + (a == ps[i][1][7] ? 1 : a + 1) + ')">&raquo;</a></li>';

            $(".pShw" + i + " > .controls > ul").html(o);
        }
    });

})(jQuery);
