
var ricercaTip='Trova un articolo';

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);
            if (window.isSpqr) url = 'http://spqr.magazineroma.it/autocompleter';
            else url = 'http://magazineroma.it/autocompleter';
			new Ajax.Autocompleter('ricercaQ', 'ricercaCompleter', url, {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 commentPreview(textarea) {
var t = textarea.value;
t = t.split(/\n/).join("<br />");
if (t=="") t = "Anteprima del commento";
var p = document.getElementById("commentPreview");
if (p) p.innerHTML = t;
}


var fullInited = false;
function initFull() {
  if (fullInited) return;
  fullInited = true;
  var objBody = document.getElementsByTagName("body").item(0);
  var objOverlay = document.createElement("div");
  objOverlay.setAttribute('id','slOverlay');
  objOverlay.style.display = 'none';
  objOverlay.onclick = function() { stopFull(); return false; }
  objBody.appendChild(objOverlay);
  
  var objFull = document.createElement("div");
  objFull.setAttribute('id','fullSL');
  objFull.style.display = 'none';
  objFull.onclick = function() { stopFull(); return false; }
  objBody.appendChild(objFull);
}

function playFull(path, pic) {
initFull();
var sl = $('fullSL');
if (!sl) return;

url = 'http://magazineroma.it/' + path + '/soundslider.swf?size=1';
mp3url = 'http://magazineroma.it/' + path + '/audio_hi.mp3';

hideElements('select');
hideElements('object');

// stretch overlay to fill page and fade in
var arrayPageSize = getPageSize();
Element.setHeight('slOverlay', arrayPageSize[1]);
new Effect.Appear('slOverlay', { duration: 0.2, from: 0.0, to: 0.8 });

var arrayPageScroll = getPageScroll();
var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 15);
Element.setTop(sl, lightboxTop);
sl.addClassName('on');

sl.innerHTML = '<object type="application/x-shockwave-flash" data="' + url + '" width="620" height="488" id="soundslider">' +
'<param name="allowScriptAccess" value="sameDomain" />' +
'<param name="movie" value="' + url + '"/>' +
'<param name="quality" value="high"/>' +
'<param name="menu" value="false"/>' +
'<p style="color:#fff">Per guardare questo servizio &egrave; richiesto il <a href="http://www.adobe.com/go/getflashplayer">Flash Player</a>.'
  + ' Puoi comunque ascoltare la <a href="' + mp3url + '">traccia audio</a> e guardare la <a href="' + pic + '" onclick="myLightbox.start(this);return false" rel="lightbox[1]">galleria fotografica</a>.</p>' +
'</object>';

Element.show(sl);

document.onkeydown = this.fullKeyboardAction;

return false;
}

function stopFull() {
var sl = $('fullSL');
if (!sl) return;
Element.hide(sl);
sl.innerHTML = '';
sl.setAttribute('class','off');
new Effect.Fade('slOverlay', { duration: 0.2});
document.onkeydown = '';
showElements('select');
showElements('object');
}

function fullKeyboardAction(e) {
if (e == null) { // ie
    keycode = event.keyCode;
} else { // mozilla
    keycode = e.which;
}
if (!e) var e = window.event;
key = String.fromCharCode(keycode).toLowerCase();
if((key == 'x') || (key == 'o') || (key == 'c') || (keycode == 27) || (keycode == 8)){
  stopFull();
  e.preventDefault();
  return false;
}
}

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