/* Gallery (fade-on-click, auto-fade) */
jQuery.fn.gallFade = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		autoSlide: 5000
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = $.browser.msie ? 0 : _options.duration;
		var _timer = _options.autoSlide;
		var _wrap = _hold.find('> li');
		var _count = _wrap.index(_wrap.filter(':last'));
		var _t;
		var _active = _wrap.index(_wrap.filter('.active'));
		if (_active < 0) _active = 0;
		var _last = _active;
		
		_wrap.css({opacity:0}).eq(_active).css({opacity:1}).css('opacity','auto');
		_wrap.removeClass('active').eq(_active).addClass('active');
		function fadeEl(){
			_wrap.eq(_last).animate({
				opacity:0
			}, {queue:false, duration: _speed});
			_wrap.eq(_active).animate({
				opacity:1
			}, {queue:false, duration: _speed, complete: function(){
				$(this).css('opacity','auto');
			}});
			_wrap.removeClass('active').eq(_active).addClass('active');
			_last = _active;
		}
		function runTimer(){
			_t = setInterval(function(){
				_active++;
				if (_active > (_count)) _active = 0;
				fadeEl();
			}, _timer);
		}
		runTimer();
	});
}

jQuery.fn.xFade = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		autoSlide: 5000
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = $.browser.msie ? _options.duration : _options.duration;
		var _timer = _options.autoSlide;
		var _wrap = _hold.find('> div.child');
		var _count = _wrap.index(_wrap.filter(':last'));
		var _t;
		var _active = _wrap.index(_wrap.filter('.active'));
		if (_active < 0) _active = 0;
		var _last = _active;
		
		_wrap.css({opacity:0}).eq(_active).css({opacity:1}).css('opacity','auto');
		_wrap.removeClass('active').eq(_active).addClass('active');

    _wrap.hover(fadeSwitch1, fadeSwitch0);

    function fadeSwitch0()
    {
      return fadeSwitch(0);
    }
    function fadeSwitch1()
    {
      return fadeSwitch(1);
    }
    function fadeSwitch(active)
    {
				//_active++;
				//if (_active > (_count)) _active = active;
				_active = active;
				fadeEl();
    }

		function fadeEl(){
			_wrap.eq(_last).animate({
				opacity:0
			}, {queue:false, duration: _speed});
			_wrap.eq(_active).animate({
				opacity:1
			}, {queue:false, duration: _speed, complete: function(){
				$(this).css('opacity','auto');
			}});
			_wrap.removeClass('active').eq(_active).addClass('active');
			_last = _active;
		}

		function runTimer(){
			_t = setInterval(function(){
				_active++;
				if (_active > (_count)) _active = 0;
				fadeEl();
			}, _timer);
		}
		//runTimer();
	});
}

$(document).ready(function()
{
  $('div#visual-p8 a').xFade(
  {
    duration: 700,
    autoSlide: 5000
  });
  $('div#visual-p6 a').xFade(
  {
    duration: 700,
    autoSlide: 5000
  });
  $('div#visual-p13 a').xFade(
  {
    duration: 700,
    autoSlide: 5000
  });

	$('ul.gallery-fade1').gallFade({
		duration: 700,
		autoSlide: 5000
	});
	$('ul.gallery-fade2').gallFade({
		duration: 700,
		autoSlide: 5000
	});
});
