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

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

(function($){

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"});
		});
	}
},

/* Uniform Height */
uniformHeight: function(n){
	$(this).each(function(){
		var num=$(this).children().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().eq(i*n+j).height();
				max_height=Math.max(max_height,height);
			}
			for(j=0;j<n;++j){
				$(this).children().eq(i*n+j).height(max_height);
			}
		}
	});
},

/* Swap Image */
swapImage: function(element, speed, background){
	$(element).css('background', background);
	$(element).find('img').css('display','none');
	var image_cache=new Object();
	$(this).each(function(){
		var img_src=$(this).attr('href');
		image_cache[img_src]=new Image();
		image_cache[img_src].src=img_src;
		$(this).click(function(){
			$(this).closest('ul').find('a').removeClass('current');
			$(this).addClass('current');
			var img_src=$(this).attr('href');
			$(element).find('img').fadeOut(speed, function(){
				$(element).find('img').attr('src',img_src);
				$(element).find('img').fadeIn(speed);
			});
			return false;
		});
	});
},

/* Auto Slide Show */
autoSlideShow: function(time, rate, loop, stop_position){
	$(this).each(function(){
		var height=$(this).height();
		var count=0;
		var images=$(this).find('img').get();
		var num=images.length;

		var i, j, tmp;
		for(i=num-1; i>=0; i--){
			j=Math.floor(Math.random()*(i+1))
			tmp=images[i];
			images[i]=images[j];
			images[j]=tmp;
		}

		$(images).each(function(){
			$(this).bind('my_event', function(){
				imageSwap();
				count++;
			});
		});
		$(this).click(function(){
			if(count==0){
				$(images[0]).trigger('my_event');
			}
		});
		
		function imageSwap(){
			var image=$(images[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[(count+1)%num]).trigger('my_event');
						image.animate({marginTop: margin3, opacity: '0'}, time*rate, 'linear');
					}else{
						count=0;
					}
				});
			});
		}
	});
}


//close
});

})(jQuery);


