var setupEditableFields = function() {
  $$('.editable').each(function(e) {
    e.onclick = function() { edit(e); return false;}
  });
}

var edit = function(element) {
  buildWindow();

  if (element.hasClassName('image'))
    var type = "image";
  else if (element.hasClassName('text'))
    var type = "text";
  else if (element.hasClassName('portfolio'))
    var type = "portfolio";
  else if (element.hasClassName('new_portfolio_entry'))
    var type = "new_portfolio_entry";
  else if (element.hasClassName('edit_portfolio_page'))
    var type = "edit_portfolio_page";
  else if (element.hasClassName('company_name'))
	  var type = "company_name";
  else if (element.hasClassName('new_portfolio_page'))
    var type = "new_portfolio_page";
	else if (element.hasClassName('help'))
	  var type = "help";
	else if (element.hasClassName('feedback'))
	  var type = "feedback";
		else if (element.hasClassName('purchase'))
		  var type = "purchase";

  new Ajax.Updater('editingPane', editURL(element.id, type), {method:'get', onComplete: function() {
    $('editingWrap').style.visibility = 'visible';
    $('loading_message').remove();
    $('overlay').style.visibility = 'visible';

    fckInit();
    var ep = document.getElementById('editingPane');
  }});
}

function fckInit() {
  // initialize the FCKeditor
  var editareas = $('epform').getElementsByTagName('textarea');
  var oFCKeditor = new FCKeditor(editareas[0].id, '100%', '240px', 'Simple'); // idstring, width, height, toolbarSet
  oFCKeditor.BasePath = "/javascripts/fckeditor/";
  oFCKeditor.Config['CustomConfigurationsPath'] = "../../fckcustom.js";
  oFCKeditor.ReplaceTextarea();
}

function GetFCKeditorContentForAjaxSubmit() {
  var oEditor = FCKeditorAPI.GetInstance('portfolio_entry_description');
  $('portfolio_entry_description_hidden').value = oEditor.GetXHTML();
}

var buildWindow = function() {
  var pageHeight = window.innerHeight > document.body.scrollHeight ? window.innerHeight : document.body.scrollHeight;
  var editing_wrap = Builder.node('div', {id:'editingWrap', style:'visibility: hidden;'},
						[Builder.node('div', {id:'editingPane'})]
					 );
  var overlay = Builder.node('div', {id:'overlay', style:'height:'+pageHeight+'px;'}); 
  var loading_message = Builder.node('div', {id:'loading_message'}, '');
  
  document.body.appendChild(overlay);
  document.body.appendChild(loading_message);
  document.body.appendChild(editing_wrap);
}

var cancelEdit = function() {
  $('overlay').remove();
  $('editingWrap').remove();
}

// This is triggered when a user clicks on a "site color" in the toolbar
var changeSiteColor = function(color_selector, color) {
  $$('link').each(function(element) {
    if (element.title == "color_scheme") {
			element.href = element.href.replace(element.id, color);
			element.id = color;
    }
  });

  $$('#color-selector li a').each(function(element) {
    element.className = "";
  });

  color_selector.className = "on";
  
  /* The next two lines are needed to make IE change the whole background's color because it doesn't redraw properly */
  document.body.style.display = 'none';
  document.body.style.display = 'block';
  
  new Ajax.Request("/account/set_theme_color/" + color);
}

var buildWelcomeScreen = function() {
  var pageHeight = window.innerHeight > document.body.scrollHeight ? window.innerHeight : document.body.scrollHeight;
  var overlay = Builder.node('div', {id:'overlay', style:'height:'+pageHeight+'px; visibility: visible;'}); 
  
  document.body.appendChild(overlay);
  $('welcome_message').style.visibility = "visible";
}

var closeWelcomeScreen = function() {
  $('overlay').remove();
  $('welcome_message').remove();
}

var Popup = {
  open: function(options)
  {
    this.options = {
      url: '#',
      width: 700,
      height: 550,
      name:"_blank",
      location:"no",
      menubar:"no",
      toolbar:"no",
      status:"no",
      scrollbars:"yes",
      resizable:"no",
      left:"",
      top:"",
      normal:false
    }
    Object.extend(this.options, options || {});

    if (this.options.normal){
        this.options.menubar = "yes";
        this.options.status = "yes";
        this.options.toolbar = "yes";
        this.options.location = "yes";
    }

    this.options.width = this.options.width < screen.availWidth?this.options.width:screen.availWidth;
    this.options.height=this.options.height < screen.availHeight?this.options.height:screen.availHeight;
    var openoptions = 'width='+this.options.width+',height='+this.options.height+',location='+this.options.location+',menubar='+this.options.menubar+',toolbar='+this.options.toolbar+',scrollbars='+this.options.scrollbars+',resizable='+this.options.resizable+',status='+this.options.status
    if (this.options.top!="")openoptions+=",top="+this.options.top;
    if (this.options.left!="")openoptions+=",left="+this.options.left;
    window.open(this.options.url, this.options.name,openoptions );
    return false;
  }
}