/*
Copyright Justin Whitford 2006.
  http://www.whitford.id.au/
Perpetual, non-exclusive license to use this code is granted
on the condition that this notice is left in tact.

http://www.whitford.id.au/webmonkey/code/breadcrumbs/history_crumbs/crumbs.js
*/

var delim = '|';
var trailLength = 8;
var chunks;
var DAY = 2;

function doCrumbs(){
	l=document.location;
	rex = baseUrl+"(en|it|de|fr|es)/";
	voicere = new RegExp (rex);
	voice =  voicere.test(l);
	
  if(cookieTest('xxx')){
    crumbList = new CrumbList();
    if(getCookie('trailLinks')){
      var staleLinkCrumbs = getCookie('trailLinks').split(delim);
      var staleTextCrumbs = getCookie('trailText').split(delim);
      var startPos=
        (staleTextCrumbs.length<trailLength ||
        document.location==staleLinkCrumbs[staleLinkCrumbs.length-1])
        ?0:1;
      for(i=startPos;i<staleLinkCrumbs.length;i++){
        crumbList.add(staleLinkCrumbs[i],staleTextCrumbs[i]);
      }
    }
    if(voice && (document.location!=crumbList.links[crumbList.links.length-1])){
      crumbList.add(document.location,document.title);
    }
    setCookie('trailLinks',crumbList.links.join(delim),1);
    setCookie('trailText',crumbList.text.join(delim),1);
    crumbList.output();
  }
}


function CrumbList(){
  this.links=new Array();
  this.text=new Array();
  this.add = crumbListAdd;
  this.output = crumbListShow;
}
  function crumbListAdd(href,text){
    this.links[this.links.length]=href;
    this.text[this.text.length]=text;
  }
  function crumbListShow(){
  if (this.links.length < 2) return;
          document.write("<ul id='path'>");
    for(var i in this.links){
     if (i==this.links.length-1) {
        document.write(          
          "<li class='current'>" + this.text[i] + "</li>"
        );
     } else {
        document.write(          
          "<li><a href='" + this.links[i] + "'>"
          + this.text[i] + "</a></li>"
        );
        }
    }
    document.write("</ul>");
  }


function cookieTest(name){
	return true;
  try{
    setCookie(name,'true',1);
    chunks = document.cookie.split("; ");
    return (getCookie(name)=='true');
  }catch(e){
    return false;
  }
}

function setCookie(name,value,days){
    if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name){
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

// ---

function googleAjaxSearch(query, id, oe) {
  var webSearch = new GwebSearch();
  webSearch.setResultSetSize(GSearch.LARGE_RESULTSET);
  webSearch.setNoHtmlGeneration();
  var searchCallbackObject = new SearchCallbackObject(webSearch, id, oe);
  webSearch.setSearchCompleteCallback(searchCallbackObject,
    SearchCallbackObject.prototype.searchCompleteHandler);
  webSearch.execute(query);
}

SearchCallbackObject.prototype.webSearch;
SearchCallbackObject.prototype.id;

function SearchCallbackObject(webSearch, id, oe) {
    this.webSearch = webSearch;
    this.id = id;
    this.oe = oe;
}

SearchCallbackObject.prototype.searchCompleteHandler = function(options) {
  var res = document.getElementById(this.id);
  var resCount = this.webSearch.results.length;
  var h;
  if (resCount > 0) {
    h = '<div class="box ' + this.oe + '"><h2>Google Search</h2><ul class="links">';
    for (i=0;i<resCount;i++) {
      var r = this.webSearch.results[i];
      h += '\n<li><a href="' + r.url + '" title="' + r.visibleUrl + '">' + r.titleNoFormatting + '</a></h2></li>';
    }
    h += '</ul></div>';
  }
  res.innerHTML = h;
  this.webSearch.clearResults();
}

// ---

function getPageSize() {
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}