// Copyright 2010 htmldrive.net Inc.
/**
 * @projectHomepage http://www.htmldrive.net/go/to/simple-images-scroller
 * @projectDescription Simple images scroller with jquery plugin
 * @author htmldrive.net
 * More script and css style : htmldrive.net
 * @version 1.0
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */
(function(a) {
    a.fn.scroller_roll = function(p) {
        var p = p || {};

        var cAf = p && p.continueAfter ? p.continueAfter : "100";
        var tAni = p && p.togglerAnimation ? p.togglerAnimation : null;
        var tID = p && p.togglerID ? p.togglerID : null;
        var st = p && p.step ? p.step : "1";
        var dir = p && p.direction ? p.direction : "forward";
        var ori = p && p.orientation ? p.orientation : "horizontal";
        var g = p && p.time_interval ? p.time_interval : "100";
        var h = p && p.title_show ? p.title_show : "enable";
        var i = p && p.window_background_color ? p.window_background_color : "white";
        var j = p && p.window_padding ? p.window_padding : "5";
        var k = p && p.border_size ? p.border_size : "1";
        var l = p && p.border_color ? p.border_color : "black";
        var m = p && p.images_width ? p.images_width : "45";
        var n = p && p.images_height ? p.images_height : "70";
        var o = p && p.title_size ? p.title_size : "12";
        var q = p && p.title_color ? p.title_color : "blue";
        var r = p && p.show_count ? p.show_count : "5";
        var i_m = p && p.images_margin ? p.images_margin : "3";
        j += "px";
        k += "px";
        m += "px";
        n += "px";
        o += "px";
        var s;
        var t = 0;
        var u;
        var v;
        var w;
        var x;
        var y = a(this);
        var z = y.children("ul").children("li").length;
        var A = Math.ceil(z / r);
        if (y.find("ul").length == 0 || a("li").length == 0) {
            alert("Require content");
            return null
        }
        y.children("ul").children("li").children("a").children("img").css("width", m).css("height", n);
        if (h == 'enable') {
            y.children("ul").children("li").children("a").each(function() {
                $(this).append('<br/>' + $(this).attr("title"))
            })
        }
        s_s_ul(y, j, k, l, i);

        if (ori == "horizontal") {
            m = parseInt(m);
            y.children("ul").children("li").css("width", m + "px");
            y.children("ul").children("li").css("margin-left", i_m + "px");
        }

        else if (ori == "vertical") {
            n = parseInt(n);
            y.children("ul").children("li").css("float", "none");
            y.children("ul").children("li").css("display", "list-item");
            y.children("ul").children("li").css("height", n + "px");
            y.children("ul").children("li").css("padding-top", i_m + "px");
        }

        y.children("ul").children("li").children("a").css("color", q);
        y.children("ul").children("li").children("a").css("font-size", o);
        y.hover(function() {
            stop()
        }, function() {
            stop();
            if ((dir == "reverse") && (ori == "horizontal"))
                playR();
            else if ((dir == "forward") && (ori == "horizontal"))
                playF();

            else if ((dir == "reverse") && (ori == "vertical"))
                playD();
            else if ((dir == "forward") && (ori == "vertical"))
                playU();
        });
        
        if (ori == "horizontal")
            beginH();
        else if (ori == "vertical")
            beginV();

        if ((dir == "reverse") && (ori == "horizontal"))
            playR();
        else if ((dir == "forward") && (ori == "horizontal"))
            playF();

        else if ((dir == "reverse") && (ori == "vertical"))
            playD();
        else if ((dir == "forward") && (ori == "vertical"))
            playU();

        function beginV() {
            x = y.children("ul").height();
            y.children("ul").children("li").hide();
            y.children("ul").children("li").slice(0, r).show();
            u = y.children("ul").outerWidth();
            v = y.children("ul").outerHeight();
            y.children("ul").height(x);
            y.width(u);
            y.height(v);
            y.children("ul").children("li").show();
            y.css("position", "relative");
            y.children("ul").css("position", "absolute");
            y.children("ul").clone().prependTo(y);
            j = parseInt(j);
            y.children("ul").css("top", "0px");
            y.find("ul:last").css("top", x);
            w = x - i_m;

            if (tID) {
                $('#' + tID).live('click', function() {
                    //toggleDirection();
                    stop();
                    
                    if (tAni)
                    {
                        eval(tAni);
                        setTimeout(toggleDirection, cAf);
                    }
                    
                    else
                        toggleDirection();
                        
                    return false;
                });
            }
        }

        function beginH() {
            x = y.children("ul").width();
            y.children("ul").children("li").hide();
            y.children("ul").children("li").slice(0, r).show();
            u = y.children("ul").outerWidth();
            v = y.children("ul").outerHeight();
            y.children("ul").width(x);
            y.width(u);
            y.height(v);
            y.children("ul").children("li").show();
            y.css("position", "relative");
            y.children("ul").css("position", "absolute");
            y.children("ul").clone().prependTo(y);
            j = parseInt(j);
            y.children("ul").css("left", "0px");
            y.find("ul:last").css("left", x);
            w = x - i_m;

            if (tID) {
                $('#' + tID).live('click', function() {
                    //toggleDirection();
                    stop();
                    
                    if (tAni)
                    {
                        eval(tAni);
                        setTimeout(toggleDirection, cAf);
                    }
                    
                    else
                        toggleDirection();
                    return false;
                });
            }
        }

        function playU() {
            if (parseInt(y.find("ul").eq(t).css("top")) <= -(x)) {
                var a = x;
                y.find("ul").eq(t).css("top", a);
                if (t == 0) {
                    t = 1
                } else {
                    t = 0
                }
            }

            y.find("ul").each(function() {
                $(this).css("top", (parseInt($(this).css("top")) - st) + "px")
            });
            s = setTimeout(playU, g);
        }

        function playD() {
            if (parseInt(y.find("ul").eq(t).css("top")) >= 0) {
                var a = -x;
                if (t == 0) {
                    t = 1
                } else {
                    t = 0
                }
                y.find("ul").eq(t).css("top", a);
            }

            y.find("ul").each(function() {
                $(this).css("top", (parseInt($(this).css("top")) + Number(st)) + "px")
            });
            s = setTimeout(playD, g)
        }

        function playF() {
            if (parseInt(y.find("ul").eq(t).css("left")) <= -(x)) {
                var a = x;
                y.find("ul").eq(t).css("left", a);
                if (t == 0) {
                    t = 1
                } else {
                    t = 0
                }
            }

            y.find("ul").each(function() {
                $(this).css("left", (parseInt($(this).css("left")) - st) + "px")
            });
            s = setTimeout(playF, g);
        }

        function playR() {
            if (parseInt(y.find("ul").eq(t).css("left")) >= 0) {
                var a = -x;
                if (t == 0) {
                    t = 1
                } else {
                    t = 0
                }
                y.find("ul").eq(t).css("left", a);
            }

            y.find("ul").each(function() {
                $(this).css("left", (parseInt($(this).css("left")) + Number(st)) + "px")
            });
            s = setTimeout(playR, g)
        }

        function toggleDirection() {
            //stop();
            if ((dir == "reverse") && (ori == "horizontal")) {
                dir = "forward";
                playF();
            }

            else if ((dir == "forward") && (ori == "horizontal")) {
                dir = "reverse";
                playR();
            }

            else if ((dir == "reverse") && (ori == "vertical")) {
                dir = "forward";
                playU();
            }

            else if ((dir == "forward") && (ori == "vertical")) {
                dir = "reverse";
                playD();
            }
        }

        function stop() {
            clearTimeout(s)
        }

        function s_s_ul(a, b, c, d, e) {
            b = parseInt(b);
            c = parseInt(c);
            var f = "border: " + d + " solid " + " " + c + "px; " + ((ori == "vertical")? "margin" : "padding") +
            ":" + b + "px; background-color:" + e;
            a.attr("style", f);
        }
    }
})(jQuery);
