
var DelayCall = function(_looptime, _func){

		var timer;
		var time = _looptime * 1000;
		var func = _func;
				
		
		var onComplete = function()
		{
			clearInterval(timer);
			timer = null;
			
			func.apply();
			//func = null;
		}
		
		
	//----------------------------------------------------
	// 	public method
	//----------------------------------------------------

		
		this.call = function()
		{
			timer = setInterval(onComplete, time);
		}

		
		this.cancel = function()
		{
			if(timer != null)
			{
				clearInterval(timer);
				timer = null;
				
				//func = null;
			}

		}
}







var AnaSlideShow  = function(){
	
	
//----------------------------------------------------
// 	initialize
//----------------------------------------------------
	
	var timer;
	var looptime = 6;
	var imgArray = [];
	var titleArray = [];
	
	var $mainimg	= $("div#mainimg");
	var $slideImgs  = $("#mainimg .slide");
	var $mainTitles = $("#maintitle li");

	var $CustomEvent = $({});
			
	var index = 3;
	var max = 2;
	var counter = 0;
	
	$mainimg.css('cursor', 'pointer').bind('click', onclick);
	
	
	var imgPos = [
					{x:0, y:180, url:"/manner/", 			color:"#DFDEDC"},
					{x:0, y:180, url:"/humanerror/",	 		color:"#C3C3C0"},
					{x:0, y:180, url:"/communication/", 	color:"#F1F8FF"},
					{x:420, y:110, color:"#1B52CF"}, 
					{x:0, y:120, color:"#1A1A1A"}
				 ];
	
			
                
	//ロード完了後、画像差し替え
	var mainImgArray = [
							"images/mainimg/img03.jpg",
							"images/mainimg/img04.jpg",
							"images/mainimg/img05.jpg",
							"images/mainimg/img01.jpg",
							"images/mainimg/img02.jpg"
						];
						
						
	
	var totalImages = mainImgArray.length,
		count 		= 0;
		
	var isMain = true;
	var tempindex = -1;
	var isMainToggle = false;
	
	
	
    
    $slideImgs.children().each(function(i)
    {
        $('<img/>').load(function()
        {
            count++;
            if (count >= totalImages)
            {
                $slideImgs.children().each(function(i){
                
               		$(this).attr("src", mainImgArray[i])
                	
             	})
                
                showImg();
				timer.call();
            }
        }).attr("src", mainImgArray[i]);
	});

	
	
		
	


	
	$slideImgs.each(function(i){
	
		var _this = $(this);
		_this.hide();
		imgArray.push(_this);
	
	});
	
	
	
	function onclick()
	{
		if(index < 3)
		{
			location.href = imgPos[index].url;
		}
	}
	
	
	
	$mainTitles.each(function(i){
		
		var _this = $(this);
		_this.hide();
		titleArray.push(_this);	
	});
	
	
	
	function loop()
	{
		if(isMain = !isMain)
		{	
			tempindex = index;
			$CustomEvent.trigger("EVENT_CHANGE_RO", [index]);
			
			
			if(isMainToggle = !isMainToggle)
			{
				changeImg(4);
			}
			else
			{
				changeImg(3);
			}
		
			return;	
		}

		changeImg(tempindex + 1);
		$CustomEvent.trigger("EVENT_CHANGE", [index]);
	}
	
	
	function changeImg(_newIndex)
	{
		hideImg();

		index = _newIndex;
		if(!isMain)
		{
			if(index > max)	index = 0;
		}		
		showImg();
		timer.call();
	}
    
    
    
	function showImg()
	{
		var img = imgArray[index];
		img.show();
		img.css({opacity : 0});
		img.stop().animate({opacity:1}, 1500);
		
				
		var title = titleArray[index];
		title.show();
		

		var pos = imgPos[index];
		

		title.css({	
					top 	: pos.y + 'px',
					left	: pos.x + 80 +"px"
				});
			
		title.stop().animate({left:pos.x + 'px'}, 1800, 'easeOutExpo');
		$mainimg.css('background-color',pos.color ) ;
	}
	
	
	
	
	function hideImg()
	{
		var img = imgArray[index];
		img.hide();
		
		var title = titleArray[index];
		title.hide();
	}
	
	

	var timer = new DelayCall(looptime, loop);




//---------------------------------------------------------------------------------
//	public method
//---------------------------------------------------------------------------------
	
	this.CustomEvent = $CustomEvent;
	
	this.onChange = function(_index)
    {
        timer.cancel();
        isMain = false;
        changeImg(_index);
    }
    
    
    this.play = function(){
        
        timer.call();
        //onRollOver(0);
    }
    
    
    this.stop = function(){
        
        timer.cancel();
    }	
	

}




