//Cross-fader class for the features slidshow on the hompage
var CrossFade = Class.create({
	initialize: function(arrayElems, nInterval, nFadeDuration, iElem) {
		this.Elems = arrayElems;
		this.Interval = (typeof nInterval == "undefined") ? 5 : Number(nInterval);
		this.FadeDuration = (typeof nFadeDuration == "undefined") ? 1 : Number(nFadeDuration);
		this.CurElem = (typeof iElem == "undefined") ? 0 : Number(iElem);

		this.Timer = setInterval(this.update.bind(this), this.Interval * 1000);
	},
	update: function() {
		//Make the current element disappear
		Effect.Fade($(this.Elems[this.CurElem]), { duration:this.FadeDuration, from:1.0, to:0.0 });
		//Increase the index
		if(++this.CurElem >= this.Elems.length) this.CurElem = 0;
		//Make the new element appear
		Effect.Appear($(this.Elems[this.CurElem]), { duration:this.FadeDuration, from:0.0, to:1.0 });
	},
	stop: function() {
		clearInterval(this.Timer);
	}
});

//Opens or closes an element
function Toggle(id) {
	Elem = $(id);
	if(Elem.style.display == "none") {
		new Effect.BlindDown(Elem);
	} else {
		new Effect.BlindUp(Elem);
	}
}

//Returns a random number between two given numbers
function Random(x, y) {
	var min = Math.min(x, y);
	var max = Math.max(x, y);
	return Math.floor(Math.random() * (max - min) + min);
}

//Request a new security image
function NewSecurityImage(img_id) {
	var ImgElem = $(img_id);
	var src = ImgElem.src.substr(0, ImgElem.src.indexOf("?"));
	$(img_id).src = src + "?" + Random(1, 10000);
}

//Adds a smilie to the message box
function AddSmilie(box_id, smilie) {
	InsertIntoTextArea($(box_id), smilie, false);
}

/**
 *	Function InsertIntoTextArea(TextArea, Replace, KeepSelected)
 *	Replaces the selection of a textarea by something else, e.g. a smilie.
 *		TextArea - The textarea DOM element
 *		Replace - The text to replace the selection with
 *		KeepSelected - Boolean defining whether the new text should be kept selected
 */
function InsertIntoTextArea(TextArea, Replace, KeepSelected) {
	//Give focus to the textarea
	var TextArea = $(TextArea);
	TextArea.focus();

	//Internet Explorer
	if(document.selection) {
		var Selection = document.selection;
		var Range = Selection.createRange();
		//Check if there's some text selected in the textarea
		if((Selection.type == "Text" || Selection.type == "None") && Range != null) {
			Range.text = Replace;
			if(KeepSelected) Range.select();
		} else {
			TextArea.value += Replace;
		}
	//Mozilla, Opera,...
	} else if(TextArea.selectionEnd) {
		//Get the selection positions
		var SelectStart = TextArea.selectionStart;
		var SelectEnd = TextArea.selectionEnd;
		var ScrollTop = TextArea.scrollTop;

		if(SelectEnd <= 2) SelectEnd = TextArea.textLength;

		//Get the selection contents
		var Start = TextArea.value.substring(0, SelectStart);
		var Middle = TextArea.value.substring(SelectStart, SelectEnd);
		var End = TextArea.value.substring(SelectEnd, TextArea.textLength);

		//Replace the selection
		Middle = Replace;

		//Set the new value
		TextArea.value = Start + Middle + End;

		//Set the selection
		if(KeepSelected) {
			TextArea.selectionStart = SelectStart;
			TextArea.selectionEnd = SelectStart + Middle.length;
		} else {
			TextArea.selectionStart = SelectStart + Middle.length;
			TextArea.selectionEnd = TextArea.selectionStart;
		}

		//Scroll back to the original position
		TextArea.scrollTop = ScrollTop;
	//Do such browsers even exist?
	} else {
		TextArea.value += Replace;
	}

	//Give focus to the textarea
	TextArea.focus();
}

//Executed right after the HTML page is loaded, but not the images yet
document.observe("dom:loaded", (function() {
	//Set the tabs
	if($$(".tab").size() > 0) {
		$$(".tab").without($(CurTabId)).each(function(Elem) {
			Elem.observe('mouseover', function(event) {
				this.addClassName("tab-hover").removeClassName("tab-normal");
			});
			Elem.observe('mouseout', function(event) {
				this.addClassName("tab-normal").removeClassName("tab-hover");
			});
		});
	}
	//Set the thumbnail effects
	if($$(".thumb").size() > 0) {
		$$(".thumb").each(function(Elem) {
			Elem.observe('mouseover', function(event) {
				new Effect.Highlight(this, {
					"startcolor" : "#ffff99",
					"endcolor" : "#ffffff",
					"restorecolor" : "#ffffff"
				});
			})
		});
	}
	//Set the security code events
	if(Object.isElement($("comment-code"))) {
		$("comment-code").observe('change', function(event) {
			var Elem = Event.element(event);
			Elem.value = $(Elem).getValue().toUpperCase();
		});
	}
}).bindAsEventListener(0));