/**
 * common.js
 *
 *  version --- 3.2
 *  updated --- 2011/06/06
 */


/* !stack ------------------------------------------------------------------- */
jQuery(document).ready(function($) {
	pageScroll();
	rollover();
	popWindow();
	localNav();
});


/* !isUA -------------------------------------------------------------------- */
var isUA = (function(){
	var ua = navigator.userAgent.toLowerCase();
	indexOfKey = function(key){ return (ua.indexOf(key) != -1)? true: false;}
	var o = {};
	o.ie      = function(){ return indexOfKey("msie"); }
	o.fx      = function(){ return indexOfKey("firefox"); }
	o.chrome  = function(){ return indexOfKey("chrome"); }
	o.opera   = function(){ return indexOfKey("opera"); }
	o.android = function(){ return indexOfKey("android"); }
	o.ipad    = function(){ return indexOfKey("ipad"); }
	o.ipod    = function(){ return indexOfKey("ipod"); }
	o.iphone  = function(){ return indexOfKey("iphone"); }
	return o;
})();
/* !fxPrint ----------------------------------------------------------------- */
(function(){
	setCSS = function(){
		elem = document.createElement('link');
		elem.setAttribute('rel','stylesheet');
		elem.setAttribute('type','text/css');
		elem.setAttribute('media','print');
		elem.setAttribute('href','/common/css/fx_print.css');
		document.getElementsByTagName('head')[0].appendChild(elem);
	}
	if( isUA.fx() ) window.addEventListener("load",setCSS,false); 
})();
/* !rollover ---------------------------------------------------------------- */
var rollover = function(){
	var suffix = { normal : '_no.', over   : '_on.'}
	$('a.over, img.over, input.over').each(function(){
		var a = null;
		var img = null;

		var elem = $(this).get(0);
		if( elem.nodeName.toLowerCase() == 'a' ){
			a = $(this);
			img = $('img',this);
		}else if( elem.nodeName.toLowerCase() == 'img' || elem.nodeName.toLowerCase() == 'input' ){
			img = $(this);
		}

		var src_no = img.attr('src');
		var src_on = src_no.replace(suffix.normal, suffix.over);

		if( elem.nodeName.toLowerCase() == 'a' ){
			a.bind("mouseover focus",function(){ img.attr('src',src_on); })
			 .bind("mouseout blur",  function(){ img.attr('src',src_no); });
		}else if( elem.nodeName.toLowerCase() == 'img' ){
			img.bind("mouseover",function(){ img.attr('src',src_on); })
			   .bind("mouseout", function(){ img.attr('src',src_no); });
		}else if( elem.nodeName.toLowerCase() == 'input' ){
			img.bind("mouseover focus",function(){ img.attr('src',src_on); })
			   .bind("mouseout blur",  function(){ img.attr('src',src_no); });
		}

		var cacheimg = document.createElement('img');
		cacheimg.src = src_on;
	});
};

$(document).ready(function(){
	$('#siteSearch').blur(function(){
		var $$=$(this);
		if($$.val()=='' || $$.val()==$$.attr('title')){
			$$.css('color', '#ccc')
			.val($$.attr('title'));
		}
	})
	.focus(function(){
		var $$=$(this);
		if($$.val()==$$.attr('title')){
			$(this).css('color', '#333')
			.val('');
		}
	})
	.parents('form:first').submit(function(){
		var $$=$('#siteSearch');
			if($$.val()==$$.attr('title')){
			$$.triggerHandler('focus');
		}
	}).end()
	.blur();
});

$(function(){
		$(".opacityOver").hover(
		  function () {
			$(this).stop().fadeTo(0, 0.7);
		  },
		  function () {
			$(this).stop().fadeTo(0, 1);
		  }
		);
});


/* !pageScroll -------------------------------------------------------------- */
var pageScroll = function(){
	jQuery.easing.easeInOutCubic = function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	}; 
	$('a.scroll, .scroll a, .pageTop a').each(function(){
		$(this).bind("click keypress",function(e){
			e.preventDefault();
			var target  = $(this).attr('href');
			var targetY = $(target).offset().top;
			var parent  = ( isUA.opera() )? (document.compatMode == 'BackCompat') ? 'body': 'html' : 'html,body';
			$(parent).animate(
				{scrollTop: targetY },
				500,
				'easeInOutCubic',
				function(){
					location.hash = target;
				}
			);
			return false;
		});
	});
}
/* !localNav ---------------------------------------------------------------- */
function localNav(){
	var _root_elm = $('body').attr('class');
	if(_root_elm.indexOf("lDef") < 0){
		$('.lNavi ul').each(function(){

			_routing = new Array;
			_routing[0] = _root_elm.match(/l[\d]+_[\d]+_[\d]+/);
			_routing[1] = _root_elm.match(/l[\d]+_[\d]+/);
			_routing[2] = _root_elm.match(/l[\d]+/);
			if(_routing[0]){ _routing[0].push(_routing[0][0].match(/[\d]+/g))};
			if(_routing[1]){ _routing[1].push(_routing[1][0].match(/[\d]+/g))};
			if(_routing[2]){ _routing[2].push(_routing[2][0].match(/[\d]+/g))};
//console.log(_routing[0]);
//console.log(_routing[1]);
//console.log(_routing[2][1]);

			if(_routing[0] != null){
				$('a.lNav'+_routing[0][1][0]+'_'+_routing[0][1][1]+'_'+_routing[0][1][2])
				.addClass('current')
				.parent().parent().show()
				.parent().parent().show();
	
			}else if(_routing[1] != null){
				$('a.lNav'+_routing[1][1][0]+'_'+_routing[1][1][1])
				.addClass('current')
				.next().show()
				.parent().parent().show();
		
			}else if(_routing[2] != null){
				$('a.lNav'+_routing[2][1])
				.addClass('current')
				.next().show();
			}else{
			}
		
		});
	}else{
	}
}
/* !popWindow --------------------------------------------------------------- */
var popWindow = function (){
	var param = null;
	// param[0] = width
	// param[1] = height
	// param[2] = window.name
	$('a[class^="js_popup"], area[class^="js_popup"]').each(function(i){
		$(this).click(function(){
			var w = null;
			param = $(this).attr('class').match(/[0-9]+/g);
			// get window.name
			param[2] = window.name ? window.name+'_' : '';
			w = window.open(this.href, param[2]+'popup'+i,'width='+param[0]+',height='+param[1]+',scrollbars=yes');
			w.focus();
			return false;
		});
	});
}
/* !defFunc ----------------------------------------------------------------- */
var defFunc = (function(){
	Print = function(){ window.print(); return false;}
	Close = function(){ window.close(); return false;}
})();
/* !Last -------------------------------------------------------------------- */
$(function(){
	$('.ul_lNavi li:last, .ul_sNavi li:last').addClass("last");
});
/* !Last -------------------------------------------------------------------- */
$(function(){
	$('#siteSeaech')
		.blur(function(){
			var $$=$(this);
			if($$.val()=='' || $$.val()==$$.attr('title')){
			$$.css('color', '#999')
				.val($$.attr('title'));
		}
	})
		.focus(function(){
			var $$=$(this);
			if($$.val()==$$.attr('title')){
			$(this).css('color', '#333')
				.val('');
		}
	})
		.parents('form:first').submit(function(){
			var $$=$('#siteSeaech');
			if($$.val()==$$.attr('title')){
			$$.triggerHandler('focus');
		}
	}).end()
	.blur();
});
/* !table -------------------------------------------------------------------- */
$(function(){
	$('.table_basic tr:nth-child(even), .table_search01 tbody tr:nth-child(even), .table_petguide01 tbody tr:nth-child(even)').addClass('odd');
});

/*----------------------------------------------------------------
Droppy （globalNavの2階層目のドロップダウンメニュー）
----------------------------------------------------------------*/
/*
 * Droppy 0.1.2
 * (c) 2008 Jason Frame (jason@onehackoranother.com)
 */
/*
 * Droppy 0.1.2
 * (c) 2008 Jason Frame (jason@onehackoranother.com)
 */
$.fn.droppy = function(options) {
  options = $.extend({speed: 250}, options || {});
  this.each(function() {
    var root = this, zIndex = 1000;
    function getSubnav(ele) {
      if (ele.nodeName.toLowerCase() == 'li') {
        var subnav = $('> ul', ele);
        return subnav.length ? subnav[0] : null;
      } else {
        return ele;
      }
    }
    function getActuator(ele) {
      if (ele.nodeName.toLowerCase() == 'ul') {
        return $(ele).parents('li')[0];
      } else {
        return ele;
      }
    }
    function hide() {
      var subnav = getSubnav(this);
      if (!subnav) return;
      $.data(subnav, 'cancelHide', false);
      setTimeout(function() {
        if (!$.data(subnav, 'cancelHide')) {
          $(subnav).slideUp(options.speed);
        }
      }, 300);
    }
    function show() {
      var subnav = getSubnav(this);
      if (!subnav) return;
      $.data(subnav, 'cancelHide', true);
      $(subnav).css({zIndex: zIndex++}).slideDown(options.speed);
      if (this.nodeName.toLowerCase() == 'ul') {
        var li = getActuator(this);
        $(li).addClass('hover');
        $('> a', li).addClass('hover');
      }
    }
    $('ul, li', this).hover(show, hide);
    $('li', this).hover(
      function() { $(this).addClass('hover'); $('> a', this).addClass('hover'); },
      function() { $(this).removeClass('hover'); $('> a', this).removeClass('hover'); }
    );
    
  });
  
};

$(function(){
	$("#utilityL").droppy({	});
});
