// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


function getLang() {
  var cookies = document.cookie.split("; ");
  for(var i = 0; i < cookies.length; i++) {
    var cookie = cookies[i].split("=");
    if(cookie[0] == "lang") {
      return cookie[1].split("_")[0];
    }
  }
}

var language = getLang();

function _(s) {
  if (typeof(i18n)!='undefined' && i18n[s] && language == "es") {
    return i18n[s];
  }
  return s;
}

var maxUploadFields = 5

function gallery_show(id, photos) {
  var imgIndex = 0;
  var onBefore = function(curr, next, opts) {
    if(!opts.addSlide)
      return;

    if(imgIndex < photos.length) {
      opts.addSlide('<a href="/galleries/' + id + '/photos" title="" style="top: 0pt; left: 0pt"><img src=' + photos[imgIndex] + ' class="imgborder" alt="album image" title="" /></a>'); 
      imgIndex++;
    } else {
      opts.addSlide = null;
      return;
    }
  };

  $("#gallery" + id).cycle({
    fx:     'fade',
    speed:   1500,
    timeout: 3000,
    before: onBefore
  });
}

function onAfter() {
  if(this.alt != '') {
    $('#caption').html(this.alt);
    $('#caption').fadeIn("slow");
  }
  else {
    $('#caption').fadeOut("slow");
  }
  $('#output').html(this.id);
}

function pause() {
  $('#gallery').cycle('pause');
  $('.pause').hide();
  $('.play').show();
}

function resume() {
  $('#gallery').cycle('resume');
  $('.pause').show();
  $('.play').hide();
}

function slideshow() {
  $('#gallery').cycle({
    fx:      'shuffle',
    shuffle: {
      top:   -200,
      left:  -200
    },
    speed:   1500,
    timeout: 8000,
    next:    '#gallery',
    //pause:   1,
    after:   onAfter
  });
  $('#spinner').hide();
}

function addFileField() {
  var len = $('p.upload_field').length
  if(len < maxUploadFields) {
    $('p.upload_field:last').after(
      "<p class='upload_field'><label>Image</label>\
      <input id='attachment_data[]' name='attachment_data[]' size='30' type='file' />\
      <a href='#' onclick='removeFileField(); return false;'>Remove</a></p>");
    $('#caption').hide();
    if(len + 1 >= maxUploadFields) {
      $('#addlink').hide();
      $('#limit').show();
    }
  }
  else {
    $('#addlink').hide();
    $('#limit').show();
  }
  //<p><label for='description'>Caption</label> <textarea cols='40' id='photo_caption' name='photo[caption]' rows='20'></textarea></p>\<p><label for='tag_list'>Tags (comma separated)</label> <input id='photo_tag_list' name='photo[tag_list]' size='30' type='text' /></p>");
}

function removeFileField() {
  var elems = $('p.upload_field');
  if(elems.length > 1) {
    $('#addlink').show();
    $('#limit').hide();
    $('p.upload_field:last').remove();
    if(elems.length == 2)
      $('#caption').show();
  }
}

function checkUsers(sel, permissions, permission) {
  var option = sel.find("option:selected");
  //var html = option.html();
  var html = permission;
  $('#permission_name').val('');

  $('[id^=friend_]').attr('checked', '');
  $('[id^=permission_]').attr('checked', '');


  var p = $('#permission_' + permission);
  if(typeof permission == 'number' && p.length > 0) {
    p.attr('checked', 'checked');
  } else if(typeof permissions[html] != 'undefined') {
    var users = permissions[html].split(",");
    for(var i = 0; i < users.length; i++) {
      $('#friend_' + users[i]).attr('checked', 'checked');
    }
    //$('#permission\\[name\\]').val(html);
  }
  
  if(option.val() == 0) 
    $("#users").show();
  else
    $("#users").hide();
}

function check_tos() {
  $('#tos_error').hide();
  if(!$('#accept').attr("checked")) {
    $('#tos_error').show();
    $('#spinner').hide();
    return false;
  } else
    return true;
}

function check_age() {
  if(!$('#age_accept').attr("checked")) {
    $('#age_error').show();
    return false;
  } else
    return true;
}

function autocomplete(field, data, multiple) {
  field.autocomplete(data,
		{multiple: multiple, width: 300, scroll: true, matchContains: true,
			formatItem: formatItem, formatResult: formatResult}
  );
}

function check_birthday(id, allow_all_blank) {
  var blank = false;
  for(var i = 1; i < 4; i++) {
    select = $('#' + id + '_' + i + 'i option:selected');
    if(select.val() == "") {
      blank = true;
      if(!allow_all_blank) {
        $('#birthday_err').show();
        return false;
      }
    }
    else if (select.val() != "" && blank) {
      $('#birthday_err').show();
      return false;
    }
  }
  return true;
}

function formatItem(row) {
  return row.name + " (" + row.login + ")";
}
function formatResult(row) {
  return row.login;
}

function toggle(index) {
  $('form:eq(' + index + ')').slideToggle("slow");
  toggle_arrows(index);
}

function toggle_arrows(index) {
  $('img.open:eq(' + index + ')').toggle();
  $('img.close:eq(' + index + ')').toggle();
}

function add_field(element, text, p_class) {
  p_class = typeof(p_class) != 'undefined' ? p_class : ''
  var paragraph = '<p class="' + p_class + '" id="' + element + '"><label>&nbsp;</label>';
  var remove = '<a class="formlnk" title="" onclick="$(this).parent().remove(); return false;" href="#">remove</a>'
  $("p[id^='" + element + "']:last").after(paragraph + text + remove + "</p>");
}

function editable(path, authenticity) {
  $('.editable_section').editable(path, {
    id: 'element_id',
    submit: 'OK',
    method: 'PUT',
    submitdata: {
      authenticity_token: authenticity
    }
  });

  $('.editable_text').editable(path, {
    id: 'element_id',
    submit: 'OK',
    type: 'textarea',	
    height: 50,
    method: 'PUT',
    submitdata: {
      authenticity_token: authenticity
    }
  });

  $('.editable_section').attr('onmouseover', "$(this).css('background-color', 'yellow')");
  $('.editable_section').attr('onmouseout', "$(this).css('background-color', 'white')");
  $('.editable_text').attr('onmouseover', "$(this).css('background-color', 'yellow')");
  $('.editable_text').attr('onmouseout', "$(this).css('background-color', 'white')");
}

function reset_task_form(id) {
  $('#popup_toggle_' + id).show();
  $('#popup_details_' + id).hide(); 
  $('#new_task').clearForm(); 
  //var instance = tinyMCE.getInstanceById('task_extra_details_' + id);
  tinyMCE.execCommand('mceRemoveControl', true, 'task_extra_details_' + id);
  $('#task_form_' + id).slideToggle(function() { 
      $('#task_main_' + id).show();
      $('#last:hidden').show();
  });
}

function add_mce(id) {
  if(tinyMCE.getInstanceById(id) == null)
    tinyMCE.execCommand('mceAddControl', true, id);
}
