/*
 * jQuery Simple Functions 
 * version 0.7.1
 *
 * name: jquery.s-functions.js
 * author: Aoki Takashi
 * date: May 01, 2011
 * copyright (c) 2011 d-spica (http://www.d-spica.com/)
 * license MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

var ie8u = (!jQuery.support.opacity) ? true : false;
//var ie8 = (!jQuery.support.opacity && jQuery.support.style) ? true : false;
var ie7u = (!jQuery.support.style) ? true : false;
//var ie6 = (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) ? true : false;
//var ie6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false;


jQuery.fn.extend({

/* Rool Over Image */
rollOverImage: function(suffix_off, suffix_on){
	var imageCache=new Object();
	$(this).not('[src*="'+suffix_on+'."]').each(function(){
		var srcOff=$(this).attr('src');
		if(suffix_off==''){
			var dot=srcOff.lastIndexOf('.');
			var srcOn=srcOff.substr(0, dot)+suffix_on+srcOff.substr(dot, 4);
		}else{
			var srcOn=srcOff.replace(suffix_off+'.', suffix_on+'.');
		}
		imageCache[this.src]=new Image();
		imageCache[this.src].src=srcOn;
		$(this).hover(
			function(){this.src=srcOn;},
			function(){this.src=srcOff;}
		);
	});
},
	
/* Scroll Up */
scrollUp: function(){
	$(this).each(function(){
		$(this).click(function(){
			window.scrollTo(0,0);
			return false;
		});
	});
},

/* 11px Font for IE */
IEFont11px: function(){
	if(ie8u){
		$(this).each(function(){
			$(this).css({fontFamily: "'メイリオ', 'ＭＳ Ｐゴシック', sans-serif"});
		});
	}
},

/* No Link */
noLink: function(){
	$(this).each(function(){
		$(this).fadeTo(100, 0.4);
		$(this).css('cursor', 'inherit');
		$(this).addClass('no-link');
		$(this).click(function(){
			return false;
		});
	});
},

/* Uniform Height */
uniformHeight: function(element, n){
	$(this).each(function(){
		var num=$(this).children(element).size();
		if(n==null || n==''){n=num;}
		var set=Math.ceil(num/n);
		for(i=0;i<set;i++){
			var max_height=0;
			for(j=0;j<n;++j){
				var height=$(this).children(element).eq(i*n+j).height();
				max_height=Math.max(max_height,height);
			}
			for(j=0;j<n;++j){
				$(this).children(element).eq(i*n+j).height(max_height);
			}
		}
	});
},


/* Load Contents */
loadContents: function(element){
	$(this).each(function(){
		var url=$(this).find('a').attr('href')+' '+element;
		url=url.replace('#', ' #');
		$(this).empty();
		$(this).load(url)
	});
},

/* View Contents (for IE7~) */
viewContents: function(mode, speed, button){
	$(this).each(function(){
		if(button==true || button==1){
			$(this).append('<span class="button">Open</span>');
		}
		$(this).addClass('viewContents');
		switch(mode){
			case 'flagment':
				var element=$($(this).attr('href'));
				break;
			case 'next':
				var element=$(this).next();
				break;
			case 'siblings':
				var element=$(this).siblings();
				break;
			default: // (mode='uncles')
				var element=$(this).parent().siblings();
				break;
		}
		$(element).css('display', 'none');
		$(this).toggle(
			function(){
				element.slideDown(speed);
				$(this).find('.button').text('Close');
				return false;
			},
			function(){
				element.slideUp(speed);
				$(this).find('.button').text('Open');
				return false;
			}
		)
	});
},

/* Pull Down Menu (for IE6) */
pullDownMenu: function(){
	if(ie6){
		$(this).each(function(){
			$(this).hover(
				function(){
					$(this).find('ul').css('display','block');
				},
				function(){
					$(this).find('ul').css('display','none');
				}
			);
		});
	}
},

/* Light Box */
lightBox: function(mode, w, element){
	$(this).each(function(){
		$(this).click(function(){
			$('body').append('<div id="lightBox-layer"><div id="lightBox"><div id="lightBox-contents"></div></div></div>');
			var window_width=$(window).width();
			var window_height=$(window).height();
			var url=$(this).attr('href');
			if(element!='undefined' || element!=null || element!=''){url=url+element;}
			var title=$(this).attr('title');
			$('#lightBox').append('<p class="caption">'+title+'</p><p class="button">Close</p>');
			var width=400;
			var height;
			var top=10;
			if(mode=='html'){
				if(w!=null || w!=''){width=w;}
				$('#lightBox').css({width: width+'px'});
				url=url.replace('#', ' #');
				$('#lightBox-contents').load(url,function(){
					height=$('#lightBox').outerHeight();
					sizingLightBox();
				});
			}else{
				$('#lightBox-contents').append('<img src="'+url+'" alt="" />');
				width=$('#lightBox-contents img').outerWidth();
				width = (width > w) ? width : w;
				height=$('#lightBox').outerHeight();
				if(width>window_width-60){width=window_width-60;}
				$('#lightBox-contents').css({width: width+'px', textAlign: 'center'});
				sizingLightBox();
			}
			function sizingLightBox(){
				top=Math.max((window_height - height)/2-50, 10) + $('html, body').scrollTop();
				$('#lightBox').css({margin: top+'px auto 20px', width: width+'px'});
				var layer_height=$('#lightBox').outerHeight({margin: true});
				height=$(document).height()-layer_height;
				height = (window_height > height) ? window_height : height;
				$('#lightBox-layer').css({position: 'absolute', top: '0', left: '0', width: '100%', height: height+'px'});
			}
			$('#lightBox-layer').css('display','none');
			$('#lightBox-layer').fadeIn(500);
			return false;
		});
		$('p.button').live('click',function(){
			$('#lightBox-layer').fadeOut(500, function(){
				$('#lightBox-layer').remove();
			});
		});
	});
},

/* Smooth Scroll */
smoothScroll: function(option, offset, hash){
	$(this).each(function(){
		$(this).click(function(){
			var id=$(this).attr('href');
			var target=$(id).offset().top - offset;
			$('html, body').animate({scrollTop: target}, option);
			if(hash!=true || hash!=1){
				return false;
			}
		});
	});
},

/* Set Vartical Middle */
setVarticalMiddle: function(h){
	$(this).each(function(){
		$(this).wrapInner('<div class="wrap"></div>');
		$(this).css('display','table');
		$(this).find('div.wrap').css({display: 'table-cell', height: h, verticalAlign: 'middle'});
	});
},

/* Auto Slide Show */
autoSlideShow: function(time, speed, loop){
	$(this).each(function(){
		var images=$(this).find('img');
		images.css('display', 'none');
		images.eq(0).css('display', 'inline');
		var count=0;
		var num=images.length;
		var timer=setInterval(imageSwap, time);
		function imageSwap(){
			images.eq(count%num).fadeOut(speed);
			images.eq((count+1)%num).fadeIn(speed);
			count++;
			if(count>loop*num){clearInterval(timer);}
		}
	});
},

/* Roll Slide Show */
rollSlideShow: function(time, rate, loop, stop_position){
	$(this).each(function(){
		var images=$(this).find('img');
		var height=$(this).height();
		var count=0;
		var num=images.length;
		images.each(function(){
			$(this).bind('my_event', function(){
				imageSwap();
				count++;
			});
		});
		$(this).click(function(){
			if(count==0){
				images.eq(0).trigger('my_event');
			}
		});
		
		function imageSwap(){
			var image=images.eq(count%num);
			var diff = height - image.height();
			if(count%2==0){
				var margin0=diff + 'px';
				var margin1=diff*(1-rate) + 'px';
				var margin2=diff*rate + 'px';
				var margin3='0';
			}else{
				var margin0='0';
				var margin1=diff*rate + 'px';
				var margin2=diff*(1-rate) + 'px';
				var margin3=diff + 'px';
			}
			image.css('margin-top', margin0);
			image.animate({marginTop: margin1, opacity: '1'}, time*rate, 'linear', function(){
				image.animate({marginTop: margin2}, time*(1-rate), 'linear', function(){
					if(count<loop*num+stop_position){
						images.eq((count+1)%num).trigger('my_event');
						image.animate({marginTop: margin3, opacity: '0'}, time*rate, 'linear');
					}else{
						count=0;
					}
				});
			});
		}
	});
},


/* gMap */
gMap: function(mLat, mLng, zoom, cLat, cLng){
	var mapdiv = $(this).get(0);
	var w=$(mapdiv).width();
	var h=$(mapdiv).height();
	if(arguments.length<4){
		cLat=mLat;
		cLng=mLng;
	}
	var mapOptions = {
		zoom: zm,
		size: new google.maps.Size(w, h),
		center: new google.maps.LatLng(cLat,cLng),
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: true,
		scaleControl: true,
		navigationControl: true,
		scrollwheel: false
	};
	var map = new google.maps.Map(mapdiv, mapOptions);
	var markerOptions = {
		map: map,
		position: new google.maps.LatLng(mLat,mLng)
	};
	var marker = new google.maps.Marker(markerOptions);
},

/* gMapAddress */
gMapByAddress:function(address_selecter, zm){
	var mapdiv = $(this).get(0);
	var w=$(mapdiv).width();
	var h=$(mapdiv).height();
	var address=$(address_selecter).text();
	geocoder=new google.maps.Geocoder();
	while(address.indexOf("(")>-1){
		pt_start=address.indexOf("(");
		pt_end=address.indexOf(")",pt_start);
		var exclude=address.substring(pt_start,pt_end+1);
		address=address.replace(exclude,'');
	}
	if(geocoder){
		geocoder.geocode({'address': address},function(results, status){
			if(status==google.maps.GeocoderStatus.OK){
				var mapOptions={
					zoom: zm,
					size: new google.maps.Size(w, h),
					center: results[0].geometry.location,
					mapTypeId: google.maps.MapTypeId.ROADMAP,
					mapTypeControl: true,
					scaleControl: true,
					navigationControl: true,
					scrollwheel: false
				}
				var map=new google.maps.Map(mapdiv, mapOptions);
				var markerOptions={
					map: map,
					position: results[0].geometry.location
				};
				var marker=new google.maps.Marker(markerOptions);
			}else{
				$(this).append('住所から地図を取得できませんでした。');
			}
		});
	}
},
		
/* Fixed Scroll (for IE7~) */
fixedScroll: function(offset){
	$(this).each(function(){
		var top=$(this).offset().top;
		var width=$(this).width();
		var height=$(this).outerHeight();
		var element=$(this);
		$(window).scroll(function(event){//c
			var y=$(this).scrollTop();
			if (y+offset<top) {
				element.css({position: 'relative'});
				element.next().css({marginTop: '0'});
			}else if(!ie6){
				element.css({position: 'fixed', zIndex: '999', top: offset, width: width});
				element.next().css({marginTop: height});
			}
		});
	});
},

/* Smooth Page Load */ 
smoothPageLoad: function(mode, speed, easing){
	$(this).each(function(){
		if(arguments.length<3){easing='linear';}
		var element=$(this)
		element.css('visibility','hidden');
		switch(mode){
			case 'slideLeft':
				var def=element.css('margin-left');
				var width = $('body').outerWidth();
				var params_in={marginLeft: def};
				var params_out={marginLeft: -width};
				$('#page').css('overflow','hidden');
				element.css({marginLeft: width});
				element.css('visibility','visible');
				break;
			case 'slideTop':
				var def=element.css('margin-top');
				var height = $('body').outerHeight();
				var params_in={marginTop: def};
				var params_out={marginTop: -height};
				element.css({marginTop: $(window).height()});
				element.css('visibility','visible');
				break;
			default: // (mode='fadeIn'	for Webkit)
				var params_in={opacity: '1'};
				var params_out={opacity: '0'};
				break;
		}
		element.animate(params_in, {duration: speed, easing: easing});
		$('a').not('[href^="#"]').click(function(){
		var url=$(this).attr('href');
		$('html, body').animate({scrollTop: 0}, {duration: 400});
		element.animate(params_out, {duration: speed,	easing: easing, complete: function(){
			location.href=url;
		}});
		element.animate(params_in, {duration: speed, easing: easing});
		return false;
		});
	});
}

//close
});



