﻿//============================================
//
// baseEvent.js
// Last Update 2011-12-07
//
//* $(function(){}); --- -------------------------------- Code to carry out after an HTML road
//* jQuery.event.add(window, "load", function(){}); ----- Code to carry out after an HTML and Images road
//* (function($){})(jQuery);
//		-Custom jQuery Function
//				-$._isIE
//				-$._getMaxWidth
//		-Original Methods
//		-Plugins
//				-autoHeight
//				-slideScroll
//
//============================================

$(function(){
	
	// Area Search
	//---------------
	$('#selectArea area').each(function(){
		var $t = $(this);
		var $map = $('#countryMap img');
		var $mapSrc= $map.attr('src');
		var $area = $t.attr('id');
		
		$t.hover(function(){
			$map.attr('src',$map.attr("src").replace(/^(.+)default(\.[a-z]+)$/, "$1"+$area+"$2"));
		},function(){
			$map.attr('src',$mapSrc);
		})
	});
	
	// subAreaSearch [for branch office]
	//---------------
	$('map#subAreaSearch > area').mapHover();
	$('map#subAreaSearch2 > area').mapHover();
	
	// Document background-image
	//---------------
	(function(){
		var $w = $(window).height();
		var $d = $('body').height();
		if($w>$d){ // form
			$('html').css({height:'100%',background:'url(/common/images/bg_body.gif) repeat left top'});
			$('body').css('height','100%');
		}
	})();
	
	// headerNav
	//---------------
	$('#headerNav li:last').css({'background-image':'none','margin-right':'0','padding-right':'0'});
	
	// footerNav
	//---------------
	$('#footer .footerNav li:last').css('background-image','none');
	$('.footBar ul li:last').css('background-image','none');
	
	// rollover
	//---------------
	$("#gNav li:not(.here) img").rollover(); // Global Navi
	$("img.rollover").rollover();
	$("input.rollover").rollover();
	
	// table stripe
	//---------------
	$("table.stripeOdd").each(function(){
		$(this).find("tbody tr:odd").addClass("odd");
	});
	
	$("table.stripeEven").each(function(){
		$(this).find("tbody tr:even").addClass("even");
	});
	
	// aside
	//--------------
	$('.asideWrap .asideSite .lumpMix:first-child').css('margin-left','0');
	$('.asideWrap .asideSite:last-child').css({'background':'none','padding-bottom':0,'margin-bottom':'0'});
	$('dl.info dd:last-child').css({'border':'none','padding-bottom':0,'margin-bottom':'0'});
	
	// Area Map Event [TOP/SRC100]
	//---------------
	$('#areaTags > li:last-child > img').toggle(function(){
		$('#attachedElem').slideDown('fast');
	},function(){
		$('#attachedElem').slideUp('fast');
	});
	
	// appeal point
	//---------------
	$('.appeal .toggle a').each(function(){
		var $t = $(this);
		$t.toggle(function(){
			$t.text('アピールポイントを隠す');
			$t.parent().next().slideDown('fast');
		},function(){
			$t.text('アピールポイントを見る');
			$t.parent().next().slideUp('fast');
		});
	});
	
	// LocalNav
	//---------------
	$('.sideBox .localNav > li:last-child').each(function(){
		if(!$(this).is('.here')){
			$(this).css({'border':'none','padding-bottom':'0'});
		}
	});
	
	// #sectionMain last-child
	//---------------
	if(document.getElementById('sectionMain')){
		var $secs = $('#sectionMain').children();
		$secs.eq($secs.length - 1).css('margin-bottom','0');
	}
	
	// .article [no-logo]
	//---------------
	$('.column2Box1 .boxInner .article').each(function(){
		var $t = $(this);
		if($t.children().get(0).tagName!= 'IMG'){
			$t.css('padding-left','0');
		};
	});
	
	
	// Execution Custom Method and Plugins
	//-------------------------------------------------------
	$('#keyword').setFocus();
	$('#keyword_again').setFocus();
	$('#val_from').setFocus();
	$('#val_from_again').setFocus();
	$("a[href*='#']").slideScroll();
	$("area[href*='#']").slideScroll();
	
	// colorbox
	//---------------
	if($('a').is('.colorbox')){
		$("a.colorbox").colorbox();
	}
	if($('a').is('.ajax')){
		$(".ajax").colorbox();
	}
	
	// FontSize Adjust[for Mac, win7, win Vista]
	//---------------
	if(navigator.userAgent.match(/Win(dows )?NT 6\.1/) || navigator.userAgent.match(/Win(dows )?NT 6\.0/) || navigator.userAgent.match(/Mac|PPC/)){
		$('body').css('font-size','11px');
		$('.imgCaptionS, .txtType1').css('font-size','10px');
		$('.slideDetails').css('font-size','11px');
	}
	
});


// 画像を含むドキュメントの読み込みが完了した後に実行
jQuery.event.add(window, "load", function(){
	// autoHeight
	$('.column2Box1,.column2Box2').each(function(){
		$(this).children().autoHeight({column:2});
	});
	$('.column3Box1,.column3Box2').each(function(){
		$(this).children().autoHeight({column:3});
	});
	$('.heightAdjust2').autoHeight({column:2});
	$('.heightAdjust3').autoHeight({column:3});
	
	// rest
	$('.columnBox').find('.rest').each(function(){
		var adj = ($(this).parent().height() - $(this).height())/2;
		$(this).css('margin-top',adj);
	});
});



(function($){
	
	//**********************************
	// Custom jQuery Function
	//**********************************
	
	//----------------------------------
	// IE Version Distinction
	//
	// IE6 : $._isIE(6)
	// IE7 : $._isIE(7)
	// IE8 : $._isIE(8)
	// IE9 : $._isIE(9)
	// IE  : $._isIE()
	//----------------------------------
	$._isIE = function(){
		var version = arguments.length >= 1 ? parseInt(arguments[0]) : '';
		switch (version){
			case 6:
				return !$.support.opacity && 
					!$.support.style && 
						typeof document.documentElement.style.maxHeight == "undefined" ;
				break;
			case 7:
				return !$.support.opacity &&
					!$.support.style &&
						typeof document.documentElement.style.maxHeight != "undefined" ;
				break;
			case 8:
				//return !$.support.opacity && $.support.style;
				return !$.support.opacity && $.support.hrefNormalized;
				break;
			case 9:
				return !$.support.noCloneEvent && $.support.opacity;
			default :
				return !$.support.opacity;
		}
		return false;
	};
	
	$._getMaxWidth = function(options){
		var op = $.extend({
			act : $('table')
		},options || {});
		
		var maxValue = 0;
		if(op.act.length >= 1){
			op.act.each(function(){
				if(maxValue < $(this).outerWidth()){
					maxValue = $(this).outerWidth();
				}
			});
			return maxValue;
		}else{
			return maxValue;
		}
	};
	
	
	//**********************************
	// Original Methods
	//**********************************
	$.fn.extend({
		
		
		// Keyword Search
		//----------------------------------
		setFocus : function(btn){
			var $t = $(this);
			var $defPhrase = '';
			if($t.attr('id')=='keyword' || $t.attr('id')=='keyword_again'){
				$defPhrase = 'キーワードを入力';
			}else if($t.attr('id')=='val_from' || $t.attr('id')=='val_from_again'){
				$defPhrase = '駅名を入力';
			}
			// Focus Action
			$t.focus(function(){
				if(!$t.attr('value').length || $t.attr('value') == $defPhrase){
					$t.attr('value','');
					$t.css('color','#333');
				}else return true;
			});
			// Blur Action
			$t.blur(function(){
				if(!$t.attr('value').length){
					$t.attr('value',$defPhrase);
					$t.css('color','#999');
				}else return true;
			});
		},
		
		history : function(options){
			var $t = $(this);
			var op = $.extend({
				dataURL      : '/form/history',
				scrollItem   : 1,
				visibleItem  : 4
			},options || {});
			
			$.ajax({
				url:op.dataURL,
				dataType:'xml',
				type: 'GET',
				data:{'rand':Math.random()},
				cache:false,
				success:function(e){
					if($('data',e).length != 0){
						$('#recommend').fadeIn();
						$('data',e).each(function(t){
							var $thumb = $("thumbnailUrl",this).text(),
									$name = $("name",this).text(),
									$url = $("url",this).text(),
									$bukkenID = $("id",this).text();
							$('ul#mycarousel').append('<li><a href="'+$url+'?b_history='+$bukkenID+':click" target="_blank"><img src="'+$thumb+'" /></a><span><a href="'+$url+'?b_history='+$bukkenID+':click" target="_blank">'+$name+'</a></span></li>');
							//$('ul#mycarousel').append('<li><a href="'+$url+'" target="_blank" onClick="click_History(\''+$bukkenID+'\');"><img src="'+$thumb+'" onload="view_History(\''+$bukkenID+'\');" /></a><span><a href="'+$url+'" target="_blank" onClick="click_History(\''+$bukkenID+'\');">'+$name+'</a></span></li>');
						});
						$('#mycarousel').jcarousel({scroll: op.scrollItem, visible: op.visibleItem});
					}
				},
				error: function(){
					$('#recommend').hide();
				}
			});
		},
		
		rollover : function(){
			$(this).mouseover(function(){
				if(!$(this).attr("src").match(/^(.+)_on(\.[a-z]+)$/)){
					$(this).attr("src",$(this).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_on$2"));
				}
			}).mouseout(function(){
				$(this).attr("src",$(this).attr("src").replace(/^(.+)_on(\.[a-z]+)$/, "$1$2"));
			}).each(function(){
				$("<img>").attr("src",$(this).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_on$2"))
			});
		},
		
		mapHover:function(){
			$(this).each(function(e){
				var $t = $(this);
				var $usemap = $t.parent().prev();
				var $mapsrc = $t.parent().prev().attr('src');
				$t.hover(function(){
					if(e<9){
					$usemap.attr('src',$usemap.attr("src").replace(/^(.+)00(\.[a-z]+)$/, "$1"+'0'+(e+1)+"$2"));
					}else if(e>=9){
					$usemap.attr('src',$usemap.attr("src").replace(/^(.+)00(\.[a-z]+)$/, "$1"+(e+1)+"$2"));
					}
				},function(){
					$usemap.attr('src',$mapsrc);
				});
			});
		}
	
	});//END $.fn.extend
	
	
//**********************************
// Plugins
//**********************************
/*---------------------------------------------------------------
jquery-auto-height.js

Copyright (c) 2010 Tomohiro Okuwaki (http://www.tinybeans.net/blog/)
Licensed under MIT Lisence:
http://www.opensource.org/licenses/mit-license.php
http://sourceforge.jp/projects/opensource/wiki/licenses%2FMIT_license

Since:   2010-04-19
Update:  2010-07-02
version: 0.03
Comment: 

jQuery 1.2 later
 ----------------------------------------------------------------*/
$.fn.autoHeight = function(options){
	var op = $.extend({
		column  : 0,
		clear   : 1,
		height  : 'min-height', // or [height]
		reset   : '', // height reset
		descend : function descend (a,b){ return b-a; }
	},options || {});
	
	var self = $(this);
	var n = 0,
		hMax,
		hList = new Array(),
		hListLine = new Array();
		hListLine[n] = 0;
		
	// get elements height
	self.each(function(i){
		if (op.reset == 'reset'){
			//$(this).removeAttr('style');
			$(this).css(op.height,'0');
			if($._isIE(6)){$(this).css('height','0');}
		}
		var h = $(this).outerHeight();
		hList[i] = h;
		if (op.column > 1){
			if (h > hListLine[n]){
				hListLine[n] = h;
			}
			if ((i > 0) && (((i+1) % op.column) == 0)){
				n++;
				hListLine[n] = 0;
			};
		}
	});
	

	hList = hList.sort(op.descend);
	hMax = hList[0];
	
	var browser = $.browser.version;
	if (op.column > 1){
		for (var j=0; j<hListLine.length; j++){
			for (var k=0; k<op.column; k++){
				if (browser == '6.0'){
					self.eq(j*op.column+k).height(hListLine[j]);
					if (k == 0 && op.clear != 0) self.eq(j*op.column+k).css('clear','both');
				} else {
					self.eq(j*op.column+k).css(op.height,hListLine[j]);
					if (k == 0 && op.clear != 0) self.eq(j*op.column+k).css('clear','both');
				}
			}
		}
	} else {
		if (browser == '6.0'){
			self.height(hMax);
		} else {
			self.css(op.height,hMax);
		}
	}
	
};



/*---------------------------------------------------------------
jQuery.slideScroll.js

jQuery required (tested on version 1.2.6)
encoding UTF-8

Copyright (c) 2008 nori (norimania@gmail.com)
http://moto-mono.net
Licensed under the MIT

$Update: 2008-12-24 20:00
$Date: 2008-12-22 23:30
 ----------------------------------------------------------------*/
$.fn.slideScroll = function(options){
	
	var c = $.extend({
		interval: 20,
		easing: 0.6, // 0.4 ~ 2.0 
		comeLink: false
	},options);
	var d = document;
	
	var timer;
	var pos;
	
	function currentPoint(){
		var current = {
			x: d.body.scrollLeft || d.documentElement.scrollLeft,
			y: d.body.scrollTop || d.documentElement.scrollTop
		}
		return current;
	}
	
	function setPoint(){
		var h = d.documentElement.clientHeight;
		var w = d.documentElement.clientWidth;
		var maxH = d.documentElement.scrollHeight;
		var maxW = d.documentElement.scrollWidth;
		
		pos.top = ((maxH-h)<pos.top && pos.top<maxH) ? maxH-h : pos.top;
		pos.left = ((maxW-w)<pos.left && pos.left<maxW) ? maxW-w : pos.left;
	}
	
	function nextPoint(){
		var x = currentPoint().x;
		var y = currentPoint().y;
		var sx = Math.ceil((x - pos.left)/(5*c.easing));
		var sy = Math.ceil((y - pos.top)/(5*c.easing));
		var next = {
			x: x - sx,
			y: y - sy,
			ax: sx,
			ay: sy
		}
		return next;
	}
	
	function scroll(){
		timer = setInterval(function(){
			nextPoint();
			
			if(Math.abs(nextPoint().ax)<1 && Math.abs(nextPoint().ay)<1){
				clearInterval(timer);
				window.scroll(pos.left,pos.top);
			}
			window.scroll(nextPoint().x,nextPoint().y);
		},c.interval);
	}
	
	function comeLink(){
		if(location.hash){
			if($(location.hash) && $(location.hash).length>0){
				pos = $(location.hash).offset();
				setPoint();
				window.scroll(0,0);
				if($.browser.msie){
					setTimeout(function(){
						scroll();
					},50);
				}else{
					scroll();
				}
			}
		}
	}
	if(c.comeLink) comeLink();
	
	$(this).each(function(){
		if(this.hash && $(this.hash).length>0 
			&& this.href.match(new RegExp(location.href.split("#")[0]))){
			var hash = this.hash;
			$(this).click(function(){
				pos = $(hash).offset();
				clearInterval(timer);
				setPoint();
				scroll();
				return false;
			});
		}
	});
};



})(jQuery);



// for Debug
//============================
if (!('console' in window)){
		window.console = {};
		window.console.log = function(str){
				return str;
		};
}
//============================
