
/*--------------------------
 * インターバル
 *--------------------------*/
(function(window){

function AInterval()
{
    if (onleft == 1 || onright == 1) return;
	slide.update();
    isupdate--;
}

AInterval.interval = 1200;

window.AInterval = AInterval;
}(window));



/*--------------------------
 * itune slide
 *--------------------------*/
(function(window){

function ITuneSlide(images, imagenames) {
	this.initialize(images, imagenames, imagelinks);
}
ITuneSlide.fadeTime = 600;
ITuneSlide.slideTime = 600;

var p = ITuneSlide.prototype;
	
	p.b;
	p.s;
	
	p.initialize = function(images, imagenames, imagelinks)
	{
		if (!(images.length >= 5))
		{
			console.log("images less");
			return;
		}
		this.b = new BigThumb(images, imagenames, imagelinks);
		this.s = new SmallThumb(images, imagenames, imagelinks);
		
	}
	
	p.update = function()
	{
	    if (updatenow == 1 || isupdate == 1) return;
		this.b.update(ITuneSlide.fadeTime);
		this.s.update(ITuneSlide.slideTime);
	}
	p.updatebt = function()
	{
	    if (updatenow == 1) return;
	    isupdate = 1;
		this.b.update(ITuneSlide.fadeTime);
		this.s.update(ITuneSlide.slideTime);
	}
	p.redatebt = function()
	{
	    if (updatenow == 1) return;
	    isupdate = 1;
		this.b.redate(ITuneSlide.fadeTime);
		this.s.redate(ITuneSlide.slideTime);
	}
	
window.ITuneSlide = ITuneSlide;
}(window));



/*--------------------------
 * 左サムネイル
 *--------------------------*/
(function(window){

function BigThumb(images, imagenames, imagelinks) {
	this.initialize(images, imagenames, imagelinks);
}

var p = BigThumb.prototype;
	
	p.i;
	p.parent;
	p.images;
	p.imagenames;
	
	p.initialize = function(images, imagenames, imagelinks)
	{
		this.parent = $(".itune-slide").eq(0).find(".itune-big");
		this.images = images;
		this.imagenames = imagenames;
		this.imagelinks = imagelinks;
		this.setup();
	}
	
	p.setup = function()
	{
		this.i = 0;
		
		this.parent.eq(0).append(this.makeIMG(this.i));
	}
	
	p.update = function(fadeTime)
	{
		fadeTime = fadeTime || 'slow';
		
		this.i++;
		
		if (this.i >= this.images.length) this.i = 0;
		
		var that = this;
		
		var func = function()
		{
			that.parent.eq(0).find("img").each(function(i)
			{
				if (i < that.parent.eq(0).find("img").length - 2)
				{
					$(this).remove();
				}
			});
		}
		
		this.parent.eq(0).append(this.makeIMG(this.i));
		this.parent.eq(0).find("img:last").hide().fadeIn(fadeTime, func);
	}
	p.redate = function(fadeTime)
	{
		fadeTime = fadeTime || 'slow';
		
		this.i--;
		
		if (this.i < 0) this.i = this.images.length - 1;
		
		var that = this;
		
		var func = function()
		{
			that.parent.eq(0).find("img").each(function(i)
			{
				if (i < that.parent.eq(0).find("img").length - 2)
				{
					$(this).remove();
				}
			});
		}
		
		this.parent.eq(0).append(this.makeIMG(this.i));
		this.parent.eq(0).find("img:last").hide().fadeIn(fadeTime, func);
	}
	
	p.makeIMG = function(i)
	{
		return $("<a href='"+this.imagelinks[i]+"'><img src='"+this.images[i]+"' alt='"+this.imagenames[i]+"'/></a>");
	}
	
window.BigThumb = BigThumb;
}(window));



/*--------------------------
 * 右サムネイル
 *--------------------------*/
(function(window){

function SmallThumb(images, imagenames, imagelinks) {
	this.initialize(images, imagenames, imagelinks);
}

var p = SmallThumb.prototype;
	
	p.w = 197;
	p.i;
	p.images;
	p.imagenames;
	p.imageindex = [];

	p.initialize = function(images, imagenames, imagelinks)
	{
		this.parent = $(".itune-slide").eq(0).find(".itune-small-wrap");
		this.images = images;
		this.imagenames = imagenames;
		this.imagelinks = imagelinks;
		this.setup();
	}
	
	p.setup = function(slideTime)
	{
		this.i = 1;
		for (var i = 0; i < this.images.length - 1; i++)
		{
			this.parent.eq(0).append(this.makeIMG(this.i));
			this.i++;
			this.parent.eq(0).find("img:last").css("left", (i) * this.w);
		}
		this.parent.eq(0).append(this.makeIMG(0));
		this.i++;
		this.parent.eq(0).find("img:last").css("left", (i) * this.w);
		
		this.i = 1;
		
		for (var i = 0; i < this.images.length - 1; i++)
		{
		    this.imageindex[i] = this.parent.eq(0).find("img:eq("+i+")");
		}
		this.imageindex[this.imageindex.length] =  this.parent.eq(0).find("img:last");
	}
/*
	p.update = function(slideTime)
	{
	    updatenow = 1;
		slideTime = slideTime || "slow";
		
		var that = this;
		
		var func = function()
		{
			var clone = that.parent.eq(0).find("img:first").clone();
			
			that.parent.eq(0).find("img:first").remove();
			
			lastLeft = that.parent.eq(0).find("img:last").position().left;
			
			if (Math.floor(Math.abs(lastLeft) / that.w) != (Math.abs(lastLeft) / that.w))
    		{
    		    lastLeft = (Math.round(Math.abs(lastLeft) / that.w) * that.w) * ((lastLeft < 0)? -1:1);
    		}
			that.parent.eq(0).append(that.makeIMG(that.i));
			that.parent.eq(0).find("img:last").css("left", lastLeft + that.w)
			
			that.i++;
			
			if (that.i >= that.images.length) that.i = 0;
			updatenow = 0;
		}
		
		var left = this.parent.eq(0).position().left - this.w;
		if (Math.floor(Math.abs(left) / this.w) != (Math.abs(left) / this.w))
		{
		    left = (Math.round(Math.abs(left) / this.w) * this.w) * ((left < 0)? -1:1);
		}
		
		this.parent.eq(0).animate({'left' : left}, slideTime, func)
	}
*/

	p.update = function(slideTime)
    {
        updatenow = 1;
		slideTime = slideTime || "slow";
		
		var that = this;
		
		var func = function()
		{
			var clone = that.imageindex[0].clone();
			that.imageindex[0].remove();
			
			for (var i = 0; i < that.imageindex.length; i++)
			{
			    if (that.imageindex[i + 1])
			    {
			        that.imageindex[i] = that.imageindex[i + 1];
			    }
			}
			
			lastLeft = that.imageindex[that.imageindex.length - 1].position().left;
			
			if (Math.floor(Math.abs(lastLeft) / that.w) != (Math.abs(lastLeft) / that.w))
    		{
    		    lastLeft = (Math.round(Math.abs(lastLeft) / that.w) * that.w) * ((lastLeft < 0)? -1:1);
    		}
    		
			that.parent.eq(0).append(that.makeIMG(that.i));
			that.imageindex[that.imageindex.length - 1] = that.parent.eq(0).find("img:last");
			
			that.imageindex[that.imageindex.length - 1].css("left", lastLeft + that.w)

			that.i++;
			
			if (that.i >= that.images.length) that.i = 0;
			updatenow = 0;
		}
		
		var left = this.parent.eq(0).position().left - this.w;
		if (Math.floor(Math.abs(left) / this.w) != (Math.abs(left) / this.w))
		{
		    left = (Math.round(Math.abs(left) / this.w) * this.w) * ((left < 0)? -1:1);
		}
		
		this.parent.eq(0).animate({'left' : left}, slideTime, func)
	}
	p.redate = function(slideTime)
	{
	    updatenow = 1;
		slideTime = slideTime || "slow";
		
		var that = this;
        lastLeft = this.imageindex[0].position().left;
			
		if (Math.floor(Math.abs(lastLeft) / this.w) != (Math.abs(lastLeft) / this.w))
    	{
    	    lastLeft = (Math.round(Math.abs(lastLeft) / this.w) * this.w) * ((lastLeft < 0)? -1:1);
    	}
		
		this.imageindex[this.imageindex.length - 1].css("left", lastLeft - this.w);
		var tm = this.imageindex[this.imageindex.length - 1];
		
		var func = function()
		{
		    for (var i = that.imageindex.length - 1; i > 0; i--)
			{
			    if (that.imageindex[i - 1])
			    {
			        that.imageindex[i] = that.imageindex[i - 1];
			    }
			}
			that.imageindex[0] = tm;
            updatenow = 0;
            
            that.i--;
            if (that.i < 0) that.i = that.images.length - 1;
		}
		
		var left = this.parent.eq(0).position().left + this.w;
		if (Math.floor(Math.abs(left) / this.w) != (Math.abs(left) / this.w))
		{
		    left = (Math.round(Math.abs(left) / this.w) * this.w) * ((left < 0)? -1:1);
		}
		
		
		this.parent.eq(0).animate({'left' : left}, slideTime, func)
	}
	
	p.makeIMG = function(i)
	{
		return $("<a href='"+this.imagelinks[i]+"'><img class='"+i+"' src='"+this.images[i]+"' alt='"+this.imagenames[i]+"' /></a>");
	}
	
window.SmallThumb = SmallThumb;
}(window));


//=========================================
// slider buttons
//=========================================

var ie  = (document.all)? true:false;
var updatenow = 0;
var isupdate = 0;
var onleft = 0;
var onright = 0;

function initbuttons()
{
  $(".bleft").fadeOut("normal");
  $(".bright").fadeOut("normal");
  
  $(".itune-big").mousemove(function(e)
  {
    var gx,gy,lx,ly,lw,lh,rx,ry,rw,rw;
    gy = gmy(e) - $(".itune-big").offset().top;
    gx = gmx(e) - $(".itune-big").offset().left;

    lx = parseInt($(".bleft").css("left"));
    ly = parseInt($(".bleft").css("top"));
    lw = $(".bleft").width();
    lh = $(".bleft").height();
    rx = parseInt($(".bright").css("left"));
    ry = parseInt($(".bright").css("top"));
    rw = $(".bright").width();
    rh = $(".bright").height();
    
    
    if ((gx >= lx - 10 && gx <= lx + lw + 159))
    {
      $(".bleft").fadeIn("normal");
      onleft = 1;
    }
    else
    {
      $(".bleft").fadeOut("normal");
      onleft = 0;
    }
    
    
    if ((gx >= rx - 159 && gx <= rx + rw + 10))
    {
      $(".bright").fadeIn("normal");
      onright = 1;
    }
    else
    {
      $(".bright").fadeOut("normal");
      onright = 0;
    }
    
  });
}
function gmx(e)
{
  var doc = document.getElementsByTagName("body")[0];
  if (ie)
  {
    return doc.scrollLeft + event.clientX;
  }
  else
  {
    return e.pageX;
  }
}
function gmy(e)
{
  var doc = document.getElementsByTagName("body")[0];
  if (ie)
  {
    return doc.scrollTop + event.clientY;
  }
  else
  {
    return e.pageY;
  }
}
function Timer(name, data, wait, func)
{
  var DEFAULT_INTERVAL = 18;

  this.name     = name;
  this.data     = data;
  this.wait     = wait;
  this.index    = 0;
  this.timer    = 0;
  this.current  = -1;
  this.interval = DEFAULT_INTERVAL;
  this.exec     = func;
  
  this.play = function()
  {
    clearTimeout(this.timer);
    var tm = this;
    if (this.flag == 0 && this.current > -1)
    {
      this.exec(this);
      this.timer = setTimeout(function () { tm.play(); }, this.interval);
    }
    else if (this.current == -1)
    {
      this.current++;
      this.flag  = 0;
      this.timer = setTimeout(function () { tm.play(); }, this.wait);
    }
  }
}

