﻿
/**
* uploader
* Project page - 
* Copyright (c) 2009 
* Version 1.0(24.09.2009)
*/
; (function($)
{

    $.helper = function(Command, ResourceString, Panel)
    {
        return $.helper.impl.init(Command, ResourceString, Panel);
    };


    $.fn.helper = function(Command, ResourceString, Panel)
    {
        return $.helper.impl.init(Command, ResourceString, Panel);
    };

    $.helper.page = function(ThisPage, Panel, Command)
    {

        var Current = $.helper.impl.getpanel(Panel);
        if (Current != null)
        {
            if (Command != null)
            {
                if (Command.postValue != null)
                    Current.postValue = $.extend({}, Current.postValue, Command.postValue);
                if (Command.targetUrl != null)
                    Current.targetUrl = $.extend({}, Current.targetUrl, Command.targetUrl);
            }
            $.helper.impl.page(Current, ThisPage);
        }
    };

    $.helper.Save = function(Command, fnsuccess, fnerror)
    {

        $.ajax({
            type: 'POST',
            //dataType: "json", //数据格式:JSON
            url: Command.targetUrl, //目标地址
            data: Command.postValue,
            success: function(result)
            {
                fnsuccess.call(this, result);
            },
            error: function(xhr, textStatus, errorThrown)
            {
                fnerror.call(this, xhr.responseText);
            }

        });
    };

    $.helper.Delete = function(Command, fnsuccess, fnerror)
    {

        $.ajax({
            type: 'POST',
            //dataType: "json", //数据格式:JSON
            url: Command.targetUrl, //目标地址
            data: Command.postValue,
            success: function(result)
            {
                fnsuccess.call(this, result);
            },
            error: function(xhr, textStatus, errorThrown)
            {
                fnerror.call(this, xhr.responseText);
            }

        });
    };

    $.helper.ValidateLogin = function(Command, fnsuccess, fnerror)
    {

        $.ajax({
            type: 'POST',
            //dataType: "json", //数据格式:JSON
            url: Command.targetUrl, //目标地址
            data: Command.postValue,
            success: function(result)
            {
                fnsuccess.call(this, result);
            },
            error: function(xhr, textStatus, errorThrown)
            {
                fnerror.call(this, xhr.responseText);
            }

        });
    };

    $.helper.Find = function(Command, fnsuccess, fnerror)
    {

        $.ajax({
            type: 'POST',
            //dataType: "json", //数据格式:JSON
            url: Command.targetUrl, //目标地址
            data: Command.postValue,
            success: function(result)
            {
                fnsuccess.call(this, result);
            },
            error: function(xhr, textStatus, errorThrown)
            {
                fnerror.call(this, xhr.responseText);
            }

        });
    };

    $.helper.defaults = {
        data: {},
        resource: { first: "first", page: "page", total: "total", previous: "Previous", next: "Next", go: "Goto" },
        onSubmit: function(file, extension) { },
        onComplete: function(file, response) { },
        success: function(data, textStatus) { },
        onError: function(xhr, textStatus, errorThrown) { }
    };

    $.helper.impl = {
        Panels: Array(),
        init: function(Command, ResourceString, Panel)
        {
            var self = this;
            var Current = null;

            Current = self.getpanel(Panel);

            if (Current == null)
            {
                Current = {
                    opts: $.helper.defaults,
                    wrapper: null,
                    showPagePanel: true,
                    thisPage: null,
                    totalPage: null,
                    btnGo: null,
                    btnPrevious: null,
                    btnNext: null,
                    txtGo: null,
                    panel: null,
                    details: null,
                    resource: null,
                    postValue: null,
                    targetUrl: null
                };
                if (Command.showPagePanel != null && !Command.showPagePanel)
                    Current.showPagePanel = Command.showPagePanel;
                Current.panel = Panel;
                Current.resource = $.extend({}, Current.opts.resource, ResourceString);
                self.create(Current);
                self.bindevent(Current);
                self.Panels.push(Current);
            }

            Current.postValue = Command.postValue;
            Current.targetUrl = Command.targetUrl;

            if (Command.ThisPage == null)
                Command.ThisPage = 1
            self.page(Current, Command.ThisPage);
        },
        getpanel: function(Panel)
        {
            var self = this;
            var Current = null;

            $.each(self.Panels, function(i, obj)
            {
                if (obj.panel == Panel)
                    Current = obj;
            });
            return Current;
        },
        create: function(Current)
        {
            var self = Current;
            if (self.details == null)
            {
                self.details = $("<div/>").attr("class", "detailpanel").appendTo(self.panel);
                $("<div/>").attr("class", "clear").appendTo(self.panel)
                $("<div/>").attr("class", "separator").appendTo(self.panel);
            }

            if (self.wrapper == null)
                self.wrapper = $("<div/>").attr("class", "pagepanel");
            if (self.showPagePanel)
                self.wrapper.appendTo(self.panel);

            $("<div/>").attr("class", "clear").appendTo(self.panel);

            if (self.thisPage == null)
                self.thisPage = $("<span/>").attr("class", "thispage");
            if (self.totalPage == null)
                self.totalPage = $("<span/>").attr("class", "totalpage");
            if (self.btnPrevious == null)
                self.btnPrevious = $("<input/>").attr({ "class": "button previous", type: "button", value: self.resource.previous });
            if (self.btnNext == null)
                self.btnNext = $("<input/>").attr({ "class": "button next", type: "button", value: self.resource.next });
            if (self.btnGo == null)
                self.btnGo = $("<input/>").attr({ "class": "button go", type: "button", value: self.resource.go });
            if (self.txtGo == null)
                self.txtGo = $("<input/>").css({ "width": "25px" })
                .attr({ id: "thispage", "class": "text", type: "text", value: "0" });


            self.wrapper.append(self.resource.first)
                .append(self.thisPage)
                .append(self.resource.page + "&nbsp;&nbsp;" + self.resource.total)
                .append(self.totalPage)
                .append(self.resource.page + "&nbsp;&nbsp;")
                .append(self.btnPrevious)
                .append("&nbsp;&nbsp;")
                .append(self.btnNext)
                .append("&nbsp;&nbsp;")
                .append(self.btnGo)
                .append("&nbsp;")
                .append(self.txtGo)
                .append("&nbsp;" + self.resource.page);

        },

        bindevent: function(Current)
        {
            var self = this;
            Current.btnPrevious.click(function()
            {
                self.previous(Current, eval(Current.thisPage.html()));
            });
            Current.btnNext.click(function()
            {
                self.next(Current, eval(Current.thisPage.html()));
            });
            Current.btnGo.click(function()
            {
                self.page(Current, eval(Current.txtGo.val()));
            });

        },
        page: function(Current, ThisPage)
        {
            var self = this;
            var postValue = Current.postValue;

            if (postValue != null)
                postValue = $.extend({}, postValue, { Page: ThisPage });
            else
                postValue = { Page: ThisPage };

            $.ajax({
                type: "POST",
                dataType: "json", //数据格式:JSON
                url: Current.targetUrl, //目标地址
                data: postValue,
                //beforeSend: function() { }, //发送数据之前
                //complete: function() { }, //接收数据完毕
                success: function(result)
                {

                    Current.thisPage.html(result.ThisPage);
                    Current.totalPage.html(result.TotalPages);
                    Current.details.html(result.ResultString);

                    if (result.TotalPages == 0)
                    {
                        Current.btnPrevious.attr('disabled', 'disabled');
                        Current.btnNext.attr('disabled', 'disabled');
                        Current.btnGo.attr('disabled', 'disabled');
                    }
                    else
                    {
                        Current.btnPrevious.removeAttr('disabled');
                        Current.btnNext.removeAttr('disabled');
                        Current.btnGo.removeAttr('disabled');
                    }

                    if (result.ThisPage == 1)
                        Current.btnPrevious.attr('disabled', 'disabled');
                    else
                        Current.btnPrevious.removeAttr('disabled');

                    if (result.ThisPage == result.TotalPages)
                        Current.btnNext.attr('disabled', 'disabled');
                    else
                        Current.btnNext.removeAttr('disabled');






                    // self.wrapper.before(result.ResultString);
                },
                error: function(xhr, textStatus, errorThrown)
                {
                    Current.wrapper.before(xhr.responseText)
                }
            });
        },

        //下一页
        next: function(Current, ThisPage)
        {
            var self = this;

            ThisPage = ThisPage + 1;
            self.page(Current, ThisPage);
        },

        //上一页
        previous: function(Current, ThisPage)
        {
            var self = this;
            ThisPage = ThisPage > 1 ? ThisPage - 1 : 1
            self.page(Current, ThisPage);
        }

    };
})(jQuery);

