/*
	About the web developer for this website.
    Muslum Torun -> Interactive Art Director of Mavi Ajans
	Personal Site: http://muslumtorun.com
	Contact: info@muslumtorun.com
*/

(function ($) {
    $.fn.slide = function (args) {
        args = $.extend({ duration: 7000, autoplay: true, mouseover: false, controlbuttons: true }, args);
        var i = 0, k = 0, t = $(this).length, tout;
        var mouse = true;
        var $this = $(this);
        var action = {
            hide: function (eq) {
                i = eq;
                $this.eq(i).animate(
				{ opacity: 0 },
				{ easing: "easeOutBack", duration: 2000 }
			);
            },
            show: function (eq) {
                k = eq;
                i = eq - 1;
                if (args.controlbuttons)
                    action.controlbutton();
                $this.eq(k).animate(
				{ opacity: 1 },
				{ duration: 3000, easing: "easeOutBack", complete: function () { }
				}
			);
                action.hide(i);
            },
            start: function () {
                if (i > t - 1)
                    i = 0;
                if (i < t - 1)
                    k = i + 1;
                if (i == t - 1)
                    k = 0;

                action.show(k);
                action.hide(i);
                i++;
                if (mouse)
                    action.setTime();
            },
            setTime: function () {
                return setTimeout(action.start, args.duration);
            },
            controlbutton: function () {
                $this.parent().find(".slidecontrollers a").removeClass();
                $this.parent().find(".slidecontrollers li:eq(" + k + ") a").addClass("selected");
            }
        };

        //CSS man.
        $this.css({ position: 'absolute', left: 0, top: 0 });
        var zindex = 0;
        $this.each(function () {
            $(this).css("z-index", 100 - zindex);
            zindex++;
        });

        //slayt control button
        if (args.controlbuttons) {
            if (t > 1) {
                $this.parent().children(".slidecontrollers").empty();
                for (var z = 0; z < t; z++)
                    $("<li></li>").html("<a href=javascript:;></a>").appendTo($this.parent().children(".slidecontrollers"));
                $this.parent().find(".slidecontrollers li:eq(0) a").addClass("selected");
                $this.parent().find(".slidecontrollers li a").bind("click", function () {
                    action.show($(this).parent().ln());
                    //alert("Successful");
                });
            }
            else
                $this.parent().children(".slidecontrollers").empty();
        }

        //check autoplay
        if (t > 1 && args.autoplay)
            tout = action.setTime();
        else if (t > 1 && !args.autoplay && args.mouseover) {
            $this.bind("mouseover", function () {
                action.start();
                mouse = true;
            });
            $this.bind("mouseleave", function () {
                mouse = false;
            });
        }
    }
})(jQuery);
