// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

is_facebook = false;
function make_id(str) {
    return str.toLowerCase().gsub(' ', '_');
}

var TabFunctions = {
  onclick: function(evt) { this.showTab(); return false;},
  showTab: function(evt) {
    var shown = $$('#customization-tab-content table.selected');
    if (shown[0] != null) {
      shown[0].removeClassName('selected').hide();
      $$('#createTabsList a.selected')[0].removeClassName('selected');
    }
    this.addClassName('selected');
    $(this.parentNode.getAttribute("id") + "_tab").addClassName('selected').show();
  }
};

function setupTabs() {
  /* Extend our tab li elts with our new functions */
  $$('#createTabsList li a').each(function(li) {
    Object.extend(li, TabFunctions);
/*
    if (document.location.hash.indexOf(li.parentNode.getAttribute('id')) != -1) {
        li.showTab();
    }
*/
  });
}

function setup() {
  setupTabs();
  changeTrackSelection($$('div.manageTrack')[0]);
}

function remotelyChangeTrackSelection(track_id) {
  changeTrackSelection($(track_id));
}

function changeTrackSelection(target) {
  /* What we really need here is ancestorOrSelf */
  var targetTrack = target;
    if (!targetTrack) {return;}
  if (!targetTrack.hasClassName('manageTrack')) {
    targetTrack = target.up('div.manageTrack');
  }

  target_id = targetTrack.getAttribute('id').sub('manageTrack_', '');
  currently_selected = $$('div.manageTrack div.bgSel')[0];

  if (currently_selected) {
    /* unselect the row */
    currently_selected.removeClassName('bgSel');
  }

  targetTrack.down('.manageTrackRow').addClassName('bgSel');

  /* show the appropriate edit form */
  $$('.manageTrackEdit').each(function(div) {div.hide();});
  $('edit_entry_' + target_id).show();
}

function updateManageTab(list_id) {
  recolorListEntries(list_id, 'bgOdd', 'bgEven');
  updateEditFormMargins(list_id);
}

/* Removes a row from the manage tab and hides the corresponding edit form. */
function removeListEntry(list, entry) {
  Effect.BlindUp(entry, {duration: 0.5});
  $(entry).remove();
  $('edit_entry_' + entry.sub('manageTrack_', '')).hide();
  updateManageTab(list);
}

function recolorListEntries(list_id, oddClassName, evenClassName) {
   if (!$(list_id)) { return; }

   var node = $(list_id).down('div.manageTrack');
   if (!node) { return; }

   var i = 0;
   do {
     if (i++%2 == 0) {
       node.down('div.manageTrackRow').removeClassName(oddClassName).addClassName(evenClassName);
     } else {
       node.down('div.manageTrackRow').removeClassName(evenClassName).addClassName(oddClassName);
     }
     node = node.next('div.manageTrack');
   } while(node);
}

function updateEditFormMargins(list_id) {

  /* crawl the list, tweaking the position of each edit form to match its associated row's position. */

   var rows = $$('#' + list_id + ' div.manageTrack');
   if (rows.length == 0) { return; }

   var idx = rows.length + 1;
   rows.each(function(row) {
     var topMargin = -1.8 * idx;
     var id = row.readAttribute('id').sub('manageTrack_', '');
     $('edit_entry_' + id).setStyle({marginTop: topMargin + 'em'});
     idx--;
   });
}

function spotRecorded() {
    if (is_facebook) {
      window.location.reload();
    } else {
    new Ajax.Request('/playlists/refresh', {asynchronous:true, evalScripts:true, method:'post'});
  }
}

function customizeSkin(str) {
    if (is_facebook) {
      new Ajax.Request('/locoloco/customize_theme', {asynchronous:true, evalScripts:true, method:'post', parameters:{customization: str, fb_user: fb_user, playlist_id: playlist_id}});
    } else {
      new Ajax.Request('/playlists/customize_theme', {asynchronous:true, evalScripts:true, method:'post', parameters:{customization: str}});
    }
    getMovieName("player").customizeSkin(str);
}
function switchSkin() {
    /* Call a function in the SWF. */
    getMovieName("player").switchSkin(null);
}
function updatePlaylist(url) {
    getMovieName("player").updatePlaylist(url);
}
/* This utility function resolves the string movieName to a Flash object reference based on browser type. */
function getMovieName(movieName) {
        if (navigator.appName.indexOf("Microsoft") != -1) {
                return window[movieName]
        } else {
                return document[movieName]
        }
}


/* Helper functions for ajax forms */
function disable_form_controls(form) {
    Element.extend(form);
    var submit_button = form.down('div.status input.submit');
    submit_button.src = '/images/btn_please_wait.gif';
  submit_button.disable();
  form.down('div.status img.progress_indicator').show();
}

function enable_form_controls(form_id, dont_reset_form) {
    form = $(form_id);
    submit_button = form.down('div.status input.submit');
    submit_button.enable();
    submit_button.src = '/images/btn_' + submit_button.alt + '.gif';
    form.down('div.status img.progress_indicator').hide();
    if (dont_reset_form == null) {
        form.reset();
    }
    form.down('input').focus();
}

function writeButtonPlayer(url, id, prefix, index) {
  var so = new SWFObject(url, 'player_' + id + '_' + index, '29', '29', '8', '#FFFFFF');
  so.addParam('wmode', 'transparent');
  so.addVariable('mixXML', '/playlists/button/' + id);
  so.addVariable('prefix', prefix);
  so.addVariable('autoplay', 'true');
  so.write('preview_' + id + '_' + index);
}

var pconf= { 
  UIConfig: '<config><display showCodeBox="false" /><body font="Verdana"><background frame-color="Transparent" corner-roundness="2" background-color="Transparent" /></body></config>',
 defaultContent: 'widget-content'
};
