$(document).ready(function(){	 
	if($.fn.tooltip){
		$('img.jTip').tooltip({showURL: false});		
		$("a.jTip").tooltip({ 
			bodyHandler: function() { 
					return $($(this).attr("href")).html(); 
			}, 
			showURL: false 
		});
	}
													 
	/* cleaning des champs input on focus/blur */
	$.fn.search = function(type) {
		return this.focus(function() {					 
			if( this.value == this.defaultValue ) {
				this.value = "";
			}
		}).blur(function() {
			if( !this.value.length ) {
				this.value = this.defaultValue;
			}
		});
	};
	$('.jClean').search();
	
	
	/* opening yellow box attivare */
	$('.jConfronta-off, .jVerifica-off').click(function(){
		var box = $(this).next();
		var c = {	on:'on',off:'off'	};
		var state = $(this).attr('class').split('-'); 
		if(state[1] == c.on){
			$(this).attr('class', state[0]+"-"+c.off);
			box.hide();
		} else if(state[1] == c.off) {
			$(this).attr('class', state[0]+"-"+c.on);
			box.show();
		}
		return false;												
	});
	
	
	$('.table04').each(function(){
		var table = this;
		$('.jswitcher', this).click(function(){
			var jtoggle = $('.jtoggle', table);
			var visible = jtoggle.css('display') != 'none';
			var jswitcher = $('.jswitcher', table);
			var src = this.src;
			if(visible){
				jswitcher.attr('src', src.replace('-on', '-off'));
				jtoggle.hide();
			} else {
				jswitcher.attr('src', src.replace('-off', '-on'));
				jtoggle.show();
			}
		});						
	});
	
	
	/* popup link */
	$('.jPopup').click(function(){
		var w = getParam(this.href, 'w') || 600;
		var h = getParam(this.href, 'h') || 400;
		var l = ($(window).width())/2 - w/2;
		var t = ($(window).height())/2 - h/2;
		var s = getParam(this.href, 's') || 1;
		window.open(this.href, '','top='+t+', left='+l+', width='+w+',height='+h+', scrollbars='+s+', resizable=1');
		return false;
	});
	
	
});


/* parsing of url parameters */
function getParam(url, name){
	var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( url );
	if( results == null )
		return "";
	else 
		return results[1];
}


/* single offer page : toggle of title */
(function($){			
	$.fn.jToggle = function(){
		var titleClass = 'bstitle';
		var retract = function(o){
			$(o).attr('class', offClass);
			var title = $(o).parent();
			title.removeClass(titleClass);
			title.next().hide();	
		}
		var expand = function(o){
			$(o).attr('class', onClass);
			var title = $(o).parent();
			title.addClass(titleClass);
			title.next().show();	
		}
		
		var toggle = function(){ if(open(this)){ retract(this) } else { expand(this);	}return false;}
		var open = function(o){	return ($(o).attr('class') == 'jToggleOn');	}
		this.bind('click', toggle);
		if(!open(this)) retract(this);
	}
	//Auto run
	$(function(){ 
		onClass = 'jToggleOn';
		offClass = 'jToggleOff';
		$('.'+onClass+', .'+offClass).jToggle();
	});
})(jQuery);

(function($){			
	$.fn.jExpand = function(){
		var toggle = function(){
			var shcontent = $($(this).attr('href'));
			if(shcontent.is(':visible')){
				shcontent.hide();	
				$(this).removeAttr('style');
			} else {
				shcontent.show().css('padding-left', '15px');	
				$(this).css('background-image', 'url(/img/layout/icon-expand-on.gif)');
			}
			return false;
		} 
		
		this.each(function(){
			var shcontent = $($(this).attr('href')).hide();								 
		}).bind('click', toggle);
	}
	//Auto run
	$(function(){ 
		$('.jExpand').jExpand();
	});
})(jQuery);