jQuery.fn.fadeIn = function(speed, callback) {
    return this.animate({opacity: 'show'}, speed, function() {
        if (jQuery.browser.msie)
            this.style.removeAttribute('filter');
        if (jQuery.isFunction(callback))
            callback();
    }); 
}; 
jQuery.fn.fadeOut = function(speed, callback) {
    return this.animate({opacity: 'hide'}, speed, function() {
        if (jQuery.browser.msie)
            this.style.removeAttribute('filter');
        if (jQuery.isFunction(callback))
            callback();
    }); 
}; 



// QUANDO LA PAGINA E' PRONTA

// QUANDO RIDIMENSIONO LA FINESTRA DEL BROWSER
$(window).resize(function(){
	ridimensiona();
});
// RIDIMENSIONA CONTAINER E IMG SLIDESHOW
function ridimensiona(){
	var imgWidth = 2500;
	var imgHeight = 2000;
	
	var bh = imgWidth/imgHeight;
	var screenWidth = $(window).width();
	var screenHeight = $(window).height();
	if(screenWidth<950) screenWidth=950;
	if(screenHeight<600) screenHeight=600;
	$('#container').hide();
	$('#container').css({'width':screenWidth,'height':screenHeight});
	if (screenWidth>screenHeight){
		var rapporto = screenWidth/screenHeight;
		if (rapporto>bh){
			$('#container img.bg').css({'width':screenWidth,'height':screenWidth/bh});
		}else{
			$('#container img.bg').css({'width':screenHeight*bh,'height':screenHeight});
		};
	}else{
		$('#container img.bg').css({'width':screenHeight*bh,'height':screenHeight});
	};
	$('#container').show();
}

$.fn.centerInClient = function(options) {
/*     /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>    
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
    ///  and attached to the body element to ensure proper absolute positioning. 
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" /> */
    var opt = { forceAbsolute: false,
				url : '',
                container: window, // selector of element to center in
				width : 420,
				height : 600,	
                completeHandler: null
              };
    $.extend(opt, options);
   
    return this.each(function(i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;
		var inside = el.children();

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).hide().delay(5000).fadeIn(1000);; //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 


		if (opt.url != '') {
		//var close = '<a href="#" class="close" style="display:block;"><img src="img/close.png" height="25" class="btn_close" title="Close Window" alt="Close" /></a>';
		var close = '<a href="#" class="close" style="display:block;color:#000;">X</a>';
			inside.load(opt.url);
			el.append(close);
		}
        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

        el.css("left", x + jWin.scrollLeft());
        el.css("top", y + jWin.scrollTop());
        el.css("z-index", '10000');
		$('.close').css({
				position:'absolute',
				top: '-' + (opt.height/2 + 20) + 'px',
				right: (opt.width/2 - 20) + 'px'
				});

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
			
		/* object.click(function(){
			el.fadeOut(3000);
		}); */
		//el.bind('click', function(){
		$('#fade').bind('click', function(){
			el.remove();
			$(this).remove();
			return false;
		});
		$('.close').bind('click', function(){
			el.remove();
			$('#fade').remove();
			return false;
		});
    });
}


$(function()
			{
			function switchText()
				{
					if ($(this).val() == $(this).attr('title'))
						$(this).val('').removeClass('exampleText');
					else if ($.trim($(this).val()) == '')
						$(this).addClass('exampleText').val($(this).attr('title'));
				}
	
			$('input[type=text][title!=""]').each(function() {
				if ($.trim($(this).val()) == '') $(this).val($(this).attr('title'));
				if ($(this).val() == $(this).attr('title')) $(this).addClass('exampleText');
			}).focus(switchText).blur(switchText);
			$('textarea[title!=""]').each(function() {
				if ($.trim($(this).val()) == '') $(this).val($(this).attr('title'));
				if ($(this).val() == $(this).attr('title')) $(this).addClass('exampleText');
			}).focus(switchText).blur(switchText);
		
			$('form').submit(function() {
				$(this).find('input[type=text][title!=""]').each(function() {
					if ($(this).val() == $(this).attr('title')) $(this).val('');
				});
				$(this).find('textarea[title!=""]').each(function() {
					if ($(this).val() == $(this).attr('title')) $(this).val('');
				});
			});
		});
