$(document).ready(function() {
	var colorpicker = $('#colorpicker');
	if (colorpicker.length) {
		// Set default colors
		$('#custom_styling input').each(function(){
			if ($(this).val() == '') {
				var color = '';
				switch ($(this).attr('id')) {
					case 'event_title_colour':
						color = '#ffffff';
						break;
					case 'event_header_colour':
						color = '#107180';
						break;
					case 'event_toplinks_colour':
						color = '#ffffff';
						break;
					case 'event_text_colour':
						color = '#000000';
						break;
					case 'event_tab_text_colour':
						color = '#ffffff';
						break;
					case 'event_tab_background_colour':
						color = '#50a6ba';
						break;
					case 'event_active_tab_text_colour':
						color = '#ec008c';
						break;
					case 'event_active_tab_background_colour':
						color = '#d2d3d5';
						break;
					case 'event_subtab_background_colour':
						color = '#50a6ba';
						break;
				}
				
				$(this).val(color);
			}
		});
		
		// Initiate color picker
		var f = $.farbtastic('#colorpicker');
	    var selected;
	    $('#custom_styling img').
			each(function(){
				f.linkTo('#'+this.className);
			}).
			click(function(){
				colorpicker.show();
				$('#'+this.className).css('border-color', '#666');
				f.linkTo('#'+this.className);
				$(selected = $('#'+this.className)).css('opacity', 1);
			});
			
			// close button
			$('#colorpicker-close').
			click(function(){
				colorpicker.hide();
				// set border back to original. Since we don't know what element we changed, we'll change em all back.
				$('#custom_styling input').
				each(function(){
				  $(this).css('border-color', '');
				})
			});
	}
});


// tinyMCE stuff
function insertImage(filename, width, height) {
  tinyMCE.execCommand('mceInsertContent', false, "<img src='" + filename + "' style='width: " + width + "; height: " + height + ";'>");
  $("#editor_image").hide();
}

$(document).ready(function(){
  hide_ul();
});
var ul_click = false;
var ul_logo_click = false;

function hide_ul() {
  editor_image = $('#editor_image');
	if (editor_image && editor_image.css('display') == '') {
		editor_image.toggle();
	}
	ul_logo_click = !ul_logo_click;
}

function insertAtCursor(element, value) {
  //IE support
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = value;
  }
  //MOZILLA/NETSCAPE support
  else if (element.attr('selectionStart') || element.attr('selectionStart') == '0') {
    var startPos = element.attr('selectionStart');
    var endPos = element.attr('selectionEnd');
    element.val(element.val().substring(0, startPos) + value + element.val().substring(endPos, element.val().length));
  } else {
    myField.value += myValue;
  }
}