/*
 * common.js
 * depends jquery.js (v1.4.2) + xxxxx.js;
 * 
 */


// セレクタ拡張
$.extend($.expr[':'], {

	// 外部リンク
	external: function(a,i,m) {
		if (!a.href) {
			return false;
		}
		return a.hostname && a.hostname !== window.location.hostname;
	}
	
});


// メソッド拡張
$.fn.extend({

	// 新規ウィンドウを開く
	openWindow: function() {
		$(this).click(function() {
			window.open(this.href);
			return false;
		});
	}
	
});

$.fn.extend({

	// ポップアップウィンドウを開く
	openPopWindow: function() {
		$(this).click(function() {
			window.open(this.href, "WindowName","width=820,height=600,resizable=yes,scrollbars=yes");
			return false;
		});
	}
	
});


// グローバルナビアクティブ処理[プラグイン]
$.fn.globalPath = function(options) {
	var o = $.extend({
		root: '/',
		ignoreFiles: '/index\.html|index\.htm|index\.shtml|index\.php|index\.cgi/i'
	}, options);
	$(this).each(function() {
		if (this.hostname != location.hostname) {return this;}
		this.path = this.pathname.replace(o.ignoreFiles, '');
		if (!this.path.match(/^\//)) {
			this.path = '/' + this.path;
		}
		if (this.path == o.root) {
			if (this.path == location.pathname.replace(o.ignoreFiles, '')) {
				$(this).find('img').addClass('active').swapImg();
				$(this).addClass('active');
			}
		}
		else if (location.pathname.search(this.path) == 0) {
			$(this).find('img').addClass('active').swapImg();
			$(this).addClass('active');
		}
	});
	return this;
};


// ローカルナビアクティブ処理[プラグイン]
$.fn.path = function() {

	$(this).each(function() {
		if (this.href == window.location.href) {
			$(this).addClass('active');
		}
	});
	return this;
	
};


$.fn.swapImg = function(options) {
	var o = $.extend({
		swapImgPostfix: '_on'
	}, options);
	$(this).each(function() {
		this.swapImgSrc = $(this).attr('src').replace(/(\.gif|\.jpeg|\.jpg|\.png)/i, o.swapImgPostfix + '$1');
		$(this).attr('src', this.swapImgSrc);
	});
	return this;
};

$.fn.rollover = function(options) {
	var o = $.extend({
		swapImgPostfix: '_on'
	}, options);
	$(this).not('.active').each(function() {
		this.defaultSrc = $(this).attr('src');
		this.hoverSrc = this.defaultSrc.replace(/(\.gif|\.jpeg|\.jpg|\.png)/i, o.swapImgPostfix + '$1');
		this.hoverImg = new Image;
		this.hoverImg.src = this.hoverSrc;
	}).hover(function() {
		$(this).attr('src', this.hoverSrc);
	}, function() {
		$(this).attr('src', this.defaultSrc);
	});
	return this;
};


// タブ切り替え[プラグイン]
$.fn.tabs = function(initial) {
	$(this).each(function() {
		$(this).find('.tab-naviOn').removeClass('tab-naviOn');
		var tabsBody = $(this).children('div');
		var tabsNav = $(this).children('ul');
		var tabsNavInitial = initial? 'nth-child(' + initial + ')': 'first';
		var tabsNavSelected = $(tabsNav).children('li:' + tabsNavInitial);
		var tabsNavLink = $(tabsNav).find('[href^="#"]');
		$(tabsBody).addClass('tabs-body').hide();
		$(tabsNav).addClass('tabs-nav');
		$(tabsNavSelected).addClass('tab-naviOn').each(function() {
			$($(this).find('[href^="#"]').attr('href')).show();
		});
		
		$(tabsNavLink).click(function() {
			$(tabsBody).hide();
			$(tabsNav).find('.tab-naviOn').removeClass('tab-naviOn');
			$(this).parent('li').addClass('tab-naviOn');
			$($(this).attr('href')).show();
			return false;
		});
	});
};


$(function() {

	// JavaScript 有効判定
	$('body').addClass('js-enabled');
	
	// ブラウザ判別
	var browserName;
	var browserVer;
	if ($.browser.msie) {
		browserName = 'msie';
		switch ($.browser.version) {
			case '6.0': browserVer = 'v6'; break;
			case '7.0': browserVer = 'v7'; break;
			case '8.0': browserVer = 'v8'; break;
			default: browserVer = 'v5';
		}
		
		$('body').addClass(browserVer);
	} else if ($.browser.mozilla) {
		browserName = 'mozilla';
	} else if ($.browser.safari) {
		browserName = 'safari';
	} else if ($.browser.opera) {
		browserName = 'opera';
	} else {
		browserName = 'unknown';
	}
	$('body').addClass(browserName);

	// リンク
	//$('a:external').addClass('external').openWindow();
	$('.external').openWindow();
	$('a[href$=".pdf"]').addClass('pdf').openWindow();
	$('a[href$=".zip"]').addClass('zip');
	$('a[href$=".exe"]').addClass('exe');
	$('a[href^="https:"]').addClass('https');
	$('a[href^="ftp:"]').addClass('ftp');
	$('a[href^="mailto:"]').addClass('mailto');
	$('.popup').openPopWindow();

	// グローバルナビのアクティブ処理
	$('#GlobalNaviArea a').globalPath();
	
	// ローカルナビのアクティブ処理 *ページ内アンカーを除く
	$('#LocalNaviArea a:not([href*="#"])').globalPath();

	// ロールオーバー
	$('.over').rollover();

	// スクロール
	$(function(){
	   // #で始まるアンカーをクリックした場合に処理
	   $('[href^=#]').click(function() {
		  // スクロールの速度
		  var speed = 400;// ミリ秒
		  // アンカーの値取得
		  var href= $(this).attr("href");
		  // 移動先を取得
		  var target = $(href == "#" || href == "" ? 'html' : href);
		  // 移動先を数値で取得
		  var position = target.offset().top;
		  // スムーススクロール
		  $($.browser.safari ? 'body' : 'html').animate({scrollTop:position}, speed, 'swing');
		  return false;
	   });
	});


	// 文字サイズ
	/*
	$('#Switcher a').click(function() {
		switchStyle($(this).attr('title'));
		return false;
	});
	var c = $.cookie('style');
	if (c) switchStyle(c);
	function switchStyle(styleName) {
		$('link[rel*="stylesheet"][title]').each(function() {
			this.disabled = true;
			if ($(this).attr('title') == styleName) this.disabled = false;
		});
		// 選択した設定をクッキーに保存します
		$.cookie('style', styleName, {
			expires: 30,  // 有効日数
			path: '/'     // 有効ディレクトリ
		});
	}
	*/

	// タブ切り替え
	/*
	$('#tabChange').tabs();
	*/

});


// その他の処理 =========================================

// Cookie
/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};



// ランダム表示
/* var images = {
	// 画像とジャンプ先の URL のペア
	url : [
		['/common/img/gl_bn01.gif', '/test/01.html'],
		['/common/img/gl_bn02.gif', '/test/02.html'],
		['/common/img/gl_bn03.gif', '/test/03.html'],
		['/common/img/gl_bn04.gif', '/test/04.html'],
		['/common/img/gl_bn05.gif', '/test/05.html']
		],

	// 順番のシャッフル
	shuffle : function() {
		for (i = this.url.length; i > 0; --i) {
			tmp = this.url[p = Math.floor(Math.random()*i)] ;
			this.url[p] = this.url[i-1] ;
			this.url[i-1] = tmp ;
		}
	},

	p : 0, // 表示画像のポインタ

	// 画像表示
	put : function() {
		document.write('<a href="'+this.url[this.p][1]+'"><img src="'+this.url[this.p++][0]+'" /></a>') ;
		if (this.p >= this.url.length) this.p = 0 ;
	}
} ;
images.shuffle() ;
*/



$(document).ready(function(){
  // 偶数行にのみクラスを指定
  $('table.chart tr:even').addClass('even');
});

