	function loadAutoscroll(){
		//alert("start loadAutoscroll");
		
		var x = get_cookie("tmkautoscrollx");
		var y = get_cookie("tmkautoscrolly");
		
		
		if(x!=null && y!=null){
			//alert("load_autoscroll:scroll to="+x+","+y);
			window.scrollTo(x,y);
		}
		
		delete_cookie("tmkautoscrollx");
		delete_cookie("tmkautoscrolly");
		//alert("end loadAutoscroll");
	    return false;
	}
	
	function setAutoscroll(){
		//alert("start setAutoscroll");
		var x = 0;
		var y = 0;
		
		if (self.pageXOffset || self.pageYOffset)
              {
                      x = self.pageXOffset;
                      y = self.pageYOffset;
              }
               else if ((document.documentElement &&
				document.documentElement.scrollLeft)||(document.documentElement &&
				document.documentElement.scrollTop)) {
                      x = document.documentElement.scrollLeft;
                      y = document.documentElement.scrollTop;
              }
               else if (document.body)
              {
                      x = document.body.scrollLeft;
                      y = document.body.scrollTop;
              }

		
		
		//alert(x+","+y);
		set_cookie("tmkautoscrollx", x, get_expire_date_cookie());
		set_cookie("tmkautoscrolly", y, get_expire_date_cookie());
		//alert("end setAutoscroll");
	    return false;
	}
	
	function set_cookie ( name, value, expires )
	{
	
	  var cookie_string = name + "=" + escape ( value );
	
	  
	  //alert("set_autoscroll:expires=" + expires.toGMTString());
	  cookie_string += "; expires=" + expires.toGMTString();
	  
	  //alert(cookie_string);
	  document.cookie = cookie_string;
	  return false;
	}
	
	function get_cookie ( cookie_name )
	{
	  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
	
	  if ( results )
	    return ( unescape ( results[2] ) );
	  else
	    return null;
	}
	
	function delete_cookie ( cookie_name )
	{
		var cookie_date = new Date ( );  // current date & time
		cookie_date.setTime ( cookie_date.getTime() - 1 );
		document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
		return false;
	}
	
	function get_expire_date_cookie(){
		var expires = new Date ();
		var hrs = expires.getHours();
		hrs += 1; 
		expires.setHours(hrs); 
		return expires;
	}
	
	function detect(Event) {
	  if(Event==null) {
	  		Event=event;
	  }
	  return Event.keyCode;
	}
	
	function submitOnEnterForm(evt, form){
	    if( detect(evt) == 13){
	        document.forms[form].submit();
	    }else{
	        return true; 
	    }
	    return false;
	}
	
	function submitOnEnter(evt, button){
	    if( detect(evt) == 13){
	       $("#" + button).click();
	       return false;
	    }
	    return true;
	}
	
	function detectBrowserOldIE(){
	    var result=false;
		var browser=navigator.appName;
		var b_version=navigator.appVersion;
		var version=parseFloat(b_version);
		if ((browser=="Microsoft Internet Explorer")){
			if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
				 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
				 if (ieversion<7){
				 	 result = true;
				 }else{
					 $(".downloadIconMimeType").css("top", "1px");
				 }
			}
		}
		return result;
	}
	
	function jhbCollapseSearch(){
		$(".jhb-crud-form-search-content").slideToggle('fast');
		return false;
	}
	
	function jhbCollapseColumns(){
		$(".jhb-crud-form-columns-content").slideToggle('fast');
		return false;
	}
	
	function jhbLoadingCrud(){
		//alert("jhbBlockCrud 1");
		jhbUnblockCrud();
		$(".jhb-crud-body").block({ 
		   message: '<br /><br />Caricamento in corso...',  
           css: { width:'300px', height:'100px', border:'2px solid #02AEEE', color:'#0167AB', '-webkit-border-radius': '10px', '-moz-border-radius': '10px'  }  
		});  
		//alert("jhbBlockCrud 2");
		return false;
	}
	
	function jhbPopupCrud(message){
		 //alert("jhbBlockCrudMessage 1");
		 //jhbUnblockCrud();
		 $(".jhb-crud-body").block({ 
		   message: $(message),
		   css: { width:'400px', border:'2px solid #02AEEE', color:'#0167AB', '-webkit-border-radius': '10px', '-moz-border-radius': '10px' }  
		 });
		 
		 //alert("jhbBlockCrudMessage 2");
	     return false;
	}
	
	function jhbUnblockCrud(){
		//alert("jhbUnblockCrud 1");
		$(".jhb-crud-body").unblock();
		//alert("jhbUnblockCrud 2");
		return false;
	}
	
	function jhbSelectBooleanList(fieldName, value){
		$("input[type=checkbox][name$='" + fieldName + "']").each(
			function(){
				//alert('this.checked = ' + this.checked);
				this.checked = value;
			}
		)
	    return false;
	}
	
	$(document).ready(function(){		
		
		if(detectBrowserOldIE()){
			$(document).pngFix(); 
			$("#feedbackDiv").remove();
		}
		
		$(".jhb-crud-form-input-date").datepicker($.extend({showAnim:"slideDown",duration:"slow",showStatus:true})); 
		$(".jhb-crud-form-columns-open").click(jhbCollapseColumns);		
		$(".jhb-crud-form-search-open").click(jhbCollapseSearch);
		
		$(".jhb-crud-showloading").click(jhbLoadingCrud);
		$(".jhb-crud-showloading-change").change(jhbLoadingCrud);
		
		$(".jhb-crud-showupload").click(function() {
			jhbPopupCrud('#jhb-crud-upload');
		});
		
		if($("#jhb-crud-message").length>0){
			jhbPopupCrud("#jhb-crud-message");
		}
		
		if($(".jhb-crud-list-container").length>0){
			loadAutoscroll();
		}
		
		$(".tmk-crud-showupload").click(function() {
			jhbPopupCrud('#tmk-crud-upload');
		});
		
		$(".autocompleteKeywords").autocomplete({ 
			url: "/servlets/autocomplete",
			minChars:1,
		    multiple:false,
		 	scroll:false,
		 	scrollHeight:300,
		 	extraParams:{type:"keywords"}
		});
		
		$(".autocompleteCategories").autocomplete({ 
			url: "/servlets/autocomplete",
			minChars:1,
		    multiple:false,
		 	scroll:false,
		 	scrollHeight:300,
		 	extraParams:{type:"categories"}
		});
		
		$(".autocompleteCodeProducts").autocomplete({ 
			url: "/servlets/autocomplete",
			minChars:1,
		    multiple:false,
		 	scroll:false,
		 	scrollHeight:300,
		 	extraParams:{type:"codeproduct"}
		});
		
		$(".autocompleteRegions").autocomplete({ 
			url: "/servlets/autocomplete",
			minChars:1,
		    multiple:false,
		 	scroll:false,
		 	scrollHeight:300,
		 	extraParams:{type:"regions"}
		});
		
		$(".autocompleteProvinces").autocomplete({ 
			url: "/servlets/autocomplete",
			minChars:1,
		    multiple:false,
		 	scroll:false,
		 	scrollHeight:300,
		 	extraParams:{type:"provinces"}
		});
		
		$(".autocompleteCities").autocomplete({ 
			url: "/servlets/autocomplete",
			minChars:1,
		    multiple:false,
		 	scroll:false,
		 	scrollHeight:300,
		 	extraParams:{type:"cities"}
		});
		
		$(".autocompleteSupplierNames").autocomplete({ 
			url: "/servlets/autocomplete",
			minChars:1,
		    multiple:false,
		 	scroll:false,
		 	scrollHeight:300,
		 	extraParams:{type:"supplierNames"}
		});
		
		$(".autocompleteCustomerNames").autocomplete({ 
			url: "/servlets/autocomplete",
			minChars:1,
		    multiple:false,
		 	scroll:false,
		 	scrollHeight:300,
		 	extraParams:{type:"customerNames"}
		});
		
		$(".showloading").click(popupLoading);
		$(".showloadingChange").change(popupLoading);
		$(".showloadingChange input").click(popupLoading);
		
		if($("#application-messages").length>0){
			popup("#application-messages");
		}
		
		$(".collapsible").each(function(i){
			
			var openedImgGray = "/img/icons/showfilter.gif";
			var closedImgGray = "/img/icons/hidefilter.gif";
			var openedImgBlue = "/img/icons/showpanel.gif";
			var closedImgBlue = "/img/icons/hidepanel.gif";
			
			var collapsible = $(this);
			
			var collapsibleId = collapsible.find(".collapsible-div").attr("id");
			
			if(collapsibleId!=null && collapsibleId!=""){
				
				var currImg = collapsible.find(".collapsible-link img").attr("src");
				
				if(get_cookie("tmkcollapsed_" + collapsibleId)=="true" && (currImg==openedImgGray || currImg==openedImgBlue)){
					collapsible.find(".collapsible-div").slideToggle('fast');
					if(currImg == openedImgGray){
						collapsible.find(".collapsible-link img").attr("src",closedImgGray);
					}else if(currImg == openedImgBlue){
						collapsible.find(".collapsible-link img").attr("src",closedImgBlue);
					}
				}else if(get_cookie("tmkcollapsed_" + collapsibleId)=="false" && (currImg==closedImgGray || currImg==closedImgBlue)){
					collapsible.find(".collapsible-div").slideToggle('fast');
					if(currImg == closedImgGray){
						collapsible.find(".collapsible-link img").attr("src",openedImgGray);
					}else if(currImg == closedImgBlue){
						collapsible.find(".collapsible-link img").attr("src",openedImgBlue);
					}
				}
			}

			collapsible.find(".collapsible-link").click(function(){
			
				collapsible.find(".collapsible-div").slideToggle('fast');
				
				var currImg = $(this).find("img").attr("src");
				
				var cookievalue = "false";
				
				if(currImg == openedImgGray){
					currImg = closedImgGray;
					cookievalue = "true";
				}else if(currImg == closedImgGray){
					currImg = openedImgGray;
					cookievalue = "false";
				}else if(currImg == openedImgBlue){
					currImg = closedImgBlue;
					cookievalue = "true";
				}else if(currImg == closedImgBlue){
					currImg = openedImgBlue;
					cookievalue = "false";
				}
				
				$(this).find("img").attr("src",currImg);
				
				if(collapsibleId!=null && collapsibleId!=""){
					set_cookie("tmkcollapsed_" + collapsibleId, cookievalue, get_expire_date_cookie());
				}

			});
			
		});
		
		/*var offerListItemBoxConfig = {    
		     sensitivity: 3,
		     interval: 750,   
		     over: function(){
				$(this).find(".offerShareToolbar").slideDown("slow");
			 },   
		     timeout: 250,  
		     out: function(){
				$(this).find(".offerShareToolbar").slideUp("fast");
			 }
		};
		
		$(".offer-list-item-box").hoverIntent(offerListItemBoxConfig);*/
		
		/*$(".offer-list-item-box .offerShareToolbar img").hover(function(){
				var path = $(this).attr("src");
				path = path.replace(".png","") + "-s.png";
				$(this).attr("src", path);
			},
			function(){
				var path = $(this).attr("src");
				path = path.replace("-s.png","") + ".png";
				$(this).attr("src", path);
			}
		);*/
		
		$(".list-item-box").hover(function(){
				$(this).attr("class",'s-list-item-box');
			},
			function(){
				$(this).attr("class",'list-item-box');
			}
		);
		
		$(".thumbnail-list-item-box").hover(function(){
				$(this).attr("class",'s-thumbnail-list-item-box');
				$(this).find(".catalog-logo").slideDown('fast');
			},
			function(){
				$(this).attr("class",'thumbnail-list-item-box');
				$(this).find(".catalog-logo").slideUp('fast');
			}
		);
		
		$("a[class^='bookmarkedLink']").click(function(){
			var styleClass = $(this).attr("class");
			var rowId = styleClass.replace("bookmarkedLink","");
			var values = $("span[class='bookmarkFnCallValues" + rowId + "']", $(this)).text().split(";");
			var productId = values[0].replace("productId:","");
			var selected = values[1].replace("selected:","");
			bookmarkOffer(rowId,productId,selected);
		});
		
		$("a[class^='exaustedLink']").click(function(){
			var styleClass = $(this).attr("class");
			var rowId = styleClass.replace("exaustedLink","");
			var values = $("span[class='exaustFnCallValues" + rowId + "']", $(this)).text().split(";");
			var productId = values[0].replace("productId:","");
			var selected = values[1].replace("selected:","");
			exaustOffer(rowId,productId,selected);
		});
		
		/*
		$(".qTipOfferPreview").each(function(i){
			
			var url = $(this).find('.urlOfferThumbnail').attr('rel');
			
			if(url.length>0){
				var vtp = "";
				
				if(i<=2){
					vtp = "Top"; 
				}else if(i>=5){
					vtp = "Bottom"; 
				}else{
					vtp = "Middle"; 
				}
				
				$(this).qtip({		    	  
					content: {
						text: "<img src='" + url + "' style='width:175px;' />"
					},
						position: {
					    corner: {
				      			tooltip: 'right'+vtp,
				      			target: 'left'+vtp
					    }
					},
					style: {
						padding:0,
						margin:0,
					  	name: 'light', 
					    width: 175,
					  	border: {
					  		width: 1,
						    radius: 4
						},
					    tip:'right'+vtp
					}  
				});
			};
		});
		*/
		
		$(".qTipOfferPreview").each(function(i){
			
			var url = $(this).find('.urlOfferThumbnail').attr('rel');
			
			if(url.length>0){
				$(this).qtip({		    	  
					content: {
						text: "<img src='" + url + "' style='width:175px;' />"
					},
					position: {
					    corner: {
				      			tooltip: 'topLeft',
				      			target: 'bottomLeft'
					    }
					},
					style: {
						padding:0,
						margin:0,
					  	name: 'blue', 
					    width: 185,
					  	border: {
					  		width: 1,
						    radius: 4
						},
					    tip:'topLeft'
					}  
				});
			};
		});
		
		
		$(".qTipTooltipRight[title]").qtip({ 
		  	  position: {
			      corner: {
		      			tooltip: 'leftTop',
		      			target: 'bottomRight'
			      }
			  },
			  style: { 
			  	  name: 'blue', 
			  	  color: '#0167AB',
			  	  border: {
				      width: 1,
				      radius: 4
				  },
			      tip: 'leftTop'
			  }
		});
		
		$(".qTipTooltipTop[title]").qtip({ 
		  	  position: {
			      corner: {
		      			tooltip: 'bottomMiddle',
		      			target: 'topMiddle'
			      }
			  },
			  style: { 
			  	  name: 'blue', 
			  	  color: '#0167AB',
			  	  border: {
				      width: 1,
				      radius: 4
				  },
			      tip: 'bottomMiddle'
			  }
		});
		
		$(".qTipTooltipBottom[title]").qtip({ 
		  	  position: {
			      corner: {
		      			tooltip: 'topMiddle',
		      			target: 'bottomMiddle'
			      }
			  },
			  style: { 
			  	  name: 'blue', 
			  	  color: '#0167AB',
			  	  border: {
				      width: 1,
				      radius: 4
				  },
			      tip: 'topMiddle'
			  }
		});
				
		$("a[rel^='jpopup']").prettyPhoto();
		
		/*
		$(".stars-rating-offers").each(function(){
			var idProduct = $(this).find("select").attr("id");
			loadStarsRating(this, idProduct);
		});
		
		$(".stars-rating-to").stars({
			cancelShow: false,
	    	inputType: "select",
	    	disabled: true
		});
		*/
		
		$(".searchInputText").keydown(function(event){
		    submitOnEnter(event, 'SEARCH_BUTTON');
		});
		
		helpTextStyleSearch();
		
	});
	
	function loadStarsRating(object, idProduct){
		//alert("loadStarsRating " + idProduct);
		$(object).stars({
			cancelShow: false,
	    	inputType: "select",
	    	oneVoteOnly: true,
	    	callback: function(ui, type, value){
	    		//alert(value);
		        $.post(
					"/servlets/ajax",
					{type:'ratingOffer', idProduct:idProduct, rating:value}
				);
		    }
    	});
	}
	
	function preloadImages(){
		
		var imageObj = new Image();
	    var images = new Array();
	    
	    images[0]="http://localhost:8180/Tripmarket/img/icons/loading.gif"
	   
	   	for(i=0; i<=1; i++) 
	    {
	       imageObj.src=images[i];
	    }
	}
	
	function popupLoading(){
		 //alert("jhbBlockCrudMessage 1");
		 popupUnblock();
		 $("#bodyToBlock").block({ 
		   message: '<br /><br /><img src="/img/icons/loading.gif" style="vertical-align:middle;" /> Caricamento in corso...',  
		   css: { width:'300px', height:'100px', border:'2px solid #02AEEE', color:'#0167AB', '-webkit-border-radius': '10px', '-moz-border-radius': '10px'  }  
		 });
		 
		 //alert("jhbBlockCrudMessage 2");
	}
	
	function popup(message){
		 $("#bodyToBlock").block({ 
		   message: $(message),
		   css: { width:'400px', border:'2px solid #02AEEE', color:'#0167AB', '-webkit-border-radius': '10px', '-moz-border-radius': '10px' }
		 });
	}
	
	function popupUnblock(){
		$("#bodyToBlock").unblock();
	}
	
	function downloadLogo(address){
		window.open(address, '', 'width=400,height=400,menubar=no,toolbar=no,location=no')	
	}
	
	function showSupplierDetail(rowId){
		//alert(rowId);
		$(".supplierDetail" + rowId).slideToggle('fast');
		return false;
	}

	function showNoteDetail(rowId){
		//alert(rowId);
		$(".noteDetail" + rowId).slideToggle('fast');
		return false;
	}
	
	function bookmarkOffer(rowId,idProduct,selected){
		$.get(
			"/servlets/ajax",
			{type: 'bookmark', idProduct: idProduct, selected: selected},
			function(){
				
				if(selected){
					newSrc = "/img/icons/bookmark.jpg";
					selected = false;
				}else{
					newSrc = "/img/icons/nobookmark.jpg";
					selected = true;
				}
				
				$(".bookmarkedImage" + rowId).attr("src",newSrc);				
				$(".bookmarkedLink" + rowId).unbind("click");
				$(".bookmarkedLink" + rowId).bind(
					"click", 
					{rowId: rowId, idProduct: idProduct, selected: selected},
					function(event){
						bookmarkOffer(event.data.rowId, event.data.idProduct, event.data.selected);
					}
				);
			
			}
		);
		
	}
		
	function exaustOffer(rowId,idProduct,selected){
		$.get(
			"/servlets/ajax",
			{type: 'exaust', idProduct: idProduct, selected: selected},
			function(){
				
				if(selected){
					newSrc = "/img/icons/exausted.jpg";
					selected = false;
				}else{
					newSrc = "/img/icons/noexausted.jpg";
					selected = true;
				}
				$(".exaustedImage" + rowId).attr("src",newSrc);

				onclickLink = "exaustOffer('" + rowId + "','" + idProduct +"'," + selected + ")";
				
				$(".exaustedLink" + rowId).attr("onClick", onclickLink);
				
				$(".exaustedLink" + rowId).unbind("click");
				$(".exaustedLink" + rowId).bind(
					"click", 
					{rowId: rowId, idProduct: idProduct, selected: selected},
					function(event){
						exaustOffer(event.data.rowId, event.data.idProduct, event.data.selected);
					}
				);
				
			}
		);
	}
	
	function addNoteOffer(rowId,idProduct){
		var annotation = $(".noteTextarea" + rowId).val();
		$.get(
			"/servlets/ajax",
			{type: 'notes', idProduct: idProduct, annotation: annotation},
			function(){
				//alert(annotation);
				if(annotation.length>0){
					newSrc = "/img/icons/notes.jpg";
				}else{
					newSrc = "/img/icons/nonotes.jpg";
				}
				//alert(newSrc);
				$(".noteImage" + rowId).attr("src",newSrc);

			}
		);
	}
	
	function sendLogin(){
		var email = $("#emailAddress").val();
		//alert(email);
		$.get(
			"/servlets/subscribe",
			{type: 'sendlogin', email: email},
			function(){
				var htmlMessage = "La procedura per l'invio dei dati di accesso via mail e' stata attivata correttamente. <br /> I dati verrano inviati all'indirizzo email:<br />" + email + "<br /><br /><br /> <a href='/public/contacts.faces'>Per eventuali problemi contattare il supporto tecnico.</a>";
				//alert(htmlMessage);
				$("#lostPassword").html(htmlMessage);
			}
		);
	}
	
	function helpTextStyleSearch(){
		var helpTextValues = new Array("Cerca per parole chiave...","Cerca per parole chiave... (es. thailandia bungalow)"," Data..."," Prezzo..."," Codice...","Cerca Agenzia di Viaggio per nome...","Cerca Tour Operator per nome..."," Per regione..."," Per provincia...", " Per Tour Operator...");
		
		$(".helpSearchInputText").each(function(i){
	      if(jQuery.inArray(this.value, helpTextValues)==-1){
	      	$(this).removeClass("helpSearchInputText");
	      }
	    });
	    
		$(".helpSearchInputText").focus(function() {
	      $(this).removeClass("helpSearchInputText");
	    });
	    
	}
	
	function selectAllToFilter(checked){
		$(".offersToFilterPanel").find("input[type='checkbox']").attr("checked",checked);
	}
	
	function scrollableOffers(){
		$(".offers-scrollable")
		.scrollable({items:".offers-items", size:1, vertical:true, keyboard:false})
		.circular()
		.autoscroll({interval:3500, steps:1, autoplay:true});
		
	}
	
	function scrollableScreenshots(){
		var ssScrollable = $(".scrollable").scrollable({size:3, keyboard:false, api:true})
		   			
		$(".left").click(function (){
			ssScrollable.prevPage();
		});
		
		$(".right").click(function (){
			ssScrollable.nextPage();
		});
	}
