var mDG = {
	forms : {
		/**
		 * Quando nell'azione form che riporta errori uno dei link viene cliccato
		 * il campo correlato riceve il focus
		 */
		errorFocus : function () {
			var href = this.href || '';
			var hrefArray = href.split('#'); 
			if (hrefArray.length > 1) {
				$('#'+hrefArray[1])[0].focus();
				return false;
			}
		},
		
		/**
		 * Hides selected elements 
		 * @param {Object} [selector] A CSS selector, defaults to 'input.special'
		 */
		hideSpecial : function (selector) {
			$((selector || 'input.special')).hide();
		}
	},
	
	/**
	 * Apre il link in una finestra popup
	 */
	popupNewWindow : function () {
		var href = this.href || null;
		if (href) {
			params = 'width=800'
			params += ', height=600'
			params += ', top=100, left=100'
			params += ', resizable=1, scrollbars=1, location=1, menubar=1';
			newwin = window.open(href, 'newwin', params);
			if (window.focus) {
				newwin.focus()
			}
			return false;
		}
	},
	
	/**
	 * Forza l'apertura degli elementi indicati in una nuova finestra
	 * 
	 * @param {String} [selector] Il selettore jQuery, default:  'a.external, a[rel*=external]'
	 * @param {Object|jQuery} [context] Un contesto opzionale default: document
	 */
	forceNewWindow : function (selector,context) {
		var selector = selector || 'a.external, a[rel*=external]';
		jQuery(selector, context || document)
		.click(function () {
			window.open(this.href);
			return false;
		});
	},
	
	/**
	 * Gestisce il mouseOver per le immagini che lo prevedono
	 */
	imgMouseOver : function() {
		var $this = $(this);
		var src = $this.attr('src');
		var extension = src.substring(src.lastIndexOf('.'),src.length);
		$this.hover(
			function() {
				$(this).attr('src',src.replace(extension,'-on' + extension));
			},
			function() {
				$(this).attr('src',src);
			}
		);
	},
	
	search : {
		toggleAdvancedHelp : function (trigger, container, context) {
			var context = context || '#advSearchHelp';
			var $trigger = $(trigger || 'dt:first',context);
			var $container = $(container || 'dd:first',context);
			$container.hide();  
			$trigger
			.addClass('clickable')
			.attr('title','Clicca per mostrare le opzioni avanzate di ricerca')
			.click(function () {
				$container.slideToggle('slow');
			});
		}
	}
};


$(document).ready(function() {
	
	// gestione IMG HOVER
	$(".icsmMouseOver").each(mDG.imgMouseOver);
	
	// sets focus on incorrect forms elements
	$('.txtError li a').click(mDG.forms.errorFocus);
	
	// binds the popup method to all elements '.popup'
	$('.popup').click(mDG.popupNewWindow);
	
	// force to open in new window
	mDG.forceNewWindow();
	mDG.forms.hideSpecial();
	//mDG.search.toggleAdvancedHelp();
	
	// Fixing IE 5/6 issues with PNG transparency
	$(document).pngFix();

});