/**
 * @author aduchesneau
 */
var cal = null;
//var calEntries = [];
var myTips = null;
var hrefListOn = false;

window.addEvent('domready', function() {
	var whereDossier, dChilds, hrefList, a, dateTEXT, ttTEXT;
	var calendrierAjax = $('home_calendrier_wrap');
	if (calendrierAjax) {
		whereDossier = calendrierAjax.get('rel');
		top.myTips = new Tips($$('.myTipz'));
		
		$(document.body).addEvent('click',function(e) { 
			if (top.hrefListOn == true) {
				top.hrefListOn == false;
				$$('.mui-box').each(function(el) {
					el.destroy();
				})
			}
		});
	
		cal = new vlaCalendar('home_calendrier', { startMonday: false });
		cal.addEvent('change', function(e) {
			if (e.ts) {
				new Request.JSON({
					url: '/services/calendrierMois.service.php',
					onSuccess: function(responseJSON, responseText){
	//					alert(responseText);
						if (responseJSON != null) {
							$$('.tip').each(function(e) {
								e.destroy();
							});
							for (var i=0; i < responseJSON.length; i++) {
								var dateevent = responseJSON[i]['dateevent'];
								var findJM = dateevent.match(/[0-9]{4}-([0-9]{2})-([0-9]{2}) [0-9]{2}:[0-9]{2}:[0-9]{2}/);
								if(typeof(findJM) == 'object') {
	
									var d = $('dcal_'+findJM[2]+'_'+findJM[1]);
									if (d) {
										hrefList = [];
										dateTEXT = d.get('text');
										d.addClass('selected');
										dChilds = d.getChildren('a');
										if ( dChilds.length == 1 ) {
											a = dChilds[0];
											hrefList = JSON.decode(a.get('hrefList'));
										}
										
										var temp = new Element('div').set('text',responseJSON[i]['name']);
										temp.injectInside(document.body);
										hrefList.push({
											href: responseJSON[i]['redirecturl'] ,
											name: temp.get('text')
										});
										temp.destroy();
										
										ttTEXT = '<ul>';
										for (var j=0;  j < hrefList.length; j++ ){
											ttTEXT += '<li>'+hrefList[j].name + '</li>';
										};
										ttTEXT += '</ul>';
	
	//									var inx = calEntries.length;
	//									calEntries[inx] = responseJSON[i];
										d.empty();
										var d_a = new Element('a',{href:    'javascript:;',
																   'title': ttTEXT,
																   'rel'  : ' '
																  });
										var randm = $random(1000, 100000);
										d_a.set('id','pag_'+randm);
										d_a.set('hrefList', JSON.encode(hrefList));
										d_a.set('href','javascript:showLinks("pag_'+randm+'");');
										d_a.appendText( dateTEXT );
										d_a.inject(d);
										tipObj = {  className: 'tip', 
													fixed: false, 
													hideDelay: 50, 
													showDelay: 50
												};
										if (top.myTips.push) {
											top.myTips.push( new Tips(d_a, tipObj) );
											
										} else {
											top.myTips = new Tips(d_a, tipObj);
										}
									}
								}
							}
								
						}
					}.bind(this),
					onFailure: function(xhr) {
						alert('onFailure: '+xhr.responseText);
					}
				}).post({
					callFunction:'getEvents',
					ts: e.ts,
					dossier: whereDossier
				});
				
			}
		})
	}
});
		

function showLinks (qui) {
	a = $(qui);
	var hrefList = JSON.decode(a.get('hrefList'));
	var links = [];
	for(var i=0; i < hrefList.length; i++) {
		if ( $chk(hrefList[i].href) ) {
			links.push(hrefList[i].href);
		}
	}
	if (links.length == 0) return;
	
	$$('.tip').each(function(e){
		 if (e.getStyle('visibility') == 'visible') {
		 	var tt = e;
			e.setStyle('visibility','hidden');
		 }
	});
	
	if (links.length == 1) {
		document.location = links[0];
		return;
	}
	
	var pos = a.getPosition();
	var box = new MUI.BaseBox({draggable: false, position:{x:pos.x, y:pos.y+10}});
	box.show();
		
	var ulRoot = new Element('ul');
	for(var i=0; i < hrefList.length; i++) {
		var aA = new Element('a',{'href':hrefList[i].href, 'html':hrefList[i].name });
		ulRoot.adopt( new Element('li').adopt(aA) );
	}
	box.el.adopt(ulRoot);
	
	top.hrefListOn = true;
}

