/**
 * jQuery PageSlide
 * @author Scott Robbin - Ian Lewis
 * @version 0.2
 * Dual licensed under the MIT and GPL licenses:
 *  http://www.opensource.org/licenses/mit-license.php
 *  http://www.gnu.org/licenses/gpl.html
 **/

(function ($) {
    $.fn.pageSlide = function (options) {

        $.data($(this).get(0), "settings", $.extend({
            width: "300px",
            duration: "normal",
            // fast,normal,ms
            start: function () {},
            stop: function () {},
            loaded: function () {}
        }, options));

        function _getSettings(el) {
            return $.data(el, "settings");
        }

        function _initialize() {
            var a = document.createElement("div");
            $(a).attr("id", "pageslide-body-wrap").width($("body").width());
            var b = document.createElement("div");
            $(b).attr("id", "pageslide-content");
            var c = document.createElement("div");
            $(c).attr("id", "pageslide-slide-wrap").append(b);
            $("body").contents().wrapAll(a);
            $("body").append(c);
            /* $(document).click(function () { // avoid parent click
                _closeSlide()
            }); */
            $("#pageslide-slide-wrap").click(function () {
                return !1
            });
            $(window).resize(function () {
                $("#pageslide-body-wrap").width($("body").width())
            })
        };


        function _openSlide(el) {
            if (!$.page_slide_settings) {
                var settings = _getSettings(el);
                settings.start();
                $("#pageslide-content").width(settings.width);
                $("#pageslide-slide-wrap").animate({
                    width: settings.width
                }, settings.duration);
                // move bck
                $("#pageslide-body-wrap").animate({
                    left: "-" + settings.width
                }, settings.duration, function () {
                    settings.stop();
                });
                $.ajax({
                    type: "GET",
                    url: $(el).attr("href"),
                    success: function (a) {
                        $("#pageslide-content").html(a).queue(function () {
                            settings.loaded();
                            $(this).dequeue()
                        })
                    }
                });
                $.closeSlide = function () {
                    _closeSlide();
                };
                $.page_slide_settings = settings;
            }
        };

        function _closeSlide() {
            var settings = $.page_slide_settings;
            settings.start();
            // move bck
            $("#pageslide-body-wrap").animate({
                left: "0"
            }, settings.duration);
            $("#pageslide-slide-wrap").animate({
                width: "0"
            }, settings.duration, function () {
                $("#pageslide-content").empty();
                settings.stop();
            });
            $.closeSlide = function () {};
            $.page_slide_settings = null;
        }

        // if not exist
        if ($("#pageslide-body-wrap").length == 0) _initialize();
        return this.each(function () {
            $(this).unbind("click").bind("click", function () {
                _openSlide(this);
                return false;
            });
        });
    };

    $.closeSlide = function () {};
    $.page_slide_settings = null;

})(jQuery);



// slide
$(document).ready(function() {
	$('a.client-slide').pageSlide({ width: "100%", duration: 1500 });
});