// JavaScript Document

// Clear form fields
$.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";
		}
	});
	
} )};

// Background-Position Animations - by Alexander Farkas v. 1.1
// jquery.bgpos.js

(function($){

	if(!document.defaultView || !document.defaultView.getComputedStyle){
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force){
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}
})(jQuery);

(function($) {

	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}
	
	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			
			var start = $.curCSS(fx.elem,'backgroundPosition');
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
			
			start = toArray(start);
			fx.start = [start[0],start[2]];
			
			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0],end[2]];
			
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);

// 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;
		})
	})
}

// Inizializzazione
$(document).ready(function() {

  // Background-Position
  	$('#nav a')
	.css( {backgroundPosition: "0 40px"} )
	.mouseover(function(){
		$(this).stop().animate(
			{backgroundPosition:"(0 0)"}, 
			{duration:100}) // 200
	})
	.mouseout(function(){
		$(this).stop().animate(
			{backgroundPosition:"(0 40px)"}, 
			{duration:100})
	})

  // Clear form fields
	$("#from, #fullname, #subject, #message, #tt").search();

  // External links
  $("a[rel='external']").attr('target', '_blank');

  // Slider
  $("a[rel='slider']").anchorAnimate();

  // Colorbox
  $("a.colorbox").colorbox();
  $("a.pano").colorbox({width:"95%", height:"95%", iframe:true});	
  $("a[rel='info']").colorbox({width:"640", height:"480", iframe:true});
  
  /* Nascondo elementi in Flash */
  $().bind('cbox_open', function(){
  $('object, embed').css({'visibility':'hidden'});
   })
    
  .bind('cbox_closed', function(){
  $('object, embed').css({'visibility':'inherit'});
  }); 
  
});
