/**
 * log_tracker
 *  (c) 2007 NEXT CO.,LTD. (http://www.next-group.jp/)
 */
 
 /**
 * 外部リンクtracker
 *   node :
 *     clickされたElement
 *   options.xxx :
 *     track方法のオプション
 */
 
var LogTracker = new Class.create();
LogTracker.prototype = {
  initialize: function(options) {
    
    Event.observe(document, 'click', this.click.bindAsEventListener(this));
    this.serverNameRegExp = new RegExp("^" + location.protocol.replace(":","") + "s?://" + location.hostname + "/(.*)$");
    this.cutQuery = true;
    if(options!= null) {
    	if(options.depth != null) {
	        this.depth = options.depth;
	    }
	    
	    if(options.cutQuery != null) {
	        this.cutQuery = options.cutQuery;
	    }
    }

  }
  ,click: function (e, node) {
	    var node = Event.element(e);
	    if (!(node.tagName && node.tagName.eqi('a'))) {
	       	node = Element.up(node, 'a', 0);
	    }
	    if (!node) return;
	    
	    try {
		if(this.serverNameRegExp.test(node.href) == true) {
			return;
		}
	
		//defaultの深さは3
		var depth = this.depth == null ? 5 : this.depth;
		var pathArr = node.href.match(/[^\/]+/g) ;
		depth = depth > pathArr.length - 1 ? pathArr.length - 1: depth;

		var out = 'external/';
		for(i = 1; i <= depth ; i++) {
			if(this.cutQuery) {
				out += pathArr[i].replace(/\?[^\/]+/, "") + '/';
			} else {
				out += pathArr[i] + '/';
			}
		}

		//alert(out);
		urchinTracker(out);
		return;
		
    	
    }
    catch(e){
    	//alert(e);
    	return;
    }
  }
}

