/* ===============================================
# トップページ　スライダー
=============================================== */
jQuery(function ($) {

var o = {
  speed   : 300,
  interval: 3000
};

var $slider     = $('#slider'),
    $container  = $slider.find('div.slider-container'),
    $contents   = $container.children(),
    $firstChild = $contents.filter(':first-child'),
    $lastChild  = $contents.filter(':last-child');

var size = {
  width : $container.width(),
  height: $container.height()
};

var count = {
  min    : 0,
  max    : $contents.length,
  current: 0
};

// fix $contaienr width
$container.css({
  width      : size.width * ($contents.length + 2),
  marginLeft : -size.width,
  paddingLeft: size.width
});

// autoslide
var play, start;

play = function () {
         start = setInterval(function () {
                     slide.next();
                 }, o.interval);
       };

// hover event (stop)
$contents.hover(
  function () {
    clearInterval(start);
  },
  function () {
    play();
  }
);

// pager event
$('#slide-prev').click(function (e) {
  fnc.pager('negative', e);
});

$('#slide-next').click(function (e) {
  fnc.pager('positive', e);
});

// create pagination
// pagination event
var $pagination = $('<div/>', {'class': 'slider-pagination'});
$contents.each(function (i) {
  $('<a/>', {'href': '#'})
    .text(i + 1)
    .appendTo($pagination)
    .click(function (e) {
      e.preventDefault();
      var indexNum = i;
      clearInterval(start);
      if(indexNum > count.current) {
        slide.next(indexNum);
      } else if(indexNum < count.current) {
        slide.prev(indexNum);
      }
      play();
    });
});
$pagination.appendTo($slider);
$pagination.find('a:first-child').addClass('current');

// slider
var distance;
var slide = {

  next: function (index) {
          fnc.range(index, 'positive');
          if(count.current < count.max - 1) {
            fnc.scroll(distance);
          } else {
            $firstChild.css('left', size.width * $contents.length);
            $container.stop(true, false)
                      .animate({left: -distance}, o.speed,
                        function () {
                          $firstChild.css('left', 0);
                          $container.css('left', 0);
                        }
                      );
            count.current = -1;
          }
          fnc.counter(index, 'increment');
          fnc.pageNav(count.current);
        },

  prev: function (index) {
          fnc.range(index, 'negative');
          if(count.current > count.min) {
            fnc.scroll(distance);
          } else {
            $lastChild.css('left', -(size.width * $contents.length));
            $container.stop(true, false)
                      .animate({left: -distance}, o.speed,
                        function () {
                          $lastChild.css('left', '');
                          $container.css('left', -(size.width * ($contents.length - 1)));
                        }
                      );
            count.current = count.max;
          }
          fnc.counter(index, 'decrement');
          fnc.pageNav(count.current);
        }

};

var fnc = {

  range  : function (n, d) {
             if(n >= 0) {
               distance = size.width * n;
             } else {
               var addNum;
               if(d === 'negative') addNum = -1;
               if(d === 'positive') addNum = +1;
               distance = size.width * (count.current + addNum);
             }
           },

  scroll : function (d) {
             $container.stop(true, false).animate({left: -d}, o.speed);
           },

  counter: function (n, c) {
             if(n >= 0) {
               count.current = n;
             } else {
               if(c === 'increment') count.current++;
               if(c === 'decrement') count.current--;
             }
           },

  pageNav: function (n) {
             $pagination.children('a').removeClass('current');
             $pagination.children('a:eq(' + n + ')').addClass('current');
           },

  pager  : function (d, e) {
               if(!$container.is(':animated')) {
                 clearInterval(start);
                 if(d === 'positive') slide.next();
                 if(d === 'negative') slide.prev();
                 play();
               }
               e.preventDefault();
           }

};

// auto start
play();

// for DEMO
$contents.find('a').click(function (e) {
  e.preventDefault();
});

});

/* ===============================================
# class="imgover" の要素に、マウスオーバーで
　"_o.gif" の画像と入れ替える
=============================================== */
function initRollovers() {
	if (!document.getElementById) return
	
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {		
		if (aImages[i].className == 'imgover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_o'+ftype);

			aImages[i].setAttribute('hsrc', hsrc);
			
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			
			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}	
			
			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_o'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}
window.onload = initRollovers;
try{
	window.addEventListener("load",initRollovers,false);
}catch(e){
	window.attachEvent("onload",initRollovers);
}

/* ===============================================
# pageTop スムーズスクロール
=============================================== */
$(function(){
		// #で始まるアンカーをクリックした場合に処理
		$('a[href^=#]').click(function() {
			// スクロールの速度
			var speed = 400;// ミリ秒
			// アンカーの値取得
			var href= $(this).attr("href");
			// 移動先を取得
			var target = $(href == "#" || href == "" ? 'html' : href);
			// 移動先を数値で取得
			var position = target.offset().top;
			// スムーススクロール
      $($.browser.safari ? 'body' : 'html').animate({scrollTop:position}, speed, 'swing');
			return false;
		});
});

window.onload = function() {
	try {
	document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
} 

