/**
* Factory System Installation
*
* @author	  RaphaeL Pralat <raph@plus2.fr>
* @version    CVS: $Id: Youjob.js,v 1.5 2007-09-12 12:46:31 cvs Exp $
* @uses       http://prototype.conio.net/
* @uses       http://bennolan.com/behaviour/
* @uses       http://script.aculo.us
*/

/**
*	you have to use this function to call the APC module on the server
*/

//function CDownloadUrl(method, url, func) {
//	var httpObj;
//	var browser = navigator.appName;
//	if(browser.indexOf("Microsoft") > -1)
//	httpObj = new ActiveXObject("Microsoft.XMLHTTP");
//	else
//	httpObj = new XMLHttpRequest();
//	httpObj.open(method, url, true);
//	httpObj.onreadystatechange = function() {
//		if(httpObj.readyState == 4){
//			if (httpObj.status == 200) {
//				var contenttype = httpObj.getResponseHeader('Content-Type');
//				if (contenttype.indexOf('xml')>-1) {
//					func(httpObj.responseXML);
//				} else {
//					func(httpObj.responseText);
//				}
//			} else {
//				func('Error: '+httpObj.status);
//			}
//		}
//	};
//	httpObj.send(null);
//}
//
///**
//*	call this function again and again
//*/
//function getProgress(test) {
//	CDownloadUrl("POST","/plugin/upload/upload.php?progress_key="+test,
//	function(response, responseCode) {
//		var decodeRep = eval('('+response+')');
//		var percent = decodeRep.current/decodeRep.total*100;
//		var progress = 100-percent;
//		document.getElementById("progressinner").style.width = progress + '%';
//		document.getElementById("texte").innerHTML = percent.toString() + '%';
//		if (percent != 100){
//			setTimeout('getProgress("'+test+'");', 100);
//		}
//		if (percent == 100) {
//			setTimeout('endProgress("'+test+'");', 1000);
//		}
//	}
//	);
//}
//
///**
//*	when you validate the form
//*/
//function startProgress(test) {
//	document.getElementById("progressinner").style.width = '0%';
//	document.getElementById("texte").innerHTML = '0%';
//	document.getElementById("progressouter").style.display="block";
//	setTimeout('getProgress("'+test+'");', 100);
//}

/**
* at the end of upload param id
*/
/*function endProgress() {
new Ajax.Updater('sendForm', '/ajaxobject/index',{
onComplete	: Youjob.reload
});
finished = false;
}

function beginUpload(sid) {
//define vars
var uform = document.getElementById('form');
var udivform = document.getElementById('formUpload');
var upb = document.getElementById('progressouter');
if (uform.fileToUpload.value=="") return; //no file, no upload
$('errorMsg').innerHTML = '';
//make appear the progress bar
udivform.style.display = 'none';
upb.style.display = 'block';

//redefine where the form post goes

uform.action = '/cgi-bin/upload.cgi?sid='+sid+'&maxsize=524288000';
uform.submit();
//ajax magic
uploadUpdater = new Ajax.PeriodicalUpdater('','/plugin/upload/FileUpload.php', {'frequency' : 1,
'encoding':'UTF-8',
'method': 'post',
'parameters': 'progress=' + sid,
'onSuccess' : updateProgress,
'onFailure' : updateProgress
});
}
var finished = false;
//update progress bar
function updateProgress(req) {
if (finished) return;
var pb = document.getElementById('progressinner');
var pbText = document.getElementById('pbtext');
//we expect a number
var percent = parseInt(req.responseText);
//not a number...
if (isNaN(percent)) {
finished = true;
uploadUpdater.stop();
if (req.responseText.indexOf("FINISHED") > -1) {
pb.style.width = "0%";
pbText.innerHTML = "100%";
setTimeout('endProgress()', 2000);
$('errorMsg').innerHTML= 'Fichier reçu dans \'Mon Profil\'';
return;
}
pb.style.width="0%";
pbText.innerHTML = "100%";
$('errorMsg').innerHTML= req.responseText;
setTimeout('endProgress()', 2000);
} else {
if(!percent) percent = 0;
if(percent > 100) percent = 100;
showPercent = 100 - percent;
pb.style.width = ""+showPercent+"%";
pbText.innerHTML = ""+percent+"%";
}
}*/

var						Youjob = {

	rules :				{

		/**
		* a user can cancel a scheduled interview
		*/

		'#userCancelRdv' : function(el) {
			el.onclick = function() {
				rdvId = findParamInClass('rdvId',this);
				new Ajax.Updater('listeRdv', '/ajaxyoujob/userCancelRdv', {
					parameters	: 'rdvId=' + rdvId,
					method		: 'post',
					onComplete	: Youjob.reload
				});
			}
		},

		'#userValidRdv' : function(el) {
			el.onclick = function() {
				rdvId = findParamInClass('rdvId',this);
				new Ajax.Updater('listeRdv', '/ajaxyoujob/userValidRdv', {
					parameters	: 'rdvId=' + rdvId,
					method		: 'post',
					onComplete	: Youjob.reload
				});
			}
		},

		'.ajaxSearchSelect' : function(el) {
			el.onclick = function() {

				searchType = findParamInClass('searchType', this);
				new Ajax.Updater('selectrecherche', '/ajaxyoujob/ajaxSearchSelect', {
					parameters	: 'searchType=' + searchType,
					method		: 'post',
					onComplete	: Youjob.reload
				});
			}
		},

		'#ajaxSubmitSearch' : function(el) {
			el.onclick = function() {
				document.search.submit();
			}
		},

		/* ---- Video ---- */

		'.ajaxSelectUserVideoType' : function(el) {
			el.onclick = function() {
				videoType = findParamInClass('videoType', this);

				if (videoType == 'formation') {
					$('videoType_formation').style.display = 'inline';
					$('videoType_experience').style.display = 'none';
					$('videoType_divers').style.display = 'none';

				} else if (videoType == 'experience') {
					$('videoType_formation').style.display = 'none';
					$('videoType_experience').style.display = 'inline';
					$('videoType_divers').style.display = 'none';

				} else if (videoType == 'divers') {
					$('videoType_formation').style.display = 'none';
					$('videoType_experience').style.display = 'none';
					$('videoType_divers').style.display = 'inline';

				}
			}
		},





		/* ---- Experience ---- */

		'.selectUserExperienceAction' : function(el) {
			el.onclick = function() {
				userId = $F('userId');
				infoType = findParamInClass('infoType', this);
				userExperienceId = findParamInClass('userExperienceId', this);

				new Ajax.Updater('modulecursus', '/ajaxyoujob/selectUserExperience', {
					parameters	: 'infoType=' + infoType + '&userExperienceId=' + userExperienceId + '&userId=' + userId,
					method		: 'post',
					onComplete	: Youjob.reload
				});
			}
		},

		'.ajaxSelectUserEditExperience' : function(el) {
			el.onclick = function() {
				infoType = findParamInClass('infoType', this);
				userExperienceId = findParamInClass('userExperienceId', this);

				new Ajax.Updater('modulecursus', '/ajaxyoujob/selectUserEditExperience', {
					parameters	: 'infoType=' + infoType + '&userExperienceId=' + userExperienceId,
					method		: 'post',
					onComplete	: Youjob.reload
				});
			}
		},

		'.ajaxUpdateUserExperience' : function(el) {
			el.onclick = function() {
				userExperienceId = findParamInClass('userExperienceId', this);
				new Ajax.Updater('modulecursus', '/ajaxyoujob/selectUserEditExperience', {
					parameters	: 'userExperienceId=' + userExperienceId + '&infoType=update',
					method		: 'post',
					onComplete	: Youjob.reload
				});
			}
		},

		'.ajaxSubmitUpdateUserExperience' : function(el) {
			el.onclick = function() {
				var form;
				var userExperienceId;
				form = $('formExperience').serialize();
				userExperienceId = findParamInClass('userExperienceId', this);
				new Ajax.Updater('modulecursus', '/ajaxyoujob/updateUserExperience', {
					parameters	: form + '&userExperienceId=' + userExperienceId,
					method		: 'post',
					onComplete	: Youjob.resetForm
				});
			}
		},

		'#ajaxInsertUserExperience' : function(el) {
			el.onclick = function() {
				new Ajax.Updater('userEditInfo', '/ajaxyoujob/insertUserExperience', {
					parameters	: 'infoType=insert',
					method		: 'post',
					onComplete	: Youjob.reload
				});
			}
		},

		'.ajaxDeleteUserExperience' : function(el) {
			el.onclick = function() {
				userExperienceId = findParamInClass('userExperienceId', this);
				new Ajax.Updater('modulecursus', '/ajaxyoujob/deleteUserExperience', {
					parameters	: 'userExperienceId=' + userExperienceId,
					method		: 'post',
					onComplete	: Youjob.reload
				});
			}
		},

		'.answerTextClick' : function(el) {
			el.onclick = function(){
				$('answerText').style.display = 'block';
			}
		},

		'.answerTextClickUser' : function(el) {
			el.onclick = function(){
				userlogin = findParamInClass('userlogin', this);
				if (userlogin){
					$('answerText').style.display = 'block';
				}
				else {
					new Ajax.Updater('littleConnexion', '/ajax/littleLogin', {onComplete : Youjob.showLogin});
				}
			}
		},

		'.ajaxFormClose' : function(el) {
			el.onclick = function(){
				$('formulaire').style.display = 'none';
			}
		},

		'.ajaxFormReset' : function(el) {
			el.onclick = function(){
				//	$('formExperience').reset();
				$('experienceTitle').value = '';
				$('experienceCompany').value = '';
				$('dateStart').value = '';
				$('dateEnd').value = '';
				$('experienceText').value = '';

			}
		},

		'#userSendTagResume' : function (el) {
			el.onclick = function () {
				form = $('formUserSendTagResume').serialize();
				new Ajax.Updater('infoSendMovie', '/ajaxyoujob/userSendTagResume', {
					parameters	: form,
					method		: 'post',
					onComplete	: Youjob.reload
				});
			}
		},

		/**
		* functions for job search agent
		**/

		'.ajaxEraseAgent' : function(el) {
			el.onclick = function() {
				idAgent = findParamInClass('agentId', this);
				new Ajax.Updater('desktopAgent', '/ajaxyoujob/userDeleteAgent', {
					parameters	: 'agentId=' + idAgent,
					method		: 'post',
					onComplete	: Youjob.reload
				});
			}
		},


		'#ajaxEditInfoVideo' : function(el) {
			el.onclick = function() {
				form = $('formEditInfoVideo').serialize();
				new Ajax.Request('/ajaxyoujob/editInfoVideo', {
					parameters	: form,
					method		: 'post',
					onComplete	: Youjob.change
				});
				//alert('Les informations ont correctement été modifiées');
			}
		},

		'#rollInscription' : function(el) {
			el.onmouseover = function(){
				$('rollInscription').src = '/themes/default/images/common/home/encard-inscrip_brille.jpg';
			};
			el.onmouseout = function() {
				$('rollInscription').src = '/themes/default/images/common/home/encard-inscrip.jpg';
			};
		},

		'#rollNewsletter' : function(el) {
			el.onmouseover = function(){
				$('rollNewsletter').src = 'http://news.youjob.com/image/NL-G.jpg';
			};
			el.onmouseout = function() {
				$('rollNewsletter').src = 'http://news.youjob.com/image/NL.jpg';
			};
		},

		'#Map' : function(el) {
			el.onmouseover = function(){
				$('rollInscription').src = '/themes/default/images/common/home/encard-inscrip_brille.jpg';
			};
			el.onmouseout = function() {
				$('rollInscription').src = '/themes/default/images/common/home/encard-inscrip.jpg';
			};
		},

		'#showWebCam' : function(el) {
			el.onclick = function() {
				new Ajax.Updater('spaceCam', '/ajaxyoujob/showWebCam',
				{onComplete	: Youjob.reload}
				);
			}
		},
		
		// for uploading file
		'.lauchUpload' : function(el) {
            el.onclick = function() {
                var id = findParamInClass('idform',this);
                var formName = ('formUpload'+id).toString();
                if($F('fileToUpload'+id) != ''){
                    var uploadId = findParamInClass('uploadId',this);
                    beginUpload(uploadId,id);
                   
                } else {
                    //alert('vide');
                }
            }
        },
        
        '.lauchUploadCvPapier' : function(el) {
            el.onclick = function() {
                var id = findParamInClass('idform',this);
                var formName = ('formUpload'+id).toString();
                if($F('fileToUpload'+id) != ''){
                    var uploadId = findParamInClass('uploadId',this);
                    beginUpload2(uploadId,id);
                   
                } else {
                    //alert('vide');
                }
            }
        },
		
		'.lauchUploadDesktop' : function(el) {
			el.onclick = function() {
				var idform = findParamInClass('idform',this);
				var formName = ('formUpload'+idform).toString();
				$(formName).submit();
				if($F('fileToUpload'+idform) != '') {
					var uploadId = findParamInClass('uploadId',this);
					beginUpload(uploadId,idform);
				}
				//$('divFormUploadContent'+idform).style.display='none';
                //$('divAddCv'+idform).style.display='block';
			}
		},
		// for delete cv uploaded
		'.userDeleteCvAction' : function(el) {
			el.onclick = function() {	
				var chaine = findParamInClass('pathFile', this);
				
				new Ajax.Updater('sendForm', '/ajaxyoujob/userDeleteCv', {
					parameters	: "link="+chaine,
					method		: 'post',
					onComplete	: Youjob.reload
				});
			}
		},
		
		/**
		* end functions for job search agent
		**/

		'.answerTextClick' : function(el) {
			el.onclick = function() {
				id = findParamInClass('id',this);
				form = "id=" + id;
				new Ajax.Updater('answerForm', '/ajaxyoujob/showTextResponseUser', {
					parameters	: form,
					method		: 'post',
					onComplete	: Youjob.reload
				});
			}
		},

		//2
		//id
		//'#balbla'
		//class
		'.updatePrompt' : function(el) {
			el.onclick = function() {
				// instructions pour ajax
				//1 recup�rer les donn�es de ton form
				form = $('formPrompt').serialize();
				//2 faire l'ajax -> updater : c'est pour mettre un div a jour
				new Ajax.Updater('videoPlayer2', '/ajaxyoujob/updatePrompt', {
					parameters	: form,
					method		: 'post',
					onComplete	: Youjob.reload
				});
			}
		},
		
		'.updatePrompOffer' : function(el) {
			el.onclick = function() {
				// instructions pour ajax
				//1 recup�rer les donn�es de ton form
				form = $('formPrompt').serialize();
				//2 faire l'ajax -> updater : c'est pour mettre un div a jour
				new Ajax.Updater('videoPlayer2', '/ajaxyoujob/updatePrompOffer', {
					parameters	: form,
					method		: 'post',
					onComplete	: Youjob.reload
				});
			}
		},

		'lastbutnotleast': {}
	}, /** rules **/

	'reload' :	function(ajax) {
		Behaviour.apply(Youjob.rules);
	},
	'resetForm' :	function(ajax) {
		$('experienceTitle').value = '';
		$('experienceCompany').value = '';
		$('dateStart').value = '';
		$('dateEnd').value = '';
		$('experienceText').value = '';
		Behaviour.apply(Youjob.rules);
	},

	'showLogin' :	function() {
		$('littleConnexion').style.display = 'block';
		Behaviour.apply(FactoryMedia.rules);
	},

	'change' : function(ajax) {
		window.location.href = "/membre/";
	}
	
}

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(Youjob.rules);