/*  ga-external.js, version 0.1.0
 *  External link Support JavaScript library for Google Analytics,
 *  (c) 2007- Noriaki UCHIYAMA <uchiyama.noriaki+js@gmail.com>
 *
 *  Require Prototype JavaScript framework: http://prototype.conio.net/
 *
 *  This library is freely distributable under the terms of an MIT license.
 *  For details, see the authors web site: http://blog.fulltext-search.biz/
 *
/*--------------------------------------------------------------------------*/

$(function() {

	var _ga_anchors = $('a');
	var hostname = location.hostname;

	_ga_anchors.each(function() {

		// 生のエレメントに変更
		var self = $(this);
		var _ga_anchor = self.get(0);
		var _ga_index = _ga_anchor.href.indexOf(hostname);

		//不必要なリンクはスルー
		if(
			_ga_index == 7
			|| _ga_index == 8
			|| _ga_anchor.href.indexOf("javascript") != -1
			|| _ga_anchor.href === ""
			|| _ga_anchor.href.indexOf("mailto:") != -1
		){
			return true;
		}

		if(self.hasClass("top-banner")){
			var prefix = "top_banner";
		}else if(self.hasClass("search-banner")){
			var prefix = "search_banner";
		}else{
			var prefix = "ext"
		}

		var _ga_prefix = '/' + prefix + window.location.pathname;
		if(_ga_prefix.lastIndexOf("/", 0) === 0){
			_ga_prefix = _ga_prefix + "/";
		}
		
		//画像バナーかどうかの判定
		var image = self.find('img');
		if(image.size() > 0){
			_ga_prefix += "image/"; 
		}else{
			_ga_prefix += "text/";
		}

		var _ga_postfix = '';
		if (_ga_anchor.rel != '') {
			var _ga_anchor_rels = _ga_anchor.rel.strip().split(' ');
			var _ga_prefix = '/' + _ga_anchor_rels[0] + '/';
			_ga_postfix = (typeof _ga_anchor_rels[1] != 'undefined') ? '/'
					+ _ga_anchor_rels[1] : '';
		}

		var _ga_index = _ga_anchor.href.indexOf(location.hostname);

		if (_ga_index != 7 && _ga_index != 8
				|| typeof _ga_anchor_rels != 'undefined') {

			self.click(function(_ga_evt) {
				_ga_analytics(_ga_evt, _ga_prefix, _ga_postfix);
			});
		}
	});

	function _ga_analytics(_ga_evt, _ga_prefix, _ga_postfix) {

		var _ga_elm = _ga_evt.target;

		// クリックされたaタグのDOMを取得
		while (_ga_elm.tagName.toLowerCase() != 'a') {
			_ga_elm = _ga_elm.parentNode;
		}

		var _ga_elm_href = new Array(_ga_elm.href);
		_ga_elm_href.unshift(_ga_prefix);
		_ga_elm_href.push(_ga_postfix);

		// 新GAコード用ページトラッカー
		_gaq.push( [ '_trackPageview', _ga_elm_href.join('')]);
	}

})
