var App; if (!App) App = {};
var Init; if (!Init) Init = {};
var Options; if (!Options) Options = {};


App = {
	
	'ie' : false,
	'ieVersion' : 0,
	'ie6' : false,
	'ie7' : false,
	'ie8' : false,

	'init' : function() {
		
		App.detectIE();
		
		$("input.hint, textarea.hint").hint();
		
		Init.Calendar("#calendar");
		
		Init.Menu("#menu");
		
		Init.Contact("#contactForm");
		
	},
	
	'detectIE' : function() {
		if ($.browser.msie) {
			App.ie = true;
			App.ieVersion = parseInt($.browser.version.substr(0,1));
			if(App.ieVersion == 6) { App.ie6 = true; }
			if(App.ieVersion == 7) { App.ie7 = true; }
			if(App.ieVersion == 8) { App.ie8 = true; }
		}
	}
	
};


/*********************************************
*
* Common options
*
**********************************************/

Options = {

	

};

/*********************************************
*
* Templates
*
**********************************************/

Templates = {

	"client" : '<div class="client"><strong>{name}</strong> ({email})<input name="client_name[]" type="hidden" value="" class="name"/><input name="client_email[]" type="hidden" value="" class="email"/> - <a href="#" class="remove">X</a></div>',
	
	"render" : function(text,values) {
		var re_cache = {};
		var tmpl = text.replace(/%7B/ig,'{').replace(/%7D/ig,'}');
		$.each(values, function(k,v) {
			tmpl = tmpl.replace( new RegExp('{'+k+'}', 'gm'), v );	
		});
		
		return tmpl;

	}
};

/*********************************************
*
* Quick Init
*
**********************************************/

Init.Menu = function(selector) {
	
	
	var $el = $(selector);
	if(!$el.length) { return; }
	
	$el.find("li").each(function() {
		$(this).mouseover(function() {
			$(this).addClass("sfhover");
		});
		$(this).mouseout(function() {
			$(this).removeClass("sfhover");
		});
	});


};

Init.Calendar = function(selector) {
	
	var $el = $(selector);
	if(!$el.length) { return; }
	
	
	function loadDate(date) {
		$el.block({
			centerY:true,
			overlayCSS:{
				background:"#ddd",
				opacity:0.9
			}
		});
		
		$el.load("/centrepreneur/ajax/calendar.tmpl?date="+date,function() {
			initNavigation();
			if($("#events-main-date").length) { initDates(); }
		});
	}
	
	function loadEvents(date) {
		
		$("#events-main-date").fadeTo('fast',0.3);
		
		$("#events-main-date").load("/centrepreneur/ajax/events.tmpl?date="+date,function() {
			$("#events-main-date").fadeTo('fast',1,function() {
				$(this).css("filter","");
			});
		});
	}
	
	function initNavigation() {
		$el.find("a.prev").click(function(e) {
			e.preventDefault();
			loadDate($(this).attr("rel"));
		});
		
		$el.find("a.next").click(function(e) {
			e.preventDefault();
			loadDate($(this).attr("rel"));
		});
	}
	
	function initDates() {
		$el.find("span.d a").click(function(e) {
			e.preventDefault();
			loadEvents($(this).attr("rel"));
		});
	}
	
	initNavigation();
	if($("#events-main-date").length) { initDates(); }

}



Init.Contact = function(selector) {
	
	var el = $(selector);
	if(!el.length) { return; }
	
	el.find("form").submit(function(e) {
		e.preventDefault();
		var form = $(this);
		el.find("div.success").remove();
		el.find("div.error").remove();
		if(form.validate()) {
			form.block(Options.block_contact);
			$.post("/centrepreneur/"+App.lang+"/contact.json",form.serialize(),function(data) {
				form.unblock();
				if(!data.error && data.success) {
					el.find("form").before("<div class='success' style='cursor:pointer;'>Votre message a été envoyé</div>");
					el.find(".success").click(function() { el.find(".success").remove(); });
					form.find("textarea[name=message]").val("");
					form.find("input[type=text]").each(function() {
						$(this).val($(this).attr("title")).addClass("hasHint");
					});
				} else {
					el.find("form").before("<div class='error'>"+data.error+"</div>");
				}
			}, "json");
		}
	});

};
