(function($){
	var empubble=false;
		var emoption={  
				type: 'index',        //生成类型:  'xpath' or 'selector', 默认是'xpath'  
		        preferenceAttr: 'id', // 属性偏好: 'id' or 'class', 默认是id  
		        highLight : true, //选择的元素是否高亮显示, 默认是true  
                //bgColor: 'yellow',        //背景色, 默认是'yellow'  
                border:'yellow 2px solid',//边框, 默认是'yellow 1px solid'  
                expansion: 0,//扩大边框, 默认是3  
                fullPath: true //是否是全路径, 默认是false  
            };
		var selector,query;
		var hasstyle=false;
        var currobj;
        var xlastpath='';
        var xnewpath='';
        var clickcount=0;
        $.fn.getQuery = function(){  
	        	emoption = $.extend({  
		                type: 'xpath',        //生成类型  'xpath' or 'selector'  
		                fullPath : true, //是否是全路径  
		                preferenceAttr: 'index', //属性偏好 'id' or 'class'  'index'
		        }, emoption||{});  
                /*
                if(emoption.highLight){  
                        this.highLight(o);  
                }  
                 */ 
                var path = getPath(this, ''); 
               if(path.substr(0,1)=='/') path=path.substr(1);
                selector = path.replaceAll('/', '>').replaceAll('\\[', ':eq(').replaceAll('\\]', ')').replaceAll('\\:eq\\(\\@', '[').replaceAll('\'\\)', '\']');  
                
                if(emoption.fullPath){  
                        query = '/' + path;  
                }  
                if(emoption.type=='selector'){  
                        return selector;  
                } else {  
                        return query;  
                }  
        };  
  
        this.getXpath = function(){  
                return query;  
        };  
  
        function getSelector(e){ 
        	var selector;
        	selector =$("input#inputxpath",parent.document).val();
        	 if(selector.substr(0,1)=='/')selector= selector.substr(1);
        	
        	var p = /\[(\d+?)\]/;//\x00: ASCII 码值为 0 的字符，在通讯中多表示字符串结束
        	var i,arr = [];
        	while(arr = p.exec(selector)) {
        		i=parseInt(arr[1])-1;
        		if(i<0)i=0;
        		selector = selector.replace(p,'%'+i+'#');
        		
        	}
        	selector = selector.replace(/%/g,'[').replace(/#/g,']');
        	selector = selector.replaceAll('/', '>').replaceAll('\\[', ':eq(').replaceAll('\\]', ')').replaceAll('\\:eq\\(\\@', '[').replaceAll('\'\\)', '\']');  
            
        	if(selector && $(selector).length){
        	 $(selector).highLight();
            }else {$("input#inputxpath",parent.document).focus();alert('请设定正确的Xpath地址！');}
        } 
  
        $.fn.highLight = function (){  
        	emoption = $.extend({  
                        border:'yellow 1px solid',//边框  
                        expansion: 3,//扩大边框  
                }, emoption||{});  
               if(!$('#eminer7903_abs-box').length) {
            	    $('body').append("<div id='eminer7903_abs-box' class='abs'></div>");  
	                if(!hasstyle){
	                	$('head').append("<style>.abs{position:absolute;zoom:1;pointer-events:none;z-index:1999;}</style>");  
	                	hasstyle = true;
	                }
               }
               var $div = $('#eminer7903_abs-box');  
                if($div != this){  
                        var pos=this.offset(), em = emoption.expansion;  
                        try{$div.css({'left':pos.left-em,'top':pos.top-em,'width':this.width()+2*em,'height':this.height()+2*em});  
                        }catch(e){
                        	alert(this)
                        };
                        $div.css({'background-color':emoption.bgColor, 'border':emoption.border});  
                }//else  $div.remove();
        };  
        function getpPath ($obj,timespan){  
        	$objparent = $obj.parent();  
        	if(empubble && !isNullOrEmpty($objparent)&& $objparent[0]!=document ){
        		$objparent.highLight();
        		 var path = getPath($objparent); 
                 if(path.substr(0,1)=='/')path= path.substr(1);
                 
        		var mypath =(emoption.fullPath?'/':'') +  path;
        		$("input#inputxpath",parent.document).val(mypath);
        		setTimeout(function(){getpPath($objparent,timespan);} ,timespan);
        	}else empubble = false;
        	
        }         
       
        
        function getPath ($obj){ 
        	if(isNullOrEmpty($obj)||$obj==window)return '';  
            var tn = $obj.get(0).tagName;  
            if(isNullOrEmpty(tn)) return '';  
            var attr = getAttr($obj);  
            tn = tn.toLowerCase() + attr;  
            //path = isNullOrEmpty(path) ? tn : tn + "/" + path;  
            var parentE = $obj.parent();  
            if(isNullOrEmpty(parentE) ||parentE==window|| (!emoption.fullPath && attr.substring(0, 5) == '[@id=')){  
                    return tn;  
            }else  
            return getPath(parentE)+'/'+tn;  
        };
        
  
        function getAttr (e){  
                var tn = e.get(0).tagName;  
               
                if(emoption.preferenceAttr.toLowerCase() != 'index'){
                	 var id = e.attr('id'), clazz = e.attr('class');  
                     var hasId = !isNullOrEmpty(id), hasClazz = !isNullOrEmpty(clazz);  
                     id = "[@id='" + id + "']";  
                     clazz = "[@class='" + clazz + "']";  
                	if(hasId && hasClazz){  
	                    if(emoption.preferenceAttr.toLowerCase() == 'class'){  
	                            return clazz;  
	                    } else {  
	                            return id;  
	                    }  
	                } else if(hasId && !hasClazz) {  
	                        return id;  
	                } else if(!hasId && hasClazz) {  
	                        return clazz;  
	                } 
                }
                if(e.siblings(tn).size() > 0) {  
                    var i = e.prevAll(tn).size();  
                    if(emoption.type=='xpath'){  
                         i++;  
                    }  
                    return '[' + i + ']';  
	            } else {  
	                   return '';  
	            }  
        }
        function isNullOrEmpty (o){  
                return  typeof o == 'undefined' || null==o || 'null'==o || ''==o || undefined==o||o==false ||o.length==0;  
        } 
        function doane(event, preventDefault, stopPropagation) {
        	var preventDefault = isNullOrEmpty(preventDefault) ? 1 : preventDefault;
        	var stopPropagation = isNullOrEmpty(stopPropagation) ? 1 : stopPropagation;
        	e = event ? event : window.event;
        	if(!e) {
        		e = getEvent();
        	}
        	if(!e) {
        		return null;
        	}
        	if(preventDefault) {
        		if(e.preventDefault) {
        			e.preventDefault();
        		} else {
        			e.returnValue = false;
        		}
        	}
        	if(stopPropagation) {
        		if(e.stopPropagation) {
        			e.stopPropagation();
        		} else {
        			e.cancelBubble = true;
        		}
        	}
        	return e;
        }
        
        
        
        var clickfune = function(e){  
        	if(empubble == true){
        		empubble = false;
        		return false;  
        	}
        	empubble = false;
            e=e||window.event;  
            doane(e);
            var target=e.target||e.srcElement; 
            target=currobj;
            var path = $(target).getQuery();  
            xlastpath=xnewpath;
            xnewpath=path;
            $("input#inputxpath",parent.document).val(xnewpath);
            if(parent.document.getElementById('xpathpubble').checked==true){
            	var timespan = parseFloat(parent.document.getElementById('pubbledelayinput').value)*1000;
            	setTimeout(function(){
            		empubble = true;
            		getpPath($(target),timespan);
            	} ,timespan);
            	
            }
            
            return false;  
        };
        var movefune = function(e){  
            e=e||window.event;  
            doane(e);
            var target=e.target||e.srcElement; 
            if($(target).is('#eminer7903_abs-box')) return false;
            if(currobj==target) return false;
            empubble = false;
            currobj=target;
            var path = $(target).highLight();  
            return false;  
        };
       
        $(document).click(clickfune); 
        $(document).mousemove(movefune); 
        $("input#xpathhighlight",parent.document).click(getSelector);
        $("input#getlastxpathbutt",parent.document).click(function(e){ 
        	if(!xlastpath||!xnewpath  ){
        		$(this).siblings("span#xpathinfo").text("请连续点击两个同类链接，然后再点击本按钮以获取通用Xpath。");
        		var owner=this;
        		//setTimeout('alert($(owner).attr("id"));',1000);
        		setTimeout('$("input#getlastxpathbutt",parent.document).siblings("span#xpathinfo").text("");',5000);
        		return;
        	}
            
        	var xlastpath_arr=xlastpath.split("/");
        	var xnewpath_arr=xnewpath.split("/");
        	var errcount=0;
        	for(var i in xlastpath_arr){
        		if(xlastpath_arr[i]!=xnewpath_arr[i]){
        			xlastpath1=xlastpath_arr[i].split("[");
        			xnewpath1=xnewpath_arr[i].split("[");
        			xlastpath_arr[i]=xlastpath1[0];
        			xnewpath_arr[i]=xnewpath1[0];
        			errcount++;
        		}
        	}
        	if(errcount>2){
        		$(this).siblings("span#xpathinfo").text("请连续点击两个同类链接，然后再点击本按钮以获取通用Xpath。");
        		setTimeout('$(this).siblings("span#xpathinfo").text("");',5000);
        		setTimeout('$("input#getlastxpathbutt",parent.document).siblings("span#xpathinfo").text("");',5000);
        		return;
        	}
            var newe5c330cc6=xnewpath_arr.join('/');
            $("input#inputxpath",parent.document).val(newe5c330cc6).focus();
            $(this).next("span").text("已在Xpath地址框内成功生成通用Xpath。请复制获取");
        	setTimeout('$("input#getlastxpathbutt",parent.document).next("span").text("");',5000);
        }); 
  
})(jQuery);  
  
String.prototype.replaceAll = function(regx,t){     
        return this.replace(new RegExp(regx,'gm'),t);     
}; 
 
 