/**
 * @class: Util
 * @description: Defines Util functions
 **/

var Util = new Class({
	initialize: function(){
		this.initVideoComment();
		this.initVideoRating();
		this.initRed5Recorder();
		this.initTabs();
		this.initPaging();
		this.initFormSearch();
		this.initFormLoginFrontend();
		this.initFormAuthentification();
		this.initFormSignaler();
		this.initShowHideCategory();
		//CMSBlog.util.showPopupLayer('popupPlayer');
		//this.showPopupLayer('popupPlayer');
	},
	initFormSignaler: function(){
		var that = this;
		var signalerData = [{
			name: 'txtEmail',
			valid: 'required|email',
			message: L10N.required.email + '|' + L10N.valid.email
		},{
			name: 'txtSignaler',
			valid: 'selected',
			message: L10N.required.contentType
		},{
			name: 'txtCommentaires',
			valid: 'required',
			message: L10N.required.comment
		}];
		if($('formSignaler')){
			$('formSignaler').mooFormObj = new FormValidator('formSignaler', signalerData, {
				ajax: true,
				onSubmit: function(){
					new Request({
						url: $('formSignaler').action,
						method: 'post',
						onRequest: function(){
							//that.ajaxLoading();
							$('formSignaler').addClass('hidden');
							$$('#layerSignaler .waiting')[0].removeClass('hidden');
						},
						onSuccess: function(){
							//that.ajaxLoading(1);
							$$('#layerSignaler .message')[0].removeClass('hidden').set('html', arguments[0]);
							$$('#layerSignaler .waiting')[0].addClass('hidden');
						}
					}).send($('formSignaler'));
				}
			});
		}
	},
	/**
	 * @description: comment video functions
	 * @para: none
	 **/
	initVideoComment: function(){
		var that = this;
		var btnRecordVideo = $('btnRecordVideo');
		var btnRecordAudio = $('btnRecordAudio');
		var btnUploadPdf = $('btnUploadPdf');
		var recordBlockVideo = $('recordBlockVideo');
		var recordBlockAudio = $('recordBlockAudio');
		var attachMediaBlock = $('attachMediaBlock');
		var uploadPdfBlock = $('uploadPdfBlock');
		var btnCancelRecordVideo = $('btnCancelRecordVideo');
		var btnCancelRecordAudio = $('btnCancelRecordAudio');
		var btnCancelUpload = $('btnCancelUpload');

		var isValidForm = false;
		var media_type = $('media_type');
		var comment_id = $('comment_id');
		var postVideoComment = $('postVideoComment');

		var frm = $('frmAuthentify');
		if(frm) {			
			var formData = [{
				name: 'txtName',
				valid: 'required',
				message: L10N.required.username
			},{
				name: 'txtEmailComment',
				valid: 'required|email',
				message: L10N.required.email+'|'+L10N.valid.email
			}];
			frm.req = new Request({
				url: frm.getProperty('action'),
				method:	'post',
				onRequest: function(){
					//that.ajaxLoading();
					document.body.style.cursor = 'wait';
				},
				onSuccess: function(html) {
					//that.ajaxLoading(1);
					document.body.style.cursor = 'default';
					document.body.style.cursor = 'default';
					frm.innerHTML = L10N.alert.postCommentSuccess + '&nbsp;<img src="'+ imgPath +'loading.gif"/>';
					setTimeout(function(){
						location.reload();
					}, 3000);
				},
				onFailure: function(e) {
					document.body.style.cursor = 'default';
				}
			});
			//frm.req.send('&txtName='+ frm.txtName.value + '&txtEmail='+ frm.txtEmail.value + '&attachFilePath='+ frm.attachFilePath.value);
			var formValid = new FormValidator(frm, formData, {
				ajax: true,
				width: 230,
				onSubmit: function(){
					if(media_type.value == 0) {						
						if(frm.txtComment.value.trim() == "") {
							formValid.showLayer(frm.txtComment, L10N.required.comment);
							return false;
						}
					}
					frm.req.send(frm);
					return false;
				}
			});

		}
		function validForm(){
			if(frm.txtName.value.trim() == "") {
				formValid.showLayer(frm.txtName, L10N.required.username);
				return false;
			}
			if(frm.txtEmailComment.value.trim() == "") {
				formValid.showLayer(frm.txtEmailComment, L10N.required.email);
				return false;
			}
			if(!formValid.executeRegExp(formValid.options.reEmail, frm.txtEmailComment.value)){
				formValid.showLayer(frm.txtEmailComment, L10N.valid.email);
				return false;
			}
			isValidForm = true;
		}
		if(btnRecordVideo) {
			btnRecordVideo.removeEvents('click').addEvents({
				'click': function(e){
					if(e) e.stop();
					validForm();
					if(!isValidForm) return false;
					attachMediaBlock.addClass('hidden');
					recordBlockVideo.removeClass('hidden');
					recordBlockAudio.addClass('hidden');
					uploadPdfBlock.addClass('hidden');
					media_type.value = 2;
				}
			});

			btnRecordAudio.removeEvents('click').addEvents({
				'click': function(e){
					if(e) e.stop();
					validForm();
					if(!isValidForm) return false;
					attachMediaBlock.addClass('hidden');
					recordBlockAudio.removeClass('hidden');
					recordBlockVideo.addClass('hidden');
					uploadPdfBlock.addClass('hidden');
					media_type.value = 1;
				}
			});

			btnUploadPdf.removeEvents('click').addEvents({
				'click': function(e){
					if(e) e.stop();
					validForm();
					if(!isValidForm) return false;
					attachMediaBlock.addClass('hidden');
					recordBlockVideo.addClass('hidden');
					recordBlockAudio.addClass('hidden');
					uploadPdfBlock.removeClass('hidden');
					that.fancyUpload();
					media_type.value = 3;
				}
			});

			btnCancelRecordVideo.removeEvents('click').addEvents({
				'click': function(e){
					if(e) e.stop();
					attachMediaBlock.removeClass('hidden');
					recordBlockVideo.addClass('hidden');
					media_type.value = 0;
					isValidForm = false;
				}
			});
			btnCancelRecordAudio.removeEvents('click').addEvents({
				'click': function(e){
					if(e) e.stop();
					attachMediaBlock.removeClass('hidden');
					recordBlockAudio.addClass('hidden');
					media_type.value = 0;
					isValidForm = false;
				}
			});

			btnCancelUpload.removeEvents('click').addEvents({
				'click': function(e){
					if(e) e.stop();
					attachMediaBlock.removeClass('hidden');
					uploadPdfBlock.addClass('hidden');
					media_type.value = 0;
					CMSBlog.uploadPdf.options.container.destroy();
					isValidForm = false;
				}
			});
		}
		/*Display all 4 replies*/
		var btnDisplayAllReply = $$('.btnDisplayAllReply');
		if(btnDisplayAllReply.length > 0) {
			btnDisplayAllReply.each(function(btn, index){
				btn.removeEvents('click').addEvents({
					'click': function(e){
						if(e) e.stop();
						new Request({
							url: btn.getProperty('href'),
							method:	'post',
							onRequest: function(){
								//that.ajaxLoading();
								document.body.style.cursor = 'wait';
							},
							onSuccess: function(html) {
								//that.ajaxLoading(1);
								document.body.style.cursor = 'default';
								if(btn.getPrevious().hasClass('contReplyUpdate')) {
									var mask = new Element('div',{
										'id': 'AllReplyUpdate',
										'html': html
									}).injectInside(btn.getPrevious());
								}
								btn.setStyle('display', 'none');
							},
							onFailure: function(e) {
								document.body.style.cursor = 'default';
							}
						}).post();
					}
				});
			});			
		}
		if(postVideoComment) {
			var arrBtnReplyComments = $$('.btnReplyComment');
			var arrTitles = postVideoComment.getElements('.titBox');
			arrBtnReplyComments.each(function(reply, index){
				reply.removeEvents('click').addEvents({
					'click': function(e){
						if(e) e.stop();
						comment_id.value = reply.getProperty('rel');
						arrTitles[0].addClass('hidden');
						arrTitles[1].removeClass('hidden');
						frm.txtName.focus();
						window.scrollTo(0, $('postVideoComment').getCoordinates().top);
					}
				});
			});
		}
	},
	/**
	 * @description: rating video functions
	 * @para: none
	 **/
	initVideoRating: function(){
		var that = this;
		var arrResourceRating = $$('.resourceRating');
		if(arrResourceRating.length == 0) return;
		function rateShowHide(resource, showHide, rateValue) {
			resource.arrRates.each(function(rate, index){
				if(rateValue >= index) {
					if(showHide == 1) {
						rate.className = 'rateHover';
					} else {
						rate.className = 'while';
					}
				}
			});
		}
		function setRate(resource, rateValue){
			var tempRate = Math.floor(rateValue);
			var remain = (rateValue - tempRate) * 10;
			resource.arrRates.each(function(rate, index){
				if(index < tempRate) {
					rate.className = '';
				} else {
					rate.className = 'while';
				}
			});
			if(remain > 0 && remain <10) {
				resource.arrRates[tempRate].className = 'half';
			} else {
				resource.arrRates[tempRate].className = 'while';
			}
		}
			
		arrResourceRating.each(function(resource, index){
			resource.ratingPath = resource.getElement('.ratingPath');
			resource.rateVal = resource.getElement('.valVotes');
			resource.arrRates = resource.getElements('li');
			resource.arrRates.each(function(rate, rIndex){
				rate.isClick = false;
				rate.removeEvents('click').addEvents({
					'click': function(e){
						if(e) e.stop();
						if(!resource.getElement('.resourceRatingSuccess').hasClass('hidden')) return;
						if(!resource.getElement('.resourceRatingError').hasClass('hidden')) return;
						rate.isClick = true;
						rateShowHide(resource, 1, rIndex);
						new Request({
							url: resource.ratingPath.get('value').replace('#val', rIndex + 1) ,
							method:	'post',
							onRequest: function(){
								//that.ajaxLoading();
								document.body.style.cursor = 'wait';
							},
							onSuccess: function(html) {
								//that.ajaxLoading(1);
								document.body.style.cursor = 'default';
								if(html == -1){
									//error;
									if(resource.getElement('.resourceRatingError')) resource.getElement('.resourceRatingError').removeClass('hidden');
								} else {									
									resource.rateVal.value = html;
									setRate(resource, html);
									if(resource.getElement('.resourceRatingSuccess')) resource.getElement('.resourceRatingSuccess').removeClass('hidden');
								}
							},
							onFailure: function(e) {
								document.body.style.cursor = 'default';
							}
						}).post();
					},
					'mouseenter': function(){
						rateShowHide(resource, 1, rIndex);
					},
					'mouseleave': function(){
						setRate(resource, resource.rateVal.value);
					}
				});
			});
		});
	},
	/**
	 * @description: tabs list comment functions
	 * @para: none
	 **/
	initTabs: function() {
		var arrTabHandlers = $$('.tabHandler');
		var arrTabContents = $$('.tabContent');
		if(arrTabHandlers.length == arrTabContents.length) {
			arrTabHandlers.each(function(tab, index){
				tab.items = tab.getElements('a');
				tab.contents = arrTabContents[index].getElements('ul');
				tab.fx1 = new Fx.Tween(tab.contents[0]);
				tab.fx2 = new Fx.Tween(tab.contents[1]);
				if(tab.items.length > 1) {
					tab.items[0].removeEvents('click').addEvents({
						'click': function(e){
							if(e) e.stop();
							tab.items[0].addClass('active');
							tab.items[1].removeClass('active');

							tab.contents[0].removeClass('hidden');
							tab.contents[0].setStyle('opacity', 0);
							tab.fx1.start('opacity', 1);

							tab.contents[1].addClass('hidden');
							tab.contents[1].setStyle('opacity', 0);

						}
					});

					tab.items[1].removeEvents('click').addEvents({
						'click': function(e){
							if(e) e.stop();
							tab.items[1].addClass('active');
							tab.items[0].removeClass('active');

							tab.contents[1].removeClass('hidden');
							tab.contents[1].setStyle('opacity', 0);
							tab.fx2.start('opacity', 1);

							tab.contents[0].addClass('hidden');
							tab.contents[0].setStyle('opacity', 0);

						}
					});
				}
			});
		}
	},
	/**
	 * @description: paging video comment functions
	 * @para: none
	 **/
	initPaging: function() {
		var pagingAjax = $('pagingAjax');
		if(pagingAjax) {
			pagingAjax.arrPages = pagingAjax.getElements('a');
			var pagingUpdateDiv = $('pagingUpdateDiv');
			if(!pagingUpdateDiv) return;
			pagingAjax.arrPages.each(function(page, pIndex){
				page.removeEvents('click').addEvents({
					'click': function(e){
						if(e) e.stop();
						CMSBlog.util.ajaxGeneral(page.getProperty('href'),{},pagingUpdateDiv.value, function(){
							CMSBlog.util.initPaging();
							CMSBlog.util.initVideoComment();
						}, true);
					}
				});
			});
		}
	},
	/**
	 * @description: show/hide category functions
	 * @para: none
	 **/
	initShowHideCategory: function() {
		var arrMores = $$('.moreCategory');
		try {
		arrMores.each(function(item, index){
			item.listCate = item.getParent().getElement('ul.lstMoreCategory');
			item.removeEvents('click').addEvents({
				'click': function(e){
					if(e) e.stop();
					if(item.listCate.hasClass('hidden')) {
						item.getElement('span').innerHTML = '[-]';
					} else {
						item.getElement('span').innerHTML = '[+]';
					}
					item.listCate.toggleClass('hidden');
					item.addClass('hidden');
				}
			});
			if(item.listCate.getElement('.active')) {
				item.fireEvent('click');
			}
		});
		} catch (e) {printLog(e);}
	},
	/**
	 * @description: valid form search functions
	 * @para: none
	 **/
	initFormSearch: function() {
		var frmSearch = $('frmSearch');
		if( !frmSearch ) {
			return;
		}
		var formData = [{
			name: 'q',
			valid: 'required',
			init: frmSearch.q.value == "" ? L10N.required.initSearchBox : frmSearch.q.value,
			maxLength: 255,
			message: L10N.required.searchBox
		}];
		var formValid = new FormValidator(frmSearch, formData, {
			width:160
		});
	},
	/**
	 * @description: valid form login functions
	 * @para: none
	 **/
	initFormLoginFrontend: function() {
		var frmLogin = $('fnLoginFrontend');
		if( !frmLogin ) {
			return;
		}
		var formData = [{
			name: 'account_login',
			valid: 'required',
			//init: L10N.required.initSearchBox,
			maxLength: 255,
			message: L10N.required.username
		},{
			name: 'account_password',
			valid: 'required',
			//init: L10N.required.initSearchBox,
			maxLength: 255,
			message: L10N.required.password
		}];
		var formValid = new FormValidator(frmLogin, formData, {
			width:110
		});
	},
	/**
	 * @description: valid form Authentification functions
	 * @para: none
	 **/
	initFormAuthentification: function() {
		var frmAuthentification = $('frmAuthentification');
		var _width = 110;
		if( !frmAuthentification ){
			frmAuthentification = $('frmAuthentifyProtected');
			_width = 234;
		}
		if( !frmAuthentification ) return;
		var formData = [{
			name: 'txtUsername',
			valid: 'required',
			//init: L10N.required.initSearchBox,
			maxLength: 255,
			message: L10N.required.username
		},{
			name: 'txtPassword',
			valid: 'required',
			//init: L10N.required.initSearchBox,
			maxLength: 255,
			message: L10N.required.password
		}];
		var formValid = new FormValidator(frmAuthentification, formData, {
			width:_width
		});
	},
	ajaxLoading: function(close){
		var that = this;
		var loading = $('ajaxLoading');
		if(!loading){
			loading = new Element('div',{
							'id': 'ajaxLoading',
							'styles': {
								'position': 'absolute',
								'top': -15000,
								'left': 0
							},
							'html': '<img src="' + imgPath + 'loading.gif"/>'
						}).inject(document.body);
		}
		if(close){
			that.hideLayer('ajaxLoading');
		}else{
			that.showPopupLayer('ajaxLoading');
		}
	},
	/**
	 * @description: valid form Authentification functions
	 * @para: path, postParas, objContent, callback
	 * 	@path: path of ajax
	 * 	@postParas: array parameters of ajax
	 * 	@objContent: update content
	 * 	@callback: callback function after success ajax
	 * 	@loading: loading function ajax (true or false)
	 **/
	ajaxGeneral: function(path, postParas, objContent, callback, loading) {
		if(path == "") return;
		objContent = $(objContent);
		if(!loading) loading = false;
		objContent._cache = objContent.innerHTML;
		if(!objContent) return;
		new Request({
			url: path,
			method:	'post',
			data:	postParas,
			onRequest: function(){
				document.body.style.cursor = 'wait';
				if(loading) {
					objContent.innerHTML = "";
					objContent.addClass('loadingAjax');
				}
			},
			onSuccess: function(html) {
				if(loading) objContent.removeClass('loadingAjax');
				document.body.style.cursor = 'default';
				objContent.innerHTML = html;
				if($defined(callback)){
					if($type(callback) == 'function'){
						callback();
					} else {
						eval(callback);
					}
				}
			},
			onFailure: function(e) {
				document.body.style.cursor = 'default';
				if(loading) {
					objContent.innerHTML = objContent._cache;
					objContent.removeClass('loadingAjax');
				}
				//printLog(e);
			}
		}).post();
	},
	/**
	 * @description: Red5Recorder Object
	 * @para: none
	 **/
	initRed5Recorder: function(){
		//var phpPath = $('record_phpPath');
		var serverIP = $('record_serverIP');
		if(!serverIP) return;
		var record_cookie = $('record_cookie');
		var pathVideo = "/applets/videoComments.swf";
		var pathAudio = "/applets/audioComments.swf";
		swfobject.embedSWF(pathVideo, 'recordVideo', "374", "341", "8", false, {ip:serverIP.value, cookie: record_cookie.value}, {});
		swfobject.embedSWF(pathAudio, 'recordAudio', "374", "341", "8", false, {ip:serverIP.value, cookie: record_cookie.value}, {});
	},
	/**
	 * @description: Upload file by fancy
	 * @para: none
	 **/
	fancyUpload: function(param1){
		var that = this;
		var divFancy = $('divFancyUpload');
		if(!divFancy) return;
		var isFancyUpload = false;
		CMSBlog.uploadPdf = new FancyUpload3.Attach('listFiles', '#attachFile, #attachAnotherFile', {
			path:  '/js/fancy/Swiff.Uploader.swf',
			url: divFancy.getElement('input[name=uploadURL]').get('value'),
			appendCookieData: true,
			allowDuplicates: true,
			multiple: false,
			fileSizeMax: $('file_size_limit').value,
			typeFilter: {'Files': ''+$('media_file_types').value},
			onSelectSuccess: function(files){
				//printLog('onSelectSuccess');
				var listErrorLis = this.list.getElements('li.file-invalid');
				var listLis = this.list.getElements('li.file');
				if(listErrorLis.length){
					listErrorLis.destroy();
				}
				if(listLis.length > 1){
					listLis[0].destroy();
				}
				//add cancel event						
				
				$('uploadPdfBlock').setStyle('height', $('uploadPdfBlock').getCoordinates().height);
				$('divListFiles').addClass('hidden');
				$('divFancyUpload').addClass('hidden');				
				$('divFancyUploadCancel').removeClass('hidden');					
				
				$('attachFileCancel').removeEvents().addEvent('click', function(e){					
					printLog(files[0]);
					$('divFancyUpload').removeClass('hidden');
					$('divFancyUploadCancel').addClass('hidden');
					$('divFancyUploadCancel').setStyles({
						'zIndex': 9
					});					
					files[0].remove();
					CMSBlog.uploadPdf.options.container.destroy();
					that.fancyUpload();					
				});
				//hide btn select file
				//$$('span.swiff-uploader-box')[0].addClass('hidden');
				var tempCoord = $$('span.swiff-uploader-box')[0].getCoordinates();
				$('divFancyUploadCancel').setStyles({
					'position': 'absolute',
					'visibility': 'visible',
					'zIndex': 9999,
					'overflow': 'hidden',
					//'width': tempCoord.width,
					'width': 244,
					'left': tempCoord.left,
					'right': tempCoord.right,
					'bottom': tempCoord.bottom,
					'height': 48,
					'top': tempCoord.top
				});
				
			},
			onSelectFail: function(files) {
				this.list.getElements('li.file-invalid').destroy();
				files.each(function(file) {
					new Element('li', {
						'class': 'file-invalid',
						events: {
							click: function() {
								this.destroy();
							}
						}
					}).adopt(
						new Element('span', {html: file.validationErrorMessage || file.validationError})
					).inject(this.list, 'bottom');
				}, this);
			},
			onFileSuccess: function(file) {
				//printLog('onFileSuccess');				
				$('divFancyUploadCancel').addClass('hidden');
				$('divFancyUpload').removeClass('hidden');
				// printLog(file);
				
				try {
					var jsText = JSON.decode(file.response.text);
					//printLog(typeof(jsText));
					if(jsText.errCode == 0) {
						//$('attachFilePath').value 		= file.name;
						$('file_physical_name').value 	= jsText.file_physical_name;
						$('file_ext').value 			= jsText.file_ext;
						$('file_encoding_status').value = jsText.file_encoding_status;
						$('file_type').value 			= jsText.file_type;
						$('format').value 				= jsText.format;
						$('duration').value 			= jsText.duration;
						$('file_volume').value 			= jsText.file_volume;
						$('attachFilePath').value 		= jsText.file_original_name;
						file.ui.element.highlight('#e6efc2');
						isFancyUpload = true;
						var frm = $('frmAuthentify');
							frm.req = new Request({
								url: frm.getProperty('action'),
								method:	'post',
								onRequest: function(){
									// that.ajaxLoading();
									document.body.style.cursor = 'wait';
								},
								onSuccess: function(html) {
									// that.ajaxLoading(1);
									document.body.style.cursor = 'default';
									frm.innerHTML = L10N.alert.postCommentSuccess + '&nbsp;<img src="'+ imgPath +'loading.gif"/>';
									setTimeout(function(){
										location.reload();
									}, 3000);
								},
								onFailure: function(e) {
									document.body.style.cursor = 'default';
								}
							});
							frm.req.send(frm);
					}
				} catch(e){
					printLog(e);
				}
			},
			onFileError: function(file) {				
				file.ui.cancel.set('html', 'Retry').removeEvents().addEvent('click', function() {
					file.requeue();
					return false;
				});

				new Element('span', {
					html: file.errorMessage,
					'class': 'file-error'
				}).inject(file.ui.cancel, 'after');
			},
			onFileRequeue: function(file) {
				file.ui.element.getElement('.file-error').destroy();

				file.ui.cancel.set('html', 'Annuler').removeEvents().addEvent('click', function() {
					file.remove();
					return false;
				});

				this.start();
			}, 
			onQueue: function(file){			
                if (!CMSBlog.uploadPdf.uploading) return;
								
				this.setEnabled(false);   
				var tempName = CMSBlog.uploadPdf.fileList[0].name;
				if(tempName.length > 8) {
					tempName = tempName.substring(0,8) + "...";
				}
				var percent = {
					percent: CMSBlog.uploadPdf.percentLoaded,
					filename: tempName
				}
				var percentLong = {
					percent: CMSBlog.uploadPdf.percentLoaded,
					filename: CMSBlog.uploadPdf.fileList[0].name
				}				
				$('attachFileCancel').value = L10N.alert.uploading.substitute(percent);
				$('attachFileCancel').setProperties({
					'alt': L10N.alert.uploading.substitute(percentLong),
					'title': L10N.alert.uploading.substitute(percentLong)
				});
			},
			onComplete: function(){				
			}
		});
	},
	/**
	 * @description: show layer functions
	 * @para: strId: id of object element
	 * @para: param1: path of ajax
	 * @para: param2: click object handle
	 **/
	showPopupLayer: function(strId, param1, param2){
		var that = this;
		var el = $(strId);
		if(!el)	return;		
		// if(el.style.display == "block") {			
			// $(param2).setProperties({
				// 'alt': L10N.text.clickToOpen,
				// 'title': L10N.text.clickToOpen
			// });
			// that.hideLayer(strId);
			// return false;
		// } else {
			// $(param2).setProperties({
				// 'alt': L10N.text.clickToClose,
				// 'title': L10N.text.clickToClose
			// });
		// }
		var _opacity = 0.5;
		var mask = new Element('div',{
			'id': 'divPopup'+ strId,
			'styles':{
				'display': 'block',
				'visibility': 'visible',
				'position': 'absolute',
				'top': 0,
				'left': 0,
				'width': window.getWidth() + 'px',
				'height': window.getScrollSize().y,
				'opacity': 0
			}
		}).injectInside($$('body')[0]);
		// if(typeof(param2) != 'undefined') {
			// mask.setStyle('display', 'none');
		// }
		new Fx.Tween($('divPopup'+strId), {
			duration: 500
		}).start('opacity', _opacity);


		el.injectInside($$('body')[0]);

		el.setStyle('display', 'block');
//		var zIndex = 999;

		el.setStyles({
			'opacity': 0,
			'position': 'absolute',
			'top': (window.getHeight() - el.getCoordinates().height)/2 + window.getScrollTop(),
			'left': (window.getWidth() - el.getCoordinates().width)/2 + 'px'
//			'z-index': zIndex
		});
		new Fx.Tween(el).start('opacity', 1);

		if(document.body.getScrollSize().y > window.getScrollTop()) {
			window.removeEvents().addEvent('scroll',function(){
				if(el){
					// el.setStyle('top',((window.getHeight() - el.getCoordinates().height)/2 + window.getScrollTop() + 'px'));
					var width = window.getScrollSize().x;
					var height = window.getScrollSize().y;
					if (height < document.body.clientHeight) {
						height = document.body.clientHeight;
					}
					if($('divPopup'+strId)){
						$('divPopup'+strId).setStyles({
							'width': width,
							'height': height
						});
					}
				}
			});
		}

		window.addEvent('resize', function(e){
			var w = window.getWidth();
			// el.setStyles({
				// 'top': (window.getHeight() - el.getCoordinates().height)/2 + window.getScrollTop(),
				// 'left': (window.getWidth() - el.getCoordinates().width)/2 + 'px'
			// });
			if($('divPopup'+strId)) {
				$('divPopup'+strId).setStyles({
					'width': window.getWidth() + 'px',
					'height': document.body.getScrollSize().y
				});
			}
		});

		mask.addEvent('click', function(e){
			if(e) e.stop();
			that.hideLayer(strId);
		});

		var btnClose = el.getElement('.btnClose');
		if(btnClose) {
			btnClose.addEvent('click', function(e){
				if(e) e.stop();
				that.hideLayer(strId);
			});
		}

		if(strId == 'popupPlayer'){
			if(param1 != ''){
				new Request({
					url: param1,
					method: 'get',
					onRequest: function(){
					},
					onSuccess: function(){
						el.set('html', arguments[0]);
						el.setStyles({
							'top': $(param2).getCoordinates().top +20,
							'left': $(param2).getCoordinates().left + ($(param2).getCoordinates().width/2) - (el.getCoordinates().width/2)
						});
					}
				}).send();
			}
			
			if(typeof(param2) != 'undefined') {
				mask.setStyle('cursor', 'pointer');
				mask.setProperties({
					'alt': L10N.text.clickToClose,
					'title': L10N.text.clickToClose
				});
				el.setStyles({
					'top': $(param2).getCoordinates().top +20,
					'left': $(param2).getCoordinates().left + ($(param2).getCoordinates().width/2) - (el.getCoordinates().width/2)
				});
			}
		}
	},
	/**
	 * @description: show layer functions
	 * @para: strId: id of object element
	 **/
	hideLayer:  function(strId){
		if($('divPopup'+strId)) {
			new Fx.Tween($('divPopup'+strId), {
				duration: 500
			}).start('opacity', 0).chain(function(){
				$('divPopup'+strId).destroy();
			});
		}
		if(strId) $(strId).setStyles({
			'display': "none",
			'top': -15000
		});
		if(Browser.Engine.trident4) {
			window.removeEvent('scroll');
		}
		window.removeEvents();
		if(strId == 'popupPlayer'){
			$(strId).set('html', "");
		}
	}
});

function postVideoCommentAjax(){
	var frm = $('frmAuthentify');
	frm.req = new Request({
		url: frm.getProperty('action'),
		method:	'post',
		onRequest: function(){
			// that.ajaxLoading();
			document.body.style.cursor = 'wait';
		},
		onSuccess: function(html) {
			// that.ajaxLoading(1);
			document.body.style.cursor = 'default';
			frm.innerHTML = L10N.alert.postCommentSuccess + '&nbsp;<img src="'+ imgPath +'loading.gif"/>';
			setTimeout(function(){
				location.reload();
			}, 3000);
		},
		onFailure: function(e) {
			document.body.style.cursor = 'default';
		}
	});
	//frm.req.send('&txtName='+ frm.txtName.value + '&txtEmail='+ frm.txtEmail.value + '&txtComment='+ frm.txtComment.value + '&attachFilePath='+ frm.attachFilePath.value);
	frm.req.send(frm);
}
function validMultimediaCommentForm(video, currentTimer) {
	$('media_time').value = currentTimer;
	new Request({
		url: $('record_phpPath').value,
		data:	{
			'media_type':	$('media_type').value,
			'video':	video,
			'currentTimer':	currentTimer
		},
		method:	'post',
		onRequest: function(){
			// that.ajaxLoading();
			document.body.style.cursor = 'wait';
		},
		onSuccess: function(html) {
			// that.ajaxLoading(1);
			var jsText = JSON.decode(html);
			$('file_physical_name').value 	= jsText.file_physical_name;
			$('duration').value 			= jsText.duration;
			$('file_volume').value 			= jsText.file_volume;
			postVideoCommentAjax();
		},
		onFailure: function(e) {
			document.body.style.cursor = 'default';
		}
	}).send();
}
function printLog(msg){
	try {
		if(window.console ) {
			if(typeof(console.log) != 'undefined') console.log(msg);
		}
	} catch(e){
	}
}
onerror = handleErr;
function handleErr(msg, url, l){
	if (window.console){
		console.log(msg + " : " + url + " : " + l);
	}
	return true;
}
