/*
  Variables Globales
*/

//liste des fichiers js a inclure
var JS_incModules	= {
	'jqeasing'		: {browser: 'all', onload: false, file: 'jquery/jquery.easing.1.3.js', method: false, comment: ''},
	'floatmsg'		: {browser: 'all', onload: false, file: 'jquery/jquery.blockUI.js', method: false, comment: ''},
	'dropdown'		: {browser: 'all', onload: false, file: 'jquery/customdropdown/2.1/msdropdown/js/jquery.dd.js', method: false, comment: ''},
	//'fancybox'		: {browser: 'all', onload: false, file: 'jquery/fancybox/1.2.6/fancybox/jquery.fancybox-1.2.6.pack.js', method: false, comment: ''},
	'fancybox'		: {browser: 'all', onload: false, file: 'jquery/fancybox/1.3.0/fancybox/jquery.fancybox-1.3.0.pack.js', method: false, comment: ''},
	'mousewheel'	: {browser: 'all', onload: false, file: 'jquery/fancybox/1.3.0/fancybox/jquery.mousewheel-3.0.2.pack.js', method: false, comment: ''},
	'formvalid'		: {browser: 'all', onload: false, file: 'jquery/formvalidation/1.6/js/jquery.validationEngine.js', method: false, comment: ''},
	'pagination'	: {browser: 'all', onload: false, file: 'jquery/pagination/1.2/lib/jquery_pagination/jquery.pagination.custom.js', method: false, comment: ''}
};

//liste des fichiers css a inclure
var CSS_incModules	= {
	'dropdown'		: {file: 'jquery/customdropdown/2.1/msdropdown/dd.css'},
	//'fancybox'		: {file: 'jquery/fancybox/1.2.6/fancybox/jquery.fancybox-1.2.6.css'},
	'fancybox'		: {file: 'jquery/fancybox/1.3.0/fancybox/jquery.fancybox-1.3.0.css'},
	'formvalid'		: {file: 'jquery/formvalidation/1.6/css/validationEngine.jquery.css'},
	'pagination'	: {file: 'jquery/pagination/1.2/lib/jquery_pagination/pagination_custom.css'}
};

/*
  Init Functions
*/

//permet de preloader les images d'un div entier
jQuery.fn.onImagesLoaded = function(_cb) {
  return this.each(function() {

    var $imgs = (this.tagName.toLowerCase()==='img')?$(this):$('img',this),
        _cont = this,
            i = 0,
    _done=function() {
      if( typeof _cb === 'function' ) _cb(_cont);
    };

    if( $imgs.length ) {
      $imgs.each(function() {
        var _img = this,
        _checki=function(e) {
          if((_img.complete) || (_img.readyState=='complete'&&e.type=='readystatechange') )
          {
            if( ++i===$imgs.length ) _done();
          }
          else if( _img.readyState === undefined ) // dont for IE
          {
            $(_img).attr('src',$(_img).attr('src')); // re-fire load event
          }
        }; // _checki \\

        $(_img).bind('load readystatechange', function(e){_checki(e);});
        _checki({type:'readystatechange'}); // bind to 'load' event...
      });
    } else _done();
  });
};

$(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);
		}
	});
});

/*
	Tools Functions
*/

//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);
			}
		}
	}
}

function _USR_Logout () {
	$.fancybox.showActivity();
	$.get(BasePath + "/_content/user/_requests.php", {mode: "LOGOUT"}, function(result){
		location.href	= '?code=LOGIN';
	});
}

function _FB_Connected_ () {
	$.fancybox(
		'<span style="color: #76261B; font: bold 12px Trebuchet MS, Arial, Tahoma;">Information !</span><p style="font-weight: bold;">Vous devez vous connectez pour utiliser cette fonction.</p>',
		{
			'autoDimensions'	: false,
			'width'        		: 350,
			'height'       		: 'auto',
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'easingIn'      	: 'easeOutBack',
			'easingOut'     	: 'easeInBack'
		}
	);
}

//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">Chargement...</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'}
	});
}

