/* -------------------------------------------------------------- 
optimus.jquery.js
sustainable a17 js library
by Nobleskine from AREA 17

Fresh homemade jQuery plugins for AREA 17 projects.

Version date: 4 Nov 2009
Version 1.0 Alpha
-------------------------------------------------------------- */
function isValidEmail(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

jQuery.fn.o_align = function(param) {
	return this.each(function(i){
		switch(param) {
			case 'v':
				var h = $(this).height();
				var oh = $(this).outerHeight();
				var mt = (h + (oh - h)) / 2;	
				
				break;
			case 'h':
				var w = $(this).width();
				var ow = $(this).outerWidth();	
				var ml = (w + (ow - w)) / 2;
				$(this).css({ 'margin-left': '-' + ml + 'px', 'left': '50%'});
				break;

			case 'c':
				var w = $(this).width();
				var ml = w / 2;
				var he = $(this).height();
				var hc = (he + he) /1.35;
				$(this).css({ 'right': '45%', 'width': + hc + 'px'});
				break;
			case 'a':
				var w = $(this).width();
				var ml = w / 2;
				var he = $(this).height();
				var hc = (he + he + he + he) /1.31;
				$(this).css({ 'right': '45%', 'width': + hc + 'px'});
				break;
			default:
				var h = $(this).height();
				var oh = $(this).outerHeight();
				var mt = (h + (oh - h)) / 2;	
				
				var w = $(this).width();
				var ow = $(this).outerWidth();	
				var ml = (w + (ow - w)) / 2;
				$(this).css({ 'margin-left': '-' + ml + 'px', 'left': '50%'});
		}
		
	});	
};

jQuery.fn.o_showhide = function(_effect, _speed) {
	return this.each(function(i) {
		var self = $(this);
		if (self.is(':visible')){
			switch(_effect) {
				case 'fade': self.fadeOut(_speed);
				case 'side': self.slideUp(_speed);
				default: self.hide(_speed);	
			}
		} else {
			switch(_effect) {
				case 'fade': self.fadeIn(_speed);
				case 'side': self.slideDown(_speed);
				default: self.show(_speed);	
			}
		}
	});
}

jQuery.fn.o_lightbox = function(_target, _effect, _speed, _click, _mouseover, _mouseout) {
	return this.each(function(i) {
		
		if (_target.length == 0) 
			return;
		
		var self = $(this);
		var lb = $(_target);
		var lbCloseButton = lb.find('.lb_close'); /* Assuming the close button has the standard .lb-close class applied */
		
		self.bind({
			click: function(e) {
				if (!_click) 
					return;
				e.preventDefault();
				lb.o_align(); /* Just in case */
				lb.o_showhide(_effect, _speed);
				lb.o_resetForm();
				lb.find('input:text')[0].focus(); /* Auto focus the first input text field found */
			},
			mouseover: function() {
				if (!_mouseover) 
					return;
			},
			mouseout: function() {
				if (!_mouseout) 
					return;
			}
		});
		
		lbCloseButton.bind({
			click: function(e) {
				e.preventDefault();
				lb.o_showhide(_effect, _speed);
			}
		});
		
	});
};

jQuery.fn.o_resetForm = function() {
	return this.each(function(i) {
		$(this).find('.subscribe_instructions').html('Entrez votre adresse e-mail ci-dessous');
		$(this).find('.error').removeClass('error');
	});
};

jQuery.fn.o_validateForm = function() {
	return this.each(function(i) {
		
		var self = $(this);
		var container = $(this).parent();
		var emailField = $(this).find('input:text');
		var instructionsField = $(this).find('.subscribe_instructions');
		var successContainer = $('#subscribed');
		
		
		if ($('html').hasClass('ie6')){
			var successContainerBg = successContainer.children('.lb_background');
			successContainerBg.css({'width': successContainer.outerWidth() + 'px','height': successContainer.outerHeight() + 'px'});
		}
		
		$(this).submit(function() {
			
			var emailField = $(this).find('input:text');
			var emailVal = $(this).find('input:text').val();
			
			if (emailVal.length > 0 && isValidEmail(emailVal)) {
				
				instructionsField.html('Entrez votre adresse e-mail ci-dessous');
				self.removeClass('error');
				
				container.o_showhide('fade', 'normal');
				successContainer.delay(500).o_showhide('fade', 'fast').delay(2000).fadeOut('slow');
				// 				
				$.ajax({
					type: "POST",
					url: "subscribe.php",
					data: "email="+ emailVal,
					success: function(){
						// show 
					}
				});
				// 				
				return false;
			} else {
				instructionsField.html('Votre adresse e-mail n’est pas valide');
				self.addClass('error');
				emailField.focus();
				return false;
			}
		});
		
		$(this).find('input:reset').bind({
			click: function() {
				instructionsField.html('Entrez votre adresse e-mail ci-dessous');
				self.removeClass('error');
				emailField.focus();
			}
		});
	});
};



 jQuery.fn.fitTo = function(element, transition)
 {
     this.each(function()
     {
 		/* check first if the element exists */
 		if ($(element).length == 0)
 			return;
 			
 		var maxWidth = $(element).width();
 		var maxHeight = $(element).height();
 		
 		var ratio = 0;  // Used for aspect ratio
         var width = $(this).width();    // Current image width
         var height = $(this).height();  // Current image height
 
 		console.log(width + '-' + maxWidth);
 		console.log(height + '-' + maxWidth);
 
 		if (width > maxWidth){
 			
 		}
     });
 };


//(function($) {
	
//window.Optimus = function Optimus() {
//};
	
//var Optimus = window.Optimus;
	
//Optimus.reset = function _reset(param) {
		
		/* expand the .body height until the .footer */
//	$('.full-H-next').each(function() {
//			var currentH = $(this).height();
//			var gapH = $(this).next().offset().top - $(this).offset().top;
//			if (gapH - currentH > 0) 
//				$(this).css('height', gapH);
//		});
		
		/* expand the height of an element to match its parent height */
//		$('.full-H').each(function() {
//			var _this = $(this);
//			var _thisParent = _this.parent();
//			_this.css('height', _thisParent.outerHeight(true));
//		});
		
		/* equalize the height of inner elements */
//		$('.equal-H').each(function() {
	//		var cols = $('> *', this);
	//		var maxH = 0;
	//		cols.each(function() {
	//			var currentH = $(this).outerHeight(true); /* outerHeight returns height+padding+border, plus margin when set to true */
	//			maxH = (maxH > currentH) ? maxH : currentH;
	//		}).each(function() {
			//	if (param == 'animate') {
				//	$(this).animate({ 'height' : maxH}, 300);
		//		} else {
//					$(this).css('height', maxH);
	//			}
//			})
//		});
		
		
//	};
	
//})(jQuery);


/* Events binding on page load */
// jQuery(window).bind('load', function() {
// 	Optimus();
// 	Optimus.reset('animate');
// });

 
