// JavaScript Document
if (Object.isUndefined(Trident)) { var Trident = { } }

function showMenu(el) {
	el.select('.sMenu')[0].show();
}

function hideMenu(el) {
	el.select('.sMenu')[0].hide();
}

function initMenu() {
	$$('#menu div.menuVoce').each(function(el) {
		hideMenu(el);
		el.observe('mouseover', function(){ showMenu(this); });
		el.observe('mouseout', function(){ hideMenu(this); });
	});
}

function initLogin() {
	var ll = $('loginlayer');
	if(ll) {
		$('loginlayer').hide();
		var lg = $('login');
		if(lg) lg.observe('mouseup', function(){ 
					$('loginlayer').toggle();
					if(Prototype.Browser.IE && $('looking')) $('looking').toggle();
				});
	}
}
function initLiveSearch() {
	new Form.Element.Observer(
  		'ls_text',
  		0.400,  // 400 milliseconds
  		function(el, value){
  			if(el.getAttribute('placeholder')==value) return;
  			if(value.length<3) return;
			var elid = $('search_results');
			if(!elid) return;
			elid.innerHTML='Searching <strong>'+value+'</strong>...';
			new Ajax.Updater(
				elid,
				'/inc/ajax/livesearch/find.php',
				{
//					onComplete: function(t,d,c) { updCourse(type,destid,course); }.bind(this),
					onFailure: function() { elid.innerHTML = '<div="noResult">server error</div>'; },
					parameters: { s: value }
				}
			);
  		}
	);
}
function initEdit() {
	$$('.dat_edit_placeholder').each(function(el) {
		var lk = el.getAttribute('rel');
		var d = el.getDimensions();
		var o = el.cumulativeOffset();
		var z = el.getStyle('zIndex')+1;
		$(document.body).insert(new Element("div", {
			id: el.id+'_mht'
		}).setStyle({
			zIndex: z,
			position: "absolute",
			background: "url(/dat/img2/marqueeHoriz.gif)",
			top: o.top+'px',
			left: o.left+'px',
			width: d.width+'px',
			height: '1px'
		}));
		$(document.body).insert(new Element("div").setStyle({
			zIndex: z,
			position: "absolute",
			background: "url(/dat/img2/marqueeVert.gif)",
			top: o.top+'px',
			left: o.left+'px',
			width: '1px',
			height: d.height+'px'
		}));
		$(document.body).insert(new Element("div").setStyle({
			zIndex: z,
			position: "absolute",
			background: "url(/dat/img2/marqueeHoriz.gif)",
			top: (o.top+d.height-1)+'px',
			left: o.left+'px',
			width: d.width+'px',
			height: '1px'
		}));
		$(document.body).insert(new Element("div").setStyle({
			zIndex: z,
			position: "absolute",
			background: "url(/dat/img2/marqueeVert.gif)",
			top: o.top+'px',
			left: (o.left+d.width-1)+'px',
			width: '1px',
			height: d.height+'px'
		}));
		$(document.body).insert(new Element("div").setStyle({
			zIndex: z,
			position: "absolute",
			background: "url(/dat/img2/marqueeVert.gif)",
			top: o.top+'px',
			left: (o.left+d.width-1)+'px',
			width: '1px',
			height: d.height+'px'
		}));
		var b = new Element("a").setStyle({
			zIndex: z,
			position: "absolute",
			top: o.top+'px',
			left: o.left+'px',
			textDecoration: 'none',
			color: 'white',
			backgroundColor: 'red',
			padding: '2px'
		});
		b.setAttribute('href','#');
//		b.setAttribute('rel','iframe');
//		b.setAttribute('title','Edit on Line :: :: width: '+(d.width+50)+', height:'+(d.height+200));
		b.update('Click to edit contents');
		b.observe('click',function(ev) {
			modal('http://www.lorenzodemedici.it/dat/editonline.php?k='+lk,'iframe',{fullscreen: true});
			Event.stop(ev);
		});
		$(document.body).insert(b);
	});
}
function initAll() {
	initMenu();
	initLogin();
	initLiveSearch();
	initEdit();
	new Trident.InputDefault('#after label');
}

function modal(link, type, opts) {
	var myoptions = Object.extend({
			backgroundColor: '#cccccc',
			border: 0,
			radius: 0,
			autosize: true,
			topclose: true,
			viewport: true
		}, opts || {});
	var t = type===true ? 'ajax' : type || '';
	Lightview.show({
		href: link,
		rel: t,
		options: myoptions
	});
}


Trident.InputDefault = Class.create({
	initialize: function() {
		if (/Safari/.test(navigator.userAgent)) return;
		var labelSelector = arguments[0] || 'label';
		$$('input[placeholder]').findAll(function(l) {return (l.readAttribute('placeholder') !== null)}).each(function(l){
			l._value = l.readAttribute('placeholder');
			if (l.value.empty()) l.value = l._value;
			l.observe('blur',function(e){
				var x = e.target || e.currentTarget;
				if(x.value == '') {
					x.value = x._value;
				}
			}.bind(document));
			l.observe('focus',function(e){
				var x = e.target || e.currentTarget;
				if(x.value == x._value) {
					x.value = '';
				}
			}.bind(document));
		});
	}
});

document.observe("dom:loaded",initAll);
// Event.observe(window, "load", initAll);
