// 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);

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

  // Webkit text size adjust
  $("body").css("-webkit-text-size-adjust", "none");

	// Clear form fields
	$("#find, #name, #subject, #email, #message, #nospam").search();
	
	// Background-Position
  	$('#nav_main a')
	.css( {backgroundPosition: "0 30px"} )
	.mouseover(function(){
		$(this).stop().animate(
			{backgroundPosition:"(0 0)"}, 
			{duration:200})
	})
	.mouseout(function(){
		$(this).stop().animate(
			{backgroundPosition:"(0 30px)"}, 
			{duration:100})
	})
    
	// External Links
	$("a[rel='external']").attr('target', '_blank');

	// Slide to top
  $("a[rel='slider']").click(function() {
    $("html, body").animate({ scrollTop : 0 }, "slow");
        return false;
    });
	
	// Slide note
  $('<div id="promo"></div>').insertBefore('#home').slideNote({ 
    url: "ajax.html",
    closeImage: "css/pic/slidenote.close.png",
    displayCount: 1,
    where: 0
  });
	
	// Colorbox
	$("a[rel='colorbox']").colorbox();
	$("a[rel='map']").colorbox({width:"80%", height:"80%", iframe:true});

});
