(function() {
var $slidePhoto= "";
var $sec_s= "";
var $= jQuery;
var index= 0;
var max= 0;
var timer= 0;
var delay= 7000;
var pagerHtml= "";
var $pager= "";
var isOriginalPager = true;
var isLoop= true;// trueの場合、最後の要素の次は最初に戻る
var isAuto= true;// trueの場合、自動的（数秒毎）に写真が切り替わる
var effect= "rotate";// 写真切り替えエフェクト

$(function() {
$slidePhoto = $("#slidePhoto");
if(effect == "rotate") {
$slidePhoto.addClass("rotate");
$(".group", $slidePhoto).width(
$slidePhoto.width() * $(".section", $slidePhoto).size()
);

$sec_s = $(".section", $slidePhoto);
$sec_s.bind("mouseover", function() {
$(".text", this).addClass("over");
});
$sec_s.bind("mouseout", function() {
$(".text", this).removeClass("over");
});
max = $sec_s.length;

$pager = $("#slidePhoto .pager");
$("[class^='page'] img", $pager).each(function(i) {
$(this)
.attr("_num", i)
.bind("click", function() {
showPhoto($(this).attr("_num"));
});
});

$pagerLink = $("#slidePhoto .pager .link a");
}

// 画像先読み
$("img").each(function() {
if($(this).attr("src").indexOf("_off.") >= 0) {
$("<img />").attr("src", $(this).attr("src").replace("_off.", "_selected."));
}
});
});

$(window).load(function() {
// 初期画像表示
showPhoto(index);
});

// 写真を表示する
function showPhoto(indexValue) {
index = Number(indexValue);

// ループする場合
if(isLoop) {
if(index >= max) {
index = 0;
} else if(index < 0) {
index = max - 1;
}
} else {
if(index >= max) {
index = max -1;
} else if(index < 0) {
index = 0;
}
}

if(effect == "normal") {
// 全部非表示
$sec_s.hide();
$("a", $pager).removeClass("selected");

// 対象のみ表示
$($sec_s[index]).show();
$("a", $pager).each(function() {
if($(this).attr("page") == index) {
$(this).addClass("selected");
return false;
}
});
} else if(effect == "rotate") {
changeNavi();

var movePosition = $("#slidePhoto").width() * index;
$("#slidePhoto .group")
.animate({
"left": - movePosition
},{"duration":500});
var href = $("a", $sec_s[index]).attr("href");
$pagerLink.attr("href", href);
}


// 自動タイマーオン
if(isAuto) {
startTimerShowPhoto();
}
}

// タイマー開始
function startTimerShowPhoto() {
clearInterval(timer);
timer = setInterval(function() {
clearInterval(timer);
showPhoto(Number(index) + 1);
}, delay);
}

// ナビの画像変更
function changeNavi() {
var src = "";

// 全てoffにする
$("[class^='page'] img", $pager).each(function(i) {
src = $(this).attr("src").replace("_selected.", "_off.");
$(this).attr("src", src);
});

// selectedにする
src = $(".page" + (index + 1)+ " img", $pager)
.attr("src").replace("_off.", "_selected.");

$(".page" + (index + 1) + " img", $pager).attr("src", src);
}

})();
