// JavaScript Document

// Funzione di ricerca 
$.fn.search = function() {
  $.each(this, function(){
//  alert($(this).attr('value') +'\n'+ $(this).attr('title') );

	if( $(this).attr('value') == '' ) $(this).attr('value', $(this).attr('title') );
	if( $(this).attr('value') != $(this).attr('title') )
	{
      $(this).removeClass( 'empty' );
      $(this).addClass( 'filled' );
	}
	else
	{
      $(this).removeClass( 'filled' );
      $(this).addClass( 'empty' );
  }

  return $(this).focus(function() {
    if( this.value == this.title ) {
      this.value = "";
      $(this).removeClass( 'empty' );
      $(this).addClass( 'filled' );
//      this.style.color = "#000000";
//      this.style.background = "#ffffcc";
    }
  }).blur(function() {
		if( !this.value.length || ( this.value == this.title ) ) {
      this.value = this.title;
      $(this).removeClass( 'filled' );
      $(this).addClass( 'empty' );

//      this.style.color = "#dfdbd0";
//      this.style.background = "#ffffff";
		}
	});
	
} )};

// Anchor Slider by Cedric Dugas - http://www.position-absolute.com
jQuery.fn.anchorAnimate = function(settings) {
  settings = jQuery.extend({
    speed : 600 //1100
}, settings);	
	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault();
			var locationHref = window.location.href;
			var elementClick = $(caller).attr("href");
			if( ( elementClick[0]!='#' ) && ( elementClick.indexOf('#') > 0 ) ) elementClick = elementClick.substr( elementClick.indexOf('#') );
      var destination = $(elementClick).offset().top;
      $("html:not(:animated),body:not(:animated)").animate({
        scrollTop: destination
      }, settings.speed, function() {
        window.location.hash = elementClick
      })
		  	return false;
		})
	})
}

// Plugin per la visualizzazione video in Flash
$.fn.activateSWFObject = function() {
  $.each(this, function(){
    var s1 = new SWFObject('media/player.swf','player','400','300','9');
    s1.addParam('allowfullscreen','true');
    s1.addParam('allowscriptaccess','always');
    s1.addParam('flashvars','file=' + $(this).attr('href') );
    s1.write('preview');
  } );
 }

// Validazione newsletter
function RITisName(what,fname)
{
var i=new RegExp("([a-zA-Z\ ]{3,})");
if(!i.test(what)) { alert("Il "+fname+" inserito non è valido!"); return false}
return true;
}
function RITisEmail(what)
{
var i=new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,6}|[0-9]{1,6})(\\]?)$");
if(!i.test(what)) { alert("E-mail inserita non è valida!"); return false}
return true;
}
function RITvalidateForm(frm)
{
if(!RITisName(frm.nome.value,'NOME')) return false;
if(!RITisName(frm.cognome.value,'COGNOME')) return false;
if(!RITisEmail(frm.emailnl.value)) return false;
return confirm("Confermi l'inserimento dei dati?");
}

// Inizializzazione
$(document).ready(function() {

  // Colorbox
    $("a[rel='colorbox']").colorbox({});
    $("a[class='info']").colorbox({iframe:true, width:"640px", height:"480px"});
    
    /* Nascondo elementi in Flash */
    $().bind('cbox_open', function(){
    $('object, embed').css({'visibility':'hidden'});
      })
    
    .bind('cbox_closed', function(){
    $('object, embed').css({'visibility':'inherit'});
    }); 

  // Clear form fields
    $("#find, #markup, #sheet").search();
    $("#name, #email, #message, #subject, #nospam").search();
    $("#cognome, #nome, #emailnl").search();

	// External Links
	$("a[rel='external']").attr('target', '_blank');

	// Anchor Slider by Cedric Dugas - http://www.position-absolute.com
	$("a[rel='slider']").anchorAnimate();
  
  // Plugin per la visualizzazione video in Flash
  $("a.SWFObject").activateSWFObject();
  
});
