$(document).ready(function(){
  	
	$(document).pngFix(); 
	searchMenu();
	newsletterForm();
	$('a.zoom').fancybox({
		'titleShow' : false
	});
	$('a.zoom80').fancybox();
	
	$("a.iframe").fancybox({
		'centerOnScroll': false,
		'height': 500,
		'width': 500,
		'padding' :5,
		'type': 'iframe'
	}); 
	
	$("a.modifImgAnnonce").fancybox({
		'centerOnScroll': false,
		'height': 320,
		'width': 500,
		'padding' :5,
		'type': 'iframe',
		'onClosed' : function(){parent.location.reload();}
	});
	
	loginBox();
	carousel();
	handleForms();
	debug();
	datePickerInit();

	$().maxlength();
	
});


/**************** FONCTIONS ****************/

function montWysiwyg(id_textarea){

	$('#'+id_textarea).wysiwyg({
	    controls: {
	      
	      strikeThrough : { visible : false },
	      underline     : { visible : true },
	      
	      separator00 : { visible : true },
	      
	      justifyLeft   : { visible : true },
	      justifyCenter : { visible : true },
	      justifyRight  : { visible : true },
	      justifyFull   : { visible : true },
	      
	      separator01 : { visible : false },
	      
	      removeFormat	: {visible : true},
	      
	      indent  : { visible : false },
	      outdent : { visible : false },
	      
	      separator02 : { visible : true },
	      
	      subscript   : { visible : false },
	      superscript : { visible : false },
	      
	      separator03 : { visible : false },
	      
	      undo : { visible : false },
	      redo : { visible : false },
	      
	      separator04 : { visible : false },
	      
	      insertOrderedList    : { visible : true },
	      insertUnorderedList  : { visible : true },
	      insertHorizontalRule : { visible : false },
	      
	      insertImage	: {visible : false },

	      h1mozilla : { visible : false && $.browser.mozilla, className : 'h1', command : 'heading', arguments : ['h1'], tags : ['h1'], tooltip : "Titre 1" },
	      h2mozilla : { visible : true && $.browser.mozilla, className : 'h2', command : 'heading', arguments : ['h2'], tags : ['h2'], tooltip : "Titre 2" },
	      h3mozilla : { visible : true && $.browser.mozilla, className : 'h3', command : 'heading', arguments : ['h3'], tags : ['h3'], tooltip : "Titre 3" },
	      h4mozilla : { visible : false && $.browser.mozilla, className : 'h4', command : 'heading', arguments : ['h4'], tags : ['h4'], tooltip : "Titre 4" },
	      h5mozilla : { visible : false && $.browser.mozilla, className : 'h5', command : 'heading', arguments : ['h5'], tags : ['h5'], tooltip : "Titre 5" },
	      h6mozilla : { visible : false && $.browser.mozilla, className : 'h6', command : 'heading', arguments : ['h6'], tags : ['h6'], tooltip : "Titre 6" },
	      
	      h1 : { visible : false && !( $.browser.mozilla ), className : 'h1', command : 'formatBlock', arguments : ['<H1>'], tags : ['h1'], tooltip : "Titre 1" },
	      h2 : { visible : true && !( $.browser.mozilla ), className : 'h2', command : 'formatBlock', arguments : ['<H2>'], tags : ['h2'], tooltip : "Titre 2" },
	      h3 : { visible : true && !( $.browser.mozilla ), className : 'h3', command : 'formatBlock', arguments : ['<H3>'], tags : ['h3'], tooltip : "Titre 3" },
	      h4 : { visible : false && !( $.browser.mozilla ), className : 'h4', command : 'formatBlock', arguments : ['<H4>'], tags : ['h4'], tooltip : "Titre 4" },
	      h5 : { visible : false && !( $.browser.mozilla ), className : 'h5', command : 'formatBlock', arguments : ['<H5>'], tags : ['h5'], tooltip : "Titre 5" },
	      h6 : { visible : false && !( $.browser.mozilla ), className : 'h6', command : 'formatBlock', arguments : ['<H6>'], tags : ['h6'], tooltip : "Titre 6" },
	      
	      separator07 : { visible : false },
	      
	      cut   : { visible : false },
	      copy  : { visible : false },
	      paste : { visible : false }
	    }
	  });

	
	
}



function datePickerInit(){
	
	$.datepicker.regional['fr'] = {
			closeText: 'Fermer',
			prevText: '&#x3c;Préc',
			nextText: 'Suiv&#x3e;',
			currentText: 'Courant',
			monthNames: ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre'],
			monthNamesShort: ['Jan','Fév','Mar','Avr','Mai','Jun','Jul','Aoû','Sep','Oct','Nov','Déc'],
			dayNames: ['Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi'],
			dayNamesShort: ['Dim','Lun','Mar','Mer','Jeu','Ven','Sam'],
			dayNamesMin: ['Di','Lu','Ma','Me','Je','Ve','Sa'],
			dateFormat: 'dd/mm/yy',
			firstDay: 1,
			isRTL: false
	};
		
	$.datepicker.setDefaults($.datepicker.regional['fr']);
	$.datepicker.setDefaults({
			changeYear: true,
    		changeMonth: true,
    		yearRange: '1910:2005',
    		defaultDate: '-20y',
    		gotoCurrent: true
	});
}

function debug(){
	$('div.debugW').draggable().fadeTo(0, 0.9).css({ cursor:'move' }).hover(
	        function() { $(this).fadeTo(100, 1); },
	        function() { $(this).fadeTo(100, 0.9); }
	);
	
	$('div.debugW a.fermerDebug').live('click', function(){$(this).parent('div').remove();} );
}

function carousel(){
	
	
	if ($("div.logoCarousel").length > 0){
	
		if($("div.logoCarousel li").length > 0){
			$("div.logoCarousel").easySlider({
	    		auto: true,
	    		continuous: true ,
	    		nextId: "slidermarquenext",
	    		prevId: "slidermarqueprev",
	    		nextText: "&nbsp;&nbsp;",
	    		prevText: "&nbsp;&nbsp;"
	    	});
		}
	}
}

	
function handleForms(){
	
	var formFields = $('input').add('textarea').add('select');
	
	formFields.focus(function() { $(this).addClass('focus'); });
	formFields.blur(function() { $(this).removeClass('focus'); });

	$('input#submit').fadeTo(0,0.75);
	$('input#submit').hover(
			function(){$(this).fadeTo(0,1);},
			function(){$(this).fadeTo(0,0.75);}
	);
	
}

function in_array(p_val,array){
	var bool = false
	$.each( array, function(i, n){
		if (typeof n!="undefined"){

		 if(n.toLowerCase() == p_val.toLowerCase()) {
	            bool = true;
	        }
		}
	});
	return bool;
}



function unset(array, value){
	var output=[];
	for (var i = 0 ; i<array.length;i++){
		if (array[i] != value)
			output[i]=array[i];
	}
	return output;
}

jQuery.fn.extend({
	   findPos : function() {
	       obj = jQuery(this).get(0);
	       var curleft = obj.offsetLeft || 0;
	       var curtop = obj.offsetTop || 0;
	       while (obj = obj.offsetParent) {
	                curleft += obj.offsetLeft
	                curtop += obj.offsetTop
	       }
	       return {x:curleft,y:curtop};
	   }
	});



function loginBox(){
	$('form#loginForm input#EMAIL').focus(function(){
		if($(this).val() == 'Adresse E-mail'){
			$(this).val('');
		}
	});
	$('form#loginForm input#EMAIL').blur(function(){
		if ($(this).val() == ''){
			$(this).val('Adresse E-mail');
		}
	});
	
	
	$('form#loginForm input#loginButton').click(function(){
		
		if(
		$('form#loginForm input#EMAIL').val() == 'Adresse E-mail'
		||
		$('form#loginForm input#EMAIL').val() == ''
		){
			$('form#loginForm input#EMAIL').focus();
			return false;
		}
	});
	
}

function searchMenu(){
	$('#inputSearch').focus(function(){$(this).attr('value','');});
	$('#inputSearch').blur( function(){
		if ($(this).attr('value') == ''){$(this).attr('value','Rechercher');}
	});
}

function newsletterForm(){
	$('#newsletterEmail').focus(function(){$(this).attr('value','');});
	$('#newsletterEmail').blur( function(){
		if ($(this).attr('value') == ''){$(this).attr('value','Votre email...');}
	});
}



/* AJAX */

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
    	var c = ca[i];
    	while (c.charAt(0)==' '){
        	c = c.substring(1,c.length);
    	}
    	if (c.indexOf(nameEQ) == 0){
        	return c.substring(nameEQ.length,c.length);
    	}
	}
	return null;
}




function alertie6(){

		if(jQuery.browser.msie && jQuery.browser.version == 6){
			if($.cookie("iecookie") == null){
				$('div#ie6nomore').show();
			}
			
			$('#closeie6banner').click(function(){
				var date = new Date();
				date.setTime(date.getTime() + (20 * 60 * 1000)); // 20 minutes
				$.cookie("iecookie", "ie6nomore", { expires: date });
				
			});

		}
	}



function editeurTexte(idtextarea){
	
	$('textarea#'+idtextarea).tinymce({
		// General options
		theme : "advanced",
        skin : 'default',
        language :'en',
        plugins : 'paste',
        theme_advanced_blockformats : "p,h2,h3,h4",

        entity_encoding : "raw",
		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,link,unlink,|,formatselect",
        theme_advanced_buttons2 : '',
        theme_advanced_buttons3 : '',
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar : false,
		theme_advanced_resizing : false,
		paste_remove_styles : true,
		paste_preprocess : function(pl, o) {
        	o.content = o.content.replace(/h1/g, 'h2');
		}
	});

}

