/**
 * script di inizializzazione richiamato in tutte le pagine del sito CFL
 */
//oggetto che contiene i parametri querystring
var cflUrlParams = {};
(function () {
    var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&=]+)=?([^&]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);

    while (e = r.exec(q))
       cflUrlParams[d(e[1])] = d(e[2]);
})();

$(document).ready (
	function () 
	{
	    $("a.izoom").fancybox({
	        'width'         : '100%',
	        'height'        : '100%',
	        'autoScale'     : false,
	        'transitionIn'  : 'elastic',
	        'transitionOut' : 'elastic',
	        'type'          : 'iframe'
	    }); 
		$('.mb-src-input input')
			.css({color:'#FFF'})
			.data('val', ('query' in cflUrlParams) ? cflUrlParams['query'] : '')
			.focus(function(e)
			{
				$(this).data('flocked', true);
			})
			.blur(function()
			{
				$(this).data('flocked', false);
				if($(this).data('hlocked'))
					return false;
				if($(this).val() != 'Cerca...')
					$(this).data('val', $(this).val());
				$(this).stop()
					 .animate(
					 {
						width:'45px',
						'background-color':'#444',
						color:'#FFF'
					 }, function()
						{
							$(this).val('Cerca...');
						});
			});
		$('#mbSrc')
			.hover(function()
			{
				var $field = $('.mb-src-input input', $(this));
				$field.data('hlocked', true);
				if($field.data('flocked'))
					return false;
				$field.val($field.data('val'))
					  .stop()
					  .css('font-weight', 'normal')
					  .animate(
					  {
						width:'150px',
						'background-color':'#FFF',
						color:'#333'
					  })
			},
			function()
			{
				var $field = $('.mb-src-input input', $(this));
				$field.data('hlocked', false);
				if($field.data('flocked'))
					return;
				if($field.val() != 'Cerca...')
					$field.data('val', $field.val());
					$field.stop()
						 .animate(
						 {
							width:'45px',
							'background-color':'#444',
							color:'#FFF'
						 }, function()
							{
								$field.val('Cerca...');
							});
			})
			.submit(function()
			{
				var $field = $('.mb-src-input input', $(this));
				$.cookie('sQuery',$field.val());
				$.cookie('iStart', 1);
			});	
		$('.div-bottone').hover(
			function(e)
			{
				$(this).addClass('hover-250')
			},
			function (e)
			{
				$(this).removeClass('hover-250')		
			})
			
		$('.div-bottone-220').hover(
			function(e)
			{
				$(this).addClass('hover-220')
			},
			function (e)
			{
				$(this).removeClass('hover-220')		
			})
			
		$('.div-bottone-209').hover(
			function(e)
			{
				$(this).addClass('hover-209')
			},
			function (e)
			{
				$(this).removeClass('hover-209')		
			})
			
		$('.banner-cfl-136').hover(
			function(e)
			{
				$(this).addClass('hover-136')
			},
			function (e)
			{
				$(this).removeClass('hover-136')		
			})
			
		$('.banner-108').hover(
			function(e)
			{
				$(this).addClass('hover-108')
			},
			function (e)
			{
				$(this).removeClass('hover-108')		
			})
			
		$('.banner-83, .bottone-dati').hover(
			function(e)
			{
				$(this).addClass('hover-83')
			},
			function (e)
			{
				$(this).removeClass('hover-83')		
			})
			
		$('.banner-pagina-154').hover(
			function(e)
			{
				$(this).addClass('hover-154')
			},
			function (e)
			{
				$(this).removeClass('hover-154')		
			})
		$('#tabs').tabs(
		{
			show: function(event, ui)
			{
				var elem = $('.tab-content', ui.panel)
				if (elem.html() == '') {
					elem.append('<h3>Caricamento dati in corso...</h3>');
					$('.testo', ui.panel).hide();
					elem.addClass('ajax-loading');
				}
				elem.load(elem.attr('rel'), function()
					{
						elem.removeClass('ajax-loading');
						$('.testo', ui.panel).show('fast');
					});	
			}
		});
		
		$('.dettaglio-news').fancybox(
			{
				width:500, 
				autoDimensions:false, 
				transitionIn:'fade',
				speedIn:700
			});
		$('.dett, .mb-pn-news').live('click',function () 
			{
				var cont, printurl;
				if($(this).attr('href') && $(this).attr('href') != '#')
				{
					cont = $(this).attr('href');
					printurl = $(this).attr('href');
				}
				else
				{
					cont = '/DettaglioNews.asp?id=' + $(this).attr('id');
					printurl = '/ajaxNews/DettaglioNewsStampa.aspx?pk=' + $(this).attr('id'); 
				}
				var myDialog = $('<div>' +
					'<div id="__contenuto" style="width:auto; height:100%; text-align:left; padding-right:15px"></div></div>');
				myDialog.dialog(
					{
				        width: 550,
				        height: 350,
				        title: 'News',
						show:'fade',
						hide:'fade',
						modal:true,
						resizable:true,
						buttons:{
							Stampa:function()
							{
								window.open(printurl,"print","location=1,status=0,width=850,height=650,scrollbars=1");
							},
							Chiudi:function () {$(this).dialog('close')}
						},
						close:function() {myDialog.remove()},
						open:function() 
							{
								$('#__contenuto').addClass('ajax-loading-mini')
												 .load(cont + ' #dettaglio_news',function()
												 	{
												 		$('#__contenuto').removeClass('ajax-loading-mini')
													});
							}
				    })
      			.css({padding:'15px 0 0 15px', overflow:'auto'});
				return false;
			});
		$('.bottone-evento-home a').hover(
			function()
			{
				var h = $(this).height();
				$(this).css('background-position', '0 -' + h + 'px');
			},
			function()
			{
				$(this).css('background-position', '0 0');
			});
		$('#nl_button').click(function(e)
		{
				var myDialog = $('<div>' +
					'<iframe id="nl_iframe" frameborder="0" width="100%" height="350" src="/mlist"></iframe></div>');
				myDialog.dialog(
					{
				        width: 500,
				        height: 400,
				        title: 'Iscriviti alla newsletter',
						show:'fade',
						hide:'fade',
						modal:true,
						resizable:false,
						buttons:{
							Chiudi:function () {$(this).dialog('close')}
						},
						close:function() {myDialog.remove()}
				    })
      			.css({padding:'15px 0 0 15px', overflow:'hidden'});
				return false;
		});
		$('#menu-cfl li a').hover( function(){
			$(this).css('background-position','center -96px');
		}, function () {
			$(this).css('background-position', 'center 0px');
		})
	}
)

