﻿(function($) {

    $.fn.extend( {
        renderBlk : function(blkattrs) {
            var $this = $(this);
            return ZZUI.Block($this, blkattrs).render();
        },

        simpleMenu : function(menuId) {
            var timeout = 500;
            var closetimer = 0;
            var ddmenuitem = 0;

            function menu_open() {
                menu_canceltimer();
                menu_close();
                ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
            }

            function menu_close() {
                if (ddmenuitem)
                    ddmenuitem.css('visibility', 'hidden');
            }

            function menu_timer() {
                closetimer = window.setTimeout(menu_close, timeout);
            }

            function menu_canceltimer() {
                if (closetimer) {
                    window.clearTimeout(closetimer);
                    closetimer = null;
                }
            }

            $(this).bind('mouseover', menu_open);
            $(this).bind('mouseout', menu_timer);
           
        },

      

        simpleTab: function(options,params){
        	var defopt = {
        			   selected:"other",
        			   style:{ cursor: "pointer" }
        	          };
        	
        	var opt = {};
        		
        	$.extend(opt,defopt,options);   
        	
        	this.each(function() {
                var $this = $(this);
                
                var lis = $this.find("ul > li");
                lis.css(opt.style);
                
                lis.click(function(){
                   
                	lis.removeClass(opt.selected);
                	$(this).addClass(opt.selected);
                	if(opt.onclick){
                		opt.onclick(this,params);
                	}
                });
                
                
            });
        	
        }, 
        
        loadFrames : function() {

            var url;
            var frame;
            return this.each(function() {
                var $this = $(this);

                frame = $this.attr("name");

                url = ZZUI.FramesMapping[frame];

                if (url) {
                    $this.load(url, function() {

                    });

                }

            });
        },

        msgBox : function(type, msg, options) {

            return this.each(function() {
                var $this = $(this);
                var box = document.createElement('div');
                var jbox = $(box);

                jbox.css("class", type);

                jbox.html(msg);
                $this.prepend(box);

                box = null;
                jbox = null;

            });

        },

        validate : function(rules, msg) {
            
             var passorNot =true;   
            
            this.each(function() {
            	
            	
               
                var $this = $(this);
                var ruleNames = [];
                var name = $this.attr("name");
                
                ruleNames.push(name);
                
                var valid = $this.attr("valid");
                if (valid != undefined) {
                    ruleNames = ruleNames.concat(valid.split(","));
                }

                var res = zzRuleEngine.check($this.val(), ruleNames, rules || {});
                if (res.res == false) {
                    // debugger;
                    // var test = $("label[for=" +$this.attr("id") +"]").text();

                    res.msg = res.msg.replace("{n}", $("label[for=" + $this.attr("id") + "]").text());


                    $("#" + $this.attr("id") + "-error").html("<span class='error_txt'>" + res.msg + "</span>");
                    $("#" + $this.attr("id") + "-error").show();
                    passorNot  = false ;
                }
                else {
                	$("#" + $this.attr("id") + "-error").html('');//清空
                }

            });
           return passorNot; 

        }

    });

})(jQuery);
