var Moderator = {
  _abused_once : false,
  abuse : function(id, url) {
    if (!this._abused_once && !confirm("Мы проверим этот файл и, в случае необходимости, удалим.\n\n Продолжить?"))
    {
      return false 
    }
    this._abused_once = true
    if ($("row-"+id)) $("row-"+id).remove() 
    var ajr = new Ajax.Request(url, {method: 'get'})
    return false;
  },
  curr_id : null,
  show : function(id) {
//    window.status = "show! " + Math.random()
  //  if (id != this.curr_id) {
  //    this.curr_id = id
   //   new Effect.Appear(id)
   // }
    $(id).show()
  },
    
  hide : function(id) {
    $(id).hide()
    
    //window.status = "hide! " + Math.random()
    //if (id == this.curr_id) {
    //  this.curr_id = id
    //  new Effect.Fade(id)
    //}
  },
  
  verify_age : function(id) {
    $$("tr.video-row").each(function(e){
      e.show()
    }) 
    $$("tr.age-alert").each(function(e){
      e.remove()
    })
  },
  discard_porn : function(id) {
    $$("tr.age-alert").each(function(e){
      e.remove()
    })
  }
}



var Manager = {
  start_spinning: function(id)
  {
    $('manage-links-' + id).hide()
   // discards focus $('spinner-' + id).show()
  }
};


function more(id){
  $('moreL-'+id).hide()
  $('moreB-'+id).show()
  return false;
}

function less(id){
  $('moreL-'+id).show()
  $('moreB-'+id).hide()  
  return false;
}



var PanelSelector = {
  sel : function (to, from, ajax_url) {
    $('switch-' + to).addClassName("sel")
    $('panel-' + to).show()
    from.each(function(e){  
      $('switch-' + e).removeClassName("sel")
      $('panel-' + e).hide()
    })
    if (ajax_url)
    {
      new Ajax.Updater('panel-' + to, ajax_url, {parameters: { _method: "get" }})
    }
    return false;
  }
};




var Placeholder = {
  create : function(id, blurred, focused) {
    var el = $(id);
    if(!el) return;

    var ph = el.getAttribute("placeholder");
    if( ph && ph != "" ) {
      el.value = ph;
      el.style.color = blurred;
      el.is_focused = 0;
      el.focused_color = focused;
      el.blurred_color = blurred;
      el.onfocus = this.onfocus;
      el.onblur  = this.onblur;
    }
  },
  
  onfocus : function() {
    if(this.is_focused) return true;
    this.is_focused = true;
    this.value = '';
    this.style.color = this.focused_color;
  },
  
  onblur : function() {
    if(!this.is_focused || this.value != "") return true;
    this.is_focused = false;
    this.value = this.getAttribute("placeholder");
    this.style.color = this.blurred_color;
  }
};

var UsersController = {
  profile_avatar: function(link) {
    var url = link.href
    link.replace('<span id="loading-avatar-edit-link">Идёт загрузка&hellip;</span>');
    new Ajax.Request(url, {
      method: 'get',
      onSuccess: function(transport) {        
        //$('loading-avatar-edit-link').replace(this.link.outerHTML);
        $('personal').nextSiblings().each(
          function(e) { e.remove(); }
        );
        $('personal').replace(transport.responseText);
      }.bind(this)
    });
  },
  profile_settings: function(link) {
    var url = link.href
    link.replace('<span id="loading-settings-edit-link">Идёт загрузка&hellip;</span>');
    new Ajax.Request(url, {
      method: 'get',
      onSuccess: function(transport) {        
        //$('loading-avatar-edit-link').replace(this.link.outerHTML);
        $('personal').nextSiblings().each(
          function(e) { e.remove(); }
        );
        $('personal').replace(transport.responseText);
      }.bind(this)
    });
  }
  
}

var ThumbnailsLister = Class.create();
ThumbnailsLister.prototype = { 
  initialize: function(lister_id, size)
  {
    this.lister_id = lister_id;
    this.panel = $$('#'+lister_id+' .thumbnails-panel')[0];
    var arrows = $$('#'+lister_id+' .lister a');
    this.link_rewind  = arrows[0];
    this.link_forward = arrows[1];
    this.size = size;
    this.skip = 0;
  },
  rewind: function(link)
  {
    if (this.skip <= 0) return false;
    this.skip -= this.size;
    this.link_rewind.style.display = this.skip > 0 ? 'inline':'none';
    this._update(link.href, this.skip, 'left');
  },
  forward: function(link)
  {
    this.skip += this.size;
    this.link_rewind.style.display = 'inline';
    this.link_rewind.style.display = this.skip > 0 ? 'inline':'none';
    this._update(link.href, this.skip, 'right');
  },
  _update: function(url, skip, direction) 
  {
    this._start_spinner(direction);
    var cs = this.panel.childNodes;
    for (var i = 0; i < cs.length; i++)
    {
      if (cs[i].style) cs[i].style.visibility='hidden';
    }
    
    url = url + '?skip=' + skip;
    var ctrl = this;
    new Ajax.Updater({ success: this.panel }, url, 
    {
      onComplete: function() { ctrl._end_spinner(); }
    });
  },
  
  _start_spinner: function(direction)
  {
    this.panel.className = ('thumbnails-panel-spinner-' + direction)
  },
  _end_spinner: function()
  {
    this.panel.className = 'thumbnails-panel'
  }
}





// Carlos Reche, http://wiki.script.aculo.us/scriptaculous/show/Cookie
// Little changes by Oleg Andreev, http://oleg.subway.net.ru

var Cookie = {
  set: function(name, value, secondsToExpire, path, domain, secure) {
    var params = '';
    if (secondsToExpire) {
      var d = new Date();
      d.setTime(d.getTime() + (1000 * parseInt(secondsToExpire)));
      params = '; expires=' + d.toGMTString();
    }
    params = params +       
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            ((secure) ? "; secure" : "");
    return (document.cookie = escape(name) + '=' + escape(value || '') + params);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = this.get(name) || true;
    this.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof(navigator.cookieEnabled) == 'boolean') {
      return navigator.cookieEnabled;
    }
    this.set('_test', '1');
    return (Cookie.erase('_test') = '1');
  }
}; 



/**
*
*  UTF-8 data encode / decode
*  http://www.webtoolkit.info/
*
**/

var Utf8 = {
  // public method for url encoding
  encode : function (string) {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";
    for (var n = 0; n < string.length; n++) {
      var c = string.charCodeAt(n);
      if (c < 128) {
        utftext += String.fromCharCode(c);
      }
      else if((c > 127) && (c < 2048)) {
        utftext += String.fromCharCode((c >> 6) | 192);
        utftext += String.fromCharCode((c & 63) | 128);
      }
      else {
        utftext += String.fromCharCode((c >> 12) | 224);
        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
        utftext += String.fromCharCode((c & 63) | 128);
      }
    }
    return utftext;
  },

  // public method for url decoding
  decode : function (utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;

    while ( i < utftext.length ) {
      c = utftext.charCodeAt(i);
      if (c < 128) {
        string += String.fromCharCode(c);
        i++;
      }
      else if((c > 191) && (c < 224)) {
        c2 = utftext.charCodeAt(i+1);
        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
        i += 2;
      }
      else {
        c2 = utftext.charCodeAt(i+1);
        c3 = utftext.charCodeAt(i+2);
        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
        i += 3;
      }
    }
    return string;
  }
}

