var oDialogAdmin=null;
var iDialogWidth=800;
var strFileEditorUrl=false;
//var	$tabScriptsToLoad = new Array('vendors/jscripts/jqueryplugins/jquery.hotkeys.js');
var	$tabScriptsToLoadDialog = new Array('vendors/jscripts/jqueryplugins/jquery.dimensions.js',
		'vendors/jscripts/jqueryui/jquery.ui-all-1.5b.min.js',
		'core/jscripts/ui.dialog.extra.js',
		'vendors/jscripts/jqueryplugins/jquery.metadata.js'
	);
//chargement de la page, on ne charge que les hotkeys
$(function(){
	//loadScript(0, $tabScriptsToLoad, initHotkeys);
	initHotkeys();
});

function loadScript(iIndex, tab, callBack){
	//fin du chargement on initialise le dialogue
	if(iIndex == tab.length) {callBack.apply(); return;}
	var iNext = iIndex+1;
	$.ajax({
		type: "GET",
		url: SITE_URL+tab[iIndex],
		data: null,
		success: function(){loadScript(iNext, tab, callBack);},
		dataType: 'script',
		cache:true
	});
}

function initHotkeys (){
	//ADMIN HOT KEYS ACTIONS
	$.hotkeys.add('Ctrl+a', function(){
		if(oDialogAdmin == null){
			loadScript(0,$tabScriptsToLoadDialog, initDialog);return;
		}
		if(oDialogAdmin.parents(".ui-dialog").is(':visible')){
			oDialogAdmin.dialog('close');
		}
		else {
			oDialogAdmin.dialog('openAdmin');
		}
	});
	$.hotkeys.add('Ctrl+j', function(){
		if(oDialogAdmin != null){oDialogAdmin.dialog('fullscreen');}
	});
	$.hotkeys.add('Ctrl+e', function(){
		if(strFileEditorUrl) {
			if(bConnected == true ){
				if(oDialogAdmin==null){loadScript(0,$tabScriptsToLoadDialog, function(){initDialog(strFileEditorUrl);});return;}
				else {
					var oIFrame = $('iframe',oDialogAdmin);
					if(oIFrame.attr('src') != strFileEditorUrl){oIFrame.attr('src', strFileEditorUrl)};
					!oDialogAdmin.parents('.ui-dialog').is(':visible') && oDialogAdmin.dialog('openAdmin');
				}
			}
			else {
				if(confirm('Vous devez vous connecter pour éditer cette page!\nVoulez vous vous connecter maintenant ?')){
					if(oDialogAdmin==null){loadScript(0,$tabScriptsToLoadDialog, function(){initDialog();});return;}
					else { oDialogAdmin.dialog('open'); }
				}
			}
		}
		else{
			alert("Cette page n'est pas éditable.");
		}
	});
}

function initDialog(strFileEditorUrl){
	//Iframe that contains the admin page
	var oIFrame = $('<iframe frameborder="0" hspace="0"></iframe>')
		.attr({'src':SITE_URL+'core/admin/admin.php', 'frameborder':'0', 'hspace':'0'})
		.css({width:'100%','border':'0px'});

	//The adming window object
	oDialogAdmin=$('<div>')
	.addClass('flora')
	.css({'overflow':'auto','background':'#F5F5F5'})
	.append(oIFrame)
	.dialog({title:"Panneau d'Administration",width: iDialogWidth,minWidth: iDialogWidth, position:'top', height: 60,resizable:false,modal:false,dragable:true,autoOpen:true,bgiframe:false})
	.dialog('close')
	.bind('dialogclose',function(){
	})
	.bind('dialogopen',function(){
		//a cause d'un bug dans ui dialog qui fait un appendTo a chaque ouverture on doit
		//passer par une autre fonction
	});

	oDialogAdmin.parents(".ui-dialog:first")
		.find(".ui-dialog-titlebar-close")
		.html('');//remove the X in the title bar;

	oDialogAdmin.dialog('openAdmin');

	if(strFileEditorUrl) {
		var oIFrame = $('iframe',oDialogAdmin);
		if(oIFrame.attr('src') != strFileEditorUrl){oIFrame.attr('src', strFileEditorUrl)};
	}


}

function max(i,j){
	return (i<j)?j:i;
}

/*
function popupoldResize(){
	var objIframe=oDialogAdmin.find('iframe');
	var frameHeight = objIframe.contents().find("body").outerHeight({ margin: true });
	var uiDialog = oDialogAdmin.parents(".ui-dialog:first");
	objIframe.css({height:(frameHeight+40)});
	uiDialog.animate({height:(frameHeight+80)}, 200,null,function(){
		//due to ie6 bug reload the window size after the effect
		setTimeout("debugWindowResize()",200);
		oDialogAdmin.dialog("title","Panneau d'Administration");
	});


}

function debugWindowResize(){
	var objIframe=oDialogAdmin.find('iframe');
	var frameHeight = objIframe.contents().find("body").outerHeight({ margin: true });
	var uiDialog = oDialogAdmin.parents(".ui-dialog:first");

	objIframe.css({height:(frameHeight+40)});
	uiDialog.css({height:(frameHeight+80)});
	oDialogAdmin.dialog("title","Panneau d'Administration");
}
*/

