// SLIDER
// Slideshow with JSON and jQuery
// Patrick Crowley


// FUNCTIONS

function load_slides_from(scope, limit) {

  // Prevent IE8 from loading two slideshows
  if (jQuery(".slides").length > 0) {
    return;
  }
    
  // Assign variables
  var scope = scope;
  var limit = limit;
  var params = [];
  var json_url = "/photos/json";
  
  // Build params
  if (limit > 0) {
    params.push("limit=" + limit);
  }
  
  if (location.pathname == "/") {
    params.push("status=not_home");
  }
  
  if (typeof scope === 'string') {
    if (scope != 'all') {
      if (scope.indexOf("|") > 0) {
        params.push("category=" + scope);
      } else {
        params.push("event=" + scope);
      }
    }
  }
  
  if (typeof scope === 'number') {
    params.push("category=" + scope);
  }
  
  if (params.length > 0) {
    params = "?" + params.join('&');
  }
  
  // Add empty slider markup
  load_slider_markup();
  
  // Number of slides
  var number_of_slides = 0;

  // Load JSON feed and cache
  jQuery.getJSON(json_url + params,
    function(data){
    
      // phr mod: if there are no slides hide the slideshow
      if (data.photos.length == 0) {
        jQuery('.slider').css('display','none');
      } 
      else {

		  jQuery(".slider").append('<div class="counter" data="' + data.photos.length + '">&nbsp;</div');
				
		  jQuery.each(data.photos, function(i,item) {
			var $slides = jQuery('.slides');
					
			// Update event name and caption for first slide
			if (i == 0) {
			  jQuery(".event_name").find("span").html(item.event);
			  jQuery(".caption").find("span").html(item.caption);
			}
			
			// Add new slide
			$slides.append('<li class="slide"></li>');
			
			// Save slide metadata
			var $slide = jQuery('.slides').find("li").last();
			var host = "http://www.bmi.com";
			$slide.attr("link", host + item.link);
			$slide.attr("event", item.event);
			$slide.attr("caption", item.caption);
			$slide.attr("src", host + encodeURI(item.photo));
			$slide.attr("cheat_crop", item.cheat_crop);
	
	
			// Activate first slide
			if (i == 0) {
			  var $first_slide = jQuery('.slides').find("li").first();
			  
			  $first_slide.addClass("active_slide");
			  $first_slide.append('<img src="' + host + encodeURI(item.photo) + '" />');
        // $first_slide.append('<img src="" />');
        // $first_slide.find("img").attr("src", host + encodeURI(item.photo));
        
			  // Apply cheat_crop to first slide if needed
			  if ($first_slide.attr("cheat_crop") != '') {
				jQuery($first_slide).find("img").css("margin-top", "-" + $first_slide.attr("cheat_crop") + "px");
			  }
			}
			
		  });
      }
    });
    
  // Start slideshow
  slider_show = setInterval(rotate_slides, 5000);
  
}

function advance_slides_to(direction, speed) {
  var $active_slide = jQuery('.slides li.active_slide');
  var speed = typeof(speed) != 'undefined' ? speed : 700;

  // Next slide
  if (direction == 'next') {
    var $next_slide =  $active_slide.next().length ? $active_slide.next()
      : jQuery('.slides li:first');
    
    if ($next_slide.find("img").length == 0) {
      $next_slide.append('<img src=' + $next_slide.attr("src") +  ' />');
      var $next_image = $next_slide.find("img");
      jQuery($next_image).load(function () {
        switch_to_next_slide($next_slide, $active_slide, speed);
      });
    } else {
      switch_to_next_slide($next_slide, $active_slide, speed);
    }
    
  }
  
  // Previous slide
  if (direction == 'previous') {
    var $previous_slide =  $active_slide.prev().length ? $active_slide.prev()
      : jQuery('.slides li:last');
    
    if ($previous_slide.find("img").length == 0) {
      $previous_slide.append('<img src=' + $previous_slide.attr("src") +  ' />');
      var $previous_image = $previous_slide.find("img");
      jQuery($previous_image).load(function () {
        switch_to_previous_slide($previous_slide, $active_slide, speed);
      });
    } else {
      switch_to_previous_slide($previous_slide, $active_slide, speed);
    }
  }
  
}

function load_slider_markup() {
  document.write('<div class="slider"></div>');
  jQuery('.slider').append('<div class="event_name"><span></span></div>');
  jQuery('.slider').append('<div class="navigation"></div>');
  jQuery('.navigation').append('<a href="#" class="view_photo"><span>View photo</span></a>');
  jQuery('.navigation').append('<a href="#" class="previous"></a>');
  jQuery('.navigation').append('<a href="#" class="next"></a>');
  jQuery('.slider').append('<div class="curtain"></div>');
  jQuery('.slider').append('<ul class="slides"></ul>');
  jQuery('.slider').append('<div class="caption"><span></span></div>');
}

function resize_caption_for(slide, height) {
  slide.find('.caption').css('margin-top', function(index) {
    return height - slide.find('.caption').height();
  });
}

function rotate_slides() {
  if (jQuery('.slides').children().length > 1) {
    advance_slides_to('next');
  }
}

function switch_to_next_slide(next_slide, active_slide, speed) {
  next_slide.css({opacity: 0.0})
    .addClass('active_slide')
    .animate({opacity: 1.0}, speed, function() {
      active_slide.removeClass('active_slide');
  });
  update_meta_information_for(next_slide);
}

function switch_to_previous_slide(previous_slide, active_slide, speed) {
  previous_slide.css({opacity: 1.0})
  previous_slide.addClass('active_slide');
  active_slide.css({opacity: 1.0})
    .animate({opacity: 0}, speed, function() {
      active_slide.removeClass('active_slide');
  });
  update_meta_information_for(previous_slide);
}

function update_meta_information_for(slide) {
  jQuery(".event_name").find("span").html(slide.attr("event"));
  jQuery(".caption").find("span").html(slide.attr("caption"));
  if (slide.attr("cheat_crop") != '') {
    jQuery(slide).find("img").css("margin-top", "-" + slide.attr("cheat_crop") + "px");
  }
}


// BEHAVIOR

jQuery(document).ready(function() {
  
  // Fade in first slide and nav
  jQuery(".caption").ready(function () { 
    
    // Show slide navigation on hover
    jQuery(".navigation").hover(function(){
      jQuery(this).fadeTo(300, 1.0);
     },function(){
        jQuery(this).fadeTo(500, 0);
    });

    // Jump to next slide
    jQuery(".next").click(function () {
      if (jQuery("li").is(":animated")) {
        return false;
      }
      clearInterval(slider_show);
      advance_slides_to('next', 350);
      return false;
  	});

    // Jump to previous slide
    jQuery(".previous").click(function () {
      if (jQuery("li").is(":animated")) {
        return false;
      }
      clearInterval(slider_show);
      advance_slides_to('previous', 350);
      return false;
  	});

    // Jump to image link if clicked
    jQuery(".view_photo").click(function () { 
      location.href = jQuery('.active_slide').attr('link');
    });
    
    // Animate navigation
    jQuery('.navigation a').animate({opacity: 1.0}, 1400, function() {
      jQuery('.event_name span').animate({opacity: 1.0}, 1000);
      jQuery('.caption span').animate({opacity: 0.6}, 1000);
      jQuery('.curtain').animate({opacity: 0}, 1000, function() {
        jQuery('.curtain').hide();
      });
    });
  
  });

});
