(function($) {

  var PRMA = {}
  
  PRMA.SelfLabeledInput = function() {
    var selector = arguments[0] || 'label';
    $(selector).each( function() {
      $(this).hide();
      var item = $('input[@id="'+$(this).attr('for')+'"]');
      var value = $(this).html();
      if (item.val() == '') {
        item.val(value);
      }
      item.blur(function() {
        if ($(this).val() == '') {
          $(this).val(value);
        }
      });
      item.focus(function() {
        if ($(this).val() == value) {
          $(this).val('');
        }
      });
    });
  };
  
  PRMA.SeparateListItems = function() {
    var items = arguments[0] || 'li a';
    $(items).each( function(index, el) {
      if (index < $(items).size() + 1) {
        $(el).after('>');
      }
    });
  };
  
  PRMA.SeparateNavItems = function() {
    var items = arguments[0] || 'li a';
    $(items).each( function(index, el) {
      if (index < $(items).size() - 1) {
        $(el).after('|');
      }
    });
  };  
  
  PRMA.InitTextarea = function() {
    var items = arguments[0] || 'textarea';
    var init_text = arguments[1] || 'Votre texte ici...';
    $(items).each( function(index, el) {
      if ($(el).val() == '') {
        $(el).val(init_text);
      }
      $(el).focus(function() {
        if ($(el).val() == init_text) {
          $(el).val('');
        }
      });
      $(el).blur(function() {
        if ($(el).val() == '') {
          $(el).val(init_text);
        }
      });
    });
  };
  
	PRMA.formColor = function() {
	    var form_elements = arguments[0] || 'form input, form textarea';
		$(form_elements).each( function(index, el) {
	      $(this).focus( function() {
			 jQuery(this).prev('label').css( 'background', 'url(/css/img/puce_detoure.gif) left 3px no-repeat' );	
			if(jQuery(this).attr('class') == 'text precision'){	
				return false;
			}
			jQuery(this).prev('label').css( "left", "-16px" );				
			jQuery(this).css( "left", "-16px" );
			jQuery(this).prev('label').css( "padding-left", "16px" );	
		  })
		  .blur( function() {
			 jQuery(this).prev('label').css( 'background', "none" );
			 if(jQuery(this).attr('class') == 'text precision'){
				return false;
			 }
			 jQuery(this).prev('label').css( "left", "0" );	
			 jQuery(this).css( "left", "0" );		 
			 jQuery(this).prev('label').css( "padding-left", "0" );	
		  });
	    });
	  };  
  
  $(document).ready( function() {
    PRMA.SelfLabeledInput('#recherche label');
		PRMA.SelfLabeledInput('#newsletter-subscription label');
		PRMA.SelfLabeledInput('#affine_article label');
		PRMA.SeparateNavItems('#nav_interne li a');
		PRMA.formColor('#contact input[type="text"], #contact textarea');
		PRMA.InitTextarea('#contact_message');	
	
	/* ajoute un target _blank au lien avec une classe external */
	 $("a[rel='external'],a[rel='document']")
	  .attr({ title: "s'ouvre dans une nouvelle fen\u00EAtre" })
	  .click(function() { window.open($(this).attr('href')); return false; });
	 /*--------------------*/
	
	
	$('.main-title').each( function() {
		var width = $('h3', this).width();
		var position = width + 10;
		$('h3', this).css('background', 'none');
		$(this).css('background', 'url(/css/img/entry-title.jpg) '+position+'px 5px no-repeat');
	});
	
	var height_nav = $('#navigation').height();
	var height_content = $('#content').height();
	
	$('a').each( function() {
		if($(this).attr('class') == "fancybox"){
			$(".fancybox").fancybox({ 'overlayShow': true });	
		}
	});	
  
  /* Recherche default label*/
	$("form#affine_cadeaux p:not(.submit) input").each(function(){
	  if(!$(this).val())
		{
		  $(this).attr('value',$(this).prev().html());
		}
		$(this).click(function() {
	      if ($(this).val() == $(this).prev().html()){
	        $(this).val('');
	      }
	    });
	    $(this).blur(function() {
	      if ($(this).val() == '')
	      {
	        $(this).val($(this).prev().html());
	      }
	    });
	});
	/*---------------------------------------------------------*/  
	
	
  });

})(jQuery);
