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

var widgetEditInit = false;
var widget_width = 195;
var widget_height = 100;

function readcss(myclass,element) {
	myclass = myclass.toLowerCase();
///Convert class to accomodate for Safari
	var safariclass = myclass.replace(/\#([\w\-]+)/g, "*[id\"$1\"]");
	for (var j = 0; j < document.styleSheets.length; j++) {
		var theRules = checkBrowser ();
		theRules =  document.styleSheets[j][theRules];
		for (var i = 0; i < theRules.length; i++) {
			if (theRules[i].selectorText.toLowerCase() == myclass || theRules[i].selectorText.toLowerCase() == safariclass) {
				return theRules[i].style[element];
			}
		}
	}
}

function setInputColor(writeTo, readElement, readProperty) {
	var hexValue = new RGBColor(readcss(readElement, readProperty)).toHex();
	if (hexValue.indexOf('#') != -1) {
		hexValue = hexValue.substring(1);
	}
	
	$(writeTo).value = hexValue;
}

function inWidgetEditor() {
	if(tinyMCE.activeEditor.id == 'new_content') {
		return false;
	}

	return true;
}

function saveWidget(form_id, type) {
	if (type == 'widget') {
		$('widget_content').value = tinyMCE.activeEditor.getContent();
	}
}

function checkWidget(form_id, type) {
	if (type == 'widget') {
		Element.show('widget_help');
		Element.show(form_id + '_form_content');
		editWidget(form_id);
		// alert($(form_id + '_form_content').innerHTML);
	} else {
		Element.hide('widget_help');
		Element.hide(form_id + '_form_content');
	}
}

function editWidget(form_id) {

	if (!widgetEditInit) {
		
    tinyMCE.init({
      theme : "advanced",
      convert_urls : false,
			plugins : "pagelink,fileupload,embed,media",
			theme_advanced_buttons1 : "|,embed,|,fileupload,|,link,|",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			extended_valid_elements : "iframe[src|style|width|height|frameborder|scrolling],object[width|height|classid|codebase],param[name|value],embed[src|type|style|width|height|flashvars|wmode|scale|bgcolor|name|align|allowscriptaccess]",
      theme_advanced_toolbar_location : "top",
      theme_advanced_toolbar_align : "left"
    });

	}

	tinyMCE.execCommand('mceAddControl', false, form_id);
}

function less(id) {
  Element.hide(id + '-full');
  Element.show(id + '-preview');
}

function more(id) {
  Element.hide(id + '-preview');
  Element.show(id + '-full');
}

var TopicForm = {
  editNewTitle: function(txtField) {
    $('new_topic').innerHTML = (txtField.value.length > 5) ? txtField.value : 'New Topic';
  }
}

var LoginForm = {
  setToPassword: function() {
    $('openid_fields').hide();
    $('password_fields').show();
  },
  
  setToOpenID: function() {
    $('password_fields').hide();
    $('openid_fields').show();
  }
}

var EditForm = {
  // show the form
  init: function(postId) {
    $('edit-post-' + postId + '_spinner').show();
    this.clearReplyId();
  },

  // sets the current post id we're editing
  setReplyId: function(postId) {
    $('edit').setAttribute('post_id', postId.toString());
    $('post_' + postId + '-row').addClassName('editing');
    if($('reply')) $('reply').hide();
  },
  
  // clears the current post id
  clearReplyId: function() {
    var currentId = this.currentReplyId()
    if(!currentId || currentId == '') return;

    var row = $('post_' + currentId + '-row');
    if(row) row.removeClassName('editing');
    $('edit').setAttribute('post_id', '');
  },
  
  // gets the current post id we're editing
  currentReplyId: function() {
    return $('edit').getAttribute('post_id');
  },
  
  // checks whether we're editing this post already
  isEditing: function(postId) {
    if (this.currentReplyId() == postId.toString())
    {
      $('edit').show();
      $('edit_post_body').focus();
      return true;
    }
    return false;
  },

  // close reply, clear current reply id
  cancel: function() {
    this.clearReplyId();
    $('edit').hide()
  }
}

var ReplyForm = {
  // yes, i use setTimeout for a reason
  init: function() {
    EditForm.cancel();
    $('reply').toggle();
    $('post_body').focus();
    // for Safari which is sometime weird
//    setTimeout('$(\"post_body\").focus();',50);
  }
}

// Event.addBehavior({
//   '#search,#monitor_submit': function() { this.hide(); }
// })