// Youkebox JavaSript file youkebox_site.js
// Copyright (c) 2008-2009 YOUKEBOX.COM, All Rights Reserved.

/*global $ jQuery log pageTracker window */

var YOUKEBOXSITE = {
  show_new_youkebox_form: function () {
    $('#youkebox_creation').load('/youkeboxes/new', null, function(){ YOUKEBOXSITE.init_form('#youkebox_form', '#youkebox_creation');});
  },
  init_form: function (form_id, target_id) {
    var options = { 
            target: target_id,   // target element(s) to be updated with server response 
            success: function (data, textStatus) {
              YOUKEBOXSITE.init_form('#youkebox_form', '#youkebox_creation');
            }
        }; 
    $(form_id).submit(function() { 
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 

        $(this).ajaxSubmit(options);

        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    }); 
  },
  show_youkebox_settings_form: function (path) {
    $('#youkebox_settings').load(path, null, function() { YOUKEBOXSITE.init_form('#youkebox_settings_form', '#youkebox_settings');});
  },
  declineInvite: function (id) {
    var url = '/youkebox_invitations/' + id;
    debug('declining invite, youkebox_invitation.id: ' + id);
    $.ajax({
      type: "DELETE",
      url: url,
      data: {},
      success: function(data){
        pageTracker._trackPageview('/youkebox_invitations/' + data.id + '/decline');
      }
    });
  },
  addYoutubePreviews: function (){
    // Use each method to gain access to all youtube links
    $('a[id*="preview"]').each(function()
    {
       // Grab video ID from the url
       var videoID = $(this).attr('id').match(/preview_(.+)/);
       videoID = videoID[1];

       // Create content using url as base
       $(this).qtip(
       {
          // Create content DIV with unique ID for swfObject replacement
          content: '<div id="youtube-embed-'+videoID+'">You need Flash player 8+ to view this video.</div>',
          position: {
             corner: {
                tooltip: 'rightMiddle', // ...and position it center of the screen
                target: 'leftMiddle' // ...and position it center of the screen
             }
          },
          show: {
             when: 'click', // Show it on click...
             solo: true // ...and hide all others when its shown
          },
          hide: 'unfocus', // Hide it when inactive...
          style: {
             width: 432,
             height: 264,
             padding: 0,
             tip: true,
             border: {
               color: '#ccc'
             },
             name: 'cream'
          },
          api: {
             onRender: function()
             {
                // Setup video paramters
                var params = { allowScriptAccess: 'always', allowfullScreen: 'false' };
                var attrs = { id: 'youtube-video-'+videoID };

                // Embed the youtube video using SWFObject script
                swfobject.embedSWF('http://www.youtube.com/v/'+videoID+'&enablejsapi=1&playerapiid=youtube-api-'+videoID,
                                  'youtube-embed-'+videoID, '425', '264', '8', null, null, params, attrs);
             },

             onHide: function(){
                // Pause the vide when hidden
                var playerAPI = this.elements.content.find('#youtube-video-'+videoID).get(0);
                if(playerAPI && playerAPI.pauseVideo) playerAPI.pauseVideo();
             }
          }
       }
       ).attr('href', '#');
    });
  }
};

$(document).ready(function(){
  //YOUKEBOXSITE.init();
});
