// ----------------------------------------------------------------------
//
// Cityhill Banner
// @author Lab-art http://www.lab-art.co.jp/
// 06.17.2011 Script created
// Requires jQuery 1.2.6 or higher, and vgrid
//
// ----------------------------------------------------------------------


(function(jQuery)
{
jQuery.fn.banner = function(params)
{
var prms;
prms = jQuery.extend(jQuery.fn.banner.defaults, params);

var wrapper;
var main;

var num = 0;
var duration = prms.duration * 1000;
var delay = prms.delay * 1000;

var mainList;
var thumbList;

var timer;


// ----------------------------------------------------------------------
// イメージ切り替えを設定
// ----------------------------------------------------------------------

function setInteractive()
{
var i = 0;
for(i; i<num; i++)
{
var t = jQuery(thumbList.current());
t.id = i;
t.bind("mouseover", {id:t.id}, setImage);
t.mouseout(function(){ timer = setInterval(nextImage, delay); });
thumbList.next();

var m = jQuery(mainList.current());
m.mouseover(function(){ clearInterval(timer); });
m.mouseout(function(){ timer = setInterval(nextImage, delay); });
mainList.next();
}
mainList.setIndex(0);
thumbList.setIndex(0);

timer = setInterval(nextImage, delay);
}

function setImage(event)
{
clearInterval(timer);

var index = event.data.id;


if(index != mainList.getIndex()){

var cnt = 0;
wrapper.find(".navInner ul img").each(function(){
if(cnt < num) {
$(this).css("opacity","1");
cnt++;
}
});

mainList.setIndex(index);
thumbList.setIndex(index);

jQuery(thumbList.current())
.css({opacity: 0.5})

jQuery(mainList.current())
.css({
display: "none"
})
.appendTo(main)
.animate({
opacity: "show"
},{
duration: 300
});
}
}

function nextImage()
{
var cnt = 0;
wrapper.find(".navInner ul img").each(function(){
if(cnt < num) {
$(this).css("opacity","1");
cnt++;
}
});

jQuery(mainList.loopNext())
.css({
display: "none"
})
.appendTo(main)
.stop()
.queue([])
.animate({
opacity: "show"
},{
duration: duration
});

thumbList.setIndex(mainList.getIndex());
jQuery(thumbList.current())
.css({opacity: 0.5});
}


// ----------------------------------------------------------------------
// コレクション
// ----------------------------------------------------------------------

function ElementList()
{
var _list = [];

this.add = function(element){
_list.push(element);
}

this.getElementAt = function(index){
return _list[index];
}

this.length = function(){
return _list.length;
}

this.createIterator = function(){
return new ElementIterator(this);
}
}


// ----------------------------------------------------------------------
// イテレータ
// ----------------------------------------------------------------------

function ElementIterator(collection)
{
var _collection = collection;
var _index = 0;

this.setIndex = function(index){
_index = index;
}

this.getIndex = function(index){
return _index;
}

this.getElementAt = function(index){
return  _collection.getElementAt(index);
}

this.current = function(){
return _collection.getElementAt(_index);
}

this.currentNext = function(){
return _collection.getElementAt(_index + 1);
}
this.currentPrev = function(){
return _collection.getElementAt(_index - 1);
}

this.next = function(){
if(this.hasNext()){
return _collection.getElementAt(++_index);
}
}
this.prev = function(){
if(this.hasPrev()){
return _collection.getElementAt(--_index);
}
}

this.hasNext = function(){
return _index < _collection.length() - 1;
}
this.hasPrev = function(){
return _index > 0;
}

this.currentLoopNext = function(){
if(this.hasNext()){
return this.currentNext();
}else{
return _collection.getElementAt(0);
}
}
this.currentLoopPrev = function(){
if(this.hasPrev()){
return this.currentPrev();
}else{
return _collection.getElementAt(_collection.length() - 1);
}
}

this.loopNext = function(){
if(this.hasNext()){
return this.next();
}else{
_index = 0;
return this.current();
}
}
this.loopPrev = function(){
if(this.hasPrev()){
return this.prev();
}else{
_index = _collection.length() - 1;
return this.current();
}
}
}


// ----------------------------------------------------------------------
// 要素抽出
// ----------------------------------------------------------------------

return this.each(function()
{
wrapper = jQuery(this);
main = wrapper.find("#ad_rotation");

var mainCollection = new ElementList();
mainList = mainCollection.createIterator();

var thumbCollection = new ElementList();
thumbList = thumbCollection.createIterator();

wrapper.find("#ad_rotation .pr_section").each(function(){
mainCollection.add(this);
num++;
});

var cnt = 0;
wrapper.find(".navInner ul img").each(function(){
if(cnt < num) {
thumbCollection.add(this);
cnt++;
}
});

main.css("visibility","visible").append(mainList.current());

jQuery(thumbList.current())
.css({opacity: 0.5});

setInteractive();
});

}


// ----------------------------------------------------------------------
// デフォルト設定
// ----------------------------------------------------------------------

jQuery.fn.banner.defaults =
{
duration: 1,
delay: 3
}

})(jQuery);
