/*
 * common.js
 * depends jquery.js (v1.3.2) + jquery.easing.js;
 * 
 */


// �Z���N�^�g��
$.extend($.expr[':'], {

	// �O�������N
	external: function(a,i,m) {
		if (!a.href) {
			return false;
		}
		return a.hostname && a.hostname !== window.location.hostname;
	}
	
});


// ���\�b�h�g��
$.fn.extend({

	// �V�K�E�B���h�E���J��
	openWindow: function() {
		$(this).click(function() {
			window.open(this.href);
			return false;
		});
	}
	
});


// �O���[�o���i�r�A�N�e�B�u����[�v���O�C��]
$.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();
			} else if (this.path + 'index.html' == location.pathname){
				$(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;
};


// ���[�J���i�r�A�N�e�B�u����[�v���O�C��]
$.fn.path = function() {

	$(this).each(function() {
		if (this.href == window.location.href) {
			$(this).addClass('active');
		}
	});
	return this;
	
};


$.fn.swapImg = function(options) {
	str_search = window.location.href;
	if(str_search.match('\/company\/.*\.php')){
		return this;
	}
	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;
};


$(function() {

	// JavaScript �L������
	$('body').addClass('js-enabled');
	
	// �u���E�U����
	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);

	// �����N
	$('a.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');

	// �O���[�o���i�r�̃A�N�e�B�u����
	$('#GlobalNavi a').globalPath();

	// ���[���I�[�o�[
	$('.over').rollover();

	// �X�N���[��
	if (!$.browser.opera) {
		$('.pageTOP > a').click(function() {
			$('html, body').animate({scrollTop: 0}, 600, 'easeOutExpo');
			return false;
		});
		$('[href^="#"]:not(.pageTOP > a)').click(function() {
			//if ($.browser.msie && $.browser.version == 7.0) return; // IE7�ł͏������Ȃ�*�s���������
			var targetOffset = $(this.hash).offset().top;
			var documentHeight = $(document).height();
			var windowHeight = $(window).height();
			if ((documentHeight - targetOffset) < windowHeight) {
				targetOffset = (documentHeight - windowHeight);
			}
			$('html, body').animate({scrollTop: targetOffset}, 600, 'easeOutExpo');
		});
	}

// File Upload Add
	var addFileUp_cnt = 1;
	$("#BtnAddFileUp").click(function(){
		if($("#addFileUp").children().is("input[name=upfile_3]") == true ){
			$("#BtnAddFileUp").hide();
		}else{
			addFileUp_cnt++;
			$('#addFileUp').append('<input type="file" name="upfile_' + addFileUp_cnt + '"  size="50" /><br />');
		}
	});

});

function openWin(wUrl , wName , Width , Height , Resize , Status , Scroll){
	wOption = "toolbar=no,location=no,directories=no,status=" + Status +",menubar=no,scrollbars=" + Scroll +",resizable=" + Resize +",width=" + Width + ",height=" + Height + ",left=";
	w = window.open(wUrl,wName,wOption);
	w.focus();
}
