var ricercaTip='Trova una località';

function ricercaInit() {
	f=document.getElementById('ricercaForm');
	if (f) f.onsubmit=ricercaOnSubmit;
	q=document.getElementById('ricercaQ');
	if (q) {
		q.onfocus=ricercaOnFocus;
		q.onblur=ricercaOnBlur;
		ricercaOnBlur();
	}
		g=document.getElementById('ricercaGo');
	if (g) {
        new Effect.Opacity(g, {duration:0, to: 0.8});
		g.onmouseover=highlightOff;
		g.onmouseout=highlightOn;
	}
		if (f && q) {
		// get the fieldset
		fs = f.firstChild;
		if (fs) {
			emptyDiv = document.createElement('div');
			emptyDiv.setAttribute('id','ricercaCompleter');
						fs.insertBefore(emptyDiv, q);
			new Ajax.Autocompleter('ricercaQ', 'ricercaCompleter', 'http://meteo.daubau.it/autocompleter', {updateElement: ricercaUpdateElement});
						q.setAttribute('autocomplete','off');
		}
	}
}

function ricercaUpdateElement(li) {
	if (li && li.innerHTML) {
		q=document.getElementById('ricercaQ');
		if (q) {
			f=document.getElementById('ricercaForm');
			if (li.className.indexOf('hr')==-1) {
				var re= /[^<]*<span[^>]*>([^<]*)<\/span>/i
		        var ar = re.exec(li.innerHTML);
		        if (ar && ar.length) q.value= ar[1];else q.value = li.innerHTML;
	        }
			if (f) f.submit();
		}
	}
}

function ricercaOnFocus() {
  q=document.getElementById('ricercaQ');
  if (q) {
    if (q.value=='' || q.value==ricercaTip) {
      q.value='';
      q.className='q';
    }
  }
}

function ricercaOnBlur() {
  q=document.getElementById('ricercaQ');
  if (q) {
    if (q.value=='' || q.value==ricercaTip) {
      q.className='q tip';
      q.value=ricercaTip;
    }
  }
}

function ricercaOnSubmit() {
	q=document.getElementById('ricercaQ');
	if (q && q.value==ricercaTip) q.value='';
}

function headerInit() {
	    if (navigator.userAgent.indexOf('MSIE') == -1) {
    	d=document.getElementById('logo');
    	if (d) {
        	d.onmouseover=highlightOn;
    		d.onmouseout=highlightOff;
    	}
    }
}

function highlightOn(e) {
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	new Effect.Opacity(tg, {duration:0.3, to:0.8, queue: {position:'front', scope: 'highlightScope', limit:2}});
}

function highlightOff(e) {
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	new Effect.Opacity(tg, {duration:0.5, to:1.0, queue: {position:'end', scope: 'highlightScope', limit:2}});
}

function isMicrosummaryEnabled() {
  return (typeof window.sidebar == "object" &&
    typeof window.sidebar.addMicrosummaryGenerator == "function");
}

function addMicrosummary(url) {
  if (isMicrosummaryEnabled())
    window.sidebar.addMicrosummaryGenerator(url);
}

function showMicrosummaryLink(url) {
  if (isMicrosummaryEnabled())
  document.write('<a class="microsummary" href="javascript:addMicrosummary(\'' + url + '\')">Installa il microsummary</a>');
}

function popitup(url, name, opt) {
  var newwindow = '';
  if (!newwindow.closed && newwindow.location) {
    newwindow.location.href = url;
  }
  else {
    newwindow=window.open(url,name,opt);
    if (!newwindow.opener) newwindow.opener = self;
  }
  if (window.focus) {newwindow.focus()}
  return false;
}

function miniMeteo(url) {
  popitup('http://meteo.daubau.it/mini/' + url, url, 'width=415,height=205,scrollbars=no,menubar=no,toolbar=no,location=no,status=no');
}

function closeMiniMeteo(url) {
  if (window.opener && window.opener.location.href == url) {
        window.opener.focus();
  } else {
    popitup(url, 'daubaumeteo', '');
  }
  window.close();
  return false;
}

function canBookmark() {
return window.sidebar || window.external;
}

function bookmark() {
if (window.sidebar) { // Mozilla
window.sidebar.addPanel(document.title, location.href, "");
} else if(window.external) { // IE
window.external.AddFavorite(location.href, document.title); }
}

function checkSVG() {
r = "none";
if (navigator.mimeTypes != null && navigator.mimeTypes.length > 0) {
if (navigator.mimeTypes["image/svg+xml"] != null || navigator.mimeTypes[0] == '*') { r = "object"; }
}
else if (window.ActiveXObject) { // Adobe plugin
try { adobe = new ActiveXObject("Adobe.SVGCtl"); r = "embed"; }
catch (e) { return "MS Error"; }
}
return r;
}

function tools(placeUrl) {
var ul = $("tools");
if (ul) {
li = document.createElement("li");
li = $(li);
li.addClassName("mini");
li.innerHTML = '<a href="javascript:miniMeteo(\'' + placeUrl + '\')">Lancia il MiniMeteo</a>';
ul.appendChild(li);
if (canBookmark()) {
li = document.createElement("li");
li = $(li);
li.addClassName("star");
li.innerHTML = '<a href="javascript:bookmark()">Aggiungi ai preferiti</a>';
ul.appendChild(li);
}
}
}

function init() {
	ricercaInit();
	headerInit();
}
