/*HOME*/
function initHomeVideoSlide() {
	$$('#video_toolbar-content a.slide').each(function(obj) {
		obj.observe('click', loadVideo);
	});
	$$('#video_title a').each(function(obj) {
		obj.observe('click', slideVideo);
	});
}
function loadVideo (event) {
	event.stop();
	var that = event.element();
	if (that.tagName!='A')
		that = that.up('a');

	$('video_player').innerHTML = '<iframe width="520" height="324" src="http://www.youtube.com/embed/' + that.readAttribute('id').split('_').last() + '" frameborder="0" allowfullscreen></iframe>';
	$$('#video_toolbar-content a.selected')[0].removeClassName('selected');
	that.addClassName('selected');
	$('video_title').down('span').innerHTML = that.readAttribute('title');
	
	return false;
}
function slideVideo(event) {
	event.stop();
  var that = event.element();
  var direction; 
  
  if(that.hasClassName('prev'))
    direction = 'p';
  else
    direction = 'n';  
	switch (direction) {
	case 'n':
		delta=1;
		break;
	case 'p':
		delta=-1;
		break;
	default:		
		return false;
		break;
	}	
  ScrollVideoLeft(428*delta);
	return false;
}


function ScrollVideoLeft (_delta) {
	var _inc=0;
	var _sign=(_delta>0?1:-1);

	new PeriodicalExecuter(function(pe) {
		$('video_toolbar').scrollLeft+=(107*_sign);
		_inc+=(107*_sign);
		if (Math.abs(_inc)>=Math.abs(_delta))
			pe.stop();
		}, .05);
}

/*RICERCA*/
function initSearch() {
	$$('#search a.lente')[0].observe('click', doSearch);
}
function doSearch(event) {
	event.stop();
	if ($('search_field').getValue() == '') {
		new Effect.Highlight('search_field', { startcolor: '#e9312b',endcolor: '#ffffff' });
		return false;
	}
	
	window.location.href = '/search/?keywords=' + $('search_field').getValue();
	
	return false;
}

/*INSEGNANTI*/
function initInsegnantiList() {
	$$('#filtro_materie a').each(function(obj) {
		obj.observe('click', toggleMateriaInsegnante);
	});
	
}
function toggleMateriaInsegnante(event) {
	var that = Event.element(event);
	var item = that.className.split('_').last();
	
	event.stop();
	
	$$('#filtro_materie a.selected').each(function(obj) {
		obj.removeClassName('selected');	
	});

	
	if (!that.hasClassName('selected')) {
		that.addClassName('selected')
		show = true;
	}
	else {
		that.removeClassName('selected');
		show = false;
	}
	
	$$('.insegnanti tr').each(function(obj) {
		if (item == 'all' || obj.hasClassName(item))
			obj.show();
		else
			obj.hide();
	});
	
	/*var selected = Array();
	$$('#filtro_materie a.selected').each(function(obj) {
		obj.removeClassName('selected');
		//selected.push($w(obj.className)[0].split('_').last());
	});*/
	
	/*$$('.insegnanti tr').each(function(obj) {
		if (selected.size() == 0)
			obj.show();
		else {
			materie = obj.classNames().toString().split(' ');
			materie.shift();
			if (getIntersect(selected,materie).size())
				obj.show();
			else
				obj.hide();
		}
	});*/
	
	return false;
}


// **************************************************************************
function getIntersect(arr1, arr2) {
    var r = [], o = {}, l = arr2.length, i, v;
    for (i = 0; i < l; i++) {
        o[arr2[i]] = true;
    }
    l = arr1.length;
    for (i = 0; i < l; i++) {
        v = arr1[i];
        if (v in o) {
            r.push(v);
        }
    }
    return r;
}

