$(document).ready(function() {
	/* BOX QUADRATI */
	$("ul#opera-box-list li").hoverIntent({    
		over: function () {
			$(this).find("div").dequeue().animate({
				top: 0,
				opacity: 1
			}, 400);
		},
		out: function () {
			$(this).find("div").dequeue().animate({
				top: 175,
				opacity: 1
			}, 400);
		},
		timeout: 200
	});	
	$.each($("ul#opera-box-list li a span"),function () {
		if($(this).height()<20) $(this).css({"margin-top":"8px","display":"block"});
	});
	$("ul#opera-box-list li").click(function () {
		document.location.href = $(this).find("a.titolo").attr("href");
		return;
	});
	$("#searchform #s").addClass("default").val(searchform_val);
	$("#searchform #s").focusin(function(){
		if($(this).val()==searchform_val) $(this).removeClass("default").val("");
	});
	$("#searchform #s").focusout(function(){
		if($(this).val()=="") $(this).addClass("default").val(searchform_val);
	});
	$("#searchform").submit(function(){
		if(($("#searchform #s").val()=="")||($("#searchform #s").val()==searchform_val)) return false;
		return true;
	});
	$.each($("#catalogo-opere ul li, .catalogo-opere ul li"),function(){
		var id = $(this).attr("class").split("-");
		id = id[id.length-1];
		$(this).find("a").text($(this).find("a").text()+" ("+$("#cat-"+id+" .lista-opere li").length+")");
	});
});
// newsBoxHW - BOX SLIDESHOW
$.fn.newsBoxHW = function(options) {
	var opts = $.extend($.fn.newsBoxHW.defaults,options);
	var newsBoxG = {};
	/* Plugin Methods */
	jQuery.extend({
		newsBoxHW_next: function() {
			if(newsBoxG['loop']>0) {
				newsBoxG['loop']--;
				if(newsBoxG['current']==(newsBoxG['num']-2)) {
					newsBoxG['current']=0;
					newsBoxG['ul_left']=0;
					newsBoxG['ul'].css("left","0px");
				}
				newsBoxG['current']++;
				newsBoxG['ul_left'] = newsBoxG['ul_left']-newsBoxG['width'];
				newsBoxG['ul'].dequeue().animate({
					left: newsBoxG['ul_left']+"px"
				}, newsBoxG['speed'], function() {
					if(!newsBoxG['timeout_wait'])newsBoxG['timeout'] = setTimeout("$.newsBoxHW_next()",newsBoxG['timeout_speed']);
				});
			}
		}
	});
	function prev() {
		if(newsBoxG['current']==1) {
			newsBoxG['current']=newsBoxG['num']-1;
			newsBoxG['ul_left']=parseInt("-"+(newsBoxG['width_tot']-newsBoxG['width']));
			newsBoxG['ul'].css("left",newsBoxG['ul_left']+"px");
		}
		newsBoxG['current']--;
		newsBoxG['ul_left'] = newsBoxG['ul_left']+newsBoxG['width'];
		newsBoxG['ul'].dequeue().animate({
			left: newsBoxG['ul_left']+"px"
		}, newsBoxG['speed'], function() {
		// Animation complete.
		});
	}
	function next() {
		if(newsBoxG['current']==(newsBoxG['num']-2)) {
			newsBoxG['current']=0;
			newsBoxG['ul_left']=0;
			newsBoxG['ul'].css("left","0px");
		}
		newsBoxG['current']++;
		newsBoxG['ul_left'] = newsBoxG['ul_left']-newsBoxG['width'];
		newsBoxG['ul'].dequeue().animate({
			left: newsBoxG['ul_left']+"px"
		}, newsBoxG['speed'], function() {
		// Animation complete.
		});
	}
	/* Main Plugin Code */
	return this.each(function() {
		$box = $(this);
		if($box.find("ul li").length < 2) return false;
		newsBoxG['ul'] = $box.find("ul");
		$first = $box.find("ul li:first").clone();
		$last = $box.find("ul li:last").clone();
		$first.appendTo(newsBoxG['ul']);
		$last.prependTo(newsBoxG['ul']);
		newsBoxG['num'] = $box.find("ul li").length;
		newsBoxG['width'] = $box.find("ul li").eq(0).outerWidth(true);
		newsBoxG['width_tot'] = (newsBoxG['width']*newsBoxG['num']);
		newsBoxG['current'] = 1;
		newsBoxG['ul_left'] = "-"+newsBoxG['width'];
		newsBoxG['ul'].width(newsBoxG['width_tot']);
		newsBoxG['ul'].css("left",newsBoxG['ul_left']+"px");
		newsBoxG['speed'] = opts.speed;
		newsBoxG['timeout_speed'] = opts.timeout_speed;
		newsBoxG['timeout'] = setTimeout("$.newsBoxHW_next()",newsBoxG['timeout_speed']);
		newsBoxG['timeout_wait'] = false;
		newsBoxG['loop'] = (opts.loop*(newsBoxG['num']-2));
		newsBoxG['ul'].after('<a class="prev" href="#prev"></a><a class="next" href="#next"></a>');
		/* live events */
		$box.hover(
			function () {
				if(newsBoxG['loop']>0) {
					clearTimeout(newsBoxG['timeout']);
					newsBoxG['timeout_wait'] = true;
				}
				$box.find("a.prev,a.next").show();
			}, 
			function () {
				if(newsBoxG['loop']>0) {
					newsBoxG['timeout'] = setTimeout("$.newsBoxHW_next()",newsBoxG['timeout_speed']);
					newsBoxG['timeout_wait'] = false;
				}
				$box.find("a.prev,a.next").hide();
			}
		);
		$box.find("a.prev").click(function(){
			prev();
			return false;
		});
		$box.find("a.next").click(function(){
			next();
			return false;
		});
	});
}
$.fn.newsBoxHW.defaults = {
	speed: 500,
	timeout_speed: 2000,
	loop: 2
}
