/* -------------------------------------------------------------------------- */
/**
 *    @fileoverview
 *       script for toppage.
 *
 *    @version 1.2.20100811
 *    @requires jquery.js
 *    @requires bajl.js
 *    @requires bajl.rollover.js
 *    @requires bajl.crossfader.js
 */
/* -------------------------------------------------------------------------- */

/* -------------------- Settings for SPR.ToppageImpression -------------------- */
SPR.settings.ToppageImpression = {
	  'expr'      : '#impression .bajl-crossfader'
	, 'wait'      : 4000
	, 'duration'  : 1200
	, 'statusSet' : {
		  'normal' : ''
		, 'hover'  : '_o'
		, 'active' : '_a'
		, 'stay'   : '_s'
	}
	, 'template'  : {
		  'image' : '<img width="950" height="248" alt="" />'
		, 'link'  : '<a class="impression-link rollover-button"><span><img src="img/index/btn_impression_01.gif" width="122" height="33" alt="事業内容" /></span></a>'
		, 'close' : '<a href="#" class="impression-close rollover-button"><span><img src="img/index/btn_close_01.gif" width="18" height="18" alt="閉じる" /></span></a>'
	}
	, 'fader'     : {
		  'autoStart' : false
		, 'interval'  : 7000
		, 'duration'  : 1600
		, 'group'     : 'div.bajl-crossfader-group'
		, 'units'     : 'div.bajl-crossfader-unit'
		, 'prevBtn'   : 'li.bajl-crossfader-prev-btn a'
		, 'nextBtn'   : 'li.bajl-crossfader-next-btn a'
		, 'selectBtn' : 'li.bajl-crossfader-select-btn a'
	}
};

/* --------------- Setup --------------- */

(function($) {

var sheets = BAJL.StyleSheets();
sheets.insertRule('#impression .bajl-crossfader .bajl-crossfader-group { visibility: hidden; }');

/* --------------- SPR.ToppageImpression --------------- */
SPR.ToppageImpression = function(node) {
	this.node       = null;
	this.group      = null;
	this.units      = null;
	this.buttons    = null;
	this.fader      = null;
	this.startTimer = null;
	this.isShown    = false;
	this.wait       = SPR.settings.ToppageImpression.wait;
	this.duration   = SPR.settings.ToppageImpression.duration;
	this.statusSet  = SPR.settings.ToppageImpression.statusSet;
	this.init(node);
}
SPR.ToppageImpression.prototype.init = function(node){
	this.node    = $(node);
	this.group   = $('<div class="bajl-crossfader-group"></div>').prependTo(this.node);
	this.buttons = this.node.find('.bajl-crossfader-btns a');
	this.units   = this.createUnits();

	this.buttons.bind('click.impression', BAJL.Delegate(this.onButtonClick, this));
	this.buttons.each(BAJL.Delegate(this.setRollover, this));

	this.fader   = this.createFader();
	this.fader.addCallback('onSelect', this.onChange, this);
	this.group.css('display', 'none').css('opacity', '0').css('visibility', 'visible');
	this.startTimer = new BAJL.Timeout(this.onStart, this.wait, this);
}
SPR.ToppageImpression.prototype.createUnits = function(){
	this.buttons.each(BAJL.Delegate(function(index, node){
		var buttonLink  = $(node);
		var buttonImage = buttonLink.find('img');

		var href   = buttonLink.attr('href');
		var alt    = buttonImage.attr('alt');
		var number = (buttonImage.attr('src').match(/btn_category_([0-9]+)/) || [])[1] || '00';
		var src    = 'img/index/pic_category_' + number + '.jpg';

		var unit   = $('<div class="bajl-crossfader-unit"></div>');

		// append image
		$(SPR.settings.ToppageImpression.template.image)
			.attr('src', src)
			.attr('alt', alt)
			.appendTo(unit)
		;

		// append link button
		if (href) {
			$(SPR.settings.ToppageImpression.template.link)
				.attr('href', href)
				.appendTo(unit)
			;
		}

		// append close button
		$(SPR.settings.ToppageImpression.template.close)
			.bind('click.impression', BAJL.Delegate(function(e){
				e.preventDefault();
				e.stopPropagation();
				this.hide();
			}, this))
			.appendTo(unit)
		;

		unit.appendTo(this.group);
	}, this));
	return this.group.find('.bajl-crossfader-unit');
}
SPR.ToppageImpression.prototype.createFader = function(){
	return new BAJL.Crossfader(this.node, SPR.settings.ToppageImpression.fader);
}
SPR.ToppageImpression.prototype.setRollover = function(index, node){
	var $node = $(node);
	var RO    = new BAJL.Rollover($node, this.statusSet);
	$node
		.data('isStay.impression', false)
		.mouseenter(function(){ RO.setStatus('hover')  })
		.mousedown( function(){ RO.setStatus('active') })
		.mouseleave(BAJL.Delegate(function(){ RO.setStatus((this.isShown && $node.data('isStay.impression')) ? 'stay' : 'default') }, this))
		.mouseup(   BAJL.Delegate(function(){ RO.setStatus((this.isShown && $node.data('isStay.impression')) ? 'stay' : 'default') }, this))
	;
}
SPR.ToppageImpression.prototype.clearTimer = function(){
	if (this.startTimer) {
		this.startTimer.clear();
		this.startTimer = null;
	}
}
SPR.ToppageImpression.prototype.stop = function(){
	if (this.fader) {
		this.fader.stop();
		this.fader.removeCallback('onSelect', this.onFirstLoop, this);
	}
}
SPR.ToppageImpression.prototype.hide = function(){
	this.isShown = false;
	this.stop();
	this.node.removeClass('pseudo-shown');
	this.group.stop().css('display', 'block').animate(
		  {
			  opacity: 0
		  }
		, this.duration
		, BAJL.Delegate(
			  function(){
				this.group.css('display', 'none');
			  }
			, this
		  )
	);
	this.onChange();
}
SPR.ToppageImpression.prototype.show = function(){
	this.clearTimer();
	this.isShown = true;
	this.stop();
	this.node.addClass('pseudo-shown');
	this.group.stop().css('display', 'block').animate(
		  {
			  opacity: 1
		  }
		, this.duration
	);
	this.onChange();
}
SPR.ToppageImpression.prototype.onButtonClick = function(e){
	var button = $(e.currentTarget);
	var group = this.group.stop();
	if (!this.isShown || !button.is('.pseudo-selected')) {
		this.show();
	} else {
		this.hide();
	}
}
SPR.ToppageImpression.prototype.onChange = function(index){
	this.buttons.each(BAJL.Delegate(function(index, node){
		var $node = $(node);
		var isStay = $node.is('.pseudo-selected');
		var RO = BAJL.Rollover.getInstance($node);
		var status = RO.getStatus();
		if (this.isShown && isStay) {
			if (status == 'default') {
				status = 'stay';
			}
		} else {
			if (status == 'stay') {
				status = 'default';
			}
		}
		$node.data('isStay.impression', isStay);
		RO.setStatus(status);
	}, this));
}
SPR.ToppageImpression.prototype.onStart = function(){
	this.clearTimer();
	this.show();
	this.fader.start();
	this.fader.addCallback('onSelect', this.onFirstLoop, this);
	this.onChange();
}
SPR.ToppageImpression.prototype.onFirstLoop = function(index){
	if (index == 0) {
		this.hide();
	}
}

/* --------------- Main --------------- */
$(function(){
	new SPR.ToppageImpression($(SPR.settings.ToppageImpression.expr));
});

})(jQuery);

