var progettoLoaded = 0;

function gotoIndex() {
	$('#pagina').fadeOut();
	$('#top ul li').removeAttr('class');
	var href = 'index.php?AJAX';
	$('#contenuti').load(href, function() {
		ajaxLink();
	});
}

function goto(href) {
	$('#pagina').fadeOut();
	$('#contenuti').load(href, function() {
		ajaxLink();
	});
}

function fb_popup2() {
	$.fancybox('std.htm', {
		'type': 'ajax',
		'width': 660,
		'height': 480,
		'autoScale': false,
		'autoDimensions': false,
	}); 
}

function fb_popup(file) {
	$.get(file, function(data) {
		$.fancybox(
			data,
			{
				'padding': 0,
				'showCloseButton': true,
				'autoDimension': true,
				'titleShow': false,
				'onStart': function() {	$('object').hide(); },
				'onClosed': function() { $('object').show(); }
			}
		);
	});
}

function ajaxLink() {
	$('a:not(.lb,.not)').unbind('click').click(function(e) {
		$('#pagina').fadeOut();
		var questo = $(this).parent('li');
		$(this).parent('li').parent('ul').find('li').removeAttr('class');
		e.preventDefault();
		var href = $(this).attr('href');
		if (!strpos(href,'AJAX')) { href += '?AJAX'; }
		$('#contenuti').load(href, function() {
			ajaxLink();
			questo.attr('class', 'selected');
		});
	});
}

function tabs() {
	$('div.tab:not(.selected)').hide();
	$('ul.tabs li').click(function() {
		$('ul.tabs li').removeAttr('class');
		$(this).attr('class', 'selected');
		$('div.tab:not(#tab_'+$(this).attr('rel')+')').hide();
		$('#tab_'+$(this).attr('rel')).show();
	});
}

function azienda_governance() {
	$('div.rollover').hide().fadeOut();
	$('#govPres').hover(function() {
		$('div.rollover').fadeIn();
	}, function() {
		$('div.rollover').fadeOut();
	});
}

function salini_output() {
	$('#salini_elenco').tablesorter( {sortList: [[3,0], [2,0]]} ); 
	$('#salini_elenco.parcheggi').tablesorter( {sortList: [[0,0]]} ); 
	
	$('div.tab:not(.selected)').hide();
	$('ul.tabs li').click(function() {
		$('ul.tabs li').removeAttr('class');
		$(this).attr('class', 'selected');
		$('div.tab:not(#'+$(this).attr('rel')+')').hide();
		$('#'+$(this).attr('rel')).show();
	});
	
	$('div.salini a').click(function(e) {
		e.preventDefault();
		progettoLoaded = 0;
		$.get($(this).attr('href'), function(data) {
			Shadowbox.open({
	      content:    data,
	      player:     "html",
	      height:     501,
	      width:      620,
	      overlayColor: '#fff'
	    });
    	setInterval("ctrl_progetto()", 100);
		});
	});
}

function ctrl_progetto() {
	if (Shadowbox.isOpen() == true && $('#pl_progetto').length && progettoLoaded == 0) {
		progettoLoaded = 1;
	  initi_progetto();
	}
}

function salini_parcheggi() {
	$('#salini_elenco').tablesorter(); 
	
	$('div.tab:not(.selected)').hide();
	$('ul.tabs li').click(function() {
		$('ul.tabs li').removeAttr('class');
		$(this).attr('class', 'selected');
		$('div.tab:not(#'+$(this).attr('rel')+')').hide();
		$('#'+$(this).attr('rel')).show();
	});

	$('div.salini a').fancybox({
		'padding': '0',
		'width': '620px',
		'height': '501px',
		'showCloseButton': true,
		'onStart': function() {	$('object').hide(); },
		'onClosed': function() { $('object').show(); }
	});
}


function slitta() {
	
	$('#slideLabel').fadeOut(500);
	
	$('#slideshow div ul').animate({
		marginLeft: '-=160px'
	}, 2000, function() {
		$('#slideLink').attr('href', $(this).find('li:eq(3) a').attr('href'));
		$('#slideLabel').html($(this).find('li:eq(3) a').attr('title')).fadeIn(500);
		var move = $('#slideshow div ul li:first');
		$('#slideshow div ul').css('margin-left', '0px').append(move);
	});

}

function strpos (haystack, needle, offset) {
    var i = (haystack+'').indexOf(needle, (offset || 0));
    return i === -1 ? false : i;
}

function flHeader() {
	$.get('/img/header/slideshow.php', function(data) {
			$('#flHeader').load('/flashHeader.htm');
	});
}

function searchBar() {
	if ($('#search').val() == '') { $('#lbSearch').show(); } else { $('#lbSearch').hide().fadeOut(); }
	$('#search').focus(function() {
		$('#lbSearch').fadeOut(200);
	});
	$('#search').blur(function() {
		if ($(this).val() == '') { $('#lbSearch').fadeIn(200); }
	});
}

function getImgNav() {
	var x = 0;
	jQuery('#imgNav img').each(function() {
		if (jQuery(this).attr('class') == 'selected') { out = x; } else {	x++; }
	});
	return out;
}

function initi_progetto() {

	jQuery('#gallery div.imgBlocco img').click(function() {
		var rel = jQuery(this).attr('rel');
		var indx = jQuery(this).index();
		var imgBl = getImgNav();
		
		if (jQuery('#imgShow').length) {
			jQuery('#imgShow').fadeOut(function () {
				$.get('/showcase.php', { 
					'file': rel,
					'width': '420',
					'height': '240'
				}, function(s) {
					jQuery('#show').html(s);
					jQuery('#show img').hide().fadeIn().click(function() { 	
						jQuery('#zoomImg').fadeIn(200);
						zoomImg(imgBl, indx); 
					});
				});
			});
		} else {
			$.get('/showcase.php', { 
				'file': rel,
				'width': '420',
				'height': '240'
			}, function(s) {
				jQuery('#show').html(s);
				jQuery('#show img').hide().fadeIn().click(function() { 	
					jQuery('#zoomImg').fadeIn(200);
					zoomImg(imgBl, indx); 
				});
			});
		}
	});
	
	jQuery('#gallery div.videoBlocco img').click(function() {
		var rel = jQuery(this).attr('rel');
		jQuery.get('/showVideo.php', {
			'video': rel,
			'width': '320',
			'height': '240'
		}, function(s) {
			jQuery('#show').html(s);
		});
	});


	jQuery('#gallery div').each(function() {
		var ind = jQuery(this).index();
		var classs = jQuery(this).attr('class');
		if (classs == 'videoBlocco') {
			var spaun = jQuery('<span rel="video"><img src="/img/r_punto.gif" alt="." /></span>').click(function() {
				jQuery('#imgNav span[rel="video"]').find('img').attr('src', "/img/r_punto.gif").removeAttr('class');
				jQuery('#imgNav span[rel="img"]').find('img').attr('src', "/img/punto.gif").removeAttr('class');
				jQuery(this).find('img').attr('src', "/img/r_puntoo.gif").attr('class', 'selected');
				jQuery('#gallery div:not(:eq('+ind+'))').hide();
				jQuery('#gallery div:eq('+ind+')').fadeIn();
			}).appendTo(jQuery('#imgNav'));
		} else {
			var spaun = jQuery('<span rel="img"><img src="/img/punto.gif" alt="." /></span>').click(function() {
				jQuery('#imgNav span[rel="video"]').find('img').attr('src', "/img/r_punto.gif").removeAttr('class');
				jQuery('#imgNav span[rel="img"]').find('img').attr('src', "/img/punto.gif").removeAttr('class');
				jQuery(this).find('img').attr('src', "/img/puntoo.gif").attr('class', 'selected');
				jQuery('#gallery div:not(:eq('+ind+'))').hide();
				jQuery('#gallery div:eq('+ind+')').fadeIn();
			}).appendTo(jQuery('#imgNav'));
		}
	});
	
	jQuery('#imgNav span:first').click();
	jQuery('#zoomImg').hide().fadeOut();
	jQuery('#imgOverlay').css('opacity', '0.7');
	
	
	jQuery('#gallery div.imgBlocco').hide().fadeOut();
	jQuery('#gallery div.imgBlocco:eq(0)').fadeIn();
	jQuery('#gallery img:first').click();



}


function zoomImg(blocco, index) {
	imgBl = getImgNav();
	jQuery('#imgBig').hide().fadeOut();
	var file = jQuery('#gallery div.imgBlocco:eq('+blocco+') img:eq('+index+')').attr('rel');
	jQuery.get('/showcase.php', { 
		'file': file,
		'width': '640',
		'height': '501',
		'index': index
	}, function(s) {
		jQuery('#imgBig').html(s);
		jQuery('#imgIndex').val(index);
		jQuery('#imgIndBlocco').val(blocco);
		setTimeout("jQuery('#imgBig').fadeIn()", 200);
		jQuery('#imgSucc').unbind('click').click(function() {
			if ( (index+1) < jQuery('#gallery div.imgBlocco:eq('+blocco+') img').length) {
				zoomImg(blocco, index+1);
			} else {
				if ((blocco+1) < jQuery('#gallery div.imgBlocco').length) {
					zoomImg(blocco+1, 0);
				} else {
					zoomImg(0,0);
				}
			}
		});
		jQuery('#imgPrev').unbind('click').click(function() {
			if ( (index-1) >= 0) {
				zoomImg(blocco, index-1);
			} else {
				if (blocco == 0) {
					var bl = jQuery('#gallery div.imgBlocco').length-1;
					zoomImg(bl, jQuery('#gallery div.imgBlocco:eq('+bl+') img').length-1);
				} else {
					zoomImg(blocco-1, jQuery('#gallery div.imgBlocco:eq('+(blocco-1)+') img').length-1);
				}
			}
		});

	});
	jQuery('#sb-nav-close').removeAttr('onclick').unbind('click').click(function() {
		jQuery('#zoomImg').fadeOut();
		jQuery(this).click(function() { Shadowbox.close(); });
	});
}

