var ModalMap = new Class({
	initialize: function(id, link) {
		this.elm = $(id);
		//$(link).addEvent('click', function(elm){
		//	elm.preventDefault();
		//	this.open();
		//}.bind(this));
		$$('.modal_close').addEvent('click', function(elm) {
			elm.preventDefault();
			this.close();		
		}.bind(this));
	},
	open: function() {
		//add a grey filter
		this.gb = new Element('div', {'class':'grayfilter'});
		this.gb.addEvent('click', this.close.bind(this));
		$(document.body).appendChild(this.gb);

		this.elm.setStyle('display', ''); //show
	},
	close: function() {
		this.gb.destroy();
		this.elm.setStyle('display', 'none');
	}
});

var MenuItemFactory = new Class({
	initialize: function() {
		$$('div.mainlink-container div.mainlink-div').each(function(elm) {
			new MenuItem(elm);
		});
	}
});

var MenuItem = new Class({
    initialize: function(elm){
        this.elm = $(elm);
		this.link = $(this.elm.getChildren('a').getLast());
		this.elm.addEvent('mouseover', this.hover.bind(this));
		this.elm.addEvent('mouseout', this.normal.bind(this));
    },
	hover: function() {
		this.elm.addClass('mainlink-div-hover');
		this.link.addClass('mainlink-hover');
	},
	normal: function() {
		this.elm.removeClass('mainlink-div-hover');
		this.link.removeClass('mainlink-hover');
	}
});

var DateToolbox = new Class({
	initialize: function() {
		this.mois = new Array('janvier','février','mars', 'avril','mai','juin','juillet',
			'août','septembre','octobre','novembre','décembre');//new Array(12);
	},
	humanize: function(sqlDate) {
		//format AAAA-MM-JJ
		var splD=sqlDate.split("-");
		indM=(splD[1]*1)-1;
		return splD[2]+" "+this.mois[indM]+" "+splD[0];
	}
});

var FormManager = new Class({
	setValue: function(id, value) {
		$(id).value = value;
	},
	submit: function(formId) {
		$(formId).submit();
	},
	confirmSubmit: function(confText,formId) {
		if (confirm(confText)) {
			this.submit(formId);
			return true;
		} else {
			return false;
		}	
	}
});

function loadTinyMCE() {
	tinyMCE.init({
		mode : "textareas",
		theme : "advanced",
		plugins : "safari",
		theme_advanced_buttons1 : "bold,italic,underline,separator,indent,outdent,separator,bullist,numlist,separator,fontselect,fontsizeselect,forecolor,removeformat",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "none",
		content_css : "example_word.css",
		theme_advanced_resizing : true,
		theme_advanced_resize_horizontal : false,
		paste_auto_cleanup_on_paste : true,
		paste_convert_headers_to_strong : false,
		paste_strip_class_attributes : "all",
		paste_remove_spans : false,
		paste_remove_styles : false		
	});
}

function multiplicateElm(id,fieldCountId) {
	var elm=document.getElementById(id);
	var fldc=document.getElementById(fieldCountId);
	if (elm.baseRepeat==undefined) {
		elm.baseRepeat=elm.innerHTML;
		elm.currIndex=0;
	}
	elm.currIndex++;
	var newLine=elm.baseRepeat.replace('___0','___'+elm.currIndex);
	elm.innerHTML+=newLine;
	fldc.value=elm.currIndex;
}

function showBigImg(id,photo_no) {
	var elm=document.getElementById(id);
	if (elm.baseRepeat==undefined) {
		elm.baseRepeat=elm.innerHTML;
	}
	var newLine=elm.baseRepeat.replace(/0000/g,photo_no);
	elm.innerHTML=newLine;		
	elm.style.visibility='visible';
}
function hideBigImg(id) {
	var elm=document.getElementById(id);
	elm.innerHTML=elm.baseRepeat;
	elm.style.visibility='hidden';
}

var menu_factory = null;
var modal_map = null;
var dtb = new DateToolbox();
var fm = new FormManager();
window.addEvent('domready', function() {
	menu_factory = new MenuItemFactory();
	modal_map = new ModalMap('doc_divmap', 'open-gmap-doc');
});