 /**
 * jsLoader - Load js code using jQuery
 *   http://labs.ansa.it/slideshow
 *
 * Author: Angelo Marrara (marrara@ansa.it)
 * Copyright: free
 *
 * Built on top of the jQuery library
 * http://jquery.com
 *
 */

//
// private function to parse the query string in your URL to grab certain values.
//
function gup( name ) {
 	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
 	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function parse(queryTerms) {
	if (queryTerms.toLowerCase().indexOf(" and ")==-1 && queryTerms.toLowerCase().indexOf(" or ")==-1) {
		return trim(queryTerms, null).replace(/\s+/g," and ");	
	}
	else
		return queryTerms;
}

var itemIndex = 0; 
var array     = null;

function rsSlideshow() {
// Special case to handle search result (multimedia panel)
  if (document.location.href.indexOf('what=')!=-1) {
	var fileContent = "";
	var path = document.location.pathname.substring(document.location.pathname.lastIndexOf('/'));
	var queryString = document.location.href.substring(document.location.pathname.lastIndexOf('?'));
	what = gup('what')
	$('#nav-selezione').html('<a class="btn-lst" href="javascript:performSearch(gup(\'from\'),gup(\'nentries\'), \'ind-multimedia\', gup(\'what\'));">Ritorna alla lista</a>');
	if (gup('what').length>0) {
		queryString = 'titolo=('+ parse(unescape(what)) +') AND idtipologia='+idTip;
		$('.path').html('Risultati della ricerca: <strong>'+unescape(what)+'</strong>');
	} else {
		queryString = 'idtipologia='+idTip;
	}
	$.post('/nseevo/newsindexed', { query: queryString, nentries: 100,  from: 0, sort: 'datetime desc', template: 'index_search.xsl' }, function(data){
		fileContent = data;
	array = $("<div>").html(fileContent).find('.hentry').find('a:eq(0)');
	for (var i=0; i <= array.length-1; i++) {
		if (i <= array.length  && ($(array[i]).attr('href').indexOf(path)!=-1)) {
			itemIndex = i;
			$('.nav-lst strong').html((i+1)+' di '+array.length);
		}
	}
	});
	//performSearch(gup('from'),gup('nentries'), 'ind-multimedia', gup('what'));
  }
}

function idxSlideshow() { 
  if (typeof(urlIndex)!='undefined') {
	var path = document.location.pathname.substring(document.location.pathname.lastIndexOf('/'));
	var fileContent = $.ajax({
		type: "GET",
 		url: urlIndex,
 		async: false
	}).responseText;

	array = $("<div>").html(fileContent).find('.element').find('li a:eq(0)');

	for (var i=0; i <= array.length-1; i++) {
		if (i <= array.length  && ($(array[i]).attr('href').indexOf(path)!=-1)) {
			itemIndex = i;
			$('.nav-lst strong').html((i+1)+' di '+array.length);
		}
	}
  }
}

function nextItem() {
	if (array.length==0) return;

	if (itemIndex < array.length -1) {
		i = itemIndex+1;
	} else {
		i = 0;
	}
	if (document.location.href.indexOf('what=')!=-1) {
		href= unescape($(array[i]).attr('href'));
		loc = href.substring(href.indexOf('"')+1, href.indexOf('?'))+document.location.href.substring(document.location.href.indexOf('?'))
		document.location = loc;	
	} else {
		document.location = $(array[i]).attr('href');
	}
}

function prevItem() {
	if (array.length==0) return;

	if (itemIndex >0) {
		i = itemIndex-1;
	} else {
		i = array.length-1;
	}

	if (document.location.href.indexOf('what=')!=-1) {
		href= unescape($(array[i]).attr('href'));
		loc = href.substring(href.indexOf('"')+1, href.indexOf('?'))+document.location.href.substring(document.location.href.indexOf('?'))
		document.location = loc;	
	} else {
		document.location = $(array[i]).attr('href');
	}
/*
	if (itemIndex >0) {
		document.location = $(array[itemIndex-1]).attr('href');
	} else {
		document.location = $(array[array.length-1]).attr('href');
	}
*/
}




