/*
 
 jQuery Tools 1.2.5 / Scrollable Autoscroll

 NO COPYRIGHTS OR LICENSES. DO WHAT YOU LIKE.

 http://flowplayer.org/tools/scrollable/autoscroll.html

 Since: September 2009
 Date:    Wed Sep 22 06:02:10 2010 +0000 
*/
(function(b){var f=b.tools.scrollable;f.autoscroll={conf:{autoplay:true,interval:3E3,autopause:true}};b.fn.autoscroll=function(c){if(typeof c=="number")c={interval:c};var d=b.extend({},f.autoscroll.conf,c),g;this.each(function(){var a=b(this).data("scrollable");if(a)g=a;var e,h=true;a.play=function(){if(!e){h=false;e=setInterval(function(){a.next()},d.interval)}};a.pause=function(){e=clearInterval(e)};a.stop=function(){a.pause();h=true};d.autopause&&a.getRoot().add(a.getNaviButtons()).hover(a.pause,
a.play);d.autoplay&&a.play()});return d.api?g:this}})(jQuery);

/******************************************************************************

ANTIQUES ROADSHOW Feature Carousel 1.0
mitch_smith@wgbh.org
COPYRIGHT 2011 WGBH Educational Foundation

Date: Wed Feb  9 15:09:43 EST 2011
*/
jQuery(window).load(function() {

  // initialize captions collapse
  $(".carousel_caption_collapse").each(function(index) {
    
    //alert(index);
    var cdiv = $(this).closest('.carousel_caption');
    var slide = cdiv.parent('div');
    var url = "#";
    if ( cdiv.find('a').length == 1) {
      var url = cdiv.find('a').eq(0).attr('href');
      slide.hover(function() {
        slide.css({'cursor':'pointer'});
      },
      function() {
        slide.css({'cursor':'default'});
      });
      slide.click(function() {
        if (slide.find('.lb_launcher').html() != null) {
          slide.find('.lb_launcher img').eq(0).click();
          
          $('.lede_pause_btn').eq(0).click();
        } else {
          document.location.href = url;
        }
      });
    } else {
      var aa = cdiv.find('a');
      var w = Math.floor(671 / aa.length);
      w = w + 'px';
      cdiv.css({'height':'392px'});
      aa.each(function() {
        $(this).css({'display':'block','text-indent':'-9000px','height':'392px','width':w,'float':'left'});
      });
    }
    /*$(this).click(function() {
      var indicator = $(this).find('.indicator').eq(0);
      if (indicator.html() == '+') {
        cdiv.animate({height: 80}, 500);
        indicator.html('&ndash;');
        $(this).toggleClass('open');
      } else {
        cdiv.animate({height: 25}, 500);
        indicator.html('+');
        $(this).toggleClass('closed');
      }
    });
    $(this).click().click();
    var timeoutID = window.setTimeout(function() {
      $('.carousel_caption').css({'visibility':'visible'});
    },500); */
  });

  // initialize feature captions
  $(".captioned_feature").each(function() {
    var fcdiv = $(this).find('.feature_caption');
    var max = fcdiv.height() + 5;
    var anchortag = fcdiv.find('a').eq(0);
    var url = anchortag.attr('href');
    $(this).hover(function() {
      fcdiv.css('cursor','pointer');
      fcdiv.animate({height: max}, 300);
    },
    function() {
      fcdiv.css('cursor','default');
      fcdiv.animate({height: 25}, 300);
    });
    fcdiv.animate({height: 25}, 300);
    $(this).click(function() {
      if (anchortag.hasClass('flashpop')) {
        window.open(url,'flashwin','toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1,width=550,height=540');
      } else if (url.match('http://')) {
        window.open(url, '_blank');
      } else {
        document.location.href = url;
      }
    });
  });
  
  // initialize news tabs
  /*
  $('.news_tabs li').click(function() {
    var section = $(this).attr('rel');
    $('.news_tabs li').removeClass('active');
    $('#news_module>div').css({'display':'none'});
    $(section).css({'display':'block'});
    $(this).addClass('active');
  });
  */

  // initialize scrollable
    $(".scrollable").scrollable({ circular: false })
    .autoscroll({ interval: 8000, autoplay: true, autopause: false });
	// customize main feature carousel
	var main_feature_api = $(".scrollable").eq(0).data("scrollable");
	var play_btn = $('<img>');
	var pause_btn = $('<img>');
	var pipe = $('<img>');	
	pipe.attr({'src':'/wgbh/roadshow/homepagerebuild/images/lede_pipe.png','alt':''}).addClass("lede_pipe");
	play_btn.attr({'src':'/wgbh/roadshow/homepagerebuild/images/lede_play_btn.png','alt':'play'})
	    .addClass("lede_play_btn")
	    .click(function() {
	      $(this).hide();
	      $('.lede_pause_btn').show();
        if (main_feature_api.getIndex() == main_feature_api.getSize() - 1) {
          main_feature_api.begin(300);
        }
        main_feature_api.play();
	    });
	  $("#feature_carousel_nav").append(play_btn);
    play_btn.hide();
    pause_btn.attr({'src':'/wgbh/roadshow/homepagerebuild/images/lede_pause_btn.png','alt':'pause'})
	    .addClass("lede_pause_btn")
	    .click(function() {
	      try {
	        window.clearTimeout($(this).data('time').t);
	        }
	      catch(err) { 
	        //
	        }
	      $('.lede_play_btn').show();
	      $(this).hide();
	      main_feature_api.pause();
	    });
	  $("#feature_carousel_nav").append(pause_btn);
	  $("#feature_carousel_nav").append(pipe);
	
	for (i = 0; i < main_feature_api.getSize(); i++) {
	  var btn = $('<img>');
	  btn.attr({'src':'/wgbh/roadshow/homepagerebuild/images/lede_indicator_off.png','alt':i})
	    .addClass("slide_selector")
	    .click(function() {
	      main_feature_api.seekTo($(this).attr('alt'));
	    });
	  $("#feature_carousel_nav").append(btn);
	}	
	main_feature_api.onSeek(function() {
	  
    if (main_feature_api.getIndex() == main_feature_api.getSize() - 1) {
      var timer = window.setTimeout(function() {
        main_feature_api.begin(300);
      }, 8000);
      pause_btn.data('time',{'t':timer});
    }
	  $('.slide_selector').attr('src','/wgbh/roadshow/homepagerebuild/images/carousel_nav_link_inactive.gif');
    $('.slide_selector').eq(main_feature_api.getIndex()).attr('src','/wgbh/roadshow/homepagerebuild/images/lede_indicator_on.png');    
  });
	$('.slide_selector').eq(0).click();
	
	// Manualy controll play/pause on hover
	$('.scrollable').hover(function() {
	  $('.lede_pause_btn').click();
	},
	function() {
	  $('.lede_play_btn').click();
	});
	
	/* tests: 
	$('#partnerPlayer').load(function(){
	  $(this).mouseout(function() {
	    try {
	        var testme = $(this).contents().get(0).location.href;
          main_feature_api.play();
	      }
	    catch(nutn) {
	        var twiddle = $(this).closest('div').find('.carousel_caption_collapse').eq(0);
	        if (twiddle.hasClass('closed')) {
	          //do nothing
	        } else {
	          twiddle.click();
	        }
	      }
    })
    .mouseover(function() {
          main_feature_api.stop();      
    });
	});	*/

});

