// This goes in your Javascript
// We need to grab hold of the OBJECT in order to call the exported function
function getFlashMovieObject(movieName) {
  var movie = document.getElementById(movieName);

  if(movie.tagName.toUpperCase() == 'OBJECT')
    return movie;

  if(window.document[movieName])
    return window.document[movieName];

  alert('Error: could not find Flash object ' + movieName);
}


// Play either YouTube or Vimeo embedded video via JS API
function playWebVideo(id) {

    var flashObj = getFlashMovieObject(id);

    // Vimeo JS API
    if(typeof(flashObj.api_play) == 'function') {
      flashObj.api_play();
    }

    // Youtube JS API
    else if(typeof(flashObj.playVideo) == 'function') {
      flashObj.playVideo();
    }

    else {
      alert('Video API error: cannot play video - please click the play button on the video itself');
    }

}



function onYouTubeStateChange(state) {
    var flashObj = getFlashMovieObject('webvideo_1');

    flashObj.setPlaybackQuality('medium');
}

// This is automatically called by YouTube
function onYouTubePlayerReady(playerId) {

  // Automatically set quality to medium when it starts playing
  var flashObj = getFlashMovieObject(playerId);
  flashObj.addEventListener('onStateChange', 'onYouTubeStateChange');
}



$(function() {


  // Homepage video
  $('#playvid').click(function() {

    // Set playback quality
    var flashObj = getFlashMovieObject('webvideo_1');
    //playWebVideo('webvideo_1');
    
    // Play/pause movie
    switch(flashObj.getPlayerState()) {
      case 1:
        flashObj.pauseVideo();
        break;
      default:
        flashObj.playVideo(); 
    }

    return false;
  })
  

  
  // Vertically align images, once they're loaded
  $('#recent img').imagesLoaded(function() {
    $('#recent img').vAlign();
  });
  $('ul.productlisting img').imagesLoaded(function() {
    $('ul.productlisting img').vAlign();
  });
  $('ul#thumbnails img').imagesLoaded(function() {
    $('ul#thumbnails img').vAlign();
  });



  // Fix nav
  $('li.current,li.ancestor').parents('ul').parents('li.ancestor').addClass('parent');





  // Homepage recently acquired
  $('#recent').find('img').css({opacity : 0.9});
  $('#recent').find('img').mouseover(function() {
    var img = this;
    $('#recent_info span').text($(img).attr('alt'));
    $('#recent_info span').hide().fadeIn();
    $('#recent').find('img').css({opacity : 0.9});
    $(this).css({opacity : 1});

  })
  
  // Contact modal popup
  $('#nav ul li a').click(function() {
    if($(this).text().toLowerCase() == 'contact') {
      $.fancybox($('#contact_info'));
      return false;
    }
  })

  // Form field placeholders
  $(':input').placeholder();

  // Image magnifiers
  $('.magnifier').loupe();

  // Product gallery
  $('#productgallery ul li a').click(function() {
    $('#mainimage').attr('src', $(this).attr('href'));
    $('#mainimage').parent('a').attr('href', $(this).attr('href').replace('273', '600'));
    $('.magnifier').loupe();
    return false;
  });

  // Make product listing fade in on mouose over
  $('ul.productlisting li a').css({opacity : 0.8});
  $('ul.productlisting li a').hover(function() {
    $(this).animate({ opacity : 1});
  }, function() {
    $(this).css({opacity : 0.8});
  });

  // Hide the video
  $('#videocontainer').hide();

  // Constant Contact subscribe message - popup in fancybox
  if($('span#cc_message').text().replace(' ', '').length > 0) {
    // Timeout so it appears over video player
    window.setTimeout(function() {
      $.fancybox($('span#cc_message'));
    }, 500);
  }


  // Make video open on click
  $('a#playvideo').click(function() {

    $.fancybox($('#video'), {
      width:  640,
      height: 360
    });

    return false;
  });

});

