var nbOfMsg=6;
var colorIndicator=6;

var len = 350;
var txt1 = 'Pseudo Manquant'
var txt2 = 'Message Manquant'
var txt3 = 'Message trop long'
var txt4 = 'Envoyer le message ?'


function validateShoutboxForm(){

	var autor=$('shoutboxFormAutor').value;
	var content=$('shoutboxFormContent').value;
	
	if(autor.replace(/^\s+|\s+$/g, '')==''){
		alert(txt1);
		return false;
	} 
	if(content.replace(/^\s+|\s+$/g, '')==''){
		alert(txt2);
		return false;
	}
	if(content.lenght > len){
		alert(txt3);
		return false;
	}
	if(!confirm(txt4)){
		return false;
	}
	return true;
}

function insertMessage() {
  var url = 'index.php?option=com_djiceshoutbox&view=ajax&format=djiceshoutbox';
  var autor=$('shoutboxFormAutor').value;
  var ip=$('shoutboxFormIp').value;
  var content=$('shoutboxFormContent').value;

  var myRequest = new Request({
		url: url,
		data: 'autor='+autor+'&ip='+ip+'&content='+content,
		method: 'post',
		onSuccess: gestionReponse
	});
  myRequest.send();

  $('shoutboxFormAutor').value='';
  $('shoutboxFormContent').value='';
}

function gestionReponse(xhr) {
	//get the last row
	firstRow = $('msg'+colorIndicator);
	secondRow = $('msg'+(colorIndicator-1));
	newRow =  $('tableshoutbox').insertRow(0);
	newRow.id="msg"+(colorIndicator+1);
	newRow.bgColor=secondRow.bgColor;
	newRow.className=secondRow.className;
	var reg = new RegExp("</?td\\s?\\S*>", "ig");
	var reg2 = new RegExp("[a-z1-9]+", "ig");
	var regflood = new RegExp("FLOOD", "ig");
	if(xhr.match(regflood)) {
		alert("Vous ne pouvez pas envoyer 2 messages en si peu de temps.");
		return;
	}
	var tableau=xhr.split(reg);
	for (var i=0; i<tableau.length; i++) {
		if(tableau[i].match(reg2)){
			newCell = newRow.insertCell(-1);
			newCell.innerHTML=tableau[i];
		}
	}
	//remove first row
	//Increment lastMessageNumber
	lastRow = $('msg'+(colorIndicator-nbOfMsg+1));
	lastRow.remove();
	colorIndicator=colorIndicator+1;
}


function showSmiley(){
	divToShow = document.getElementById('divSmileys');
	if(divToShow.style.display=="block"){
		divToShow.style.display = "none";
	} else {
		divToShow.style.display = "block";
	}
}

function addSmiley(smiley){
	var content = document.getElementById('shoutboxFormContent').value;
	document.getElementById('shoutboxFormContent').value = content + smiley;
}

function get_photo() {

	var div1 = $('imgpad').setStyles({opacity: 1});
	div1.get('tween', {property: 'opacity', duration: 2000}).start(0);

	var jsonRequest = new Request.JSON({method: 'get', url: "./data/get_randphoto.php", onSuccess: function(image){
	
		$('anneeimg').innerHTML = image.annee;
		$('albumimg').innerHTML = image.album;
		//$('albumimg').innerHTML = image.album;
		$('dayimg').setProperty('src', image.urlimage);
		$('imglink').setProperty('href', image.urlpage);
		var div2 = $('imgpad').setStyles({opacity: 0}); 
		div2.get('tween', {property: 'opacity', duration: 1000}).start(1);
	}}).send();
}

/***************** Entrainements ********************/

function toggle_details(idline) {

	if($(idline).isDisplayed()) {
		$(idline).hide();
	} else {
		$(idline).show('table-row');
	}
}

function drawtable(data) {

	var img_details = imgprefix + "images/balise10.gif";
	var linestyles = new Array();
	linestyles[-1] = 'line_def';
	linestyles["we"] = 'line_we';
	linestyles[1] = 'line_co';
	linestyles[2] = 'line_phys';
	linestyles[3] = 'line_phys';
	linestyles[7] = 'line_eco';
	linestyles["depl"] = 'line_depl';
	var reg1=new RegExp("Lun|Mon","ig");
	
	
	//alert(data.periode);
	$('curmonthtxt').set('text', data.periodtxt);
	$('curmonthtxt2').set('text', data.periodtxt);
	$('period').set('value', data.period);
	
	var seances = data.jours;

	seances.each(function(item, index) {
		hasdetails = 0;
		seancedetails = "&nbsp;";
		var fetestyle = "";
		lineid = "";
		idwendweek = "";
		if(item.txttime.match(reg1)) {
			idwendweek = "fin";
		}

		switch(item.type) {

			// entrainement
			case '1':
			case '2':
			case '3':
			case '7' :
				hasdetails = 1;
				details[item.content_id] = item;			
				seancedetails = '<a href="javascript:;" id="imgdetails'+ item.content_id +'" onclick="toggle_details(\'details'+ item.content_id +'\')">'
								+'<img src="'+img_details+'" border="0" /></a>';
			break;
			// normal
			case "we":
				fetestyle = 'col_fete';
			break;
			// deplacement
			case 'depl':
				//seancedetails = "2";
			break;
			default:
				fetestyle = 'col_fete';
			break;
		};
		//seancedetails = item.type;
		calTable.push([ {content: item.txttime, properties: {'style': 'text-align: right;', 'width': '60'}},
						{ content: item.content, properties: {'class': fetestyle, 'width': '150'}}, 
						{ content: seancedetails, properties: {'align': 'center', 'width': '40'}}], 
					{'class': linestyles[item.type], 'id': lineid, 'id': idwendweek });

		if (hasdetails) {
			var content = '';
			var d = details[item.content_id].details;
			d.each(function(el,i){
				content += '<b>'+el.key+'</b> '+el.val+'<br />';
			});

			var detailsstyle = linestyles[item.type] + "_details";
			calTable.push( [ {content: content, properties: {'colspan': 3, 'class' : detailsstyle, 'id': idwendweek} } ], 
						{'id': 'details'+ item.content_id, 'style' : 'display: none'});
		}
	});
}

function getMonthData(urlcal, period) {

	if (period != 0) {
		urlcal += "?period="+period;
	}
	calTable.empty();
	var jsonRequest = new Request.JSON({url: urlcal, method: 'get', onSuccess: drawtable}).send();
}

