//function roundCorners(aid) {
//	new Rico.Effect.Round(null, "box_gray",{bgColor:"#ffffff"});
//}
/* Client-side access to querystring name=value pairs
	Version 1.2.3
	22 Jun 2005
	Adam Vandenberg
*/


function initHistory(strLocation, strMessage) {
	// initialize the DHTML History
	// framework
	dhtmlHistory.initialize();

	// subscribe to DHTML history change
	// events
	dhtmlHistory.addListener(historyChange);

	var initialLocation = null;//dhtmlHistory.getCurrentLocation();
 
	// if no location specified, use the default
	if (initialLocation == null)
		initialLocation = "Start";

	if(strLocation) {
		dhtmlHistory.add(
                    strLocation,
                     {message: strMessage})
	}
}

function historyChange(newLocation, historyData) {
	updateUI(newLocation, historyData);
}

function updateUI(newLocation, historyData) {
	switch(newLocation) {
		case "articleList":
			simple_search(null, null);
		break;

		case "articleListSearch":
			simple_search(historyData.message, null);
		break;
		
		case "Start":
			window.location.href="http://www.alltforbarn.se/";
		break;
	}
}



function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=Querystring_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}

function init() {
	var search_field = document.getElementById("search_field");
	search_field.focus()
}
window.onload = init;

function getNewXMLHttpRequest() {
  var obj;
    try {
      // For Internet Explorer.
      obj = new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch(e) {
      try {
        // Gecko-based browsers, Safari, and Opera.
        obj = new XMLHttpRequest();
      }
      catch (e) {
        // Browser supports Javascript but not XMLHttpRequest.
        obj = false;
      }
    }
    return obj;
}

function simple_search(search_field, limit) {
	if(limit == null) {
		limit = "10";
	}
	var content_main = document.getElementById("content_main");
	//var search_field = document.getElementById("search_field").value;
	
	xmlhttp = getNewXMLHttpRequest();
	xmlhttp.open("POST","includes/search.php",true);
	xmlhttp.onreadystatechange = function() {
									var field = document.getElementById("search_field");
									if(xmlhttp.readyState==1) {
										content_main.style.backgroundImage = "url(images/indicator.gif)";
										content_main.style.backgroundRepeat = "no-repeat";
										content_main.style.backgroundPosition = "center center";
									}
									if(xmlhttp.readyState==4) {
										content_main.style.backgroundImage = "url()";
										content_main.innerHTML = xmlhttp.responseText;
									}	
								 }
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	xmlhttp.send("string="+search_field+"&limit="+limit);
}
// Comments
function addComment(aid) {
	var commentFormArea = document.getElementById("commentFormArea");
	var comments = document.getElementById("comments");
	
	var cName = document.getElementById("name").value;
	var cContent = document.getElementById("comment").value;
	
	xmlhttp = getNewXMLHttpRequest();
	xmlhttp.open("POST","includes/comments.php?action=insertComment&ajax=yes&aid="+aid,true);
	xmlhttp.onreadystatechange = function() {
									if(xmlhttp.readyState==1) {
										commentFormArea.style.backgroundImage = "url(images/indicator.gif)";
										commentFormArea.style.backgroundRepeat = "no-repeat";
										commentFormArea.style.backgroundPosition = "center center";
									}
									if(xmlhttp.readyState==4) {
										comments.style.backgroundImage = "none";
										comments.innerHTML = xmlhttp.responseText;
									}	
								 }
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	xmlhttp.send("name="+cName+"&content="+cContent);
}

/* ----------- SEACHING AND FILTERING ----------- */
var lastTimeCalled = 0;
var filterField;

function filterArticlesByName(filterFieldObj) {
	filterField = filterFieldObj;
	var now = new Date();
	var oneSec = 1000;
	if(lastTimeCalled < (now.getTime()-oneSec))
	{
		lastTimeCalled = now.getTime();
		//fetchResults();
		//timeout = setTimeout("fetchResults()", 1000);
		fetchResults();
		//alert(string);
	}
}
function fetchResults() {
	simple_search(encodeURIComponent("AnyNode|"+filterField.value, null));
	// Some bug fucks this up
	dhtmlHistory.add(
		'articleListSearch',
		{message: encodeURIComponent("AnyNode|"+filterField.value, null)})
}

function list_today(Y,m,n,d) {
	var list_today = document.getElementById("list_today");
		
	xmlhttp = getNewXMLHttpRequest();
	xmlhttp.open("GET","includes/listToday.php?s=calSearch&date="+Y+"-"+m+"-"+d+"&dag="+d+"&show_year="+Y+"&show_month="+n,true);
	xmlhttp.onreadystatechange = function() {
									if(xmlhttp.readyState==1) {
										list_today.innerHTML = "<img src='images/indicator.gif' alt='Laddar aktiviteter' />";
									}
									if(xmlhttp.readyState==4) {
										list_today.innerHTML = xmlhttp.responseText;
									}	
								 }
	//xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	xmlhttp.send();
}

