var defaultTexts = new Array();

window.onload = function(e) {
	var sections=get_sections();
	for(var i=0;i<sections.length;i++) toggle(sections[i]);

	initGray("search_input", "Search");
	initGray("commentBox", "Comment");
	initGray("tagBox", "tagme");
	
	// if we're going to show with JS, hide with JS first
	pass_confirm = byId("pass_confirm");
	if(pass_confirm) {
		pass_confirm.style.display = "none";
	}   
	
	if (this_reply = get_GET_param('reply')) {
        clink(this_reply);
    }
    
    if (this_hl = get_GET_param('hl')) {
        highlight(this_hl);
    }
}

function get_GET_params() {
   var GET = new Array();
   if(location.search.length > 0) {
      var get_param_str = location.search.substring(1, location.search.length);
      var get_params = get_param_str.split("&");
      for(i = 0; i < get_params.length; i++) {
         var key_value = get_params[i].split("=");
         if(key_value.length == 2) {
            var key = key_value[0];
            var value = key_value[1];
            GET[key] = value;
         }
      }
   }
   return(GET);
}
 
function get_GET_param(key) {
   var get_params = get_GET_params();
   if(get_params[key])
      return(get_params[key]);
   else
      return false;
}


function initGray(boxname, text) {
	var box = byId(boxname);
	if(!box) return;

	var clr = function () {cleargray(box, text);};
	var set = function () {setgray(box, text);};

	addEvent(box, "focus", clr, false);
	addEvent(box, "blur",  set, false);

	if(box.value == text) {
		box.style.color = "#999";
		box.style.textAlign = "center";
	}
	else {
		box.style.color = "#000";
		box.style.textAlign = "left";
	}
}

function cleargray(box, text) {
	if(box.value == text) {
		box.value = "";
		box.style.color = "#000";
		box.style.textAlign = "left";
	}
}
function setgray(box, text) {
	if(box.value == "") {
		box.style.textAlign = "center";
		box.style.color = "gray";
		box.value = text;
	}
}

function showUp(elem) {
	e = document.getElementById(elem)
	if(!e) return;
	e.style.display = "";
//	alert(e.type+": "+e.value);
	if(e.value.match(/^http|^ftp/)) {
		e.type = "text";
		alert("Box is web upload");
	}
}

function clink(text) {
  document.post.comment.focus();
  if (document.selection) {
    sel=document.selection.createRange();
    sel.text="@"+text+":\n"; 
  } else if (document.post.comment.selectionStart || document.post.comment.selectionStart=="0") { 
    var startPos=document.post.comment.selectionStart;
    var endPos=document.post.comment.selectionEnd;
    document.post.comment.value=document.post.comment.value.substring(0,startPos)+"@"+text+":\n"+document.post.comment.value.substring(endPos,document.post.comment.value.length);
  } else {
    document.post.comment.value+="@"+text+":\n";
  }
}

var highlighted = "GLOBAL";

function highlight(id) {
    if (oldhl = byId('block'+highlighted)) {
        oldhl.className = "comment commentnohl";
    }
    highlighted = id;
    byId('block'+highlighted).className = "comment commenthl";
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
*                              LibShish-JS                                  *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function addEvent(obj, event, func, capture){
	if (obj.addEventListener){
		obj.addEventListener(event, func, capture);
	} else if (obj.attachEvent){
		obj.attachEvent("on"+event, func);
	}
}


function byId(id) {
	return document.getElementById(id);
}


function getHTTPObject() { 
	if (window.XMLHttpRequest){
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject){
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function ajaxRequest(url, callback) {
	var http = getHTTPObject();
	http.open("GET", url, true);
	http.onreadystatechange = function() {
		if(http.readyState == 4) callback(http.responseText);
	}
	http.send(null);
}


/* get, set, and delete cookies */
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
	
function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}
	
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

