﻿//注：本文件很多方法Lswweb.Controls中都用到，所以必须引入本文件

//仿阿里巴巴右则滑动快速导航
//调用方式：$("#slideTabs").SlideTabs();
//Theme代码：
//.slideTabs{border:1px solid #000;}
//.slideTabs .slideTabs-tab{border:1px solid #000;padding:5px;margin:5px;float:left;}
//.slideTabs .slideTabs-tab-show{border:1px solid red;}
//.slideTabs .slideTabs-tab-show-left{border-left:1px solid red;float:left;}
//.slideTabs .slideTabs-tab-show-right{border-right:1px solid red;float:left;}
//.slideTabs .slideTabs-panel{}
(function($) {
    $.fn.SlideTabs = function(settingOptions) {
        liControls = this.find("li"), divControls = this.find("div"), ulControls = this.find("ul");
        var columns = liControls.length / ulControls.length;
        var settings = { className: "slideTabs", index: "0" }
        $.extend(true, settings, settingOptions);

        this.addClass(settings.className);

        liControls.each(function(index) {
            $(this).addClass("slideTabs-tab");
            if (index == settings.index) $(this).toggleClass("slideTabs-tab-show");
        }).mouseover(function() {
            var index = liControls.index($(this));
            if (settings.index != index) {
                liControls.filter("li:eq(" + settings.index + "),li:eq(" + index + ")").toggleClass("slideTabs-tab-show");
                divControls.filter("div:eq(" + settings.index + "),div:eq(" + index + ")").toggle();
                settings.index = index;
            }
        });

        divControls.each(function(index) {
            $(this).addClass("slideTabs-panel").css({ clear: "both" });
            if (index != settings.index) $(this).hide();
        });

        ulControls.each(function() {
            $(this).css({ display: "block", zoom: "1", clear: "both" });
        });
    }
})(jQuery);

//给控件选择配置不同的css样式
//$("control").CssChange({defaultClass:"class1",items[{controlId:"control1",className:"class1"},{controlId:"control2",className:"class2"}]});
(function($) {
    $.fn.CssChange = function(settingOptions) {
        var settings = { defaultCss: "", items: [] }, self = this;
        $.extend(true, settings, settingOptions);

        if (!!settings.defaultCss)
            self.attr("class", settings.defaultCss);

        for (var i = 0; i < settings.items.length; i++) {
            var item = settings.items[i];
            if (!item || !item.controlId || !item.className) continue;

            $("#" + item.controlId).attr("setClass", item.className).click(function() {
                self.attr("class", $(this).attr("setClass"));
            });
        }
    }
})(jQuery);

(function($) {
    $.fn.ImageChange = function() {
        this.find("img").mousemove(function() {
            $(this).attr("src", $(this).attr("imageAction"));
        }).mouseout(function() {
            $(this).attr("src", $(this).attr("image"));
        });
    }
})(jQuery);

//图片幻灯效果
//.slides{width: 408px;height: 168px;border:1px solid #000;list-style:none;}
//.slides ul{padding:0px;margin:0px; }
//.slides .slider li{padding:0px;margin:0px;height:168px;overflow:hidden;}
//.slides .number{position:absolute; right:5px;bottom:5px;}
//.slides .number li{border:1px solid #000;padding:0px 5px; background-color:#fff;float:left; margin-left:5px;}
//.slides .number .selected{background-color:Red; }
(function($) {
    $.fn.Slides = function(settingOptions) {
        var self = this;
        this.settings =
        {
            Timer: null, //setTimeout事件
            Slider: this.find("ul:eq(0)"), //滑动对像
            Number: this.find("ul:eq(1)"), //导航按钮对像
            Index: 0, //当前对像索引
            Options: {
                Up: true, //是否向上(否则向左)
                Step: 5, //滑动变化率
                Time: 10, //滑动延时
                Auto: true, //是否自动转换
                Pause: 2000, //停顿时间(Auto为true时有效)
                Parameter: 0, //切换参数
                Count: this.find("ul:eq(0) li").length
            }
        }
        $.extend(true, this.settings.Options, settingOptions || {});

        if (!this[0] || !this.settings.Slider[0] || !this.settings.Number[0]) return;

        this.addClass("slides");
        this.css({ overflow: "hidden", position: "relative" });
        if (!this.settings.Options.Parameter) this.settings.Options.Parameter = this.settings.Options.Up ? parseInt(this.height()) : parseInt(this.width());

        this.settings.Slider.css({ position: "absolute", left: "0", top: "0" }).addClass("slider");
        this.settings.Number.addClass("number").find("li").each(function(i) {
            $(this).mouseover(function() {
                self.settings.Index = i;
                self.settings.Options.Auto = false;
                self.SetCurrentNumberClass();
                self.Move();
            }).mouseout(function() {
                self.settings.Options.Auto = !settingOptions ? true : settingOptions.Auto == null ? true : settingOptions.Auto;
                self.Move();
            });
        });

        this.extend({
            Move: function() {
                clearTimeout(this.settings.Timer);
                if (this.GetStep() != 0) {
                    this.timer = setTimeout(function() { self.Move(); }, this.settings.Time);
                }
                else {
                    if (self.settings.Options.Auto) {
                        this.settings.Timer = setTimeout(function() {
                            self.settings.Index = self.GetTargetIndex();
                            self.SetCurrentNumberClass();
                            self.Move();
                        }, this.settings.Options.Pause);
                    }
                }
                this.settings.Slider.css(this.settings.Options.Up ? "top" : "left", (this.GetNowPosition() + this.GetStep()) + "px");
            },

            GetStep: function() {
                var iStep = (this.GetTargetPosition() - this.GetNowPosition()) / this.settings.Options.Step;
                if (iStep == 0) return 0;
                if (Math.abs(iStep) < 1) iStep = (iStep > 0 ? 1 : -1);
                return iStep;
            },

            //获取目标位置
            GetTargetPosition: function() {
                return this.settings.Index * this.settings.Options.Parameter * -1;
            },

            //取得目标位置的索引
            GetTargetIndex: function() {
                if (this.settings.Index >= (this.settings.Options.Count - 1)) return 0;
                else return (this.settings.Index + 1);
            },

            SetCurrentNumberClass: function() {
                this.settings.Number.find("li").each(function(i) {
                    if (i == self.settings.Index)
                        $(this).addClass("selected");
                    else
                        $(this).removeClass("selected");
                })
            },

            //获取当前位置
            GetNowPosition: function() {
                return parseInt(this.settings.Slider.css(this.settings.Options.Up ? "top" : "left")) || 0;
            }
        });

        this.Move();
        this.SetCurrentNumberClass();
    }
})(jQuery);

//取消点时控件四周出现虚框
(function($) {
    $.fn.AutoBlur = function() {
        this.focus(function() { this.blur(); });
    }
})(jQuery);

//添加鼠标移动到控件上样式
(function($) {
    $.fn.MousemoveCss = function(className) {
        this.mousemove(function() { $(this).addClass(className); });
        this.mouseout(function() { $(this).removeClass(className); });
    }
})(jQuery);

//Ajax方式访问WebServices
//例：$.webService({url: "/WebServices/WebService.asmx/GetWeather",data: "{ cityCode:\"440301\" }",success: function(msg) { alert(msg.d); }});
//GetWeather是WebServices服务名称，注意它后边不能带“/”。
//参数data:"{parameterName:1}"，参数名parameterName必须和webserivce中的参数名一至
//数组参数的写法data:"{parameterName:[1,2,3,4]}"
$.webService = function(controlOptions) {
    var settings =
    {
        url: "",
        success: function(msg) { },
        data: "{}",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }
    controlOptions = controlOptions || {};
    $.extend(true, settings, controlOptions);

    $.ajax(settings);
}

$(function() {
    $("table[summary=InputTable]").each(function() {
        var cells = parseInt($(this).attr("cells"))
        if (!cells) cells = 2;
        $(this).find('td:not([isNot=true])').each(function(i) {
            if (i % cells == 0) $(this).addClass("title");
        });
    })

    //取消点击连接时连接字符四周出现虚框
    $("a").AutoBlur();
})

///转换
Convert = function() { };
Convert.ToInt32 = function(strNum) {
    var label = parseInt(strNum);
    if (isNaN(label))
        label = 0;
    return label;
}

//字符串，添加format方法 使用方法 var a="1213{0},fdsfdsf{1}";a.format("1","2");
String.prototype.format = function() {
    var args = arguments;
    return this.replace(/\{(\d+)\}/g,
        function(m, i) { return args[i]; }
    );
} 

//取得页面大小。pageWidth,pageHeight为整个页面大小，windowWidth,windowHeight为windows窗口可视区域大小。
function GetPageSize() {

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }


    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
    return arrayPageSize;
}


//根据窗口大小配置改变控件大小
function Resize(controlId, removeWidth, removeHeight, orientationMode, referToControlId) {
    var control = $("#" + controlId)
    if (control[0] == null) return;

    control.hide(); //当control潜套在referControl时，因为在鼠标拖动缩小窗口时control会撑着referToControl使其不会变小（最小也是当前control的大小）所以会造成窗口不会缩小的现像。
    //在这里先隐藏control使referControl大小正常无误的进行改变，之后再参考referControl的大小改变control的大小，最后再将control显示出来。

    var height, width;
    if (referToControlId == null || referToControlId == '' || referToControlId == undefined) {
        var pageSize = GetPageSize();
        width = pageSize[2];
        height = pageSize[3];
    }
    else {
        var referToControl = $("#" + referToControlId);
        if (referToControl[0] == null) return;
        width = referToControl.width();
        height = referToControl.height();
    }

    width = width - removeWidth - Convert.ToInt32(control.css("border-left-width")) - Convert.ToInt32(control.css("border-right-width")) - Convert.ToInt32(control.css("padding-left")) - Convert.ToInt32(control.css("padding-right")) - Convert.ToInt32(control.css("margin-left")) - Convert.ToInt32(control.css("margin-right"));
    height = height - removeHeight - Convert.ToInt32(control.css("border-top-width")) - Convert.ToInt32(control.css("border-bottom-width")) - Convert.ToInt32(control.css("padding-top")) - Convert.ToInt32(control.css("padding-bottom")) - Convert.ToInt32(control.css("margin-top")) - Convert.ToInt32(control.css("margin-bottom"));

    if (orientationMode == 'All')
        control.height(height).width(width);
    else if (orientationMode == 'Width')
        control.width(width);
    else if (orientationMode == 'Height')
        control.height(height);

    control.show();
}

function AjaxHandler(url, isAlertMessage) {
    $.get(url, function(data) {
        var reg = /\$\.blockUI\((.*)\$\.unblockUI\(\)\;\}\)\;/;
        var script = data.match(reg)
        if (isAlertMessage)
            setTimeout(script[0], 100);
    }
    );
}

function ImagePreviewClick(obj) {
    $.blockUI({ css: { padding: 15 }, message: '<p><img src=' + $(obj).attr('PreviewUrl') + ' /></p><input id=closeAlertMessage type=button value=关闭 />' }); $('#closeAlertMessage').click(function() { $.unblockUI(); });
}

//FloatTipTextBox.cs用到
//设置浮动框位置
function SetFloatLocation(obj, floatId) {
    var fload = $("#" + floatId);
    fload.css("position", "absolute");
    var left = $(obj).offset().left;
    var label = document.body.offsetWidth - fload.width();
    if (label < left)
        left = label;
    var top = $(obj).offset().top + $(obj)[0].offsetHeight;
    fload.css("left", left).css("top", top);
}

     
