﻿String.prototype.trim = function()
{
    return this.replace(/(^[\s\xA0\n]+)|([\s\xA0\n]+$)/g, '');
}
String.prototype.isMatch = function(reg)
{
    if(reg.constructor == String)
    {
        reg = new RegExp(reg);
    }
    
    return(this.search(reg) != -1);
}
String.prototype.Replace = function(reg, ment)
{
    if(reg.constructor == String)
    {
        reg = new RegExp(reg);
    }
    
    return(this.replace(reg, ment));
}



function link(href)
{
    if(href.search(/\*/g) > -1)
    {
        var pn = window.location.pathname + '';
        href = href.replace(/^[^\*]+/g, '');
        var values_ = href.replace(/^\*/g, '').split('&');
        var search = window.location.search;
        
        for(var i = 0; i < values_.length; i++)
        {
            var value = values_[i];
            var p = value.replace(/^([^\=]+)(\=).+/g, '(\\?|\\&)$1\\$2.+');
            
            if(search.isMatch(p))
            {
                search = search.Replace(p, '$1' + value);
            }
            else
            {
                search += '&' + value;
            }
        }
        
        search = search.replace(/^\&+|\&+$/g, '');
        if(!search.isMatch(/^\?/g))
        {
            search = '?' + search;
        }
        
        href = pn + search;
    }
    
    
    href.replace(/([^\w]lang\=)(\w*)/g, '$1' + lang());
    
    if(href.isMatch(/[^\w]lang\=/g))
    {
        
    }
    else if(href.isMatch(/\?/g))
    {
        href += '&lang=' + lang();
    }
    else
    {
        href += '?lang=' + lang();
    }
    
    window.location.href = href;
}

function lang(s, l)
{
    if(s == null)
    {
        var l = $('body').attr('class').match(/(^|\s)([a-z]{2})(\s|$)/i)[0];
        return(l.trim());
    }
    else if(s != null)
    {
        var r = s.split('$');
        
        if($('body').is('.zh'))
        {
            if(r.length == 2)
            {
                return r[1].trim();
            }
        }
        
        return r[0].trim();
    }
}

function rootURL()
{
    var root = window.location.pathname;
    
    root = root.replace(/[^\/]+$/g, '');
    root = root.replace(/\/admin\/$/g, '/');
    
    return (root);
}
function locationURL(url)
{
    return (rootURL() + url);
}

function newGuid()
{
    return Math.random().toString().replace(/0\./, 'o');
}


/*添加扩展*/
$.extend(
{
    link: function(href)
    {
        window.location.href = href;
    },
    querys: function()
    {
        var values = window.location.search.replace(/^\?/g, '').split(/&/g);
        var r = {};
        for(var i = 0; i < values.length; i++)
        {
            var value = values[i];
            var m = value.match(/^(\w+)\=(.*)/);
            if(m)
            {
                r[m[1]] = m[2];
            }
        }
        
        return r;
    },
    lang: function()
    {
        return $('body').attr('lang');
    },
    getJQuery: function(key)
    {
        return $('*[jq_guid=' + key + ']');
    }
});
$.extend(
{
    setHomePage: function()
    {
        var body = $('body').get(0);
        body.style.behavior = 'url(#default#homepage)';
        body.setHomePage(window.location.href.replace(/[^\/]*?$/, ''));
    },
    
    addFavorite: function()
    {
        window.external.addFavorite(window.location.href, window.document.title);
    },
    
    open: function(url)
    {
        window.open(url);
    },
    
    getStyle: function(url, fn)
    {
        if($('link[href=' + url + ']', document).size() == 0)
        {
            if($.browser.msie)
            {
                document.createStyleSheet(url);
            }
            else
            {
                var link = $('<link type="text/css" rel="stylesheet" />');
                link.attr('href', url);
                $('head').append(link);
            }
        }
        if(fn)
        {
            fn();
        }
    }
});


$.fn.extend(
{
    float: function(options)
    {
        var t = $(this);
        options = $.extend({
            content: null,
            width: -1,
            height: -1,
            left: 0,
            top: 0,
            offsetX: 0,
            offsetY: 0,
            animate: 'fade', //slide fade
            place: 'right', //left right [leftTop top] [leftBottom bottom] rightTop rightBottom centerBottom
            event: 'click',
            menuMode: ''
        }, options);
        
        
        var dialog = $(options.content);
        dialog.data('under', t);
        
        
        if(dialog.parent().size() == 0)
        {
            dialog.appendTo($('body'));
        }
        dialog.css({position: 'absolute', display: 'none'});
        
        var menuMode;
        if(options.menuMode.length > 0)
        {
            menuMode = 'jq_extend_float_' + options.menuMode;
            var jq = $('body').data(menuMode);

            if(!jq)
            {
                jq = $(menuMode);
            }
            $('body').data(menuMode, jq.add(dialog));
            options.event = 'mouseover';
        }
        
        t.bind(options.event, function()
        {
            var position = t.offset();
            var left = position.left;
            var top = position.top;
            
            if(options.place == 'left')
            {
                left -= dialog.outerWidth();
            }
            else if(options.place == 'right')
            {
                left += t.outerWidth();
            }
            else if(options.place == 'leftTop' || options.place == 'top')
            {
                top = top - dialog.outerHeight();
            }
            else if(options.place == 'leftBottom' || options.place == 'bottom')
            {
                top += t.outerHeight();
            }
            else if(options.place == 'rightTop')
            {
                left += t.outerWidth();
                top = top - dialog.outerHeight();
            }
            else if(options.place == 'rightBottom')
            {
                left += t.outerWidth();
                top += t.outerHeight();
            }
            else if(options.place == 'centerBottom')
            {
                left -= options.content.outerWidth() / 2 - t.outerWidth() / 2;
                top += t.outerHeight();
            }
            
            left += options.offsetX;
            top += options.offsetY;
            
            dialog.css({left: left + 'px', top: top + 'px'})
            dialog.data('show', true);
            
            dialogShow(dialog);
            if(menuMode)
            {
                $('body').data(menuMode).not(dialog).hide();
            }
            
            t.addClass('hover');
        });
        
        var dialogShow = function(jq)
        {
            if(!options.animate)
            {
                jq.show();
            }
            else if(options.animate == 'fade')
            {
                jq.fadeIn();
            }
            else if(options.animate == 'slide')
            {
                jq.slideDown();
            }
        };
        var dialogHide = function(jq)
        {
            if(!options.animate)
            {
                jq.hide();
            }
            else if(options.animate == 'fade')
            {
                jq.fadeOut();
            }
            else if(options.animate == 'slide')
            {
                jq.slideUp();
            }
        };
        
        var hideDialog = function()
        {
            if(dialog.data('show'))
            {
                dialog.show();
            }
            else
            {
                dialogHide(dialog);
                t.removeClass('hover');
            }
        };
        
        
        if(menuMode)
        {
        }
        else
        {
            if(options.event == 'mouseover')
            {
                t.bind('mouseout', function()
                {
                    dialog.data('show', false);
                    setTimeout(hideDialog, 300);
                });
                
                dialog.mouseover(function()
                {
                    $(this).data('show', true);
                    t.addClass('hover');
                });
                dialog.mouseout(function()
                {
                    $(this).data('show', false);
                    setTimeout(hideDialog, 300);
                });
            }
        }
        
        return t;
    },
    zoom: function()
    {
        var t = $(this);
        var scriptURL = locationURL('jquery/lightbox/lightbox.js');
        var cssURL = locationURL('jquery/lightbox/css/jquery.lightbox.css');
        
        t.css({cursor: 'pointer'})
        
        $.getScript(scriptURL, function()
        {
            t.lightBox({fixedNavigation:true});
        });
        
        $.getStyle(cssURL);
        
        return t;
    },
    datebox: function()
    {
        var t = $(this);
        
        var scriptURL = locationURL('jquery/ui/ui.datepicker.js');
        
        var cssURL_1 = locationURL('jquery/ui/themes/base/ui.theme.css');
        var cssURL_2 = locationURL('jquery/ui/themes/base/ui.core.css');
        var cssURL_3 = locationURL('jquery/ui/themes/base/ui.datepicker.css');
        
        
        $.getScript(scriptURL, function()
        {
            var data = {
            dateFormat: 'yy-mm-dd',
            buttonImage: 'calendar.gif',
            buttonImageOnly: false,
            yearRange: '1950:2020',
            clearText:'清除',
            closeText:'关闭',
            prevText:'前一月',
            nextText:'后一月',
            hideIfNoPrevNext: false,
            currentText: '',
            dayNames: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
            dayNamesMin: ['日', '一', '二', '三', '四', '五', '六'],
            changeMonth: true,
            changeYear: true,
            monthNames: ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
            monthNamesShort: ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
            }
            
            t.datepicker(data);
        });
        
        $.getStyle(cssURL_1);
        $.getStyle(cssURL_2);
        $.getStyle(cssURL_3);
    },
    thumbnail: function(width, height)
    {
        if(!$.browser.msie)
        {
            return false;
        }
        
        var t = $(this);
        
        var imageSize = function(img, src, fn)
        {
            img.style.position = 'absolute';
            img.style.visibility = 'hidden';
            img.style.top = '-2000px';
            img.style.left = '0px';
            img.attachEvent('onreadystatechange', fn);
            document.body.insertAdjacentElement('beforeend', img);
            img.src = src;
        }
        
        var img = document.createElement('img');
        var fn = function()
        {
            if(img.readyState!="complete")
            {
                return false;
            }
            
            var _w = width;
            var _h = height;
            
            var W = 0;
            var H = 0;
            
            var WW = img.offsetWidth;
            var HH = img.offsetHeight;
            
            if(WW > _w)
            {
                H = (_w * HH) / WW;
                if(H > _h)
                {
                    H = _h;
                    W = (H * WW) / HH;
                }
                else
                {
                    W = _w;
                }
            }
            else if(HH > _h)
            {
                W = (_h * WW) / HH;
                if (WW > _w)
                {
                    W = _w
                    H = (W * HH) / WW
                }
                else
                {
                    H = _h
                }
            }
            else
            {
                W = WW;
                H = HH;
            }
            
            if(WW < _w && HH < _h)
            {
                t.height(HH);
                t.width(WW);
            }
            else
            {
                t.height(H);
                t.width(W);
            }
        }
        
        imageSize(img, t.attr('src'), fn);
        
        return t;
    },
    dropMenu: function(options, data)
    {
        if(options == null)
        {
            options = {};
        }
        var defaults = {
            css: 'sinoDropMenu',
            
            left: 0,
            top: 0,
            offsetX: 5,
            offsetY: 0,
            place: 'right',
            event: 'mouseover'
        }
        
        var t = $(this);
        options = $.extend(defaults, options);
        var flag = 'prentDropMenu';
        
        var traversal = function(data, main, prent)
        {
            var table = $('<table class="dropMenu ' + options.css + '" cellpadding="0" cellspacing="0"></table>');
            table.hide();
            table.css({position: 'absolute'});
            table.hide();
            table.data('prent', prent);
            $('body').append(table);
            
            for(key in data)
            {
                var menu = data[key];
                
                var tr = $('<tr></tr>');
                table.append(tr);
                
                var td = $('<td></td>');
                tr.append(td);
                td.hover(function(){$(this).addClass('hover')}, function(){$(this).removeClass('hover')});
                td.mouseover(function()
                {
                    var p = prent;
                    while(true)
                    {
                        if(p == null)
                        {
                            break;
                        }
                        else
                        {
                            p.data('show', true);
                            
                            p = p.data('prent');
                        }
                    }
                });
                
                
                td.mouseout(function()
                {
                    var p = prent;
                    while(true)
                    {
                        if(p == null)
                        {
                            break;
                        }
                        else
                        {
                            var dialog = p;
                            var hideDialog = function()
                            {
                                if(dialog.data('show'))
                                {
                                    dialog.show();
                                }
                                else
                                {
                                    dialog.fadeOut('slow');
                                }
                            }
                            
                            p.data('show', false);
                            setTimeout(hideDialog, 200);
                            p = p.data('prent');
                        }
                    }
                });
                
                
                
                
                
                /*
                if(options.event == 'mouseover')
                {
                    t.bind('mouseout', function()
                    {
                        dialog.data('show', false);
                        setTimeout(hideDialog, 200);
                    });
                    
                    dialog.mouseover(function()
                    {
                        $(this).data('show', true);
                    });
                    dialog.mouseout(function()
                    {
                        $(this).data('show', false);
                        setTimeout(hideDialog, 200);
                    });
                }*/
                
                
                var a = $('<a></a>');
                td.append(a);
                
                a.html(menu.title + '&nbsp;');
                a.attr('href', menu.link);
                
                if(menu.menus)
                {
                    td.addClass('menus');
                    traversal(menu.menus, td, table);
                }
            }
            
            
            var floatOptions;
            if(t == main)
            {
                floatOptions = {
                    content: table,
                    
                    left: options.left,
                    top: options.top,
                    offsetX: options.offsetX,
                    offsetY: options.offsetY,
                    place: options.place,
                    event: options.event
                }
            }
            else
            {
                floatOptions = {
                    content: table,
                    
                    left: 0,
                    top: 0,
                    offsetX: 1,
                    offsetY: -1,
                    place: 'right',
                    event: 'mouseover'
                }
            }
            
            main.float(floatOptions);
        }
        traversal(data, t, null);
        
        return t;
    },
    form: function()
    {
        var t = $(this);
        var scriptURL = locationURL('libs/jquery/validate.js');
        
        $.getScript(scriptURL, function()
        {
            if($.lang == 'zh')
            {
                $.validator.messages = {
                    required: '必选字段',
                    remote: '请修正该字段',
                    email: '请输入正确格式的电子邮件',
                    url: '请输入合法的网址',
                    date: '请输入合法的日期',
                    dateISO: '请输入合法的日期 (ISO).',
                    number: '请输入合法的数字',
                    digits: '只能输入整数',
                    creditcard: '请输入合法的信用卡号',
                    equalTo: '请再次输入相同的值',
                    accept: '请输入拥有合法后缀名的字符串',
                    maxlength: '请输入一个长度最多是 {0} 的字符串',
                    minlength: '请输入一个长度最少是 {0} 的字符串',
                    rangelength: '请输入一个长度介于 {0} 和 {1} 之间的字符串',
                    range: '请输入一个介于 {0} 和 {1} 之间的值',
                    max: '请输入一个最大为 {0} 的值',
                    min: '请输入一个最小为 {0} 的值'
                };
            }
            
            
            t.validate({
                errorElement: 'div',
                showErrors: function(errorMap, errorList)
                {
                    if(errorList[0])
                    {
                        alert(errorList[0].message);
                    }
                },
                onkeyup: false,
                onfocusout: false
            });
            
        });
        
        return t;
    },
    dialog: function(options)
    {
        var defaults = {
            width: 700,
            style: 'concise',
            lock: true,
            yesText: ' '
        }
        
        var t = $(this);
        options = $.extend(defaults, options);
        var scriptURL = locationURL('libs/artDialog/artDialog.js');
        var cssURL = locationURL('libs/artDialog/skin/default.css');
        if(!($.browser.msie && $.browser.version * 1 == 6))
        {
            $('body').load(function()
            {
                $.getScript(scriptURL, function(){});
            });
        }
        
        t.click(function()
        {
            $.getScript(scriptURL, function()
            {
                art.dialog(options, function(){});
            });
        });
        
        $.getStyle(cssURL);
        
        return t;
    },
    Hover: function()
    {
        var t = $(this);
        t.hover(function(){$(this).addClass('hover')}, function(){$(this).removeClass('hover')});
    }
});





$(document).ready(function()
{
    //下拉菜单
    $('._DropMenu').each(function()
    {
        var data = ($(this).attr('data') || '').split('#');
        var id = data[0];
        var offsetX = '';
        var offsetY = '';
        if(id.length > 0)
        {
            id = '_DropMenu_' + id;
            if(data[1] && !$(document).is('#' + id))
            {
                var ul = $('<table class="DropMenu" style="position: absolute;"></table>');
                ul.attr('id', id);
                
                var items = data[1].split('$');
                for(var i = 0; i < items.length; i++)
                {
                    var item = items[i].split('|');
                    
                    ul.append('<tr><td class="' + (i == 0 ? 'first' : '') + '" onclick="link(\'' + item[0] + '\')"><a class="png" href="' + item[0] + '">' + lang(item[1]) + '</a></td></tr>');
                }
                ul.hide();
                
                
                var offset = data[2];
                if(offset)
                {
                    var offsetXY = offset.split('|');
                    offsetX = offsetXY[0];
                    offsetY = offsetXY[1];
                }
                
                
                $('body', document).append(ul);
                ul.mouseover(function()
                {
                    ul.show();
                });
                ul.mouseout(function()
                {
                    ul.hide();
                });
            }
            else
            {
                var ul = $('#' + id);
                ul.mouseover(function()
                {
                    ul.show();
                });
                ul.mouseout(function()
                {
                    ul.hide();
                });
            }
            $(this).mouseover(function()
            {
                var ul = $('#' + id);
                
                var x = 0;
                if($(this).is('._right'))
                {
                    x = $(this).width() - ul.width();
                }
                
                
                
                if(offsetX.constructor != Number)
                {
                    var this_width = $(this).width();
                    if(offsetX == 'left')
                    {
                        offsetX = 0;
                    }
                    else if(offsetX == 'right')
                    {
                        offsetX = this_width;
                    }
                    else if(offsetX.isMatch('^\d+$'))
                    {
                        offsetX = this_width * 1
                    }
                    else
                    {
                        offsetX = 0;
                    }
                }

                
                if(offsetY.constructor != Number)
                {
                    var this_height = $(this).height();
                    if(offsetY == 'top')
                    {
                        offsetY = this_height * -1;
                    }
                    else if(offsetX == 'bottom')
                    {
                        offsetY = 0;
                    }
                    else if(offsetY.isMatch('^\d+$'))
                    {
                        offsetY = this_width * 1
                    }
                    else
                    {
                        this_width = 0;
                    }
                }
                
                
                
                
                
                var oX = data[2].split('|')[0];
                var oY = data[2].split('|')[1];
                
                if(oX == 'right')
                {
                    oX = $(this).width();
                }
                if(oY == 'top')
                {
                    oY = $(this).height();
                }
                
                
                oX = oX * 1;
                oY = oY * 1;
                
                ul.css('left', ($(this).position().left + oX) + 'px');
                ul.css('top', ($(this).position().top + oY) + 'px');
                ul.show();
                
                
                
            });
            
            $(this).mouseout(function()
            {
                $('#' + id).hide();
            });
        }
    });
});

 

$(document).ready(function()
{
    $('table[ key=multilingual]').each(function()
    {
        var inputs = $(this).find('input');
        if(inputs.size() == 3)
        {
            var texts = inputs.eq(0).val().split('$');
            if(texts.length > 1)
            {
                inputs.eq(1).val(texts[0]);
                inputs.eq(2).val(texts[1]);
            }
            else
            {
                inputs.eq(1).val(texts[0]);
                inputs.eq(2).val(texts[0]);
            }
            

            
            var f = function()
            {
                inputs.eq(0).val(inputs.eq(1).val() + '$' + inputs.eq(2).val());
            }
            inputs.eq(1).change(function()
            {
                f();
            })
            inputs.eq(2).change(function()
            {
                f();
            });
        }
    });
    
    
    $('._Virtual').each(function()
    {
        $(this).children('*:last').addClass('last');
        $(this).children('*:first').addClass('first');
    });
    
    $('._Virtual>*:even').each(function()
    {
        $(this).addClass('even');
    });
    $('._Virtual>*:odd').each(function()
    {
        $(this).addClass('odd');
    });
    
    
    
    

    
    
    
    $('.number,.serialnumber', this).keyup(function()
    {
        this.value = this.value.replace(/[^\d\.]/g, '');
    });
    
    //身份证限制字数
    $('.identity .serialnumber', this).attr('maxlength', 18);
    
    
    
    $('form[key=search][method=get]').each(function()
    {
        var searchs = window.location.search.toString().replace(/^\?/i, '').split('&');
        for(var i = 0; i < searchs.length; i++)
        {
            var v = searchs[i].split('=');
            if(v.length == 2)
            {
                if(v[0] == 'page')
                {
                    v[1] = '0';
                }
                
                $(this).append('<input type="hidden" name="' + v[0] + '" value="' + v[1] + '" />');
            }
        }
    });
    
    
    
    
    
    
    
    
    
    
    
    $('input').each(function()
    {
        var t = $(this).attr('type');
        $(this).addClass(t);
        
        if(t == 'checkbox')
        {
            var v = $(this).attr('checked_');
            if(v != '' && v != '0' && v != null && v != undefined)
            {
                $(this).attr('checked', true);
            }
            else
            {
                $(this).attr('checked', false);
            }
        }
    });
    
    
    
    
    
    
    
    
    //单选的SELECT
    $('select').each(function()
    {
        for(var i = 0; i < this.options.length; i++)
        {
            this.options[i].innerHTML = lang(this.options[i].innerHTML);
        }
    });
    
    
    

    
    
    
    
    $('select[value_]').each(function()
    {
        var value_ = $(this).attr('value_');
        for(var i = 0; i < this.options.length; i++)
        {
            if(this.options[i].value == value_)
            {
                this.selectedIndex = i;
            }
        }
    });
    
    
    
    //多选的SELECT
    $('.MultipleValue').each(function()
    {
        var optgroup;
        var multiple = $(this).next();
        
        var user_or_admin = '';
        $('option', multiple).each(function()
        {
            if($(this).text().isMatch(/^.+?\-\-\-\-/g))
            {
                var newLabel = $(this).text().match(/^.+?\-\-\-\-/g) + '';
                newLabel = newLabel.replace(/\-\-\-\-$/g, '');
                
                if(newLabel != '')
                {
                    var label = '';
                    if(optgroup)
                    {
                        label = optgroup.attr('label');
                    }
                    
                    if(label == newLabel)
                    {
                        
                    }
                    else
                    {
                        optgroup = $('<optgroup label="">');
                        optgroup.attr('label', newLabel);
                        multiple.append(optgroup);
                    }
                    
                    var text_ = $(this).text().replace(/^.+?\-\-\-\-/g, '');
                    text_ = text_.replace(/\$/, '和')
                    text_ = text_ + ' [' + $(this).attr('value') + ']'
                    
                    if($(this).attr('value').search(/^(user|admin)_/g) != -1)
                    {
                        if($(this).attr('value').search(/_form$/g) != -1)
                        {
                            text_ = text_;
                        }
                        if($(this).attr('value').search(/_list$/g) != -1)
                        {
                            text_ = text_;
                        }
                        if($(this).attr('value').search(/_out$/g) != -1)
                        {
                            text_ = text_;
                        }
                    }
                    
                    $(this).text(text_);
                    optgroup.append($(this));
                    
                    
                    if($(this).attr('value').search(/^user_/g) != -1)
                    {
                        user_or_admin = 'user';
                    }
                    else if($(this).attr('value').search(/^admin_/g) != -1)
                    {
                        if(user_or_admin == 'user')
                        {
                            optgroup = $('<optgroup label="">');
                            optgroup.attr('label', '----------'.replace(/\-/g, '----------'));
                            multiple.append(optgroup);
                        }
                        user_or_admin = 'admin';
                    }
                    else
                    {
                        
                    }
                }
            }

        });
        
        
        
        
        
        
        var symbol = {on: '■', off: '□'};
        
        if($.browser.msie && $.browser.version > 6)
        {
            symbol = {on: '☑', off: '☐'};
        }
        
        $(this).val($(this).val().replace(/^\,*|\,*$|^$/g, ','));
        var MultipleValue = $(this);
        $('option', $(this).next()).each(function()
        {
            var option = $(this);
            var ids = MultipleValue.val();
            
            if(ids.isMatch('\,' + option.val() + '\,'))
            {
                $(this).text(symbol.on + ' ' + $(this).text());
            }
            else
            {
                $(this).text(symbol.off + ' ' + $(this).text());
            }
        });
        $(this).next().dblclick(function()
        {
            var ids = MultipleValue.val();
            
            var option;
            for(var i = 0; i < $(this).get(0).options.length; i++)
            {
                if($($(this).get(0).options[i]).val() == $(this).val())
                {
                    option = $($(this).get(0).options[i]);
                }
            }
            
            if(ids.isMatch('\,' + option.val() + '\,'))
            {
                ids = ids.Replace('\,' + option.val() + '\,', ',');
                option.text(option.text().Replace('^\\' + symbol.on, symbol.off));
            }
            else
            {
                option.text(option.text().Replace('^\\' + symbol.off, symbol.on));
                ids += option.val() + ','
            }
            
            MultipleValue.val(ids);
            
            this.blur();
        });
    })
    
});










$(document).ready(function()
{
    $('.ListTable *[key] input:checkbox', this).each(function()
    {
        $(this).attr('title', lang('双击全选$Double-click Select All'))
        $(this).dblclick(function()
        {
            $('input[name=' + $(this).attr('name') + ']', document).attr('checked', $(this).attr('checked'));
        });
    });
    
    $('input[name=search]', this).each(function()
    {
        var value = $(this).val();
        $(this).click(function()
        {
            if($(this).val() == value)
            {
                $(this).val('');
            }
        });
    });
    
    
    $('a[href*=*]', this).each(function()
    {
        var pn = window.location.pathname + '';
        var href = $(this).attr('href');
        href = href.replace(/^[^\*]+/g, '');
        var values_ = href.replace(/^\*/g, '').split('&');
        var search = window.location.search;
        
        for(var i = 0; i < values_.length; i++)
        {
            var value = values_[i];
            var p = value.replace(/^([^\=]+)(\=).+/g, '(\\?|\\&)$1\\$2.+');
            
            if(search.isMatch(p))
            {
                search = search.Replace(p, '$1' + value);
            }
            else
            {
                //value = value.replace(/^page\=\d+$/i, 'page=0');
                search += '&' + value;
            }
        }
        
        
        search = search.replace(/^\&+|\&+$/g, '');
        if(!search.isMatch(/^\?/g))
        {
            search = '?' + search;
        }
        
        $(this).attr('href', pn + search);
        
        
    });
    
    
    $('marquee>*', this).each(function()
    {
	    $(this).css('display', 'inline');
	
	    for(var i = 0; i < 8; i++)
        {
            $(this).after($(this).clone());
        }
    });
    
    
    //表单验证
    $('form._Validate', this).submit(function()
    {
        var err;
        $(this).find('*[tip]').each(function()
        {
            if($(this).val().length == 0)
            {
                err = $(this);
                return false;
            }
        });
        if(err)
        {
            err.get(0).focus();
            var t;
            if(err.parent().prev('th').size() > 0)
            {
                t = err.parent().prev('th').text().trim().replace(/\:$/g, '');
            }
            
            
            var tip = err.attr('tip');
            if(tip.isMatch(/\*/g))
            {
                alert('"' + t + '" 必须填写!');
            }
            
            return false;
        }
    });
    $('form._Validate', this).each(function()
    {
        $(this).find('*[tip]').each(function()
        {
            $(this).after('<span class="tip">&nbsp;' + $(this).attr('tip') + '</span>');
        });
    });
    
    
});


































var fly_ = {};
$(document).ready(function()
{
    $('._Fly>*').each(function()
    {
        var parent = $(this).parent();
        parent.css({'overflow': 'hidden', 'white-space': 'nowrap'});
        var id = parent.attr('id');
        
        fly_[id] = {element: parent.get(0), speed: 1};
        
        $(this).css({'display': 'inline'})
	    $(this).after($(this).clone());
    });
    setInterval(Fly, 20);
});
function Fly()
{
    for(key in fly_)
    {
        var z = fly_[key].element.scrollLeft;
        fly_[key].element.scrollLeft += fly_[key].speed;
        
        if(z == fly_[key].element.scrollLeft && fly_[key].speed >= 1)
        {
            fly_[key].element.scrollLeft = 0;
        }
    }
}
function FlyStop(obj)
{
    var id = "";
    if(obj.constructor == String)
    {
        id = obj;
    }
    else if(obj.constructor == Object)
    {
        id = obj.attr('id');
    }
    
    fly_[id].speed = 0;
}
function FlyPlay(obj)
{
    var id = "";
    if(obj.constructor == String)
    {
        id = obj;
    }
    else if(obj.constructor == Object)
    {
        id = obj.attr('id');
    }
    
    fly_[id].speed = 1;
}











$(document).ready(function()
{
    $('*[_Current]', this).each(function()
    {
        $('#' + $(this).attr('_Current')).addClass('current');
    });
    
    
    $('.MessageBox', document).submit(function()
    {
        var r = true;
        $('input:text,textarea', this).each(function()
        {
            if($(this).val().length == 0 && r)
            {
                r = false;
                
                var text = $(this).parent().prev().text();
                text = text.replace(/[^\w\u4e00-\u9fa5]/i, '');
                
                alert('请填写 “' +text + '” 的内容。');
            }
        });
        
        return(r);
    });
    
    $('.MessageBox input:text,.MessageBox textarea', document).each(function()
    {
        var text = $(this).parent().prev().text();
        text = text.replace(/[^\w\u4e00-\u9fa5]/i, '');
        
        $(this).attr('name', 'MessageBox_' + text)
    });
    
    
    
    

    
    
    
    $('._ImageBox').each(function()
    {
        $(this).wrap('<table style="background-image: url(App_Themes/main/module/frm_shadow_1.jpg);" cellpadding="0" cellspacing="0"><tr><td style="padding: 4px 0 0 4px;"></td><td style="background-image: url(App_Themes/main/module/frm_shadow_1.jpg); background-position: right top; width: 5px;">&nbsp;&nbsp;</td></tr><tr><td style="background-image: url(App_Themes/main/module/frm_shadow_1.jpg); background-position: left bottom; height: 6px;"></td><td style="width: 5px; height: 6px; background-image: url(App_Themes/main/module/frm_shadow_1.jpg); background-position: right bottom; "></td></tr></table>');
    });
    
    
    
    
});





















var _Basket = {};
$(document).ready(function()
{
    $('._Basket>*').each(function()
    {
        var parent = $(this).parent();
        parent.css({'overflow': 'hidden', 'white-space': 'nowrap'});
        var id = parent.attr('id');
        
        _Basket[id] = {element: parent, current: 0, width: $(this).width(), width_: $(this).width()};
    });
    
    setInterval(Basket, 150);
});
var _ToBasket = -1;
function SetBasket()
{
    for(key in _Basket)
    {
        _Basket[key].current += 219 * _ToBasket;
    }
}
function ToBasket(i)
{
    _ToBasket = i;
    for(key in _Basket)
    {
        _Basket[key].current += (_Basket[key].element.width() / 2) * _ToBasket;
        if(i == -1)
        {
            if(_Basket[key].current == 0)
            {
                var maxScrollLeft = _Basket[key].width_ - _Basket[key].element.width();
                _Basket[key].current = maxScrollLeft;
                _Basket[key].element.get(0).scrollLeft = maxScrollLeft;
            }
        }
    }
}



var _Basket_i = 0;
function Basket()
{
    for(key in _Basket)
    {
        var $element = _Basket[key].element;
        var element = _Basket[key].element.get(0);
        var maxScrollLeft = _Basket[key].width_ - $element.width();
        var width = _Basket[key].width;
        var current = _Basket[key].current;
        
        var v = 40;
        if(element.scrollLeft > current)
        {
            _Basket_i = 0;
            if(element.scrollLeft - v > current)
            {
                element.scrollLeft -= v;
            }
            else
            {
                element.scrollLeft -= (element.scrollLeft - current) / 2;
            }
        }
        if(element.scrollLeft < current)
        {
            _Basket_i = 0;
            if(element.scrollLeft + v < current)
            {
                element.scrollLeft += v;
            }
            else
            {
                element.scrollLeft += (current - element.scrollLeft) / 2;
            }
        }
        if(element.scrollLeft == current)
        {
            _Basket_i += 1;
            if(_Basket_i > 50)
            {
                _Basket_i = 0;
                SetBasket();
            }
        }
        
        
        if(_ToBasket == 1)
        {
            if(element.scrollLeft >= maxScrollLeft)
            {
                element.scrollLeft = 0;
                _Basket[key].current = 0;
            }
        }
        if(_ToBasket == -1)
        {
            if(element.scrollLeft <= 0)
            {
                element.scrollLeft = maxScrollLeft;
                _Basket[key].current = maxScrollLeft;
            }
        }
        
        
        
    }
}






$(function()
{
    $('*[oninit]', document).each(function()
    {
        try
        {
            eval($(this).attr('oninit'));
        }
        catch(e)
        {
            alert($(this).attr('oninit'));
        }
    });
    
    var alert_ = $.querys().alert || '';
    if(alert_.length > 0)
    {
       alert(lang(decodeURI(alert_)));
    }
});




