/*
 * 	Easy Slider - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/3783/jquery-plugin-easy-image-or-content-slider
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
/*
 *	markup example for $("#images").easySlider();
 *	
 * 	<div id="images">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="" /></li>
 *			<li><img src="images/02.jpg" alt="" /></li>
 *			<li><img src="images/03.jpg" alt="" /></li>
 *			<li><img src="images/04.jpg" alt="" /></li>
 *			<li><img src="images/05.jpg" alt="" /></li>
 *		</ul>
 *	</div>
 *
 */

(function($) {

	$.fn.easySlider = function(note){
	  
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			prevText: 		'',
			nextId: 		'nextBtn',	
			nextText: 		'',
			orientation:	'', //  'vertical' is optional;
			speed: 			300			
		}; 
		
		var options = $.extend(defaults, options);  
		
		return this.each(function() {  
			obj = $(this); 				
			var s = $("ul.slide > li", obj).length;
			var w = obj.width(); 
			var h = obj.height(); 
			var ts = s-1;
			
			var link_p = "";
			var link_n = "";
			var now;
			
			if(note){
				var t = note;
			}else{
				var t = 0;
			}
			now = note;
			
			linkSwitcher(now);
			
			var vertical = (options.orientation == 'vertical');
			$("ul.slide", obj).css('width',s*w);			
			if(!vertical) $("ul.slide > li", obj).css('float','left');
			
			
			$(obj).before('<div class="ctrl"><div id="'+ options.prevId +'" class="prev"><a href="javascript:void(0);" onfocus="this.blur();">'+ options.prevText + link_p +'</a></div> <div id="'+ options.nextId +'" class="next"><a href="javascript:void(0);" onfocus="this.blur();">'+ link_n + options.nextText +'</a></div></div>');
			
			$("a","#"+options.prevId).hide();
			$("a","#"+options.nextId).hide();
			
			$("a","#"+options.nextId).click(function(){		
				animate("next");
				linkSwitcher(now+1);
				if (t>=ts) $(this).fadeOut();
				$("a","#"+options.prevId).fadeIn();
			});
			
			$("a","#"+options.prevId).click(function(){		
				animate("prev");
				linkSwitcher(now-1);
				if (t<=0) $(this).fadeOut();
				$("a","#"+options.nextId).fadeIn();
			});
			
			function linkSwitcher(e){
				switch(e){
					case 0:
						link_p = "";
						link_n = "社長出身校";
						break;
					case 1:
						link_p = "業界地図";
						link_n = "本社所在地";
						break;
					case 2:
						link_p = "社長出身校";
						link_n = "社長誕生日";
						break;
					case 3:
						link_p = "本社所在地";
						link_n = "スポーツ支援";
						break;
					case 4:
						link_p = "社長誕生日";
						link_n = "";
						break;
					default:
						break;
				}
				$("a","#"+options.nextId).html(link_n + options.nextText);
				$("a","#"+options.prevId).html(options.prevText + link_p);
				now = e;
			}
			
			function animate(dir){
				if(dir == "next"){
					t = (t>=ts) ? ts : t+1;	
				} else {
					t = (t<=0) ? 0 : t-1;
				};								
				if(!vertical) {
					p = (t*w*-1);
					$("ul.slide",obj).animate(
						{ marginLeft: p }, 
						options.speed
					);				
				} else {
					p = (t*h*-1);
					$("ul.slide",obj).animate(
						{ marginTop: p }, 
						options.speed
					);					
				}
			};
			
			
			if(note != ts){
				$("a","#"+options.nextId).fadeIn();
			}
			if(note != 0){
				$("a","#"+options.prevId).fadeIn();
			}
			
		});
	  
	};

})(jQuery);