
var domain = 'www.campusnet.it';
var path = '/';
var tipsurl = '/tips.html';


function getPage(page) {

	if (Object.isUndefined(page)) {
		var page = '';
		var href = window.location.href;
		var p = href.indexOf('!');
		if (p<0) {p = href.indexOf('?');}
		if (p >= 0)	{
			page = href.substr(p+1);
		} else {
			page = 'home';
		}
	} else {
		 window.location.href = '/index.html#!' + page;
	}

	if (page == $('pageloaded').value)	{
		return;
	}
	$('pageloaded').value = page;

	if (page.match(/q=/)) {
		page = page.replace(/^q=/, "");
		page = page.replace(/\+/g, " ");
		$('q').value = page;
		new site_search(page);
		return false;
	} else {
		hlcurrent(page);
		var url = path + page + '.html';
		new Ajax.Updater('main', url, {method: 'get'});
	}
}



function tip() {

	var tips = [];
	var tip = document.getElementById('tip');
	new Ajax.Request(tipsurl, {
		method: 'get',
		onSuccess: function(transport) {
			var html = transport.responseText;
			html.scan(/<LI>.+[^<LI>]/i, function(match){tips.push(match[0])});
			var n = Math.floor(Math.random()*tips.length);
			tip.innerHTML = tips[n].replace(/<LI>/i, "<P>");
		}
	});

	setTimeout('tip()',12*1000);

}

function setLinks() {

	var mn = $$('#menu a[rel=ajx]');
	var sb = $$('#sidebar a[rel=ajx]');
	var links = mn.concat(sb);

	links.each(function(item) {
		item.onclick=function  () {
			var  hash = this.href;
			hash = hash.replace(/^.*#!/, '');
			getPage(hash);
			this.blur();
		};
	});

	return;
}


function navigate(page) {

	window.location.href = 'http://www.campusnet.it/?' + page;
	return false;
	
}

function hlcurrent(id) {

	var list=document.getElementById("menulist")
	for (i=0; i<list.childNodes.length; i++) {
		if (list.childNodes[i].nodeName=="LI") {
			list.childNodes[i].className="";			
		}
	}
	if (document.getElementById(id) != null) {
		document.getElementById(id).className = "current";
	}
}

function checkSearch() {
	if (document.forms['search'].q.value.length == 0)	{
		return false;
	}
return true;
}


function site_search(query) {

  this.query = query;
  this.page = document.getElementById('main');
  this.page.innerHTML += '<span class="ajax-message">Ricerca in corso ...</span>';

  // set up the search
  this.gwebsearch = new GwebSearch();
  this.gwebsearch.setResultSetSize(GSearch.LARGE_RESULTSET);
  this.gwebsearch.setUserDefinedLabel('');
  this.gwebsearch.setLinkTarget(GSearch.LINK_TARGET_SELF);
  this.gwebsearch.setSiteRestriction(domain);

  this.gwebsearch.clearResults();
  this.gwebsearch.setSearchCompleteCallback(this, site_search.prototype.done);

  // set up the page
  this.page.innerHTML = '<div class="gsc-title"><P>Risultati per: <b>' + this.query + '</b></div>';
  this.branding = GSearch.getBranding();
  this.page.appendChild(this.branding);

  // go!
  this.gwebsearch.execute(query);
}

site_search.prototype.done = function() {
  results = this.gwebsearch.results;
  for (i = 0; i < results.length; i++) {
   this.page.insertBefore(results[i].html.cloneNode(true), this.branding);
  }

this.page.innerHTML = this.page.innerHTML.replace(/\/index\.html#!/g,'/');
this.page.innerHTML = this.page.innerHTML.replace(/\/index\.html#%21/g,'/');
this.page.innerHTML = this.page.innerHTML.replace(/\.html/g,'');
this.page.innerHTML = this.page.innerHTML.replace(/www.campusnet.it\//g,'www.campusnet.it/index.html#!');

  cursor = this.gwebsearch.cursor;
  if (!cursor && results.length == 0) {
    this.page.innerHTML += '<p>Nessun risultato.</p>';
  } else if (cursor.currentPageIndex < cursor.pages.length - 1) {
    // this is recursive. GWebSearch will re-call its callback, ie this
    // function, when it gets the next page of result.
    this.gwebsearch.gotoPage(cursor.currentPageIndex + 1);
  } else {
     // this.page.innerHTML += '<a href="' + cursor.moreResultsUrl + '">More results...</a>';
  }
}


