/*
  Variables Globales
*/

var MENU_CurrActivePrimary		= 'home';
var MENU_CurrActiveSecondary	= false;

//liste des fichiers js a inclure
var JS_incModules	= {
	'dropdown'		: {browser: 'all', onload: false, file: 'js/jquery/forms/customdropdown/2.1/msdropdown/js/jquery.dd.js', method: false, comment: ''},
	'cycle'			: {browser: 'all', onload: false, file: 'js/jquery/galleries/cycle/2.88/jquery.cycle.all.pack.js', method: false, comment: ''},
	'formvalid'		: {browser: 'all', onload: false, file: 'js/jquery/forms/formvalidation/1.6.5/js/jquery.validationEngine.js', method: false, comment: ''},
	'formvalidFR'	: {browser: 'all', onload: false, file: 'js/jquery/forms/formvalidation/1.6.5/js/jquery.validationEngine-fr.js', method: false, comment: ''},
	'pagination'	: {browser: 'all', onload: false, file: 'js/jquery/tools/pagination/1.2/lib/jquery_pagination/jquery.pagination.custom.js', method: false, comment: ''},
	'fancybox'		: {browser: 'all', onload: false, file: 'js/jquery/modal/fancybox/1.3.0/fancybox/jquery.fancybox-1.3.0.pack.js', method: false, comment: ''}
};

//liste des fichiers css a inclure
var CSS_incModules	= {
	'dropdown'		: {file: 'js/jquery/forms/customdropdown/2.1/msdropdown/dd.css'},
	'formvalid'		: {file: 'js/jquery/forms/formvalidation/1.6.5/css/sgr_validationEngine.jquery.css'},
	'pagination'	: {file: 'js/jquery/tools/pagination/1.2/lib/jquery_pagination/pagination_custom.css'},
	'fancybox'		: {file: 'js/jquery/modal/fancybox/1.3.0/fancybox/jquery.fancybox-1.3.0.min.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);
		}
	});

	//gestion du menu principal up
	$('.menu').hover (
		function () {
			var CurrId	= $(this).attr('id');
			if (CurrId) {
				//ouvrir le menu secondaire
				if ($(this).attr('class') != 'menu active') $('#' + CurrId).css('border-bottom', 'solid 4px #F4877E');
				$('#' + CurrId + '-secondary').css('display', 'block');
				$(this).css('cursor', 'pointer');
			}
		},
		function () {
			var CurrId	= $(this).attr('id');
			if (CurrId) {
				if ($(this).attr('class') != 'menu active') $('#' + CurrId).css('border-bottom', 'none');
				$('#' + CurrId + '-secondary').css('display', 'none');
				$(this).css('cursor', 'normal');
			}
		}
	);
	$('.submenu').hover (
		function () {
			$(this).css('background', '#F4877E');
			$(this).css('cursor', 'pointer');
		},
		function () {
			$(this).css('background', '#fff');
			$(this).css('cursor', 'normal');
		}
	);
	$('.dock').hover (
		function () {$(this).css('color', '#F4877E'); $(this).css('cursor', 'pointer');},
		function () {$(this).css('color', '#0782ac'); $(this).css('cursor', 'normal');}
	);
	$('.dock').click (function () {location. href = '?code=' + $(this).attr('id');});

});

/*
 * Users functions
 */

function _USR_Logout () {
	$.fancybox.showActivity();
	$.get(BasePath + "/_security/_requests.php", {mode: "LOGOUT"}, function(result){
		location.href	= '?code=LOGIN';
	});
}

/*
	Tools Functions
*/

//inclusion des js files
function _ToolsLoadOnDemandJsFile () {
	var argsRef = _ToolsLoadOnDemandJsFile.arguments;
	for (var i = 0 ; i < argsRef.length ; i++) {
		if (CSS_incModules[argsRef[i]]) {
			file	= CSS_incModules[argsRef[i]].file;
			if (file) {
				$.include('_libs/' + file);
			}
		}
		if (JS_incModules[argsRef[i]]) {
			file	= JS_incModules[argsRef[i]].file;
			method	= JS_incModules[argsRef[i]].method;
			if (file) {
				$.include('_libs/' + file, (method ? eval(method) : null));
			}
		}
	}
}

//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('_libs/' + file);
			}
		}
	}
}

/*
	FancyBox functions
*/

function _FB_Conditions_ () {
	$.fancybox({
		'width'				: 640,
		'height'			: 480,
		'autoScale'     	: false,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'easingIn'      	: 'easeOutBack',
		'easingOut'     	: 'easeInBack',
		'href'				: BasePath + '/_security/readconditions.php',
		'type'				: 'iframe'
	});
}

function _FB_DlTenshin_ (id) {
	$.fancybox({
		'width'				: 600,
		'height'			: 500,
		'autoScale'     	: false,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'easingIn'      	: 'easeOutBack',
		'easingOut'     	: 'easeInBack',
		'href'				: BasePath + '/_content/telecharger.php?type=TENSHIN&id=' + id,
		'type'				: 'iframe'
	});
}

function _FB_DlMusik_ (id_gal, id_elm) {
	$.fancybox({
		'width'				: 600,
		'height'			: 500,
		'autoScale'     	: false,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'easingIn'      	: 'easeOutBack',
		'easingOut'     	: 'easeInBack',
		'href'				: BasePath + '/_content/telecharger.php?type=MUSIKS&id_gal=' + id_gal + '&id_elm=' + id_elm,
		'type'				: 'iframe'
	});
}

function _FB_formatTitle (title, currentArray, currentIndex, currentOpts) {
	return '<div style="background: #333333; height: 25px;"><div style="float: left; width: 25px;"><img src="_medias/pictures/modal/logo_sg.gif" width="25" height="25" /></div><div style="float: left; padding: 5px 0 0 5px; font-weight: bold; font-variant: small-caps;">' + title + '</div><div style="float: right; padding: 6px 5px 0 0; color: #ccc; font-size: 10px;">' + _CurrDateTime_ + '</div></div>';
}

function _FB_Closed_ () {
	$.fancybox(
		'<span style="color: #0782ac; font: bold 12px Trebuchet MS, Arial, Tahoma;">Information !</span><p style="font-weight: bold;">Cette section est pour le moment ferm&#233;e.<br/>Veuillez nous escuser pour la g&#232;ne occasionn&#233;e.</p>',
		{
			'autoDimensions'	: false,
			'width'        		: 350,
			'height'       		: 'auto',
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'easingIn'      	: 'easeOutBack',
			'easingOut'     	: 'easeInBack'
		}
	);
}

function _FB_Unauthorized_ () {
	$.fancybox(
		'<span style="color: #0782ac; font: bold 12px Trebuchet MS, Arial, Tahoma;">Information !</span><p style="font-weight: bold;">Vous n\'&#234;tes pas autoris&eacute; &#224; acc&eacute;der &#224; cette section.<br/>Veuillez vous identifiez ou vous enregistrez ou en faire la demande.</p>',
		{
			'autoDimensions'	: false,
			'width'        		: 350,
			'height'       		: 'auto',
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'easingIn'      	: 'easeOutBack',
			'easingOut'     	: 'easeInBack'
		}
	);
}

function _FB_Inactive_ () {
	$.fancybox(
		'<span style="color: #0782ac; font: bold 12px Trebuchet MS, Arial, Tahoma;">Information !</span><p style="font-weight: bold;">Cette fonction est pour le moment inactive.<br/>Veuillez nous escuser pour la g&#232;ne occasionn&#233;e.</p>',
		{
			'autoDimensions'	: false,
			'width'        		: 350,
			'height'       		: 'auto',
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'easingIn'      	: 'easeOutBack',
			'easingOut'     	: 'easeInBack'
		}
	);
}

function _FB_Connected_ () {
	$.fancybox(
		'<span style="color: #0782ac; 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'
		}
	);
}

function _FB_Data_ () {
	$.fancybox(
		'<span style="color: #0782ac; font: bold 12px Trebuchet MS, Arial, Tahoma;">Information !</span><p style="font-weight: bold;">Vous devez avant tout mettre &#224; jour votre profil pour acc&eacute;der &#224; cette section. Utilisez le formulaire ci-joint.</p>',
		{
			'autoDimensions'	: false,
			'width'        		: 350,
			'height'       		: 'auto',
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'easingIn'      	: 'easeOutBack',
			'easingOut'     	: 'easeInBack'
		}
	);
}

/*
	Blocks functions
*/

//expand/collpase right side block
function _BLK_expandThisRightBlock (imgObj){
	if(imgObj.tagName=="DIV"){
		imgObj = imgObj.getElementsByTagName("IMG")[1];
	}
	if(imgObj.src.indexOf("expand")!=-1){
		imgObj.src = imgObj.src.replace("expand","collapse");
		imgObj.alt = "Collapse Block";
		imgObj.parentNode.parentNode.className = "block_head_clear";
		_BLK_hideContentNextTo(imgObj.parentNode.parentNode,false)
	}else{
		imgObj.src = imgObj.src.replace("collapse","expand");
		imgObj.alt = "Expand Block";
		imgObj.parentNode.parentNode.className = "block_head_clear_c";
		_BLK_hideContentNextTo(imgObj.parentNode.parentNode,true)
	}
}
function _BLK_hideContentNextTo(divObj,hideFl){
	if(hideFl)
		var displayVal = "none"
	else
		var displayVal = ""
	var chNodes = divObj.parentNode.childNodes;
	for(var i=0;i<chNodes.length;i++){
		if(chNodes[i]!=divObj && chNodes[i].tagName)
			chNodes[i].style.display = displayVal;
	}
}
