/*
  Variables Globales
*/

var MSG_RequestExecuted	= 'Votre requ&#234;te a &#233;t&#233; ex&#233;cut&#233;e.';

//liste des fichiers js a inclure
var JS_incModules	= {
	'pngfix'		: {browser: 'ie', onload: false, file: 'js/pngfixall.js', method: 'PngFixImg();', comment: ''},

	'jqui'			: {browser: 'all', onload: false, file: 'jquery/ui-1.7.1/js/jquery-ui-1.7.1.custom.min.js', method: false, comment: ''},
	'jqui-core'		: {browser: 'all', onload: false, file: 'jquery/ui-1.7.1/development-bundle/ui/ui.core.js', method: false, comment: ''},
	'jqui-drag'		: {browser: 'all', onload: false, file: 'jquery/ui-1.7.1/development-bundle/ui/ui.draggable.js', method: false, comment: ''},
	'jqui-drop'		: {browser: 'all', onload: false, file: 'jquery/ui-1.7.1/development-bundle/ui/ui.droppable.js', method: false, comment: ''},

	'jqeasing'		: {browser: 'all', onload: false, file: 'jquery/jquery.easing.1.3.js', method: false, comment: ''},
	'floatmsg'		: {browser: 'all', onload: true, file: 'jquery/blockUI/2.31/jquery.blockUI.js', method: false, comment: ''},
	'requests'		: {browser: 'all', onload: false, file: 'js/_requests.js', method: false, comment: ''},
	'formvalid'		: {browser: 'all', onload: false, file: 'jquery/formvalidation/1.6/js/jquery.validationEngine.js', method: false, comment: ''},
	'countdown'		: {browser: 'all', onload: false, file: 'jquery/jquery.countDown.js', method: false, comment: ''},
	'dropdown'		: {browser: 'all', onload: false, file: 'jquery/customdropdown/2.1/msdropdown/js/jquery.dd.panda-min.js', method: false, comment: ''},
	'dropdown36'	: {browser: 'all', onload: false, file: 'jquery/customdropdown/2.36/msdropdown/js/jquery.dd.js', method: false, comment: ''},
	'smoothmenu'	: {browser: 'all', onload: false, file: 'jquery/smoothmenu/0.2.0/jquery.lavalamp.min.js', method: false, comment: ''},
	'smeasing'		: {browser: 'all', onload: false, file: 'jquery/smoothmenu/0.2.0/jquery.easing.min.js', method: false, comment: ''},
	'fancybox'		: {browser: 'all', onload: false, file: 'jquery/fancybox/1.2.5/fancybox/jquery.fancybox-1.2.5.pack.js', method: false, comment: ''},
	'timepicker'	: {browser: 'all', onload: false, file: 'jquery/timepicker/0.0.1/timepicker.js', method: false, comment: ''},
	'tinymceinit'	: {browser: 'all', onload: false, file: 'jquery/tinymce/3.2.7/tinymce/jscripts/tiny_mce/jquery.tinymce.js', method: false, comment: ''},
	'tinymcescpt'	: {browser: 'all', onload: false, file: 'jquery/tinymce/3.2.7/tinymce/jscripts/tiny_mce/tiny_mce.js', method: false, comment: ''},
	'stickjq'		: {browser: 'all', onload: false, file: 'jquery/stick/1.0/jquery.stickto.js', method: false, comment: ''},
	'stickinit'		: {browser: 'all', onload: false, file: 'jquery/stick/1.0/sticktoInit.js', method: false, comment: ''},
	'overscroll'	: {browser: 'all', onload: false, file: 'jquery/overscroll/1.3.0/jquery.overscroll.min.js', method: false, comment: ''},
	'cycle'			: {browser: 'all', onload: false, file: 'jquery/cycle/2.88/jquery.cycle.all.min.js', method: false, comment: ''},
	'tooltip'		: {browser: 'all', onload: false, file: 'jquery/qtip/1.0.0-rc3/jquery.qtip-1.0.0-rc3.min.js', method: false, comment: ''},
	'pagination'	: {browser: 'all', onload: false, file: 'jquery/pagination/1.2/lib/jquery_pagination/jquery.pagination.panda.js', method: false, comment: ''}
};

//liste des fichiers css a inclure
var CSS_incModules	= {
	'jqui'			: {file: 'jquery/ui-1.7.1/development-bundle/themes/base/ui.all.css'},
	'formvalid'		: {file: 'jquery/formvalidation/1.6/css/validationEngine.jquery.css'},
	'dropdown'		: {file: 'jquery/customdropdown/2.1/msdropdown/dd.panda.css'},
	'dropdown36'	: {file: 'jquery/customdropdown/2.36/msdropdown/dd.css'},
	'smoothmenu'	: {file: 'jquery/smoothmenu/0.2.0/lavalamp_panda.css'},
	'fancybox'		: {file: 'jquery/fancybox/1.2.5/fancybox/jquery.fancybox-1.2.5.css'},
	'stickjq'		: {file: 'jquery/stick/1.0/stickto.css'},
	'pagination'	: {file: 'jquery/pagination/1.2/lib/jquery_pagination/pagination_panda.css'}
};

/*
  Init Functions
*/

$(function($) {
	//inclusion des js par default au load des pages
	$.each(JS_incModules, function (i, val) {
		if (val.onload && val.file) {
			_ToolsLoadOnDemandJsFile (i);
			if (CSS_incModules[i]) _ToolsLoadOnDemandCssFile (i);
		}
	});
	
	/*
	 * initialisation des tooltip
	 */
	//niveau comprehension et utilisation de chaque package
	try {
		$('.mytip-PackageLevelRequired').qtip({
			content: {
			   text: false,
			   title: { text: '<span style="font-variant: small-caps;">Expertise Requise</span>' }
			},
			style		: {name: 'dark'},
			position	: {target: 'mouse', adjust: {mouse: true}}
		});
	} catch (e) {}

});

/*
	Tools Functions
*/

//Pagination
function _Pagination(params) {
	if (params.total > 0 && params.total > params.bypage) {
		if (_PaginationHtml()) {
		var btnPrevious	= (((params.currpage * params.bypage) - params.bypage) > 0 ? true : false);
		var btnNext		= ((params.currpage * params.bypage) < params.total ? true : false);
		if (btnPrevious) {
			$('#PREVIOUS').attr('disabled', false).click(function(){_myChangePage(params.currpage - 1); return false;});
			$('#FIRSTPAGE').attr('disabled', false).click(function(){_myChangePage(1); return false;});
		}
		if (btnNext) {
			$('#NEXT').attr('disabled', false).click(function(){_myChangePage(params.currpage + 1); return false;});
			$('#LASTPAGE').attr('disabled', false).click(function(){_myChangePage(params.totalpage); return false;});
		}
		if (params.totalpage > 0) $('#NBPAGES').html(params.currpage + ' <b>/ ' + params.totalpage + '</b>');
		}
	}
}
function _PaginationHtml() {
	$('#BlockPagination').html(
		'<div style="float: left; margin-right: 2px;"><button disabled id="FIRSTPAGE">&#171;&#171;</button></div>'
		+ '<div style="float: left;"><button disabled id="PREVIOUS">&#171;</button></div>'
		+ '<div id="NBPAGES" style="float: left; padding: 5px;"></div>'
		+ '<div style="float: left;"><button disabled id="NEXT">&#187;</button></div>'
		+ '<div style="float: left; margin-left: 2px;"><button disabled id="LASTPAGE">&#187;&#187;</button></div>'
	);
	return true;
}

//Access & key
function _AccessPackagesByProject(params) {
	$.get(BasePath + '/__key.php', { v: params.prj },
		function(data){
			_ToolsSpecificReload('?page=' + params.page + '&project=' + params.prj);
		}
	);
}

//inclusion des js files
function _ToolsLoadOnDemandJsFile () {
	var argsRef = _ToolsLoadOnDemandJsFile.arguments;
	for (var i = 0 ; i < argsRef.length ; i++) {
		if (JS_incModules[argsRef[i]]) {
			file	= JS_incModules[argsRef[i]].file;
			method	= JS_incModules[argsRef[i]].method;
			if (file) {
				$.include(BasePath + '/_libs/' + file, (method ? eval(method) : null));
			}
		}
		if (CSS_incModules[argsRef[i]]) {
			file	= CSS_incModules[argsRef[i]].file;
			if (file) {
				$.include(BasePath + '/_libs/' + file);
			}
		}
	}
}

//inclusion des css files
function _ToolsLoadOnDemandCssFile () {
	var argsRef = _ToolsLoadOnDemandCssFile.arguments;
	for (var i = 0 ; i < argsRef.length ; i++) {
		if (CSS_incModules[argsRef[i]]) {
			file	= CSS_incModules[argsRef[i]].file;
			if (file) {
				$.include(BasePath + '/_libs/' + file);
			}
		}
	}
}

//init fancybox
function _ToolsInitFancybox () {
	$("a.myfancybox").fancybox({
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 300,
		'zoomOpacity' : true,
		'overlayShow': true,
		'frameWidth': 640,
		'frameHeight': 480,
		'hideOnContentClick': false,
		'hideOnOverlayClick': false
	});
}
//fermeture de fancybox
function _ToolsCloseFancybox () {
	var argsRef = _ToolsCloseFancybox.arguments;
	$('#fancy_close').trigger('click');
	if (argsRef.length > 0) _ToolsSpecificReload (argsRef[0]);
	else _ToolsGlobalReload ();
}

//reload global
function _ToolsGlobalReload () {
	_MsgShowFloatProcessing ();
	location.reload ();
}
//reload specific
function _ToolsSpecificReload (url) {
	_MsgShowFloatProcessing ();
	location.href = url;
}

/*
	Messages Functions
*/

function _MsgShowFloatProcessing () {
	$.blockUI ({
		message				: '<span class="floatmsg">Processing...</span>',
		centerX				: true,
		centerY				: true,
		allowBodyStretch	: true,
		bindEvents			: true,
		css					: {border: 'none', padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: .5, color: '#fff'}
	});
}

function _MsgShowFloatReloading () {
	$.blockUI ({
		message				: '<span class="floatmsg">Reloading...</span>',
		centerX				: true,
		centerY				: true,
		allowBodyStretch	: true,
		bindEvents			: true,
		css					: {border: 'none', padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: .5, color: '#fff'}
	});
}

function _MsgShowFloatProblem () {
	$.blockUI ({
		message				: '<span class="floatmsg">A problem ocurred, the system does not recognize you please try again, sorry for the inconvenience. Or contact the administrator.</span>',
		centerX				: true,
		centerY				: true,
		showOverlay			: true,
		allowBodyStretch	: true,
		bindEvents			: true,
		timeout				: 4000,
		css					: {border: 'none', padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: .5, color: '#fff'}
	});
}

