
acms.modules.video = {

	init:function(){ //{{{
		$('.acms-module-video').each(function(){ 
		});
	},//}}}
	initToolBar:function(){//{{{
		$('body').append("<div id='acms-module-video'></div>");
		var w = $('#acms-module-video');
		w.dialog({ 'title':"Vidéo", 'width':600, 'modal':true, 'autoOpen':false });

		$('.acms-module-video').each(function(){
			var that = this;
			var id_box = $(this).attr('id').substr(4);

			$('#btEdit', that).button({'icons':{'primary':'ui-icon-pencil'}, 'text':false}).click(function(){
				acms.modules.video.admin(id_box);
			});

		});
	},//}}}
	add:function(box_parent){ //{{{
		acms.modules.video.admin(undefined, box_parent);
	},//}}}
	admin:function(id_box, box_parent){//{{{

		var that = this;
		var ratio = undefined;

		var w = $('#acms-module-video');

		$.ajax({
			'url':'/modules/video/controller/form.html', 
			'dataType':'html',
			'data':'id_box='+id_box,
			'success':function(data){
				w.html(data);
				that.init();
			}
		});
		
		this.init = function(){//{{{

			$('#btFermer', w).button({'icons':{'primary':'ui-icon-circle-close'}}).click(function(){ w.dialog('close'); });
			$('#btValider', w).button({'icons':{'primary':'ui-icon-circle-check'}}).click(validate);

			$('#btRefresh', w).button({'icons':{'primary':'ui-icon-refresh'}}).click(updatePreview);
			
				$('#preview', w).html($('[name=content]', w).val());

				$('#content', w).change(function(){
					$('#preview').html($(this).val());
					parse();
				});

				$('[name=height]', w).keyup(function(){
					var height = parseInt($(this).val());
					if(isNaN(height )){
						height = 0;
					}
					$('[name=height]', w).val(height);
					var width = $('[name=width]', w).val();
					width  = parseInt(height / ratio);
					$('[name=width]', w).val(width);
				});

				$('[name=width]', w).keyup(function(){
					var width = parseInt($(this).val());
					if(isNaN(width )){
						width = 0;
					}
					$('[name=width]', w).val(width);
					var height = $('[name=height]', w).val();
					height  = parseInt(width * ratio);
					$('[name=height]', w).val(height);
				});

				parse();

			if($('[name=id_video]', w).val() !== undefined){
			}else{

			}

			w.dialog('open');
		}//}}}
		var parse = function(){//{{{
			var html = $('#preview', w);

			var height = $("iframe", html).attr('height');
			$('[name=height]', w).val(height);

			var width = $("iframe", html).attr('width');
			$('[name=width]', w).val(width);

			ratio = height / width;

			updatePreview();
		};//}}}
		var updatePreview = function(){//{{{
			console.log('!!');
			var html = $('#preview', w);
			$("iframe", html).attr('height', $('[name=height]', w).val());
			$("iframe", html).attr('width', $('[name=width]', w).val());
			$("#content", w).html(html.html());
		}//}}}
		var validate = function(){//{{{

			var data = {
				'id_video':$("[name=id_video]", w).val(),
				'content':$("[name=content]", w).val(),
				'action':$("[name=action]", w).val(),
			};

			$.ajax({
				'url':'/modules/video/controller.php', 
				'data':data,
				'success':function(data){
					if(data.error === true){
						$("#msg").alert(data.msg);
					}else{
						if(box_parent !== undefined && data.id_box !== undefined){
							w.dialog('close');
							var html = "<div id=box_"+data.id_box+" class='acms-box'/>";
							if($(box_parent).hasClass('acms-zone')){
								box_parent.before(html);
							}else{
								box_parent.after(html);
							}
							acms.updateContainer({'container_id':box_parent.parent().attr('id'), 'boxes':box_parent.parent().sortable("toArray"), 'id_page':$(".acms-page").attr('id').substr(5), 'success':function(){
								w.dialog('close');
								window.location = window.location;
							}});
						}else{
							w.dialog('close');
							window.location = window.location;
						}
					}
				}
			});

		};//}}}

	}//}}}

};


