// JavaScript GLOBAL class
var Global = {
	Init : function(){
		var H = $(window).height();
		var W = $(window).width();
		$('#workspace').css({
			'height': (H - $('#header').height() - $('#footer').height() ) + 'px'
		});
		$('#footer').css({
			'top': ($('#workspace').height() + $('#header').height()) + 'px'
		});
	},
	Wait : function(IdDiv, url_img){
		var Retour = '';
		!url_img ? url_img=URL_THEME_DIR : false;
		Retour += '<table style="width:100%; min-height:100%; height:100%"><tr><td align="center" valign="middle" class="center noborder">';
		Retour += '<img src="'+url_img+'ajax-loader.gif" alt="Wait" style="height:16px; width:auto" /><br />';
		Retour += '</td></tr></table>';
		$('#'+IdDiv).html(Retour);
		return false;
	},
	//--------------------------------------------------------------//
	//	Teste l'existence d'un fichier								//
	//--------------------------------------------------------------//
	file_exists : function(url) {
		var req = this.window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
		if (!req) {
			throw new Error('XMLHttpRequest not supported');
		}
		req.open('HEAD', url, false);
		req.send(null);
		if (req.status == 200) {
			return true;
		}
		return false;
	},
	//--------------------------------------------------------------//
	//	Traitement des tableaux										//
	//--------------------------------------------------------------//
	array_search : function(needle, haystack, argStrict) {
		var key = '';
		for (key in haystack) {
			if ((argStrict && haystack[key] === needle) || (!argStrict && haystack[key] == needle)) {
				return key;
			}
		}
	    return false;
	},
	array_reverse : function(array, preserve_keys) {
		var arr_len = array.length,
			newkey = 0,
			tmp_arr = new Array,
			key = '';
		preserve_keys = !! preserve_keys;
		for (key in array) {
			newkey = arr_len - key - 1;
			tmp_arr[preserve_keys ? key : newkey] = array[key];
		}
		return tmp_arr;
	},
	explode : function(delimiter, string, limit) {
		var emptyArray = {
			0: ''
		};
		if (arguments.length < 2 || typeof arguments[0] == 'undefined' || typeof arguments[1] == 'undefined') return null;
	
		if (delimiter === '' || delimiter === false || delimiter === null) return false;
	
		if (typeof delimiter == 'function' || typeof delimiter == 'object' || typeof string == 'function' || typeof string == 'object') return emptyArray;
	
		if (delimiter === true) delimiter = '1';
	
		if (!limit) {
			return string.toString().split(delimiter.toString());
		} else {
			var splitted = string.toString().split(delimiter.toString());
			var partA = splitted.splice(0, limit - 1);
			var partB = splitted.join(delimiter.toString());
			partA.push(partB);
			return partA;
		}
	},
	//--------------------------------------------------------------//
	//	Zoomm images												//
	//  Utilisation : 												//
	//		Ajouter la classe "zoomable" aux images					//
	//--------------------------------------------------------------//
	makeZoomable : function(){
		var Src, Alt, Style, newElt;
		$('img.zoomable').each( function(){
			Src 	= $(this).attr('src');
			Alt 	= $(this).attr('alt');
			Style 	= $(this).attr('style');
			Id 		= $(this).attr('id');
			Title	= $(this).attr('title');
			newElt 	= '<a href="'+Src+'" class="fancybox" rel="groupImg"><img src="'+Src+'" alt="'+Alt+'" style="'+Style+'" id="'+Id+'" title="'+Title+'" /></a>';
			$(this).parent().append(newElt);
			$(this).remove();
		});
		if ( $('a.fancybox').size()!=0 ){
			window.setTimeout( function(){ 
				$('a.fancybox').fancybox();
			}, 500);
		}
	},
	//--------------------------------------------------------------//
	//	Scroll														//
	//--------------------------------------------------------------//
	makeScrollable : function(Div){
		!Div ? Div='workspace' : false;
		if ( $('div.overview').size()==0 ){
			var Content = $('#'+Div).html();
			var newContent = '<div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div>'
			newContent += 	 '<div class="viewport"><div class="overview">' + Content + '</div></div>';
			$('#'+Div).html(newContent).delay(500).tinyscrollbar({ sizethumb: 50 });
		} else {
			var Content = $('div.overview').html();
			var newContent = '<div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div>'
			newContent += 	 '<div class="viewport"><div class="overview">' + Content + '</div></div>';
			$('#'+Div).html(newContent).delay(500).tinyscrollbar({ sizethumb: 50 });
		}
	},
	//--------------------------------------------------------------//
	//	Chargement d'une page interne								//
	//	Appel :														//
	//		Id : Identifiant de l'élément							//
	//			ou "page_"+Id ou "product_"+Id ou "bundle_"+Id		//
	//		IdLang : Identifiant de la langue à afficher			//
	//--------------------------------------------------------------//
	openPage : function(Id, IdLang){
		var Page = 'page.php';
		if ( typeof(Id)=='string' && Id.indexOf('_')!=-1 ){
			var Ligne = Id.split('_');
			Id	 = Ligne[1];
			switch(Ligne[0]){
				case 'page':
					Page = 'page.php';
					break;
				case 'product':
					Page = 'modules/catalog/product.php';
					break;
				case 'bundle':
					Page = 'modules/catalog/bundle.php';
					break;
				case 'module':
					Page = 'modules/'+Ligne[1]+'/home.php';
					break;
			}
		}
		$.ajax({
			type: 'post',
			url: Page,
			cache: false,
			data: {
				id: Id,
				id_lang: IdLang
			},
			beforeSend: Global.Wait('workspace'),
			success: function(msg){
				$('#workspace').html(msg);
			}
		});
		return false;
	},
	//--------------------------------------------------------------//
	//	Onglets dans une page										//
	//--------------------------------------------------------------//
	selectOnglet : function(IdPageElt){
		$('.onglet_button').removeClass('selected');
		$('#onglet_button_'+IdPageElt).addClass('selected');
		$('.onglet').hide();
		$('#onglet_'+IdPageElt).show().jScrollPane();
		return false;
	},
	//--------------------------------------------------------------//
	//	Chargement d'une fonction d'un module						//
	//--------------------------------------------------------------//
	openModule : function(ModuleFolder, Action, Datas){
		$.ajax({
			type: 'post',
			url: 'modules/' + ModuleFolder + '/module.php',
			cache: false,
			data: {
				action: Action,
				datas: Datas
			},
			beforeSend: Global.Wait('workspace'),
			success: function(msg){
				$('#workspace').html(msg);
			}
		});
		return false;
	},
	//--------------------------------------------------------------//
	//	Gestion des Cookies											//
	//--------------------------------------------------------------//
	setCookie : function(name,value,days){
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},
	getCookie : function(name){
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	//----------------------------------------------------------------------//
	//	VERIFICATION D'UN FORMULAIRE										//
	//----------------------------------------------------------------------//
	verifForm: function(){
		var Message = new Array;
		$('input.imposed, textarea.imposed').each( function(){
			if ( $(this).val()=='' )
				Message[Message.length] = ' - ' + $(this).attr('rel');
		});
		if ( Message.length!=0 ){
			Message = 'Les champs suivants sont obligatoires :\<br />' + Message.join('<br />');
			messagebox('alert', Message);
			return false;
		}
		return true;
	},
	//----------------------------------------------------------------------//
	//	Envoi D'UN FORMULAIRE												//
	//----------------------------------------------------------------------//
	postForm: function(IdForm){
		var Datas = $('#' + IdForm).serialize();
		$.ajax({
			type: 'post', 
			url: $('#' + IdForm).attr('action'),
			data: { datas: Datas }
		});
		return true;
	}
}

$(document).ready( function(){
	Global.Init();
});
$(window).resize( function(){
	Global.Init();
});
