// JavaScript Document
window.addEvent('domready', function()
{
  var currentButton = 1;
  var slideShowDriven = 1;
  var focusButton;
  var nextSlide;
  var showbuttons = $$('.feature_slideshow_button');
  var slideHolder = $('feature_slideshow_currentImage_holder');
  var resume_scone = $('resume_slideshow_scone');
  var sconeMorph = new Fx.Slide(resume_scone, {link:'chain'});
  var focusOverlay = $$('.slideshow_button_focus_overlay');
  focusOverlay.setStyle('opacity',0);
  sconeMorph.hide();
  var focusShow;
  var rolloverTitle = $('feature_slideshow_rollover_title');
  
  var playOverlay = $('slideshow_play_video_over');
  
  
  var showTitle = $('slideshow_headline');
  var titleMorph = new Fx.Morph(showTitle, {link:'chain'});
  var contentAbout = $('feature_slideshow_currentAbout');
  var aboutMorph = new Fx.Morph(contentAbout, {link:'chain'});
  

  var titleStyle = 'style="font-size:24px; color:#000000; text-decoration:none"';
  var videoAboutHeader = '';
  
  var mode = 'play';
  var firstData = $('image_data_1');
  var firstSlide = $('image_set_1');
  var firsttitle = firstSlide.name;
  var firstLink = firstData.value;
  var firstSourceURL = firstSlide.value;
  var linkTitle = $('feature_slideshow_buttons_title').get('text').indexOf('Article') == -1 ? 'watch video':'read article';
  var aboutFirstWatchTail = '<a href="'+firstLink+'">'+linkTitle+'</a>';
  
  var firstAbout = firstData.name +" "+aboutFirstWatchTail;
  
  var transistionTime = 10000;
  
  var slideImages = $$('.feature_slideshow_image_set');
  //alert(slideHolder.getPosition().x+', '+slideHolder.getPosition().y);
  
  playOverlay.setStyles({'opacity':[0.5,1], 'top':'125px', 'left':'272px'});
  
  slideImages.each(function(slideImage, i) {
    if(i > 0) {
        slideImage.fade(0);
        slideImage.setStyles({'display':'none', 'margin-top':'0px'});
    } else {
        slideImage.setStyle('margin-top', '0px');
    }
  });
/*  slideHolder.addEvents({
    'mouseenter':function(){
        playOverlay.morph({'opacity':[0.5,1]});
        playOverlay.set('src','/css/i/PlayButton270x202.png');
    },
    'mouseleave':function(){
        playOverlay.morph({'opacity':[0.5,1]})
        playOverlay.set('src','/css/i/PlayButtonRolledOver270x202.png');
    }
  }); */
  
  
  
  titleMorph.start({'opacity':0}).chain(function(){
    showTitle.set('text', firsttitle);
    showTitle.href = firstLink;
    titleMorph.start({'opacity':1, duration:3000});
  });
  aboutMorph.start({'opacity':0}).chain(function(){
    contentAbout.set('html', firstAbout);
    aboutMorph.start({'opacity':1, duration:3000});
  });
  
  var changeSlide = function(clickTarget) {  
      var whichShow = clickTarget.id.substring(clickTarget.id.indexOf('_')+1);            
      if(whichShow == currentButton) {
        // Do nothing;      
      } else if(whichShow == null) { 
        // Do nothing;
      }else {
        var targetSlide = $('image_set_'+whichShow);
        if($defined(targetSlide)) {
          var targetData = $('image_data_'+whichShow);
          var showtitle = targetSlide.name;
          var showSourceURL = targetSlide.value;
          var showLink = targetData.value;   
          var aboutWatchTail = '<a href="'+showLink+'">'+linkTitle+'</a>';
          var showAbout = targetData.name +" "+aboutWatchTail;
                                       
          slideImages.each(function(slideImage, i) {
            var target = i + 1;
            if(whichShow != target) {
                slideImage.setStyle('display', 'none');
                slideImage.fade(0);
            }
          });
          $('feature_slideshow_image_'+whichShow).setStyle('display', 'block');
          $('feature_slideshow_image_'+whichShow).morph({'opacity':1, 'border':'0px'});
          
          titleMorph.start({'opacity':0}).chain(function(){
            showTitle.set('html', showtitle);
            showTitle.href = showLink;
            titleMorph.start({'opacity':1, duration:5000});
          });
          aboutMorph.start({'opacity':0}).chain(function(){
            contentAbout.set('html', showAbout);
            aboutMorph.start({'opacity':1, duration:3000});
          });
          
          clickTarget.morph({'opacity':1, 'border':'1px solid '+channelColor[channelName]});
          //'opacity':0.6,
          $('slideshow_'+currentButton).morph({ 'border':'1px solid #ffffff'});
          currentButton = whichShow;  
          $clear(runShow);
          runShow = slideShow.periodical(transistionTime);
        }     
/*        if(focusShow == currentButton) {
            playOverlay.morph({'opacity':1});
            playOverlay.set('src','/css/i/PlayButton270x202.png');
        } else {
//            playOverlay.morph({'opacity':0.5})
            playOverlay.set('src','/css/i/PlayButtonRolledOver270x202.png');
        }*/
      }
    };
  
  
  var slideShow = function() {
      nextSlide = parseInt(currentButton) + 1;
      if(nextSlide > 5) {
        nextSlide =1;
      }
      //alert('Running Show: ' + nextSlide); 
      var nextClick = $('slideshow_'+nextSlide);  
      if($defined(nextClick)){
        changeSlide(nextClick);
      //  nextClick.fireEvent('click');
      } else {
        changeSlide($('slideshow_1'));
        //$('slideshow_1').fireEvent('click');
      }
  };
  
  playOverlay.addEvent('click', function(){
        window.location = showTitle.href;
  });
  var runShow = slideShow.periodical(transistionTime);
  /*
  $('slideshow_control').addEvent('click', function()
  {
      if(mode == 'play'){
        mode = 'pause';
        
        slideImages.each(function(slideImage, i) {
                slideImage.fade(0.8);
        });
        
        $clear(runShow);
        $('slideshow_control').morph({'background-image':'url(/css/i/PHG_Video_Transparent_play.png)'});        
      } else if(mode == 'pause') {
        mode = 'play';
        slideImages.each(function(slideImage, i) {
                slideImage.fade(1);
        });
        runShow = slideShow.periodical(transistionTime);;
        $('slideshow_control').morph({'background-image':'url(/css/i/PHG_Video_Transparent_pause.png)'});  
        slideShow.call();       
      }
  });
  */  
  showbuttons.each(function(showbutton, i) {
  
    var thumbSrc = $('image_thumb_'+(i+1)).value;
    //'opacity':0.6
    showbutton.setStyles({'background-image':'url('+thumbSrc+')'});
    if(i == 0){
      showbutton.setStyles({'opacity':1, 'border':'1px solid '+channelColor[channelName]});
    }
    showbutton.addEvents({    
        'click':function(){
            changeSlide(this);
            $clear(runShow);
            //sconeMorph.slideIn();
            //sconeMorph.start({'margin-top':'-6px'}).chain(function(){
             //   resume_scone.setStyle({'z-index':15});
            //});
            
        },
        'mouseenter':function(){
            focusOverlay.setStyle('opacity',0);
            focusShow = this.id.substring(this.id.indexOf('_')+1);
/*            if(currentButton == focusShow) {     
                playOverlay.morph({'opacity':1});
                playOverlay.set('src','/css/i/PlayButton270x202.png');
            } else {
                //playOverlay.morph({'opacity':0.5})
                playOverlay.set('src','/css/i/PlayButtonRolledOver270x202.png');
            }        */
            $('focus_overlay_'+focusShow).setStyles({'top':300+'px', 'left':(25+(84*(focusShow-1)))+'px', 'z-index':5});
            $('focus_overlay_'+focusShow).setStyle('opacity', 1);
            focusButton = focusShow;
            rolloverTitle.set('text', $('image_set_'+focusShow).name);
            rolloverTitle.setStyle('visibility', 'visible');
            
        },
        'mouseleave':function(){
        //    focusOverlay.setStyle('opacity', 0);
        }    
    });
    
    
  });
  focusOverlay.addEvents({
    'mouseleave':function(){
        focusOverlay.setStyle('opacity',0);
//        playOverlay.morph({'opacity':0.5})
//        playOverlay.set('src','/css/i/PlayButtonRolledOver270x202.png');
        rolloverTitle.setStyle('visibility', 'hidden');
    },
    'click':function(){
        $('slideshow_'+focusButton).fireEvent('click');
    }
  }); 
  
  resume_scone.addEvents({
    'mouseenter':function(){
    	this.set('src','/css/'+channelName+'/i/expertpage/PHG_ResSlideShow_Expert_CatRolledOver.png');
//        this.removeClass('scone_normal');
//        this.addClass('scone_rolled');      
    },
    'mouseleave':function(){
    	this.set('src','/css/'+channelName+'/i/expertpage/PHG_ResSlideShow_Expert_Cat.png');
//        this.removeClass('scone_rolled');
//        this.addClass('scone_normal');    
    },
    'click':function(){
        //sconeMorph.start({'z-index':1}).chain(function(){
        //    sconeMorph.start({'margin-top':'-40px'});
        //});
        //sconeMorph.toggle();
        runShow = slideShow.periodical(transistionTime);
    }
  });
});