   
   var livesite = "http://www.tipicodelsalento.it";
   // Replaces all instances of the given substring.
   String.prototype.replaceAll = function(
   strTarget, // The substring you want to replace
   strSubString // The string you want to replace in.
   ){
   var strText = this;
   var intIndexOfMatch = strText.indexOf( strTarget );
    
   // Keep looping while an instance of the target string
   // still exists in the string.
   while (intIndexOfMatch != -1){
   // Relace out the current instance.
   strText = strText.replace( strTarget, strSubString )
    
   // Get the index of any next matching substring.
   intIndexOfMatch = strText.indexOf( strTarget );
   }
    
   // Return the updated string with ALL the target strings
   // replaced out with the new substring.
   return( strText );
   }

jQuery(document).ready(function(){

	

	jQuery("select#maybay_cat").selectmenu({
		style:'dropdown',
		width:150,
		height:300
	});
	
	jQuery("select#maybay_cat").selectmenu({
		change: function(e, object){
			jQuery('#vmlp_search_str').flushCache();
		}
	});

	//tag_cloud();
	
	ajax_search();
	
	adesivi_colori();
	
	jQuery("#h1").trigger("click");
	
	jQuery('span.leggitutto').click(function(event) {
		jQuery('#descrizione').css("height","99%");
		jQuery('span.nasconditutto').css("display","inline");
		jQuery('span.leggitutto').css("display","none");
	});
	
	jQuery('span.nasconditutto').click(function(event) {
		jQuery('#descrizione').css("height","100px");
		jQuery('span.nasconditutto').css("display","none");
		jQuery('span.leggitutto').css("display","inline");
	});
	
	jQuery('#tagList a.termine').click(function() { 
		jQuery('#vmlp_search_str').val(jQuery(this).text());
		jQuery('#vmlp_search_btn').trigger('click');
	});
	
	
	
    
	});
	
	
	
	function adesivi_colori() {
	 
	 
	jQuery('#h1').click(function(event) {
	
		var page = jQuery('#h1').attr("href");
		
		jQuery('div.rel_prod_top td.h2').removeClass('sel');
		jQuery('div.rel_prod_top td.h1').addClass('sel');
		
		jQuery('#result-colori').html("<img src='"+livesite+"/js/loadingAnimation2.gif' />");
			
		jQuery('#result-colori').load(page +' #vmCartContainer').slideDown();
			
		return false;
	
	
	}); 
	
	jQuery('#h2').click(function(event) {
	
		var page = jQuery('#h2').attr("href");
		
		jQuery('div.rel_prod_top td.h1').removeClass('sel');
		jQuery('div.rel_prod_top td.h2').addClass('sel');
		
		jQuery('#result-colori').html("<img src='"+livesite+"/js/loadingAnimation2.gif' />");
			
		jQuery('#result-colori').load(page +' #vmCartContainer').slideDown();
			
		return false;
	
	
	});
	
	}
	
	
	function suggest_cat(keyword) {
	search_string = keyword;
	search_string = search_string.replaceAll(' ','%20');
		if (search_string) {
			page = livesite + "/suggest_cat.php?keyword="+search_string+"&cat="+jQuery('#maybay_cat').selectmenu("value");
			jQuery('#suggest_cat').load(page +' #risultato').slideDown();
			jQuery('div.ac_results').append(jQuery('#suggest_cat'));
			//jQuery('#suggest_cat').css("display","block");
			search_string = "";
		}
	}
	
	function ajax_search() {
	
	
	
	jQuery("#vmlp_search_str").autocomplete(livesite+"/suggest.php", {
		width: 440,
		max: 10,
		minChars: 2,
		scrollHeight: 250,
		selectFirst: false,
		highlightItem: false,
		extraParams: {
			cat: function() { return jQuery('#maybay_cat').selectmenu("value"); }
		},
		formatItem: function(data, i, n, value) {
			var temp = data.toString();
			img_url = livesite+"/components/com_virtuemart/shop_image/product/";
			temp2 = "<img src='" + img_url + temp.split(",")[1] + "'/> " + temp.split(",")[0];
			suggest_cat(jQuery("#vmlp_search_str").attr("value"));
			return temp2; 
		},
		formatResult: function(data, value) {
			var temp = data.toString();
			//alert(temp.split(",")[0]);
			return temp;
		}
	
	}).flushCache();
	
	jQuery('#vmlp_search_str').result(function(event, data, formatted) {
		var temp = data.toString();
		url = livesite+"/index.php?page=shop.product_details&option=com_virtuemart&product_id=" + temp.split(",")[2];
		window.location = url;
		jQuery('#vmlp_search_str').flushCache();
		return temp.split(",")[0];
	});
	
	
	
	jQuery("#suggest_cat").mouseover(function() {
		jQuery("div.ac_results li").removeClass("ac_over");
    });
	  
	jQuery("#suggest_cat ul li a").mouseover(function() {
		this.addClass("ac_over");
    });
	
	

	
	/*$("#imageSearch").autocomplete("images.php", {
		width: 320,
		max: 4,
		highlight: false,
		scroll: true,
		scrollHeight: 300,
		formatItem: function(data, i, n, value) {
			return "<img src='images/" + value + "'/> " + value.split(".")[0];
		},
		formatResult: function(data, value) {
			return value.split(".")[0];
		}
	});*/
	
	jQuery('#vmlp_search_str').focusin(function() {
        jQuery('#vmlp_search_str').attr("value","");
		jQuery('#vmlp_search_str').flushCache();
		
    });
	


	
	 
	
	jQuery('#vmlp_search_btn').click(function(event) {
	
			var search_string = jQuery('#vmlp_search_str').val();
			
			search_string = search_string.replaceAll(' ','%20');
			
			if (search_string.length > 2) {
				
				jQuery('#vmMainPage').html("");
				page = livesite+"/livesearch.php?keyword="+search_string;	
				
				jQuery('#vmMainPage').html("<img src='"+livesite+"/js/loadingAnimation2.gif' />");
				
				jQuery('#vmMainPage').load(page +' #risultato').slideDown();
				search_string = "";
				}
			
	
	});
	
	}
	
	function tag_cloud() {
	
	//get tag feed
		jQuery.getJSON(livesite+"/tagcloud/tagcloud.php?callback=", function(data) {
		
			//create list for tag links
			jQuery("<ul>").attr("id", "tagList").appendTo("#tagCloud");
			
			//create tags
			jQuery.each(data.tags, function(i, val) {
				
				//create item
				var li = jQuery("<li>");
				
				//create link
				//jQuery("<a>").text(val.tag).attr({title:"Effettua ricerca per " + val.tag, href:"http://www.mybay.it/it/Vedi-tutti-i-prodotti-del-negozio.html?keyword2=&search=Cerca&search_category=&search_limiter=anywhere&search_op=and&keyword1=" + val.tag + ""}).appendTo(li);
				jQuery("<a class='termine'>").text(val.tag).appendTo(li);
				
				//set tag size
				li.children().css("fontSize", (val.freq / 100 < 1) ? val.freq / 100 + 1 + "em": (val.freq / 100 > 2) ? "2em" : val.freq / 100 + "em");
				
				//add to list
				li.appendTo("#tagList");

			});
		});
	}
	

