function sizeConteneur(bloc_id,height){
	jQuery('#contenu').css('height',height+'px');
	var w =jQuery('#contenu').width();
	$.post('../../inclusion/bloc_aj.php',
			{
				action:'modif',
				bloc_id:bloc_id,
				bloc_width:w,
				bloc_height:height,
				bloc_left:'0',
				bloc_top:'0'
			},
			function(result){/*alert(result);*/});
	jQuery('#conteneurHeight').val(height);
}
function redimention(elem,save){
	var tmpW =jQuery(elem).css('width');
	var w=parseInt(tmpW.replace('px',''));
	var w1=jQuery(elem).find('.blocTopLeft').width();
	var w2=jQuery(elem).find('.blocTopRight').width();
	
	var tmpH =jQuery(elem).css('height');
	var h=parseInt(tmpH.replace('px',''));
	var h1=jQuery(elem).find('.blocTopLeft').height();
	var h2=jQuery(elem).find('.blocBottomLeft').height();
	
	//alert('('+w+'||'+jQuery(elem).css('width')+'-('+w1+'+'+w2+'))='+(w-(w1+w2))+' <==> ('+h+'||'+jQuery(elem).css('height')+'-('+h1+'+'+h2+'))='+(h-(h1+h2)));
	
	//alert(w1+' - '+w2+' - '+jQuery(elem).width());
	jQuery(elem).find('.blocTop,.blocCenter,.blocBottom').css('width',(w-(w1+w2))+'px');
	jQuery(elem).find('.blocLeft,.blocCenter,.blocRight').css('height',(h-(h1+h2))+'px');

	if(save==true){
		var L=(jQuery(elem).position().left+parseInt(jQuery(elem).css('margin-left').replace('px','')));
		var T=(jQuery(elem).position().top+parseInt(jQuery(elem).css('margin-top').replace('px','')));
		$.post('../../inclusion/bloc_aj.php',
			{
				action:'modif',
				bloc_id:jQuery(elem).attr('rel'),
				bloc_width:w+'|'+(w-(w1+w2)),
				bloc_height:h+'|'+(h-(h1+h2)),
				bloc_left:L,
				bloc_top:T
			},
			function(result){/*alert(result);*/}
		);
	}

}

function parametrable(selector){
	jQuery(selector).draggable({	
		grid: [10, 10],
		containment: 'parent',
		start: function(event, ui){
			floatBlocEdition(jQuery(this));
		},
		drag: function(event, ui){
			floatBlocEdition(jQuery(this),true);
		},
		stop: function(event, ui) { 
//			var L=(jQuery( this).position().left+parseInt(jQuery(this).css('margin-left').replace('px','')));
//			//alert(jQuery( this).position().left+' + '+parseInt(jQuery(this).css('margin-left').replace('px',''))+' = '+L);
//			var T=(jQuery( this).position().top+parseInt(jQuery(this).css('margin-top').replace('px','')));
//			var w =jQuery(this).width();
//			var w1=jQuery(this).find('.blocTopLeft').width();
//			var w2=jQuery(this).find('.blocTopRight').width();
//			var h =jQuery(this).height();
//			var h1=jQuery(this).find('.blocTopLeft').height();
//			var h2=jQuery(this).find('.blocBottomLeft').height();
//			$.post('../../inclusion/bloc_aj.php',
//					{
//						action:'modif',
//						bloc_id:jQuery(this).attr('rel'),
//						bloc_width:w+'|'+(w-(w1+w2)),
//						bloc_height:h+'|'+(h-(h1+h2)),
//						bloc_left:L,
//						bloc_top:T
//					},
//					function(result){/*alert(result);*/}
//				);
			saveModif($(this));
			floatBlocEdition($(this));
		}
	});

	jQuery(selector).resizable({
		grid: 10,
		resize: function(event, ui) {
			redimention(jQuery(this),false);
			floatBlocEdition(jQuery(this),true);
		},
		start: function(event, ui){
			floatBlocEdition(jQuery(this));
		},
		stop: function(event, ui) { 
//			var L=(jQuery( this).position().left+parseInt(jQuery(this).css('margin-left').replace('px','')));
//			var T=(jQuery( this).position().top+parseInt(jQuery(this).css('margin-top').replace('px','')));
//			var w =jQuery(this).width();
//			var w1=jQuery(this).find('.blocTopLeft').width();
//			var w2=jQuery(this).find('.blocTopRight').width();
//			var h =jQuery(this).height();
//			var h1=jQuery(this).find('.blocTopLeft').height();
//			var h2=jQuery(this).find('.blocBottomLeft').height();
//			//alert(w1+' - '+w2+' - '+jQuery(elem).width());
//			$.post('../../inclusion/bloc_aj.php',
//					{
//						action:'modif',
//						bloc_id:jQuery(this).attr('rel'),
//						bloc_width:w+'|'+(w-(w1+w2)),
//						bloc_height:h+'|'+(h-(h1+h2)),
//						bloc_left:L,
//						bloc_top:T
//					},
//					function(result){/*alert(result);*/});
			saveModif($(this));
			floatBlocEdition(jQuery(this));
		}
	});
	jQuery(selector).hover(
			function(){jQuery(selector).css('cursor','move');},
			function(){jQuery(selector).css('cursor','auto');}
	);
	editionBloc(selector);
	
}

function editionBloc(selector){
	jQuery(selector).click(function(){
		floatBlocEdition(jQuery(this));
	});
	
	jQuery(selector).dblclick(function(){
		editBlocText(jQuery(this).attr('rel'));
	});
}

function createBloc(bloc,page_id,version_id,type,anim){
	$.post(
			'../../inclusion/bloc_aj.php',
			{action:'getBloc',bloc_id:bloc,bloc_type:type,page:page_id,version:version_id},
			function(result){
				jQuery('#contenu').append(result);
				var elem=jQuery('#contenu').children().last();
				redimention(elem,false);
				parametrable(elem);
			}
	);
}

function createBlocAccueil(bloc,type){
	//alert(bloc);
	$.post(
			'inclusion/bloc_aj.php',
			{action:'getBloc',bloc_id:bloc,bloc_type:type,edit:'N'},
			function(result){
				//alert(result);
				jQuery('#contenu').append(result);
				var elem=jQuery('#contenu').children().last();
				redimention(elem,false);
				//parametrable(elem);
			}
	);
}

function newBloc(type,page,version){
	createBloc('-'+NumBloc,page,version,type,'none');	
	NumBloc++;
}

function editBlocRapide(bloc){
	
	var elem='';
	elem+="\n"+'<div id="dialogEditRapide" rel="'+bloc+'" title="Edition">';
	elem+="\n"+'</div>';
	
	//création de la popup
	jQuery('body').append(elem);
	
	// Dialog			
	jQuery('#dialogEditRapide').dialog({
		autoOpen: false,
		modal:true,
		width: 950,
		height: 620,
		buttons: {
			"Ok": function() { 
				//document.all.frame_editor.contentWindow.wp_send_to_html(document.all.frame_editor.contentWindow.myEditorCode);
				//var contenu=encodeChar(jQuery('#frame_editor').contents().find('#myEditorCode').val());
				var contenu=WPro.editors['myEditorCode'].getValue();
				$.post('inclusion/bloc_aj.php',
						{
							action:'contenu',
							bloc_id:jQuery('#dialogEditRapide').attr('rel'),
							bloc_contenu:contenu,
							//bloc_animation:jQuery('#effectTypesText option:selected').val()
							bloc_animation:'none'
						},
						function(result){location.reload();});
				//jQuery('#conteneur_bloc'+jQuery('#dialog').attr('rel')).html(contenu);
//				WPro.deleteEditor('myEditorCode');
//				jQuery(this).dialog("close");
//				jQuery('.wproEditor').remove();
//				jQuery('#dialogEditRapide').remove();
//				location.reload();
			}, 
			"Cancel": function() { 
				WPro.deleteEditor('myEditorCode');
				jQuery(this).dialog("close"); 
				jQuery('.wproEditor').remove();
				jQuery('#dialogEditRapide').remove();
			} 
		}
	});
	
	jQuery('#dialogEditRapide').attr('rel',bloc);
	jQuery('#dialogEditRapide').dialog('open');
	
	/*jQuery.get("editor_bloc.php",
				{
					bloc_id:bloc,
					name:'myEditorCode'+bloc
				},
				function(result){
					jQuery('#dialog').html(result);
					jQuery('.wproLoadMessageHolder').css('display','none');
				});
	*/
	if (typeof(WPro) != 'undefined'){
		WPro.deleteEditor('myEditorCode');
	}
	
	$('#dialogEditRapide').load("admin/contenu/editor_bloc.php", "bloc_id="+bloc);
	/*alert(WPro);*/
	
	/*jQuery('#dialog').html('<iframe name="frame_editor" id="frame_editor" src="editor_bloc.php?bloc_id='+bloc+'" width="810" height="520" frameborder="0" align="middle"></iframe>');*/
	
}

function editBlocText(bloc){
	
	var elem='';
	elem+="\n"+'<div id="dialog" rel="'+bloc+'" title="Edition d\'un bloc de text">';
	elem+="\n"+'</div>';
	
	//création de la popup
	jQuery('body').append(elem);
	
	// Dialog			
	jQuery('#dialog').dialog({
		autoOpen: false,
		modal:true,
		width: 950,
		height: 620,
		buttons: {
			"Ok": function() { 
				//document.all.frame_editor.contentWindow.wp_send_to_html(document.all.frame_editor.contentWindow.myEditorCode);
				//var contenu=encodeChar(jQuery('#frame_editor').contents().find('#myEditorCode').val());
				var contenu=WPro.editors['myEditorCode'].getValue();
				$.post('../../inclusion/bloc_aj.php',
						{
							action:'contenu',
							bloc_id:jQuery('#dialog').attr('rel'),
							bloc_contenu:contenu,
							//bloc_animation:jQuery('#effectTypesText option:selected').val()
							bloc_animation:'none'
						},
						function(){});
				jQuery('#conteneur_bloc'+jQuery('#dialog').attr('rel')).html(contenu);
				WPro.deleteEditor('myEditorCode');
				jQuery(this).dialog("close"); 
				jQuery('.wproEditor').remove();
				jQuery('#dialog').remove();
			}, 
			"Cancel": function() { 
				WPro.deleteEditor('myEditorCode');
				jQuery(this).dialog("close"); 
				jQuery('.wproEditor').remove();
				jQuery('#dialog').remove();
			} 
		}
	});
	
	jQuery('#dialog').attr('rel',bloc);
	jQuery('#dialog').dialog('open');
	
	/*jQuery.get("editor_bloc.php",
				{
					bloc_id:bloc,
					name:'myEditorCode'+bloc
				},
				function(result){
					jQuery('#dialog').html(result);
					jQuery('.wproLoadMessageHolder').css('display','none');
				});
	*/
	if (typeof(WPro) != 'undefined'){
		WPro.deleteEditor('myEditorCode');
	}
	
	$('#dialog').load("editor_bloc.php", "bloc_id="+bloc);
	/*alert(WPro);*/
	
	/*jQuery('#dialog').html('<iframe name="frame_editor" id="frame_editor" src="editor_bloc.php?bloc_id='+bloc+'" width="810" height="520" frameborder="0" align="middle"></iframe>');*/
	
}

function editBlocYoutube(bloc){

	var content=jQuery('#conteneur_bloc'+bloc).html();	
	var tab= new Array('');
	var url='';
	if(content.indexOf('<div name="embed_vid"',0)!=-1){
		tab=content.split('<div name="embed_vid"');
		tmp=tab[1].split('<div name="url_vid" style="display: none;">');
		url=tmp[1].replace('</div>','');
	}
	//url=tmp[1].replace('</div>','');
	//jQuery('#dialogYoutube').attr('rel',bloc);
	//jQuery('#editorYoutube').html('<iframe id="frame_editor" src="editor_bloc.php?bloc_id='+bloc+'" width="800" height="500" border="0"></iframe>');
	//jQuery('#urlYoutube').val(url[1].replace('</div>',''));
	
	var elem='';
	elem+="\n"+'<div id="dialogYoutube" rel="'+bloc+'" title="Edition d\'un bloc Youtube">';
	elem+="\n"+'	<div style="text-align:center;">';
	elem+="\n"+'		<div id="editorYoutube"><iframe id="frame_editor" src="editor_bloc.php?bloc_id='+bloc+'" width="815" height="515" style="border:none;"></iframe></div>';
	elem+="\n"+'		<br/>URL Youtube : <input size="60" type="text" name="urlYoutube" id="urlYoutube" value="'+url+'" />';
	elem+="\n"+'	</div>';
	elem+="\n"+'</div>';
	
	//création de la popup
	jQuery('body').append(elem);
	
	// DialogYoutube			
	jQuery('#dialogYoutube').dialog({
		autoOpen: true,
		modal:true,
		width: 900,
		buttons: {
			"Ok": function() { 

				var url = jQuery('#urlYoutube').val();
		
				if(url.indexOf('youtube')==-1){
					alert("Vous devez saisir l'URL d'une vidéo youtube");
				}else{				
					document.all.frame_editor.contentWindow.wp_send_to_html(document.all.frame_editor.contentWindow.myEditorCode);
					var content=encodeChar(jQuery('#frame_editor').contents().find('#myEditorCode').val());
					content+='<div name="embed_vid" style="text-align:center;"><p><embed width="336" height="204" type="application/x-shockwave-flash" allowfullscreen="true" src="';
	
					var param = url.split("?");
					var video='';
					
					first = param[1].split("&");						
				    for(i=0;i<first.length;i++){
				        second = first[i].split("=");
				        if(second[0]=='v') video=second[1];
				    }
					
					content+='http://www.youtube.com/v/'+video+'&feature=player_embedded&version=3';
					content+='" pluginspage="http://www.adobe.com/go/getflashplayer" flashvars=""/></p></div><div name="url_vid" style="display: none;">'+url+'</div>';
					$.post('../../inclusion/bloc_aj.php',
							{
								action:'contenu',
								bloc_id:jQuery('#dialogYoutube').attr('rel'),
								bloc_contenu:content,
								//bloc_animation:jQuery('#effectTypesYoutube option:selected').val()
								bloc_animation:'none'
							},
							function(){});
					jQuery('#conteneur_bloc'+jQuery('#dialogYoutube').attr('rel')).html(content);
					jQuery(this).dialog("close"); 
					jQuery('#dialogYoutube').remove();
				}
			}, 
			"Cancel": function() { 
				jQuery(this).dialog("close");
				jQuery('#dialogYoutube').remove();
			} 
		}
	});
	//jQuery('#dialogYoutube').dialog('open');
}

function supprBloc(bloc){
	$.post('../../inclusion/bloc_aj.php',{action:'del',bloc_id:bloc},function(result){/*alert(result);*/});
	jQuery( "#draggable"+bloc).remove();
}

function dimBloc(bloc,type){
	
	if(type=='left'){
		jQuery('#draggable'+bloc).css('width',(jQuery('#draggable'+bloc).width()-10));
	}
	if(type=='right'){
		jQuery('#draggable'+bloc).css('width',(jQuery('#draggable'+bloc).width()+10));
	}
	if(type=='top'){
		jQuery('#draggable'+bloc).css('height',(jQuery('#draggable'+bloc).height()-10));
	}
	if(type=='bottom'){
		jQuery('#draggable'+bloc).css('height',(jQuery('#draggable'+bloc).height()+10));
	}
	
	redimention(jQuery('#draggable'+bloc),false);
	
	var L=(jQuery('#draggable'+bloc).position().left+parseInt(jQuery('#draggable'+bloc).css('margin-left').replace('px','')));			
	var T=(jQuery('#draggable'+bloc).position().top+parseInt(jQuery('#draggable'+bloc).css('margin-top').replace('px','')));
	var w =jQuery('#draggable'+bloc).width();
	var w1=jQuery('#draggable'+bloc).find('.blocTopLeft').width();
	var w2=jQuery('#draggable'+bloc).find('.blocTopRight').width();
	var h =jQuery('#draggable'+bloc).height();
	var h1=jQuery('#draggable'+bloc).find('.blocTopLeft').height();
	var h2=jQuery('#draggable'+bloc).find('.blocBottomLeft').height();
	$.post('../../inclusion/bloc_aj.php',
			{
				action:'modif',
				bloc_id:jQuery('#draggable'+bloc).attr('rel'),
				bloc_width:w+'|'+(w-(w1+w2)),
				bloc_height:h+'|'+(h-(h1+h2)),
				bloc_left:L,
				bloc_top:T
			},
			function(result){/*alert(result);*/});	
}

function floatBlocEdition(elem,noreloadEntete){
	
	jQuery('.selectedBloc').css('border','');
	jQuery('.selectedBloc').removeClass('selectedBloc');
	jQuery(elem).addClass('selectedBloc');
	jQuery(elem).css('border','solid 1px red');
	
	isOver=jQuery(elem).find('.blocCenter').css('overflow');
	if(isOver=='hidden')
		chkOver='checked="checked" ';
	else
		chkOver='';
	
	id=jQuery(elem).attr('rel');
	drag='draggable'+id;
	cont='<div style="font-size:10px;width:108px;margin:5px 0 0 6px;background:url(../images/stickySel.gif) no-repeat;text-align:center;padding-top:5px;">';
	cont+='<b style="color:#FFF;font-size:12px;">Bloc'+id+'</b>';
	cont+='<br><img src="../images/stickyEdit.gif" alt="edit" onclick="editBlocText('+id+');" />&nbsp;&nbsp;';
	if(jQuery('.btnSupprBloc',jQuery(elem)).length){
		cont+='<img src="../images/stickySupp.gif" alt="supp" onclick="supprBloc('+id+');jQuery(\'#stickyScrollBloc\').html(\'\');" />';
	}
	cont+='<br>Position : <br>'+getPosition(jQuery(elem),'L')+' x '+getPosition(jQuery(elem),'T');
	cont+='<br>Dim total : <br>'+jQuery(elem).width()+' x '+jQuery(elem).height();
	cont+='<br>Dimen éditable : <br>'+jQuery(elem).find('.blocCenter').width()+' x '+jQuery(elem).find('.blocCenter').height();
	cont+='</div>';
	cont+='<div style="font-size:10px;width:108px;margin:-5px 0 0 6px;background:url(../images/stickySel.gif) no-repeat bottom;text-align:center;padding-bottom:3px;">';
	cont+='Supp scroll : <input type="checkbox" name="blockOverflow" '+chkOver+' value="O" onclick="if(this.checked){jQuery(\'#'+drag+'\').find(\'.blocCenter\').css(\'overflow\',\'hidden\');overflowBloc('+id+',\'hidden\');}else{jQuery(\'#'+drag+'\').find(\'.blocCenter\').css(\'overflow\',\'auto\');overflowBloc('+id+',\'auto\');}"/>';
	cont+='<div id="stickyEntete">'+jQuery('#stickyEntete').html()+'</div>';
	cont+='</div>';
	cont+='</div>';
	jQuery('#stickyScrollBloc').html(cont);
	
	if(noreloadEntete!=true){
	$.post('../../inclusion/bloc_aj.php',
			{
				action:'getEntete'
			},
			function(result){
				jQuery("#stickyEntete").html(result);
				id=jQuery(elem).attr('rel');
				drag='draggable'+id;
				var allClass=jQuery('#'+drag).find('.blocTop').attr('class');
				if(typeof(allClass)!='undefined'){
					var tabClass=allClass.split(' ');
					//alert(tabClass.length);
					if(tabClass.length>2)jQuery('select[name=selBlocModel]').val(tabClass[(tabClass.length-1)]);
				}
				jQuery('select[name=selBlocModel]').change(function(){changeClassBloc(id,$(this).val(),drag);});

			}
	);
	}

}
function overflowBloc(bloc,over){
	$.post('../../inclusion/bloc_aj.php',{action:'overflow',bloc_id:bloc,bloc_overflow:over},function(result){/*alert(result);*/});
}

function titleBloc(bloc,title){
	//alert(bloc+' <-----> '+title);
	$.post('../../inclusion/bloc_aj.php',{action:'title',bloc_id:bloc,bloc_title:title},function(result){/*alert(result);*/});
}

function changeClassBloc(bloc,newClass,drag){
	//alert(bloc+' -- '+newClass+' -- '+drag);
	$.post(
			'../../inclusion/bloc_aj.php',
			{action:'model',bloc_id:bloc,bloc_model:newClass},
			function(res){/*alert(res);*/}
	);
	var allClass=jQuery('#'+drag).find('.blocTop').attr('class');
	if(typeof(allClass)!='undefined'){
		var tabClass=allClass.split(' ');
		//alert(tabClass.length);
		if(tabClass.length>2)jQuery('#'+drag).find('.blocTop,.blocTopLeft,.blocTopRight,.blocLeft,.blocCenter,.blocRight,.blocBottomLeft,.blocBottom,.blocBottomRight').removeClass(tabClass[(tabClass.length-1)]);
	}
	jQuery('#'+drag).find('.blocTop,.blocTopLeft,.blocTopRight,.blocLeft,.blocCenter,.blocRight,.blocBottomLeft,.blocBottom,.blocBottomRight').addClass(newClass);
	redimention(jQuery('#'+drag));
	//alert(jQuery('#'+bloc).find('.blocTop').attr('class'));
}

function saveModif(argThis){
	var L=getPosition(argThis,'L');
	var T=getPosition(argThis,'T');
	var w =jQuery(argThis).width();
	var w1=jQuery(argThis).find('.blocTopLeft').width();
	var w2=jQuery(argThis).find('.blocTopRight').width();
	var h =jQuery(argThis).height();
	var h1=jQuery(argThis).find('.blocTopLeft').height();
	var h2=jQuery(argThis).find('.blocBottomLeft').height();
	//alert(w1+' - '+w2+' - '+jQuery(elem).width());
	$.post('../../inclusion/bloc_aj.php',
			{
				action:'modif',
				bloc_id:jQuery(argThis).attr('rel'),
				bloc_width:w+'|'+(w-(w1+w2)),
				bloc_height:h+'|'+(h-(h1+h2)),
				bloc_left:L,
				bloc_top:T
			},
			function(result){/*alert(result);*/});
}

function getPosition(argThis,pos){
	if(pos=='L'){
		return Math.round((jQuery(argThis).position().left+parseInt(jQuery(argThis).css('margin-left').replace('px','')))); //left
	}else{
		return Math.round((jQuery(argThis).position().top+parseInt(jQuery(argThis).css('margin-top').replace('px','')))); //top
	}
}

function unlock(idPage){
	$.ajax({
		type: 'POST',
		url: '../../inclusion/bloc_aj.php',
		data: {action:'annuleBloc',page:idPage},
		success: function(result){},
		async:false
	});

}
