

/*  OBJECT EXTENSIONS  */

String.prototype.trimLeft = function() { return this.replace(/^\s+/,'') }
String.prototype.trimRight = function() { return this.replace(/\s+$/,'') }
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,'') }
String.prototype.insert = function(idx,value) { return this.slice(0,idx) + value + this.slice(idx) }




//  PROTOTYPE.JS EXTENSIONS

Object.extend(Element, {
	toggleClassName: function(element,className1, className2) {
		if (Element.hasClassName(element, className1)) {
			Element.removeClassName(element, className1);
			if (className2) {
				Element.addClassName(element, className2);
			}
		}
		else {
			Element.addClassName(element, className1);
			if (className2) {
				Element.removeClassName(element, className2);
			}
		}
	}
})



//  SCRIPTACULOUS EXTENSIONS

//  add markdown syntax links to edit in place text areas
Object.extend(Ajax.InPlaceEditor.prototype, {
	createForm: function() {
    var ipe = this;
    function addText(mode, condition) {
      var text = ipe.options['text' + mode + 'Controls'];
      if (!text || condition === false) return;
      ipe._form.appendChild(document.createTextNode(text));
    };
    this._form = $(document.createElement('form'));
    this._form.id = this.options.formId;
    this._form.addClassName(this.options.formClassName);
    this._form.onsubmit = this._boundSubmitHandler;
    this.createEditField();
    if ('textarea' == this._controls.editor.tagName.toLowerCase())
      this._form.appendChild(document.createElement('br'));
    if (this.options.onFormCustomization)
      this.options.onFormCustomization(this, this._form);
    addText('Before', this.options.okControl || this.options.cancelControl);
    this.createControl('ok', this._boundSubmitHandler);
    addText('Between', this.options.okControl && this.options.cancelControl);
    this.createControl('cancel', this._boundCancelHandler, 'editor_cancel');
    addText('After', this.options.okControl || this.options.cancelControl);
	  if (this.options.instructions) { // mods: add documentation links
	    documentation = document.createElement("span");
	    documentation.className = "documentation";
	    documentation.appendChild(document.createElement("span").appendChild(document.createTextNode("This field uses Markdown for formatting. ")));
	    cheatLink = document.createElement("a");
	    cheatLink.appendChild(document.createTextNode("cheat sheet"));
	    cheatLink.href = "javascript:popup('/able/content/markdown_help');";
//	    cheatLink.setAttribute("onclick", "popup('/Mackay/content/markdown_help');return false;");
	    documentation.appendChild(cheatLink);
	    documentation.appendChild(document.createElement("span").appendChild(document.createTextNode(" | ")));
	    fullLink = document.createElement("a");
	    fullLink.appendChild(document.createTextNode("documentation"));
	    fullLink.href = "http://daringfireball.net/projects/markdown/syntax";
	    fullLink.target = "_blank";
	    documentation.appendChild(fullLink);
	    this._form.appendChild(documentation);
	  }
  }
})

// allow html in in place editor
// Object.extend(Ajax.InPlaceEditor.prototype, {
// 	onLoadedExternalText: function(transport) {
// 	  Element.removeClassName(this.form, this.options.loadingClassName);
// 	  this.editField.disabled = false;
// 	  this.editField.value = transport.responseText;
// 	  Field.scrollFreeActivate(this.editField);
// 	}
// })



/*  / OBJECT EXTENSIONS  */


//  popup markdown cheat sheet
popup = function(path, width, height) {
	if (path == undefined)   alert('no path specified to popup');
	if (width == undefined)  width = 450;
	if (height == undefined) height = 400;
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open('" + path + "', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=" + width + ",height=" + height + "');");
}




/*  FORM HELPERS  */

// takes a string, looks for a div id'd as form_status, blinds it up, swaps the text and blinds it down. if no str, doesn't blind down. if alert == false, will not set the text to class="alert"
set_status = function(str, add_alert_class) {
	var bdown = function() { return };  // callback used if str is undefined
	if (add_alert_class == undefined) add_alert_class = true;
	if (str != undefined) { // wrap the text in a p
		if (add_alert_class) {
			form_status_str = '<p class="alert">' + str + '</p>';
		} else {
			form_status_str = '<p>' + str + '</p>';
		}
		bdown = function() { // callback function to show the text
			Element.update('form_status', form_status_str);
			Effect.BlindDown('form_status', {duration: 0.3})
		}
	}
	if (Element.visible('form_status')) { // there's currently text showing, so hide it
		Effect.BlindUp('form_status', {duration: 0.3, afterFinish: bdown})
	} else {
		bdown();
	}
}

// adds a class to all fields passed in field_names array, adding a prefix if defined
highlight_form_fields = function(field_names, field_prefix, css_class) {
	if (css_class == undefined) {
		css_class = "alert";
	}
	if ((field_prefix != undefined) && (field_prefix != ''))
		field_prefix += "_";
	field_names = eval(field_names);
	for (var i=0; i<field_names.length; i++) {
		Element.addClassName(field_prefix + field_names[i], css_class);
		Element.addClassName(field_prefix + field_names[i] + "_label", css_class);
	}
}

// clears all fields with the given class, of that class, optional prefix
clear_classes = function(class_name, el_prefix) {
	els = document.getElementsByClassName(class_name);
	if (el_prefix == undefined) 
		el_prefix = '';
	for (var i=0; i < els.length; i++) {
		if (els[i].id.substr(0, el_prefix.length) == el_prefix)
			Element.removeClassName(els[i], class_name);
	}
}


/*  / FORM HELPERS  */



/*  HOMEPAGE SLIDESHOW  */

var Slideshow = Class.create({	
	
	initialize: function(parent_div, time_to_wait) {
		// this.time_to_wait = time_to_wait;
		this.parent_div = $(parent_div);
		this.image = $(parent_div).down('.image');
		this.image_list = this.image.down('ul');
		this.text = $(parent_div).down('.text');
		this.text_list = this.text.down('ul');
		this.next_link = $(parent_div).down('.next');
		this.prev_link = $(parent_div).down('.prev');
		this.current_image = 1;
		
		//  duplicate first image and text and throw it onto the end so that the slideshow always goes to the left
		var first_image = this.image_list.down('li');
		var dup_first_image = document.createElement('LI');
		dup_first_image.id = "image" + this.image_list.select('li').length + 1;
		dup_first_image.innerHTML = first_image.innerHTML;
		this.image_list.appendChild(dup_first_image);

		var first_text = this.text_list.down('li');
		var dup_first_text = document.createElement('LI');
		dup_first_text.id = "text" + this.text_list.select('li').length + 1;
		dup_first_text.innerHTML = first_text.innerHTML;
		this.text_list.appendChild(dup_first_text);

		this.number_of_images = this.image_list.select('li').length;
		
		
		this.next_link.onclick = function() {
			slideshow.next_image();
		};
		this.prev_link.onclick = function() {
			slideshow.prev_image();
		};
		
		this.enabled = true;
		// this.next_image_timer = setTimeout("slideshow.next_image();", this.time_to_wait);
  },

	next_image: function() {
		if (this.enabled) {
			this.enabled = false;
			// clearTimeout(this.next_image_timer);
			if (this.current_image == this.number_of_images) {
				this.image_list.setStyle({marginLeft: '0px'});
				this.text_list.setStyle({marginLeft: '0px'});
				this.current_image = 1;
			}
			var image_width = this.image.select('li')[this.current_image].getWidth();
			var image_margin = parseInt(this.image_list.getStyle('marginLeft'));
			var new_image_margin = image_margin - image_width;
			new Effect.Morph(this.image_list, {style: {marginLeft: new_image_margin + 'px'}, afterFinish: function() {slideshow.enabled = true}})

			var text_width = this.text.select('li')[this.current_image].getWidth();
			var text_margin = parseInt(this.text_list.getStyle('marginLeft'));
			var new_text_margin = text_margin - text_width;
			new Effect.Morph(this.text_list, {style: {marginLeft: new_text_margin + 'px'}})
		
			this.current_image++;
			// this.next_image_timer = setTimeout("slideshow.next_image();", this.time_to_wait);
		}
	},
  
	prev_image: function() {
		if (this.enabled) {
			this.enabled = false;
			// clearTimeout(this.next_image_timer);
			if (this.current_image == 1) {
				var image_margin_left = 0;
				var image_lis = this.image_list.select('li');
				image_lis.each(function(e) { if (e != image_lis.last()) { image_margin_left -= e.getWidth(); }});
			
				var text_margin_left = 0;
				var text_lis = this.text_list.select('li');
				text_lis.each(function(e) { if (e != text_lis.last()) { text_margin_left -= e.getWidth(); }});
			
				this.image_list.setStyle({marginLeft: image_margin_left + 'px'});
				this.text_list.setStyle({marginLeft: text_margin_left + 'px'});
				this.current_image = this.number_of_images;
			}
			var image_width = this.image.select('li')[this.current_image - 1].getWidth();
			var image_margin = parseInt(this.image_list.getStyle('marginLeft'));
			var new_image_margin = image_margin + image_width;
			new Effect.Morph(this.image_list, {style: {marginLeft: new_image_margin + 'px'}, afterFinish: function() {slideshow.enabled = true}});

			var text_width = this.text.select('li')[this.current_image - 1].getWidth();
			var text_margin = parseInt(this.text_list.getStyle('marginLeft'));
			var new_text_margin = text_margin + text_width;
			new Effect.Morph(this.text_list, {style: {marginLeft: new_text_margin + 'px'}});
		
			this.current_image--;
			// this.next_image_timer = setTimeout("slideshow.next_image();", this.time_to_wait);
		}
	}
  
});


// set left column to minimum height or height of left column

var set_left_column_height = function() {
	var body_id = $$('body').first().id;
	var body_classNames = $$('body').first().classNames();
	var exempt_ids = $A(new Array('home_index', 'courses_detail'));
	if ((!exempt_ids.include(body_id)) && (!$$('body').first().classNames().include('popup'))) {
		var min_height = 570;
		var right_column_height = $$('.content').first().down('.right_column').getHeight();
		var left_column = $$('.content').first().down('.left_column');
		var left_height = (right_column_height > min_height) ? right_column_height : min_height;
		left_column.setStyle({height: left_height + 'px'});
	}
}

Event.observe(window, 'load', function() {
  set_left_column_height();
});
