function waiting() {
	$("#waiting").css("display", "block");
	$("#waiting").html("<div>... loading ...</div>");
}

function unwaiting() {
	$("#waiting").css("display", "none");
	$("#waiting").html("<div></div>");
}

jQuery.preloadImages = function() {
	for (var i = 0; i < arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	}
}

function linkTopToggle(div, open, close) {
	if(open==undefined) open=false;
	if(close==undefined) close=false;
	if(($('#'+div+'Content').css("display")=="none" || open==true) && close==false) {
		//$('#'+div+'Link').css('background-color', '#f0f0f0');
		$('#'+div+'Link').css('border-bottom-color', '#f4f4f4');
		if($('#'+div+'Link').hasClass("linguettiva"))
			$('#'+div+'Img').attr('src', '/img/arrow2on-up.gif');
		else
			$('#'+div+'Img').attr('src', '/img/arrow2-up.gif');
		$('#'+div+'Content').show();
	} else {
		$('#'+div+'Content').hide();
		//$('#'+div+'Link').css('background-color', '#FFF');
		$('#'+div+'Link').css('border-bottom-color', '#003366');
		if($('#'+div+'Link').hasClass("linguettiva"))
			$('#'+div+'Img').attr('src', '/img/arrow2on-down.gif');
		else
			$('#'+div+'Img').attr('src', '/img/arrow2-down.gif');
	}
}

function MyLoadImage(els, i) {
	var myEl = $(els[i]).find('a');
	var newImg = $('<img />').attr('src', myEl.find('img').attr("alt"));
	//newImg.attr("height", myEl.find('img').attr("height"));
	//newImg.attr("width", myEl.find('img').attr("width"));
	newImg.load(function(){
		if(i==1) {
			$('ul#animated-portfolio').animatedinnerfade({
				speed: 1000, 
				timeout:  10000, 
				type: 'sequence', 
				containerheight: '260px',
				containerwidth: '715px',
				animationSpeed: 15000,
				animationtype: 'fade',
				bgFrame: 'none',
				controlBox: 'none',
				displayTitle: 'yes'
			});
			
			//$('#homeSlideShowanimated-portfolio').show();
			//$('#homeSlideShowanimated-portfolio').css({ width:"715px", heigth:"260px", overflow:"auto" });
			$('#homeSlideShowanimated-portfolio').css({ position:"relative", left:"0"});
			$('#homeSlideShow').hide();
		}
		var title = myEl.attr("title");
		newImg.attr("title", title);
		newImg.mouseover(function(){ $(this).attr("title", "") }).mouseout(function(){ $(this).attr("title", title) });
		
		myEl.attr("title", "");
		
		if(i < (els.length-1))
			MyLoadImage(els, i+1) 
	});
	myEl.html(newImg);
}

function funcPassword() {
	if($('input').is('.jpassword'))
		$('.jpassword').jpassword({lang: { please: "Inserisci una Password sicura...", low: "Poco sicura.", correct: "Abbastanza sicura.", high: "Molto sicura."}, length: 6});
}

function commonReady(div){
	setTimeout("funcPassword()", 100);
	$(function() {         
	    if($('a').is('.btnOverlay'))
	    	$(div+" a.btnOverlay").overlay();
	});
	
	$(function() {         
	    if($('a').is('.btnOverlayExt'))
		    // if function argument is given to overlay it is assumed onBeforeLoad event listener 
		    $(div+"a.btnOverlayExt").overlay(function() { 
		             
		        // grab wrapper element inside content 
		        var wrap = this.getContent().find("div.wrap"); 
		         
		        // load only for the first time it is opened 
		        if (wrap.is(":empty")) {             
		            wrap.load(this.getTrigger().attr("href"));     
		        }         
		    });             
	});
	if($("#carrelloContent").find("#buttonCarrelloGo1").attr("id")) {
		$("#carrelloLink").addClass("linguettiva");
		$("#carrelloImg").attr('src', '/img/arrow2on-down.gif');
	} else {
		$("#carrelloLink").removeClass("linguettiva");
		$("#carrelloImg").attr('src', '/img/arrow2-down.gif');
	}
	
	if($("#listaContent").find(".textLittle2").attr("class")) {
		$("#listaLink").addClass("linguettiva");
		$("#listaImg").attr('src', '/img/arrow2on-down.gif');
	} else {
		$("#listaLink").removeClass("linguettiva");
		$("#listaImg").attr('src', '/img/arrow2-down.gif');
	} 
	if($('ul').is('#newsticker'))
		$('#newsticker').innerfade({ animationtype: 'fade', speed: 750, timeout: 10000, type: 'sequence', containerheight: '100px' }); 
}

$(document).ready(function(){
	
	if($('ul').is('#animated-portfolio')) {
		var elements = $('ul#animated-portfolio').children();
		MyLoadImage(elements, 0);
	}
	
	if($('a').is('.preview')) {
		imagePreview();
	}
	if($('a').is('.tooltip')) {
		tooltip();
	}
	$('#carrelloContent').bind("mouseleave", function(){
      linkTopToggle('carrello', false, true);
    });
    $('#carrelloLink').bind("mouseenter", function(){
      linkTopToggle('carrello', true);
      linkTopToggle('lista', false, true);
    });
	$('#listaContent').bind("mouseleave", function(){
      linkTopToggle('lista', false, true);
    });
    $('#listaLink').bind("mouseenter", function(){
      linkTopToggle('lista', true);
      linkTopToggle('carrello', false, true);
    });
    $('#topList').bind("mouseleave", function(){
      linkTopToggle('carrello', false, true);
      linkTopToggle('lista', false, true);
    });
    
    $("#artista_ricercaTop").autocomplete('/ajax/artisti.php', {
		width:210,
		matchContains: true,
		minChars: 2,
		delay: 150,
		selectFirst: false,
		formatItem: function (row, i, max) {
			return '<div class="cleared"><div style="float:left; width:45px; margin-right:2px; text-align:center;"><img src="/media/artisti/img-'+row[0]+'.jpg" width="40" class="borded" /></div><div style="float:left; width:135px;">'+row[1]+'</div></div>';
		},
		formatResult: function (row) {
			return row[1].replace(/(<.+?>)/gi, '');
		}
	});
	$("#artista_ricercaTop").result(function(event, data, formatted) {
		waiting();
		window.location.href = data[2];
	});
	
	$("#artista_ricerca").autocomplete('/ajax/artisti.php', {
		width:180,
		matchContains: true,
		minChars: 2,
		delay: 150,
		selectFirst: false,
		formatItem: function (row, i, max) {
			return '<div class="cleared"><div style="float:left; width:45px; margin-right:2px; text-align:center;"><img src="/media/artisti/img-'+row[0]+'.jpg" width="40" class="borded" /></div><div style="float:left; width:105px;">'+row[1]+'</div></div>';
		},
		formatResult: function (row) {
			return row[1].replace(/(<.+?>)/gi, '');
		}
	});
	$("#artista_ricerca").result(function(event, data, formatted) {
		waiting();
		window.location.href = data[2];
	});
	//homeSlideShowStart();
});

function AddToBookmarks(title, url) {
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
}

function createCookie(name,value,days) {
	var expires = "";
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*3600*1000));
		expires = "; expires="+date.toGMTString();
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function deleteCookie(name) {
	createCookie(name,"",-1);
}

function switchListView() {
	if($('#worksList').hasClass('compact')) {
		$('#worksList').removeClass('compact');
		$('#listViewChange').html('Visualizza come tabella');
		deleteCookie('listView');
	} else {
		$('#worksList').addClass('compact');
		$('#listViewChange').html('Visualizza come elenco');
		createCookie('listView', 'compact', 30);
	}
}
function offerteChange(url) {
	$('#worksListContainer').css('height', $('#worksListContainer').height());
	$('#worksList').css('height', $('#worksListContainer').height());
	$('#worksList').fadeOut('normal', function(){loadContent(url, true, "$('#worksList').fadeIn(); $('#worksListContainer').css('height', 'auto'); $('#worksList').css('height', 'auto'); ");});
}
