/**
* Factory System Installation
*
* @author	  RaphaeL Pralat <raph@plus2.fr>
* @version    CVS: $Id:$
* @uses       http://prototype.conio.net/
* @uses       http://bennolan.com/behaviour/
* @uses       http://script.aculo.us
*/

var						ActionMedia = {

	rules :				{
	
	// Flash Object Action
	'.flashObjectAction' : function(el) {
		
		el.onclick = function() {
			
			var type = findParamInClass('type', this);
			var action = findParamInClass('action', this);
			var id = findParamInClass('id', this);
			var element = findParamInClass('element', this);
			var code = findParamInClass('code', this);
			
			var form = "type=" + type + "&action=" + action + "&id=" + id + "&element=" + element + "&code=" + code;
			
			new Ajax.Updater(id, '/ajax/flashObject', {
				parameters	: form,
				method		: 'post',
				onComplete	: ActionMedia.reload
			});
		}
	},
	
	// Display Media Detail
	'.mediaDetail' : function(el) {
		el.onclick = function() {
			var action = findParamInClass('action', this);
			
			if (action == 'show') {
				if ($('mediaDescription').style.display == 'none') {
					$('mediaDescription').style.display = 'inline';
				} else {
					$('mediaDescription').style.display = 'none';
				}
			} else if (action == 'hide') {
				$('mediaDescription').style.display = 'none';
			}
		}
	},
	
	// User Media Action
	'.userMediaAction' : function(el) {
		
		el.onclick = function() {
			
			var action = findParamInClass('action', this);
			var id = findParamInClass('id', this);
			
			var param = "action=" + action + "&id=" + id;
			
			if ($('mediaForm')) {
				param = param + '&' + $('mediaForm').serialize();
			}
			
			new Ajax.Updater("mediaContent", '/ajax/userMedia', {
				parameters	: param,
				method		: 'post',
				onComplete	: ActionMedia.reload
			});
		}
	},
	
	// Rendez Vous Action
	'.rendezVousAction' : function(el) {
		
		el.onclick = function() {
			var action = findParamInClass('action', this);
			var id = findParamInClass('id', this);
			var date = findParamInClass('date', this);
			
			var param = "action=" + action + "&id=" + id + "&date=" + date;
			
			if (action == 'valid') {
				updateRendezVousToValid(param, this);
				updateRendezVousOnline(param, this);
				updateRendezVousList(param, this);
				updateRendezVousDetail(param, this);
				updateRendezVousCalendar(param, this);
			} else if (action == 'delete') {
				updateRendezVousOnline(param, this);
				updateRendezVousList(param, this);
				updateRendezVousToValid(param, this);
				updateRendezVousWaitingValid(param, this);
				updateRendezVousDetail(param, this);
				updateRendezVousCalendar(param, this);
			} else if (action == 'detail') {
				updateRendezVousDetail(param, this);
			} else if (action == 'showAsk') {
				showAskRendezVousList(param, this);
			} else if (action == 'hideAsk') {
				$('askRendezVousContent').style.display = 'none';
			} else if (action == 'sendAsk') {
				sendAskRendezVousList(param, this);
			}
		}
	},
	
	
	'lastbutnotleast': {}
	}, /** rules **/
	
	'reload' :	function(ajax) {
		 Behaviour.apply(ActionMedia.rules);
	}
}


function showAskRendezVousList(param, el) {
	
	new Ajax.Updater("askRendezVousContent", '/ajax/videoconferenceRendezVous', {
		parameters	: param + "&content=ask",
		method		: 'post',
		onComplete	: ActionMedia.reload
	});
	$('askRendezVousContent').style.display = 'block';
	
}

function sendAskRendezVousList(param, el) {
	
	if ($('formRendezVous')) {
		form = $('formRendezVous').serialize();
		param = param + '&' + form;
	}
	
	new Ajax.Updater("askRendezVousContent", '/ajax/videoconferenceRendezVous', {
		parameters	: param + "&content=ask",
		method		: 'post',
		onComplete	: ActionMedia.reload
	});
	
}

function updateRendezVousList(param, el) {
	
	new Ajax.Updater("rendezVousListContent", '/ajax/videoconferenceRendezVous', {
		parameters	: param + "&content=list",
		method		: 'post',
		onComplete	: ActionMedia.reload
	});
}


function updateRendezVousDetail(param, el) {
	
	new Ajax.Updater("rendezVousDetailContent", '/ajax/videoconferenceRendezVous', {
		parameters	: param + "&content=detail",
		method		: 'post',
		onComplete	: ActionMedia.reload
	});
}


function updateRendezVousOnline(param, el) {
	
	new Ajax.Updater("rendezVousOnlineContent", '/ajax/videoconferenceRendezVous', {
		parameters	: param + "&content=online",
		method		: 'post',
		onComplete	: ActionMedia.reload
	});
}


function updateRendezVousToValid(param, el) {
	
	new Ajax.Updater("rendezVousToValidContent", '/ajax/videoconferenceRendezVous', {
		parameters	: param + "&content=toValid",
		method		: 'post',
		onComplete	: ActionMedia.reload
	});
	
}


function updateRendezVousWaitingValid(param, el) {
	
	new Ajax.Updater("rendezVousWaitingValidContent", '/ajax/videoconferenceRendezVous', {
		parameters	: param + "&content=waitingValid",
		method		: 'post',
		onComplete	: ActionMedia.reload
	});
	
}


function updateRendezVousCalendar(param, el) {
	
	new Ajax.Updater("rendezVousCalendarContent", '/ajax/videoconferenceRendezVous', {
		parameters	: param + "&content=calendar",
		method		: 'post',
		onComplete	: ActionMedia.reload
	});
	
}



function findParamInClass(param, el) {
   var regexp = new RegExp(param + '_([A-Za-z0-9/:?&\-\._]+)');
   var mymatch = regexp.exec(el.className);
   if(mymatch) {
       return mymatch[1];
   }
   return false;
}

Behaviour.register(ActionMedia.rules);