var media = {

  "video" : [
    {
      "caption" : "KGO TV 5:30pm - Local News Broadcast",
        "src" : "http://www.youtube.com/v/aiiBPut80G0&hl=en&fs=0&rel=0"
    }, {
      "caption" : "KNTV TV 7:00pm - Local News Broadcast",
        "src" : "http://www.youtube.com/v/Ks4fdCKF718&hl=en&fs=0&rel=0"
    }, {
      "caption" : "KTVU TV 8:50pm - Local News Broadcast",
        "src" : "http://www.youtube.com/v/JI8I-YLdwZ4&hl=en&fs=0&rel=0"
    }, {
      "caption" : "KTVU TV 10:00pm - Local News Broadcast",
        "src" : "http://www.youtube.com/v/v3RSKaf-C6o&hl=en&fs=0&rel=0"
    }, {
      "caption" : "KTVU TV, Part 1 - Local News Broadcast",
        "src" : "http://www.youtube.com/v/pq_4xvYc9X4&hl=en&fs=0&rel=0"
    }, {
      "caption" : "KTVU TV, Part 2 - Local News Broadcast",
        "src" : "http://www.youtube.com/v/XaSBpGEusEc&hl=en&fs=0&rel=0"
    }
  ],


//    }, {
//    "caption" : "KGO TV 7:30pm",
//    "src" : "http://www.youtube.com/v/p6Z4Aih8aQM&hl=en&fs=0&rel=0"

  
  "photo" : [
    {
      "caption" : "Demolition of collapsed building and watering down of burned area, October 18, 1989, Beach and Divisadero Streets, Marina District. [C.E. Meyer, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/005sr.jpg"
    }, {
      "caption" : "Ground view of collapsed building and burned area shown in photo 4, Beach and Divisadero, Marina District. [C.E. Meyer, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/006sr.jpg"
    }, {
      "caption" : "Crushed car near the intersection of Fifth and Townsend Streets, South of Market. [C.E. Meyer, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/003sr.jpg"
    }, {
      "caption" : "An automobile lies crushed under the third story of this apartment building in the Marina District. The ground levels are no longer visible because of structural failure and sinking due to liquefaction. [J.K. Nakata, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/009sr.jpg"
    }, {
      "caption" : "Smoldering remains of the apartment complex at the corner of Beach and Divisadero Streets, Marina District. [J.K. Nakata, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/011sr.jpg"
    }, {
      "caption" : "A search and rescue team, Marina District. [J.K. Nakata, U.S. Geological Survey] ",
      "src" : "/lomaprieta/images/012sr.jpg"
    }, {
      "caption" : "Tom Brokaw of NBC News prepares script for a live broadcast from the Marina District. [J.K. Nakata, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/013sr.jpg"
    }, {
      "caption" : "Aerial view of collapsed sections of the Cypress viaduct of Interstate Highway 880. [H.G. Wilshire, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/019sr.jpg"
    }, {
      "caption" : "Side view of support-column failure and collapsed upper deck, Cypress viaduct. [H.G. Wilshire, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/022sr.jpg"
    }, {
      "caption" : "Landslide north of Fort Funston. Slide mass contains approximately 2,830 cubic meters (3,700 cubic yards) and is 30 m (100 ft) high. Photo 35 provides an aerial view of this slide. [D.M. Peterson, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/033sr.jpg"
    }, {
      "caption" : "Aerial view of large slides north of Fort Funston. A ground perspective of this slide is shown in photo 33. [S.D. Ellen, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/035sr.jpg"
    }, {
      "caption" : "KGO radio transmission towers. [H.G. Wilshire, U.S. Geological Survey] ",
      "src" : "/lomaprieta/images/038sr.jpg"
    }, {
      "caption" : "Books and air-conditioning duct were dislodged during earthquake. [J.K. Nakata, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/040sr.jpg"
    }, {
      "caption" : "Aerial view of collapsed five-story tower, St. Joseph's Seminary. One person working in tower was killed. [H.G. Wilshire, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/042sr.jpg"
    }, {
      "caption" : "Personal messages posted on van at Emergency Center. [C.E. Meyer, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/050sr.jpg"
    }, {
      "caption" : "Many residents camped in their yards after the earthquake. [J.K. Nakata, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/049sr.jpg"
    }, {
      "caption" : "A crack system destroys driveway adjacent to summit road 0.8 km (1/2 mi) southeast of Highway 17. [J.K. Nakata, U.S. Geological Survey] ",
      "src" : "/lomaprieta/images/056sr.jpg"
    }, {
      "caption" : "House moved laterally off cement foundation. [J.K. Nakata, U.S. Geological Survey] ",
      "src" : "/lomaprieta/images/063sr.jpg"
    }, {
      "caption" : "Construction on fill and the absence of adequate shear walls ccontributed to the failure of this structure. [J.K. Nakata, U.S. Geological Survey] ",
      "src" : "/lomaprieta/images/065sr.jpg"
    }, {
      "caption" : "Rescue efforts, Pacific Garden Mall. [C.E. Meyer, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/075sr.jpg"
    }, {
      "caption" : "Searching for victims at collapsed department store, Pacific Garden Mall. [C.E. Meyer, U.S. Geological Survey] ",
      "src" : "/lomaprieta/images/077sr.jpg"
    }, {
      "caption" : "Collapsed outer wall of the Medico Dental Building, Pacific Garden Mall. [J.K. Nakata, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/080sr.jpg"
    }, {
      "caption" : "Ground shaking triggered liquefaction in a subsurface layer of sand, producing differential lateral and vertical movement in a overlying carapace of unliquified sand and silt, which moved from right to left towards the Pajaro River. This mode of ground failure, termed \"lateral spreading,\" is a principal causet of liquefaction-related earthquake damage. [S.D. Ellen, U.S. Geological Survey] ",
      "src" : "/lomaprieta/images/087sr.jpg"
    }, {
      "caption" : "Crack down front of Ford's Department Store, downtown Watsonville. Although this fracture appears minor, the building suffered significant damage. [H.G. Wilshire, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/088sr.jpg"
    }, {
      "caption" : "Structural failure of twin bridges carrying Highway 1 across Struve Slough, near Watsonville. [J.C. Tinsley, U.S. Geological Survey] ",
      "src" : "/lomaprieta/images/094sr.jpg"
    }, {
      "caption" : "Partially razed earthquake-damaged unreinforced masonry buildings in Old Town historical district, City of Salinas. [J.C. Tinsley, U.S. Geological Survey]",
      "src" : "/lomaprieta/images/103sr.jpg"
    }
  ]
  
};


$(document).ready(function() {
  
  setupMediaNav('video');
  setupMediaNav('photo');
  
  // preload images
  var photo_list = "";
  for (var i = 0; i < media['photo'].length; i ++) {
    $.preloadImages(media['photo'][i].src);
  }
    
});


function setupMediaNav(media_type) {

  var sel_media_id = 1;
  var total_media = media[media_type].length;
  var media_tag = { 
    'video' : 'embed',
    'photo' : 'img'
  };

  // insert navigation links
  $('<p id="nav-' + media_type + '"><span>' + upperCaseWords(media_type) + ' ' + sel_media_id + ' of ' + total_media + ' </span> <a id="prev" href="' + total_media + '">Prev</a> | <a id="next" href="2">Next</a></p>').insertBefore('#' + media_type);
  
  $('#nav-' + media_type + ' a').bind("click", function(e) {
    
    e.preventDefault();
    
    sel_media_id = ($(this).attr('href')) * 1; // get selected media id and convert string -> number 
    sel_media_id == total_media ? next_media_id = 1 : next_media_id = sel_media_id + 1; // next link id
    sel_media_id == 1 ? prev_media_id = total_media : prev_media_id = sel_media_id - 1; // prev link id

    var media_index = sel_media_id - 1;

    $('#nav-' + media_type + ' span').html(upperCaseWords(media_type) + ' ' + sel_media_id + ' of ' + total_media); // replace nav text
    $('#nav-' + media_type + ' a#prev').attr('href', prev_media_id); // replace prev link
    $('#nav-' + media_type + ' a#next').attr('href', next_media_id); // replace next link
    //$('#' + media_type).hide();
    $('#' + media_type).fadeOut(200, function () {
      $('#' + media_type + ' ' + media_tag[media_type]).attr('src', media[media_type][media_index].src); // replace media (video / photo)
      $('#' + media_type + ' p').html(media[media_type][media_index].caption); // replace caption
      $('#' + media_type).fadeIn(100);
    });
    
  });

}


function upperCaseWords (s) {
  r = s.substr(0, 1).toUpperCase() + s.substr(1);
  return r;
}


jQuery.preloadImages = function() {
  for(var i = 0; i < arguments.length; i++) {
    jQuery("<img>").attr("src", arguments[i]);
  }
}