/*!
 * jQuery jcca plugin: 
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Author: HomePageShop http://www.hp-shop.co.jp
 */

;(function(jQuery) {

	jQuery.fn.jcca_all_select = function(options) {
		var options = jQuery.extend(
			{},
			jQuery.fn.jcca_all_select.defaults, 
			options
		);
		
		var selected = false;
		
		this.click(function(){
			selected = !selected;
			var target = $(this).attr("rel");
			if (target) {
				$(target).attr('checked', selected);
				return false;
			}
		});
	};
	jQuery.fn.jcca_all_select.defaults = {
		target: null,
		dummy: 0
	};

	jQuery.fn.jcca_val = function(val) {
		val = jQuery.trim(val);
		this.children('option').each(function(){
			if (val == $(this).val() || val == $(this).text()) {
				$(this).attr('selected', true);
			}
		});
	};



	jQuery.fn.jcca_main_visual = function(options) {
		var options = jQuery.extend(
			{},
			jQuery.fn.jcca_main_visual.defaults, 
			options
		);


		var index = 0;
		var container = jQuery(this);
		jQuery('li a img', container).hpsrollover({useAlpha:false});
		
		var timer = null;
		var animation = false;
		
		var changeImage = function() {
			animation = true;
			var prev =  jQuery(options.visuals[index]);
			index ++;
			if (index >= options.num_image) {
				index = 0;
			}
			var next = jQuery(options.visuals[index]);
			if (next && prev) {
				prev.fadeOut(options.speed);
				next.fadeIn(options.speed, function(){
					animation = false;
					timer = setTimeout(function(){changeImage()},options.duration);
				});
				
				
			} else {
				animation = false;
				timer = setTimeout(function(){changeImage()},options.duration);
			}
		}
		
		timer = setTimeout(function(){changeImage()},options.duration);
		
		container.click(function(){
			if (options.changeOnClick) {
				if (!animation) {
					clearTimeout(timer);
					changeImage();
				}
			}
		});
	}
	jQuery.fn.jcca_main_visual.defaults = {
		visuals: ['#main_visual_01', '#main_visual_02', '#main_visual_03'],
		duration: 5000,
		speed: 1250,
		changeOnClick: true,
		num_image: 3
	};
	
})(jQuery);

