/* Placeholders */
function activatePlaceholders() {
  if (Prototype.Browser.WebKit) return false;
    
  $$('input').each(function(item) {
    if (item.readAttribute("placeholder") != null) {
      if (item.value == null || item.value == "") {
        item.value = item.readAttribute("placeholder");
        item.addClassName("placeholder");
      }
      
      // On focus
      item.onfocus = function () {
        if (this.value == this.readAttribute("placeholder")) {
          this.value = "";
          this.removeClassName("placeholder");
        }
        return false;
      }
      
      // On blur
      item.onblur = function() {
        if (this.value.length<1) {
          this.value = this.readAttribute("placeholder");
          this.addClassName("placeholder");
        }
      }
    }
  });
}

window.onload = function() {
  activatePlaceholders();
}

/* Ticker controll */
var fading; // for protecting fade break
function controllTicker(direction) {
  if (fading) return;
  var tickers = $('ticker').getElementsByClassName('tick');
  var current = 0;
  var next;
  var previous;
  
  // Check what ticker is the active one
  for (var i=0; i<tickers.length; i++) {
    if (tickers[i].visible()) {
      current = i;
      next = current+1;
      previous = current-1;
      break;
    }
  }
  
  // Loop through whole array
  if (next>=tickers.length) next = 0;
  if (previous<0) previous = tickers.length-1;
  
  // Fade current ticker and show next or previous one
  fading = true;
  Effect.Fade(tickers[current], {afterFinish:function(f){
    Effect.Appear(direction>0 ? tickers[next] : tickers[previous], {duration:0.4, afterFinish:function(ff) {
      fading = false;
    }});
  }, duration:0.2});
}

// Converts a string to json
function string2json(string) {
  return eval('(' + string +')');
}

// Pops the div for file upload
function popup() {
  if ($('popup') && $('popup').visible()) return;
  $('popup').show();
}

// Hides the popup
function popdown() {
  if ($('popup') && $('popup').visible()) {
    Effect.Fade('popup', {duration:0.3, afterFinish:function(t){
      $('upload_loading').hide();
      $('upload_error').hide();
      $('upload_success').hide();
      $('upload_field').show();
    }});
  }
}

/* List operations by Rikas */
// Checks if the item id is on ids list
function itemOnList(id, list) {
  var ids = list.split(",");
  for (var i=0;i<ids.size();i++) {
    if (ids[i] == id) return true;
  }
  return false;
}

// Add id to ids list and returns new list
function addToList(id, list) {
  if (!itemOnList(id,list)) {
    list += ","+id;
  }
  return list;
}

// Remove id from ids list and returns new list
function removeFromList(id, list) {
  if (itemOnList(id,list)) {
    var ids = list.split(",");
    var newList = "";
    for (var i=0;i<ids.length;i++) {
      if (ids[i] != id && ids[i] != "") {
        newList += ","+ids[i];
      }
    }
    return newList;
  }
  return list;
}

// AIM class for file uploads
AIM = {
  frame : function(c) {
    var n = 'f' + Math.floor(Math.random() * 99999);
    var d = document.createElement('DIV');
    d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
    document.body.appendChild(d);

    var i = document.getElementById(n);
    if (c && typeof(c.onComplete) == 'function') {
      i.onComplete = c.onComplete;
    }
    return n;
  },

  form : function(f, name) {
    f.setAttribute('target', name);
  },

  submit : function(f, c) {
    AIM.form(f, AIM.frame(c));
    if (c && typeof(c.onStart) == 'function') {
      return c.onStart();
    } else {
      return true;
    }
  },

  loaded : function(id) {
    var i = document.getElementById(id);
    if (i.contentDocument) {
      // firefox and safari
      var d = i.contentDocument;
    } else if (i.contentWindow) {
      // IE 6 and 7
      var d = i.contentWindow.document;
    } else {
      var d = window.frames[id].document;
    }
    
    if (d.location.href == "about:blank") {
      return;
    }
    
    if (typeof(i.onComplete) == 'function') {
      //i.onComplete(d.body.textContent);
      i.onComplete(d.body.innerHTML);
    }
  }
}
