function slideshow(start, last, interval, div_pre) {
  var frame = start;
  var nextframe = start+1;
  var slideshowId = setInterval(function() {
    Effect.Fade(div_pre + frame,{duration:.5,from:1.0,to:0.0,afterFinish:function(){
        $(div_pre + frame).hide();
        Effect.Appear(div_pre + nextframe,{duration:.5,from:0.0,to:1.0});
        frame = nextframe;
        nextframe = (frame == last) ? start : nextframe+1;
      }});
  },interval);
  return slideshowId;
};

var thumbSelection = 2;
var homeSlideshowId;
var thumbShowId;

function doHomePageSlideShow() {
  homeSlideshowId = slideshow(0, 4, 3000, "content-");
  thumbShowId = setInterval("changeThumbSelected()", 3000);
}

function stopHomePageSlideShow(idOfDiv) {

  clearInterval(homeSlideshowId);
  clearInterval(thumbShowId);

  thumbSelection = idOfDiv + 1;
  changeThumbSelected();
  
  $("content-0").hide();
  $("content-1").hide();
  $("content-2").hide();
  $("content-3").hide();
  $("content-4").hide();
  
  Effect.Appear("content-" + idOfDiv);
}

function changeThumbSelected() {

  if(thumbSelection == 1) {
    document.getElementById("thumb_selected_1").setAttribute("class", "selected1 visible");
    
    for(var i = 2; i <= 5; i++) {
      document.getElementById("thumb_selected_" + i).setAttribute("class", "selected");
    }

    thumbSelection = 2;
  } else {

    document.getElementById("thumb_selected_1").setAttribute("class", "selected1");
    for(var k = 2; k <= 5; k++) {
      if(thumbSelection == k)
        document.getElementById("thumb_selected_" + k).setAttribute("class", "selected visible");
      else
        document.getElementById("thumb_selected_" + k).setAttribute("class", "selected");
    }   
    
    if(++thumbSelection == 6) thumbSelection = 1;
  }
}

// inserts text at where the cursor is within a textarea.
function insertAtCaret(areaId,text) {
	var txtarea = document.getElementById(areaId);
	var scrollPos = txtarea.scrollTop;
	var strPos = 0;
	var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ?
		"ff" : (document.selection ? "ie" : false ) );
	if (br == "ie") {
		txtarea.focus();
		var range = document.selection.createRange();
		range.moveStart ('character', -txtarea.value.length);
		strPos = range.text.length;
	}
	else if (br == "ff") strPos = txtarea.selectionStart;

	var front = (txtarea.value).substring(0,strPos);
	var back = (txtarea.value).substring(strPos,txtarea.value.length);
	txtarea.value=front+text+back;
	strPos = strPos + text.length;
	if (br == "ie") {
		txtarea.focus();
		var range = document.selection.createRange();
		range.moveStart ('character', -txtarea.value.length);
		range.moveStart ('character', strPos);
		range.moveEnd ('character', 0);
		range.select();
	}
	else if (br == "ff") {
		txtarea.selectionStart = strPos;
		txtarea.selectionEnd = strPos;
		txtarea.focus();
	}
	txtarea.scrollTop = scrollPos;
}   
