/*—~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ M A I N.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

var mainClass = {

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ VARIABLES.
	cacheImage: [],
	formNotificationDuration: 2000,
	menuSlideDuration: 150,

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ INITIALIZE.
	initialize: function() {	
		// Ajax.
		$.ajaxSetup({
			url: 'frontHtml.php',
			type: 'POST',
			dataType: 'json',
			async: true,
			cache: false
		});
		// Overlay.
		$('<div>').attr('id','overlayFrame').appendTo($('body'));
		$('#overlayFrame').fadeOut(0);
		// Init.
		this.listTokenRemove();
		this.pageInit();
		this.formInit();
		//this.googleAnalyticsInit();
	},

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DETECT IE7 BROWSER.
	detectIE7: function() {
		this.isIE7 = ($.browser.msie && ($.browser.version.substr(0, 1) == '7')) ? true : false;
	},

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GOOGLE ANALYTICS.
	googleAnalyticsInit: function() {
		var apiHost = (('https:' == document.location.protocol) ? 'https://ssl.' : 'http://www.');
		$.getScript(apiHost + 'google-analytics.com/ga.js', function() {
			var pageTracker = _gat._getTracker('UA-23168392-1');
			pageTracker._initData();
			pageTracker._trackPageview();
		});
	},

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PRELOAD IMAGES.
	preLoadImage: function() {
		var argsCount = arguments.length;
		for (var i=argsCount; i--;) {
			var imgObj = document.createElement('img');
			imgObj.src = arguments[i];
			mainClass.cacheImage.push(imgObj);
		}
	},

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ROLL-OVER IMAGES.
	rolloverImage: function() {
		$('*[data-rollover]').each(function(i) {
			tagObj = ($(this).is('img')) ? $(this) : $(this).find('img');
			tagObj.each(function(j) {
				var imgSrc = $(this).attr('src');
				var imgPath = imgSrc.substring(0, imgSrc.lastIndexOf('.'));
				var imgExtension = imgSrc.substr(imgSrc.lastIndexOf('.'));
				mainClass.preLoadImage(imgPath+imgExtension, imgPath+'-over'+imgExtension);
				$(this).hover(
					function () {
						$(this).attr('src', imgPath+'-over'+imgExtension);
					},
					function () {
						$(this).attr('src', imgPath+imgExtension);
					}
				);
			});
		});
	},

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ LIST TOKEN.
	listTokenRemove: function() {
		$('li:last-child').each(function(i) {
			if ($(this).css('backgroundImage').search(/token/i) != -1)  $(this).add($(this).children('a')).css({backgroundImage:'none'});
			if (($(this).css('float') == 'left' || $(this).css('float') == 'right')) {
				($(this).css('float') == 'right') ? $(this).add($(this).children('a')).css({marginLeft:'0px'}) : $(this).add($(this).children('a')).css({marginRight:'0px'});
			}
		});
		$('li:first-child').each(function(i) {
			if (($(this).css('float') == 'left' || $(this).css('float') == 'right')) {
				if (($(this).css('float') == 'right') && ($(this).css('backgroundImage').search(/token/i) != -1))  $(this).add($(this).children('a')).css({backgroundImage:'none'});
				($(this).css('float') == 'right') ? $(this).add($(this).children('a')).css({marginRight:'0px'}) : $(this).add($(this).children('a')).css({marginLeft:'0px'});
			}
		});
		$('ul[data-cols], ol[data-cols]').each(function(i) {
			$('li:nth-child('+$(this).data('cols')+'n)', $(this)).each(function(i) {
				if (($(this).css('float') == 'left') && ($(this).css('backgroundImage').search(/token/i) != -1))  $(this).add($(this).children('a')).css({backgroundImage:'none'});
				($(this).css('float') == 'right') ? $(this).add($(this).children('a')).css({marginLeft:'0px'}) : $(this).add($(this).children('a')).css({marginRight:'0px'});
				//alert($(this).css('marginRight'));
			});
		});
	},



/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ H E A D E R   M E N U.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MENU — INIT.
	menuInit: function() {
	},



/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ P A G E.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PAGE — INIT.
	pageInit: function() {
		this.linkInit();
		$('#brandsMap area').removeAttr('href');
		//this.pageOffset();
	},

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PAGE — POSITIONNING.
	pageOffset: function() {
	},

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ LINKS.
	linkInit: function() {
		$('a[href^=http]').each(function(i) {
			if (!$(this).attr('href').match($('base').attr('href')))
				$(this).attr('target', '_blank');
		});
	},

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ STYLES.
	styleSwitch: function(styleName) {
		$('link[@rel*=style][title]').each(function(i) {
			this.disabled = true;
			if ($(this).attr('title') == styleName)
				this.disabled = false;
		});
	},



/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ F O R M S.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FORM — INIT.
	formInit: function() {
		// Init labels.
		this.formFieldLabel();
		// `Return` Key Event.
		$('form[id$="Form"]').keypress(function(e) {
			var code = e.keyCode || e.which;
			if (code==13) {
				if ($(this).data('running') || $('form.answers').is(':visible')) {
					e.preventDefault();
					$(':input', $('form.answers')).trigger('click');
				}
			}
		});
		// Submit Event.
		$('form[id$="Form"]').bind('submit', function(e) {
			e.preventDefault();
			var tagObj = this;
			// Form with a submit button.
			if ($('input[type="submit"]', tagObj).is('*')) {
				mainClass.formFieldRequired(tagObj);
				if (!$(tagObj).data('formError') && $('input[id$="Email"]', tagObj).is('*') && !emailCheck($('input[id$="Email"]', tagObj).val())) {
					mainClass.formNotification(tagObj, $('input[id$="Email"]', tagObj).data('invalid').replace(/\*/g, '“'+trim($('input[id$="Email"]', tagObj).val())+'”'), mainClass.formNotificationDuration);
					$(tagObj).data('formError', true);
				}
				if (!$(tagObj).data('formError')) {
					$(tagObj).data('running', true);
					//mainClass.formNotification(tagObj, $(tagObj).data('loading'));
					$.ajax({
						data: $(tagObj).serialize(),
						success: function(jsonObj) {
							if (jsonObj.responseSuccess) {
								mainClass.formNotification(tagObj, ((jsonObj.responseSuccess=='true') ? $(tagObj).data('success') : jsonObj.responseSuccess), mainClass.formNotificationDuration);
								mainClass.formFieldEmpty(tagObj);
							}
							else if (jsonObj.responseError) {
								mainClass.formNotification(tagObj, ((jsonObj.responseError=='true') ? $(tagObj).data('error') : jsonObj.responseError), mainClass.formNotificationDuration);
							}
							$(tagObj).data('running', false);
						},
						error: function() {
							mainClass.formNotification(tagObj, 'Erreur', mainClass.formNotificationDuration);
							$(tagObj).data('running', false);
						}
					});
				}
			}
		});
	},

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FIELDS — LABEL.
	formFieldLabel: function(inputObj) {
		var inputObj = (inputObj) ? inputObj : $(':input[data-value]');
		inputObj.each(function() {		
			$(this).bind({
				focus: function() {
					if ($(this).val() == $(this).data('value'))
						$(this).removeClass('empty').val('');
					if ($(this).hasClass('password')) {
						$(this).hide();
						$(this).next().show().focus();
					}
				},
				change: function() {
					if (empty($(this).val()))
						$(this).addClass('empty');
					else
						$(this).removeClass('empty');
				},
				blur: function() {
					if (empty($(this).val())) {
						$(this).addClass('empty');
						if (!$(this).is('select'))
							$(this).val($(this).data('value'));
						else
							$('option', this).first().text($(this).data('value'));
					}
				}
			});
			$(this).trigger('blur');
		});
	},

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FIELDS — EMPTY.
	formFieldEmpty: function(tagObj) {
		if ($(tagObj).is('*')) {
			$(tagObj).find(':input').each(function() {
				switch(this.type) {
					case 'password':
					case 'select-multiple':
					case 'select-one':
					case 'text':
					case 'textarea':
						($(this).data('value')) ? $(this).val('').trigger('blur') : $(this).val(''); break;
					case 'checkbox':
						if ($(this).hasClass('switch')) $(this).trigger('switch');
						else this.checked = false;
						break;
					case 'radio':
						this.checked = false; break;
				}
			});
		}
	},

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FORM — REQUIRED FIELDS.
	formFieldRequired: function(tagObj) {
		$(tagObj).removeData('formError');
		$(':input:visible', tagObj).each(function(i) {
			if ($(this).hasClass('required') && (empty($(this).val()) || ($(this).val() == $(this).data('value')))) {
				$(tagObj).data('formError', true);
				if ($(this).data('required'))
					mainClass.formNotification(tagObj, $(this).data('required'));
				else
					mainClass.formNotification(tagObj, 'Le champs “'+trim($(this).data('value').replace(/[:*]+/g,''))+'” est requis.');
				return false;
			}
		});
	},

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FORM — NOTIFICATION HIGHLIGHT.
	formNotification: function(tagObj, str, time) {
		Boxy.alert('<p>'+str+'</p>', null, {title:''});
	}

};



/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ O N   D O M   R E A D Y.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*
jQuery(window).bind({
	resize: function(e) {
		mainClass.pageOffset();
	}
});
*/
jQuery(document).ready(function() {
	mainClass.initialize();
});
