/**
 * function
 * 汎用jQueryサブクラス
 * @version 1.2 スライドショー 個別に表示時間を設定できるように
 */
var $f = jQuery.sub();

(function(jQuery){
	/**
	 * slideshow
	 * スライドショー
	 */
	$f.fn.slideshow = function(options){
		var self = this;
		options = $.extend({
			defaultTimer: 8000
		}, options);

    var slides = $(self).children('li').get();
    var slideTimers = new Array();
		var slideNum = $(self).children('li').length;
		var visibleSlide = $(self).children('li:visible');
		var visibleIndex = $(self).children('li').index(visibleSlide);
    var slideTimer = visibleSlide.attr('data-slide_timer');

    if (typeof slideTimer != 'undefined' & slideTimer != '') {
      var nextTimer = slideTimer * 1000;
    } else {
      var nextTimer = options.defaultTimer;
    }

		setTimeout(function(){
      if (!$.support.opacity) {
        visibleSlide.hide();
      } else {
        visibleSlide.fadeOut('slow');
      }
			if (slideNum == visibleIndex + 1) {
        var nextSlide = $(self).children('li:first');
			} else {
        var nextSlide = visibleSlide.next('li');
			}
      nextSlide.fadeIn('slow', function(){
        $f(self).slideshow();
      });
		}, nextTimer);
  }

	/**
	 * tabContents
	 * タブ表示
	 */
	$f.fn.tabContents = function (options) {
		var self = this;
    var tabAnimateFlg = false;
		options = $.extend({
			activeClassName: 'stay'
		}, options);

		$(self).children('li:first').children('a').addClass(options.activeClassName);
		var tabAnchors = $(self).children('li').children('a').get();
		var tabContentsIds = new Array();
		for (key in tabAnchors) {
			var contentsId = $(tabAnchors[key]).attr('href');
			tabContentsIds[key] = contentsId;
			if (key != 0) $(contentsId).hide();
		}

		$(this).find('a:not(.'+options.activeClassName+')').live('click', function(e){
      if (tabAnimateFlg == false) {
  			$(e.target).parent('li').siblings().children('.'+options.activeClassName).removeClass(options.activeClassName);
  			$(e.target).addClass(options.activeClassName);
  			for (key in tabContentsIds) {
  				if ($(tabContentsIds[key]+':visible').length > 0) {
            tabAnimateFlg = true;
  					$(tabContentsIds[key]).hide(0, function(){
  						var contentsId = $(e.target).attr('href');
  						$(contentsId).fadeIn('fast', function(){
                tabAnimateFlg = false;
              });
  					});
  					break;
  				}
  			}
			}
			return false;
		});
	}

	/**
	 * displaySideMenu
	 * サイドメニューの表示状態を調整
	 */
	$f.fn.displaySideMenu = function (key) {
    var self = this;
    $(self).find('ul').hide(0, function(){
      if (typeof key != 'undefined' & key != '') {
        var target = $(self).find('#'+key);
        if (target.length) target.parents('ul').show();
      }
    });
	}

	/**
	 * setFontSize
	 * 文字サイズをセット
	 */
	$f.fn.setFontSize = function (style) {
		if (!style == 'large' & !style == 'small' & !style == 'default') {
			var style = 'default';
		}
		var font_css = 'http://www.shiroyama-g.co.jp/functions/styles/font/'+style+'.css';
		$("#jstyle").attr('href', font_css);
		$('#header .font_size li.selected').removeClass('selected');
		$('#header .font_size li.'+style).addClass('selected');
		return this;
	}

	/**
	 * changeFontSize
	 * 文字サイズを変更
	 */
	$f.fn.changeFontSize = function (options) {
		var self = this;
		options = $.extend({
			cookieName: 'style'
		}, options);

		self.find('a').click(function(){
			var font_class = $(this).parent('li').attr('class');
			$.cookie(options.cookieName, font_class, {expires:30,path:'/'});
			$f().setFontSize(font_class);
			return false;
		});
		return this;
	}
})(jQuery);
