﻿(function(window, undefined) {

	var rollover = (function(){

		var
		HOVER_CLASS = "hover",
		SUFFIX     = "_on",
	
		over_src = function(orig_src) {
			return orig_src.replace(/\.([^.]+)$/, SUFFIX+'.$1');
		},
	
		addEvent = (function(window, document){
			if(document.addEventListener){
				return function(elem, type, cb){
					if((elem && !elem.length) || elem === window){
						elem.addEventListener(type, cb, false);
					}
					else if(elem && elem.length){
						var len = elem.length;
						for (var i = 0; i < len; i++){
							addEvent(elem[i], type, cb);
						}
					}
				};
			}
			//IE
			else if(document.attachEvent){
				return function (elem, type, cb){
					if((elem && !elem.length) || elem === window){
						elem.attachEvent('on' + type, function(){
							return cb.call(elem, window.event)
						});
					}
					else if(elem.length){
						var len = elem.length;
						for (var i = 0; i < len; i++){
							addEvent(elem[i], type, cb);
						}
					}
				};
			}
		})(this, document);
	
	
		addEvent(window, 'load', function(){
	
			var img = document.getElementsByTagName('img');
			var input = document.getElementsByTagName('input');
	
			for( var i = 0; i < img.length + input.length; i++ ){
	
				var e = ( i < img.length ) ? img[i] : input[(i-img.length)];
	
				if((e.className).indexOf(HOVER_CLASS) == -1) continue;
	
				(function(e){
	
					var orig_src = e.src;
	
					var preload = new Image();
	
					preload.src = over_src(orig_src);
	
					preload.onload = function(){
	
						addEvent(e, 'mouseover', function(){
							e.src = over_src(orig_src);
						});
	
						addEvent(e, 'mouseout', function(){
							e.src = orig_src;
						});
	
						addEvent(window, 'unload', function(){
							e.src = orig_src;
						});
	
					};
	
				})(e);
	
			}
	
		});
	
	
	})();

	var textsize = (function() {
	
		readCookie = function(name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		};
		
		var setCookie = function(name, value){
			document.cookie = name+"="+value+"; path=/";
		};
		var setTextSize = function(name){
			switch(name){
			case 'small':
				$('body').css('font-size', '68.8%');
				break;
			case 'medium':
				$('body').css('font-size', '75%');
				break;
			case 'large':
				$('body').css('font-size', '93.8%');
				break;
			default:
				$('body').css('font-size', '75%');
			}
		};
	
		var textsizeBtn = $('.textsize').find('li');
	
		var cookieValue = readCookie('textsize');
		
		if(cookieValue){
			$(textsizeBtn).each(function(){
				if($(this).is("."+cookieValue)){
					$(this).addClass('cur');	
				}							 
			});
			setTextSize(cookieValue);
		}
		else {
			$('.textsize').find('li.medium').addClass('cur');
			setCookie('textsize', 'medium');
		}
	
		$(textsizeBtn).each(function(){
			var className = $(this).attr('class');
			className = className.replace(/cur/gi, '');
			$(this).click(function(e){
				e.preventDefault();
				setCookie('textsize', className);
				$(this).siblings().removeClass('cur');
				$(this).addClass('cur');
				setTextSize(className);
			});
		});
	
	})();

	//Equal Heights
	var eqh01 = (function() {
						  
		var wrapperCol = $('.mod_column01, .mod_column02');

		function compareNumbers(a, b){  
			return a - b;  
		}

		wrapperCol.each(function(){
			var elmBox = $(this).find('.bd'),
			elems = elmBox.length, i, arr = [], maxHeight = 0, setHeight = 0;
			
			var topPadding = parseInt(elmBox.css('padding-top'));
			var bottomPadding = parseInt(elmBox.css('padding-bottom'));
			
			for(i=0;i<elems;i++){
				
				arr[i] = elmBox[i].offsetHeight;
			};
			
			maxHeight = arr.sort(compareNumbers).pop();
			
			setHeight = maxHeight - topPadding - bottomPadding;

			elmBox.css(($.browser.msie && $.browser.version < 7 ? '' : 'min-') + 'height', setHeight); 
			

		});
					  
	})();


	var gnavi = (function() {

		function convPathToArray(path){
			var splitpath = path.split('/');
			for (var i = 0; i < splitpath.length; i++) {
				if (splitpath[i] == "" || splitpath[i] == "index.html") {         
					splitpath.splice(i, 1);
					i--;
				}
			}
			return splitpath;
		}

		var windowPath = convPathToArray(window.location.pathname);
		
		$('#nav').find('li').each(function() {
			var linkPath =  convPathToArray($(this).find('a').attr('href'));			
			if( windowPath[0] == linkPath[0]){
				$(this).addClass('cur');
			}
		});
		
		if(windowPath[0] == 'health'){
			$('#nav').find('.item_03').addClass('cur');
		}	
		
		$('#nav').find('.item_03').bind('mouseover mouseout', function(e){		
			var hiddenUl = $(this).find('ul');
			if(e.type == 'mouseover'){
				hiddenUl.stop().animate({ height: '70px', opacity: 1 }, {queue : false, duration: 300});
			}
			else if(e.type == 'mouseout'){
				hiddenUl.stop().animate({ height: '0px', opacity: 0 }, {queue : false, duration: 300});
			}
		});


	})();


	var forminput = (function() {
		var defaultValue = 'サイト内検索';
		var inputField = $('#nav').find('.inpuText');
		inputField.attr('value', defaultValue);

		inputField.bind('focus blur', function(e){
			if(e.type == 'focus' && this.value != '' ){
				this.value = '';
			}
			if(e.type == 'blur' && this.value == '' ){
				this.value = defaultValue;
			}
			
		});
		
	})();


	var sidenavi = (function() {
						  
		var pathname = window.location.pathname;
		pathname = pathname.replace('index.html','');
		var menu = $('#side').find('.mod_sidenavi01');
		var secondLevel = menu.find('ul');
		var thirdLevel = secondLevel.find('ul');
		
		function setIrregLink(wherepath, ispath){
			if( wherepath instanceof Array){
				for(i = 0; i<wherepath.length;i++){
					if(pathname == wherepath[i]){				
						menu.find("a[href$='"+ ispath +"']").parent().addClass('cur').parent().addClass('open');
					}
				}
			}
			else{
				if(pathname == wherepath){				
					menu.find("a[href$='"+ ispath +"']").parent().addClass('cur').parent().addClass('open');
				}
			}
		}

		menu.find('li').each(function() {
	
			if( $(this).find('a').attr('href') == pathname){
				$(this).addClass('cur').parent().addClass('open');
			}
			
		});
		
		setIrregLink(["/recruit/intro/gyo02_kai.html", "/recruit/intro/gyo03_ei.html", "/recruit/intro/gyo04_sei.html"], "/recruit/intro/gyo01_ken.html");

		setIrregLink("/health/smbg/parts/qa.html", "/health/smbg/parts/qa_002.html");

		menu.find('ul.open').parent().addClass('sel');
		
		secondLevel.find('.cur').find('ul').parent().addClass('opened');
		
		thirdLevel.addClass('closed');
		
		menu.find('li.opened').find('ul').removeClass('closed');			

	})();
	

	$('a.movie_popup').live('click', function(){
		var newwindow=window.open($(this).attr('href'),'PopUpMovie','width=860,height=742,menubar=no,toolbar=no,scrollbars=yes');
		if (window.focus) {newwindow.focus();}
		return false;
	});

	

})(window);



