// buca di ricerca
var defaultValue =''
var claim = '';

function buca(kw){
	if (kw==undefined) kw = claim;
	defaultValue = kw;
	document.getElementById('ricerca').innerHTML = '<form onsubmit="return search($(\'#search_field\').val(), \'http://www.liquida.it/\', \'http://directory.liquida.it/\')" id="search_form"><input class="qs" type="text" onclick="setSearchField(this);" onblur="blurSearchField(this);" value="'+kw+'" name="search_field" id="search_field" /><input class="trigger" type="submit" name="submit" value="" /><input type="hidden" name="search_what" id="search_what" value="post"><input type="hidden" name="search_cat" id="search_cat" value="www"></form>';
}

function setSearchField(qs) {

}

function blurSearchField(qs) {
	if (qs.value=='') qs.value=defaultValue;
}

function search(sField, sBaseUrl, sDirUrl) {
	
	if (sField != claim && sField != '') {
		// tracciamento click
		trackSearchClick(sField, sTrackSearch);
		if(sBaseUrl===undefined) sBaseUrl = "/";
		if(sDirUrl===undefined) sDirUrl = "/";
		// trim
		sField = sField.trim();
		if(sField == '' || sField == 'blog:') return false;
		var sUrl = sBaseUrl;
		// ricerca post blog
		if(sField.indexOf("blog:") == 0) {
			sUrl += "search/";
			var spacePos = sField.indexOf(" ");
			if(spacePos > 0)
				sField = "blog:" + cleanSearchUrl(sField.substring(5, spacePos)) + "/" + cleanSearchFld(sField.substring(spacePos+1));
			else
				sField = "blog:" + cleanSearchUrl(sField.substring(5));
		// ricerca classica
		} else {
			sField = cleanSearchFld(sField);
		}
		document.location.href = sUrl + sField + "/";
	}
	
	return false;
}

function cleanSearchFld(sField) {
	// lowercase
	sField = sField.toLowerCase();
	// delete querystring
	if(sField.indexOf('?') != -1) sField = sField.substring(0, sField.indexOf('?'));
	// html decode
	sField = sField.replace(/</g,"&lt;").replace(/>/g,"&gt;");
	// replace &nbsp; in -
	sField = sField.replace(/&nbsp;/g, "-");
	// replace blank space in -
	sField = sField.replace(/ /g, "-");
	// replace + in -
	sField = sField.replace(/\+/g, "-");
	// replace _ in -
	sField = sField.replace(/_/g, "-");
	// replace %C2%A0 in -
	sField = sField.replace(/%C2%A0/g, "-");
	// replace %20 in -
	sField = sField.replace(/%20/g, "-");
	// delete http://
	sField = sField.replace(/http:\/\//g, "");
	// deletee https://
	sField = sField.replace(/https:\/\//g, "");
	// replace / in -
	sField = sField.replace(/\//g, "-");
	// trim -
	if(sField.charAt(0) == '-') sField = sField.substring(1);
	if(sField.charAt(sField.length-1) == '-') sField = sField.substring(0, sField.length - 1);
	// urlencode
	sField = encodeURIComponent(sField);
	
	return sField;
}

function cleanSearchUrl(sField) {

	// lowercase
	sField = sField.toLowerCase();
	// delete querystring
	//if(sField.indexOf('?') != -1) sField = sField.substring(0, sField.indexOf('?'));
	// replace ? in !
	sField = sField.replace(/\?/g, "!");
	// html decode
	sField = sField.replace(/</g,"&lt;").replace(/>/g,"&gt;");
	// replace &nbsp; in -
	sField = sField.replace(/&nbsp;/g, "-");
	// replace blank space in -
	sField = sField.replace(/ /g, "-");
	// replace + in -
	sField = sField.replace(/\+/g, "-");
	// replace %C2%A0 in -
	sField = sField.replace(/%C2%A0/g, "-");
	// replace %20 in -
	sField = sField.replace(/%20/g, "-");
	// delete http://
	sField = sField.replace(/http:\/\//g, "");
	// deletee https://
	sField = sField.replace(/https:\/\//g, "");
	// replace / in -
	sField = sField.replace(/\//g, "|");
	// trim -
	if(sField.charAt(0) == '-') sField = sField.substring(1);
	if(sField.charAt(sField.length-1) == '-') sField = sField.substring(0, sField.length - 1);
	
	return sField;
}

function trackSearchClick(sField, sTrackSearch) {

	var img = new Image(1, 1);
	now = new Date;
	img.src = sTrackSearch + sField + '|' + now.getTime();
	img.onload = function() { return true; };
	img.onerror = function() { return true; };
}


function traceGoClick(blogId, postId, sGoUrl) {

	return true;
	
	if(sGoUrl===undefined) sGoUrl = "/";
	
	var img = new Image(1, 1);
	now = new Date;
	img.src = sGoUrl + blogId + '|'+ postId + '|' + now.getTime();
	img.onload = function() { return true; };
	img.onerror = function() { return true; };
}

// animati allo scroll
var scrollStart;
var scrollLimit;
var sidebarbOffset;
var lmOffset=0;
var lmOffsetDiff=0;
var pagingAutomatico=1;
var paginate = 0;

/* temporaneo per paginazione */
var news = 0;

function scrollAnimation(){
	
	var scrolltop = $(window).scrollTop();	
	
	// aggiorno il limite dell'adv che scrolla in sidebar
	sidebarHeight =  $('.vertsep').css('height');
	sidebarHeight = parseInt(sidebarHeight.replace('px',''));
	scrollLimit = sidebarHeight;
	
	// stickyadv
	if(scrollStart < scrolltop) { $('#stickyadv').css({ position: 'fixed', top: '5px'	}); }
	if(scrollStart >= scrolltop) { $('#stickyadv').css({ position: 'static', top: 'auto' }); }
	if(scrolltop>=scrollLimit-100) { $('#stickyadv').css({ position: 'absolute', top: 'auto', bottom: '0' }); }
	
	if ($(".loadMore").length > 0) {
		lmOffset = $('.loadMore').offset();
		lmOffset = lmOffset.top;
	}	
	
	lmOffsetDiff = ($(window).height()/3)*2;
	
	// paging ajax al primo scroll	
	if (pagingAutomatico==1) {
		if( lmOffset!=0 && (scrolltop>lmOffset-lmOffsetDiff)) {
			pagingAutomatico=0;
			$('.loadMore').addClass('working');
			simuloloading = setTimeout('moreItems()', 300);
		}
	}

}

$(window).scroll(function () { scrollAnimation(); });

// slider immagini

var fotoItems = new Array();
var fotoSliderPanels;
var fotoSliderStartPos;
var fotoThumbWidth_top = '25%';
var fotoThumbWidth_bot = '100%';
var curFotoThumb_top = 1;
var curFotoThumb_bot = 4;
var curFotoZoom_top;
var curFotoZoom_bot;
var fotoNextLimit;
var fotoPrevLimit_top = -2;
var fotoPrevLimit_bot = 4;

var fotoSliding = 0;


function fotoSliderInit(suffix) {

	curFotoZoom_top = (fotoSliderPanels*4)-1;
	curFotoZoom_bot = 3;
	
	if (suffix=='_top') curFotoZoom = curFotoZoom_top;
	else  curFotoZoom = curFotoZoom_bot;   
	
	fotoNextLimit = (fotoSliderPanels*4);

	var prev_loopPanel = $(document.createElement('div'));
	prev_loopPanel.attr({id: 'tpanel'+suffix+(fotoSliderPanels+1)});
	prev_loopPanel.addClass('thumbspanel');
	prev_loopPanel.html($('#tpanel'+suffix+(fotoSliderPanels)).html());
	$('#tpanel'+suffix+'1').before(prev_loopPanel);
	
	var next_loopPanel = $(document.createElement('div'));
	next_loopPanel.attr({id: 'tpanel'+suffix+(fotoSliderPanels+2)});
	next_loopPanel.addClass('thumbspanel');
	next_loopPanel.html($('#tpanel'+suffix+'1').html());
	$('#tpanel'+suffix+fotoSliderPanels).after(next_loopPanel);
	
	$('#thumbs'+suffix).css('display','block');
}

function fotoSliderNext(suffix) {
	
	if (fotoSliding==0) {
		
		fotoSliding=1;
	
		var nowshowing;
		
		var zoomSlideWidth = $('#zoom'+suffix).css('width');
		zoomSlideWidth = parseInt(zoomSlideWidth.replace('px',''));	
		fotoSliderStartPos = zoomSlideWidth*-1;
		
		if (suffix=='_top') {
			if (curFotoZoom_top == fotoItems.length-1) { nowshowing = 0; }
			else { nowshowing = curFotoZoom_top+1; }
			
			fotoThumbWidth = fotoThumbWidth_top;
			
		} else {
			if (curFotoZoom_bot == fotoItems.length-1) { nowshowing = 0; }
			else { nowshowing = curFotoZoom_bot+1; }
			
			fotoThumbWidth = fotoThumbWidth_bot;		
		}
		
		var zoomItem = $(document.createElement('div'));
		zoomItem.attr({id: 'zoomitem'+suffix+nowshowing});
		zoomItem.addClass('zoomitem');
		$('.zoomitem').css('width',zoomSlideWidth);
		zoomItem.css('width',zoomSlideWidth);
		zoomItem.html('<a class="img" href="'+fotoItems[nowshowing][3]+'" title="'+fotoItems[nowshowing][1]+'"><img src="'+fotoItems[nowshowing][0]+'" alt="'+fotoItems[nowshowing][1]+'" /><span><strong>Guarda le foto</strong></span></a><a class="title" href="'+fotoItems[nowshowing][3]+'" title="'+fotoItems[nowshowing][1]+'">'+fotoItems[nowshowing][1]+'</a><p>Fonte: '+fotoItems[nowshowing][2]+'</p>');
		$('#zoomslide'+suffix).css('width','200.1%');
		zoomItem.appendTo($('#zoomslide'+suffix));
	
		$('#zoomslide'+suffix).animate(
			{marginLeft: '-='+zoomSlideWidth}, 
			750,
			function() {
				
				if (nowshowing==0) {
					
					if (suffix=='_top') {
						$('#zoomitem'+suffix+curFotoZoom_top).remove();
						curFotoZoom_top=0;
					} else {
						$('#zoomitem'+suffix+curFotoZoom_bot).remove();
						curFotoZoom_bot=0;
					}
	
				} else {
					$('#zoomitem'+suffix+(nowshowing-1)).remove();
					if (suffix=='_top') { curFotoZoom_top++; }
					else { curFotoZoom_bot++; }
				}
				$('#zoomslide'+suffix).css({width:'100%', marginLeft:0});
			}		
		);
	
		$('#thumbs'+suffix).animate(
			{marginLeft: '-='+fotoThumbWidth}, 
			750, 
			function() {
				if (suffix=='_top') { curFotoThumb = curFotoThumb_top; }
				else { curFotoThumb = curFotoThumb_bot; }
	
				if (curFotoThumb==fotoNextLimit) {

					$('#thumbs'+suffix).css('marginLeft','-100%');
	
					if (suffix=='_top') { curFotoThumb_top=1; }
					else { curFotoThumb_bot=4; }
					
				} else {
					
					if (suffix=='_top') { curFotoThumb_top++; }
					else { curFotoThumb_bot+=4; }
	
				}
				
				fotoSliding=0;
			} 
		);
		
	}

}

function fotoSliderPrev(suffix) {

	if (fotoSliding==0) {
	
		fotoSliding=1;
		
		var nowshowing;
		
		var zoomSlideWidth = $('#zoom'+suffix).css('width');
		zoomSlideWidth = parseInt(zoomSlideWidth.replace('px',''));	
		fotoSliderStartPos = zoomSlideWidth*-1;
		
		if (suffix=='_top') {	
			if (curFotoZoom_top==0) { nowshowing=fotoItems.length-1; }
			else { nowshowing=curFotoZoom_top-1; }
			
			fotoThumbWidth = fotoThumbWidth_top;
			
		} else {
			if (curFotoZoom_bot==0) { nowshowing=fotoItems.length-1; }
			else { nowshowing=curFotoZoom_bot-1; }
			
			fotoThumbWidth = fotoThumbWidth_bot;		
		}
			
		var zoomItem = $(document.createElement('div'));
		zoomItem.attr({id: 'zoomitem'+suffix+nowshowing});
		zoomItem.addClass('zoomitem');
		$('.zoomitem').css('width',zoomSlideWidth);
		zoomItem.css('width',zoomSlideWidth);
		zoomItem.html('<a class="img" href="'+fotoItems[nowshowing][3]+'" title="'+fotoItems[nowshowing][1]+'"><img src="'+fotoItems[nowshowing][0]+'" alt="'+fotoItems[nowshowing][1]+'" /><span><strong>Guarda le foto</strong></span></a><a class="title" href="'+fotoItems[nowshowing][3]+'" title="'+fotoItems[nowshowing][1]+'">'+fotoItems[nowshowing][1]+'</a><p>Fonte: '+fotoItems[nowshowing][2]+'</p>');
		$('#zoomslide'+suffix).css({'width': '200.1%',marginLeft:-zoomSlideWidth});
		zoomItem.prependTo($('#zoomslide'+suffix));
		
		$('#zoomslide'+suffix).animate(
			{marginLeft: '+='+zoomSlideWidth}, 
			750,
			function() {
				
				if (suffix=='_top') {
					curFotoZoom=curFotoZoom_top;
				} else {
					curFotoZoom=curFotoZoom_bot;
				}
				
				if (curFotoZoom==0) {
					if (suffix=='_top') {
						$('#zoomitem'+suffix+curFotoZoom_top).remove();
						curFotoZoom_top=fotoItems.length-1;
					} else {
						$('#zoomitem'+suffix+curFotoZoom_bot).remove();
						curFotoZoom_bot=fotoItems.length-1;
					}				
				} else {
					$('#zoomitem'+suffix+(nowshowing+1)).remove();
					if (suffix=='_top') {
						curFotoZoom_top--;
					} else {
						curFotoZoom_bot--;
					}
				}
				$('#zoomslide'+suffix).css({width:'100%', marginLeft:0});		
			}		
		);
	
		$('#thumbs'+suffix).animate(
			{marginLeft: '+='+fotoThumbWidth},
			750,
			function() {
				
				if (suffix=='_top') { 
					curFotoThumb=curFotoThumb_top;
					fotoPrevLimit=fotoPrevLimit_top;
				} else{ 
					curFotoThumb=curFotoThumb_bot;
					fotoPrevLimit=fotoPrevLimit_bot;
				}
				
				if (curFotoThumb==fotoPrevLimit) {
					$('#thumbs'+suffix).css('marginLeft','-200%');
					if (suffix=='_top') { 
						curFotoThumb_top=((fotoSliderPanels-1)*4)+1;
					} else {
						curFotoThumb_bot=fotoSliderPanels*4;
					}
				} else {
					if (suffix=='_top') {
						curFotoThumb_top--;
					} else {
						curFotoThumb_bot-=4;
					}	
				}
				
				fotoSliding=0;
				
			}
		);
	
	}

}

// slider video

var videoItems = new Array();
var videoItemsTot;
var baseVideoUrl;
var baseVideoUrlFirst;
var videoHeaderWidth;
var videoSlideWidth;
var curVideoZoom = 1;

function videoSliderInit() {
	videoItemsTot = videoItems.length-1;
}

function videoSliderNext(videoPagUrl) {

	videoHeaderWidth = $('.videoheaderslider').css('width');
	videoHeaderWidth = parseInt(videoHeaderWidth.replace('px',''));
	
	videoSlideWidth = $('.videozoom').css('width');
	videoSlideWidth = parseInt(videoSlideWidth.replace('px',''));	

	var nowshowing;
	if (curVideoZoom == videoItemsTot) { nowshowing = 1; }
	else { nowshowing = curVideoZoom+1; }

	var zoomHeader = $(document.createElement('div'));
	zoomHeader.attr({id: 'headeritem'+(nowshowing)});
	zoomHeader.addClass('headeritem');
	$('.headeritem').css('width',videoHeaderWidth);
	zoomHeader.css('width',videoHeaderWidth);
	zoomHeader.html('<h2><a href="'+videoItems[nowshowing][4]+'" title="">'+videoItems[nowshowing][1]+'</a></h2><p>Postato il '+videoItems[nowshowing][2]+'. Condiviso da <em>'+videoItems[nowshowing][3]+'</em> persone.</p>');
	$('.videoheaderslider').css({ width: '200.1%' });	
	zoomHeader.appendTo($('.videoheaderslider'));
	
	var zoomItem = $(document.createElement('div'));
	zoomItem.attr({id: 'videozoomitem'+nowshowing});
	zoomItem.addClass('videozoomitem');
	$('.videozoomitem').css('width',videoSlideWidth);
	zoomItem.css('width',videoSlideWidth);
	zoomItem.html('<iframe src="'+videoItems[nowshowing][0]+'" frameborder="0" allowfullscreen></iframe>')
	$('.videozoomslider').css({ width: '200.1%' });
	zoomItem.appendTo($('.videozoomslider'));
		
	$('.videoheaderslider').animate(
		{ marginLeft: '-='+'100%' },
		500,
		function() {
			$('#headeritem'+curVideoZoom).remove();
			$('.videoheaderslider').css({width:'100%', marginLeft:0});	
		}
	);
	
	$('.videozoomslider').animate(
		{ marginLeft: '-='+'100%' },
		500,
		function() {
			$('#videozoomitem'+curVideoZoom).remove();
			$('.videozoomslider').css({width:'100%', marginLeft:0});
			curVideoZoom=nowshowing;
			
			var prevlink = ((curVideoZoom-1)>1) ? baseVideoUrl+(curVideoZoom-1)+'/' : baseVideoUrlFirst;
			if ((curVideoZoom-1)==0) prevlink = baseVideoUrl+videoItemsTot+'/';
				
			var nextlink = ((curVideoZoom-1)<videoItemsTot) ? baseVideoUrl+(curVideoZoom+1)+'/' : baseVideoUrlFirst;			
			if ((curVideoZoom)==videoItemsTot) nextlink = baseVideoUrlFirst;
			
			$('#videoboxprev').attr({href: prevlink});
			$('#videoboxnext').attr({href: nextlink});	
			
			//ajaxRefresh(videoPagUrl);
		}
	);

}

function videoSliderPrev(videoPagUrl) {

	videoHeaderWidth = $('.videoheaderslider').css('width');
	videoHeaderWidth = parseInt(videoHeaderWidth.replace('px',''));
	
	videoSlideWidth = $('.videozoom').css('width');
	videoSlideWidth = parseInt(videoSlideWidth.replace('px',''));

	var nowshowing;
	if (curVideoZoom == 1) { nowshowing = videoItemsTot; }
	else { nowshowing = curVideoZoom-1; }
	
	var zoomHeader = $(document.createElement('div'));
	zoomHeader.attr({id: 'headeritem'+(nowshowing)});
	zoomHeader.addClass('headeritem');
	$('.headeritem').css('width',videoHeaderWidth);
	zoomHeader.css('width',videoHeaderWidth);
	zoomHeader.html('<h2>'+videoItems[nowshowing][1]+'</h2><p>Postato il '+videoItems[nowshowing][2]+'. Condiviso da <em>'+videoItems[nowshowing][3]+'</em> persone.</p>');
	$('.videoheaderslider').css({ width: '200.1%', marginLeft: '-100%' });	
	zoomHeader.prependTo($('.videoheaderslider'));
	
	var zoomItem = $(document.createElement('div'));
	zoomItem.attr({id: 'videozoomitem'+nowshowing});
	zoomItem.addClass('videozoomitem');
	$('.videozoomitem').css('width',videoSlideWidth);
	zoomItem.css('width',videoSlideWidth);
	zoomItem.html('<iframe src="'+videoItems[nowshowing][0]+'" frameborder="0" allowfullscreen></iframe>')
	$('.videozoomslider').css({ width: '200.1%', marginLeft: '-100%' });
	zoomItem.prependTo($('.videozoomslider'));	
	
	$('.videoheaderslider').animate(
		{ marginLeft: '+=100%' },
		500,
		function() {
			$('#headeritem'+curVideoZoom).remove();
			$('.videoheaderslider').css({width:'100%'});	
		}
	);	
	
	$('.videozoomslider').animate(
		{ marginLeft: '+=100%' },
		500,
		function() {
			$('#videozoomitem'+curVideoZoom).remove();
			$('.videozoomslider').css({width:'100%', marginLeft:0});
			curVideoZoom=nowshowing;
			
			var prevlink = ((curVideoZoom-1)>1) ? baseVideoUrl+(curVideoZoom-1) : baseVideoUrlFirst;
			if ((curVideoZoom-1)==0) prevlink = baseVideoUrl+videoItemsTot;
			
			var nextlink = ((curVideoZoom-1)<videoItemsTot) ? baseVideoUrl+(curVideoZoom+1) : baseVideoUrlFirst;			
			if ((curVideoZoom)==videoItemsTot) nextlink = baseVideoUrlFirst;
			
			$('#videoboxprev').attr({href: prevlink});
			$('#videoboxnext').attr({href: nextlink});	
			
			//ajaxRefresh(videoPagUrl);
		}
	);	

}

// slider gallery sidebar
var galleryItems = 4;
var gallerySlideWidth;
var curGalleryPanel=1;

function gallerySliderInit() {
	
	gallerySlideWidth = $('.lanciogallery').css('width');
	gallerySlideWidth = parseInt(gallerySlideWidth.replace('px',''));	
	$('.galpanel').css('width',gallerySlideWidth);	
	
	var prev_loopPanel = $(document.createElement('div'));
	prev_loopPanel.attr({id: 'galpanel'+(galleryItems+1)});
	prev_loopPanel.addClass('galpanel');
	prev_loopPanel.css('width',gallerySlideWidth);
	prev_loopPanel.html($('#galpanel'+(galleryItems)).html());
	$('#galpanel1').before(prev_loopPanel);
	
	var next_loopPanel = $(document.createElement('div'));
	next_loopPanel.attr({id: 'galpanel'+(galleryItems+2)});
	next_loopPanel.addClass('galpanel');
	next_loopPanel.css('width',gallerySlideWidth);
	next_loopPanel.html($('#galpanel1').html());
	$('#galpanel4').after(next_loopPanel);	
	
	$('.galslider').css('width', gallerySlideWidth*6);
	$('.galslider').css('marginLeft', gallerySlideWidth*-1);
	
}

function gallerySliderNext() {
	$('.galslider').animate(
		{ marginLeft: '-='+gallerySlideWidth },
		500,
		function() {
			if (curGalleryPanel==galleryItems) {
				curGalleryPanel=1;
				$('.galslider').css('marginLeft', gallerySlideWidth*-1);
			} else {
				curGalleryPanel++;
			}
		}
	);	
}

function gallerySliderPrev() {
	$('.galslider').animate(
		{ marginLeft: '+='+gallerySlideWidth },
		500,
		function() {
			if (curGalleryPanel==1) {
				curGalleryPanel=galleryItems;
				$('.galslider').css('marginLeft', (gallerySlideWidth*-1)*galleryItems);
			} else {
				curGalleryPanel--;
			}
		}
	);	
}

// scrolla sino ad un determinato DIV (commenti)
function goToByScroll(id){
	pagingAutomatico=0;
	$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}
