$(document).ready(function(){

    var adItems = [
        {id:1, text: '<img src="/jp/top/images/btn_detail.gif" alt="" width="80" height="17" />', url: '#', title: '', image: '/jp/top/images/campaign/main01.jpg'},
        {id:2, text: '<img src="/jp/top/images/btn_detail.gif" alt="" width="80" height="17" />', url: '#', title: '', image: '/jp/top/images/campaign/main02.jpg'},
        {id:3, text: '<img src="/jp/top/images/btn_detail.gif" alt="" width="80" height="17" />', url: '#', title: '', image: '/jp/top/images/campaign/main03.jpg'},
        {id:4, text: '<img src="/jp/top/images/btn_detail.gif" alt="" width="80" height="17" />', url: '#', title: '', image: '/jp/top/images/campaign/main04.jpg'}
    ];
    
    for (i=0;i < adItems.length;i++){
		$('#adimage'+adItems[i].id).css('background', 'url('+adItems[i].image+') top no-repeat');
		//$('#adlink'+adItems[i].id).html(adItems[i].title);
		//$('#adlink'+adItems[i].id).attr("href", adItems[i].url);
		
		//$('#adtext'+adItems[i].id+' a').html(adItems[i].text);
		//$('#adtext'+adItems[i].id+' a').attr("href", adItems[i].url);
	}
    
    var current_index = 0;
    var t = null;

    // setup a function to show the current one and increase the index
    var doNext = function() {  
      // increment the index - if it is beyond the number of li's - reset it to 0
      
      var current_index = $('#adimages div').index($('.show'));
      if (current_index+1 >= adItems.length){
        next_index = 0;
      }else{
        next_index = current_index + 1;
      }
      
      $("ul.flashMenu li.adlink.on").removeClass('on');
      $("ul.flashMenu li").eq(next_index).addClass('on');
      
      animateAd(current_index, next_index);
    };

    // set it up to be called every 7000 ms
    t = setInterval(doNext, 7000);

    $(".flashMenu .adlink").hover(function(){
        
        $("ul.flashMenu li.adlink.on").removeClass('on');
        $(this).addClass('on');
        
        clearTimeout(t);
        // set it up to be called every 7000 ms
        //t = setInterval(resumeRotation, 10000);
        
		var current_index = $('#adimages div').index($('.show'));
		var next_index = $("ul.flashMenu li").index($(this));
		animateAd(current_index, next_index);
	}, function(){
        t = setInterval(doNext, 7000);
    });
    
    function animateAd(current_index, next_index){
	   
	   var current = $("#adimages div").eq(current_index);		 
	   var next = $("#adimages div").eq(next_index);
	   
	   if(!next.hasClass("show")){
		   //Set the fade in effect for the next image, show class has higher z-index 
		   next.css({opacity: 0.0}).animate({opacity: 1.0}, 500)
		   .addClass('show');
			
		   //Hide the current image  
		   current.animate({opacity: 0.0}, 500)
		   .removeClass('show');
           
           $("#adtexts div").eq(current_index).fadeOut("fast", function(){$("#adtexts div").eq(next_index).fadeIn("fast");});
	   }
    }
});