/***
 * Placeholder pour les champs textes de formulaire
 *************************************************************/
function init_placeholder_text(elem, placeholder) {
	elem = $(elem);
	var form = elem.parents('form:first');
	
	if (elem.val() == '') {
		elem.val(placeholder);
		elem.data('isEmpty', true);
	} else {
		elem.data('isEmpty', false);
	}
	
	elem.focus(function () {
		if (elem.data('isEmpty')) {
			elem.val('');
		}
	});
	
	elem.blur(function () {
		if (elem.val() == '') {
			elem.val(placeholder);
			elem.data('isEmpty', true);
		} else {
			elem.data('isEmpty', false);
		}
	});
	
	elem.change(function () {
		if (elem.val() == '') {
			elem.data('isEmpty', true);
		} else {
			elem.data('isEmpty', false);
		}
	});
	
	$(window).unload(function () {
		if (elem.data('isEmpty')) {
			elem.val('');
		}
	});
	
	form.submit(function () {
		if (elem.data('isEmpty')) {
			elem.val('');
		}
	});
}

/***
 * Main
 *********/
$(function() {
	/***
	 * Cette fonction étire un élément à la verticale 
	 * pour lui faire remplir le bas de la page
	 ********************************************/
	
	/*
	function stretch(params) {
		$('.stretch-content').each(function () {
			var init = typeof params != 'undefined' && params.init;
			var content = $(this);
			if (init) {
				content.data('minValue', content.offset().top + content.outerHeight());
				content.data('padding', content.css('padding-bottom'));
			}
			var min = content.offset().top + content.outerHeight();
			var h = Math.max($(document).height(), $(window).height());
			if (min < h) {
				var padding = h - min + parseInt(content.css('padding-bottom'));
				content.css('padding-bottom', padding + 'px'); 
			} 
		});
	}
	
	$(window).resize(function() {
		stretch();
	});
	
	stretch({init:true});
	*/
	
	/***
	 * Exécuter seulement en dehors de Unify
	 *****************************************/
	if (!$('body.unify-edit').length) {
		/***
		 * Panneaux collapsibles
		 ********************************************/
		$('.tab-collapsible').each(function (i) {
			var tabCollapsible = $(this);
			var tab = $('.tab', this);
			var tabContent = $('.tab-content', this);
			
			tabContent.data('maxHeight', tabContent.innerHeight());
			tabCollapsible.addClass('close');
			tabContent.height(0);
			//stretch();
			
			tab.click(function () {
				$('.tab-collapsible.open').each(function() {
					var tab = $(this);
					$('.tab-content', this).stop().animate({'height':0}, function () {
						//stretch();
						tab.removeClass('open');
						tab.addClass('close');
					});					
				});
				if (tabCollapsible.hasClass('close')) {
					tabCollapsible.removeClass('close');
					tabCollapsible.addClass('open');
					tabContent.stop().animate({'height':tabContent.data('maxHeight')}, function () {
						//stretch();
					});
				} else {
					tabContent.stop().animate({'height':0}, function () {
						//stretch();
						tabCollapsible.removeClass('open');
						tabCollapsible.addClass('close');
					});
				}
			});
		});
	
		/***
		 * Zebra stripes
		 *******************/
		$('.zebra-area').each(function() {
			$('.zebra', this).filter(':odd').addClass('even');
		});
	
		/***
		 * Coins rond pour internet explorer
		 ************************************/
		 /*
		 $('.content .block').each(function() {
		 	$(':first-child', this).addClass('first');
		 	$(':last-child', this).addClass('last');
		 	$(this).wrapInner($('<div class="border-radius-padding">'));
		 	var top = $('<div class="border-radius-top-right"><div class="border-radius-top-left"><div class="border-radius-top-center"></div></div></div>');
		 	var bottom = $('<div class="border-radius-bottom-right"><div class="border-radius-bottom-left"><div class="border-radius-bottom-center"></div></div></div>');
		 	$(this).prepend(top).append(bottom).addClass('border-radius');
		 });
		 */
	}
	
	/***
	 * Lightbox
	 *************/
	var params = {
		overlayOpacity:0.25,
		fixedNavigation:true,
		txtOf: 'de',
		imageLoading:			window.urlBase + '_lib/jquery-lightbox/images_fr/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
		imageBtnPrev:			window.urlBase + '_lib/jquery-lightbox/images_fr/lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
		imageBtnNext:			window.urlBase + '_lib/jquery-lightbox/images_fr/lightbox-btn-next.gif',			// (string) Path and the name of the next button image
		imageBtnClose:			window.urlBase + '_lib/jquery-lightbox/images_fr/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
		imageBlank:				window.urlBase + '_lib/jquery-lightbox/images_fr/lightbox-blank.gif'				// (string) Path and the name of a blank image (one pixel)
	};
	if ($('html').attr('lang') == 'en') {
		params = {
			overlayOpacity:0.25,
			fixedNavigation:true,
			txtOf: 'of',
			imageLoading:			window.urlBase + '_lib/jquery-lightbox/images/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
			imageBtnPrev:			window.urlBase + '_lib/jquery-lightbox/images/lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
			imageBtnNext:			window.urlBase + '_lib/jquery-lightbox/images/lightbox-btn-next.gif',			// (string) Path and the name of the next button image
			imageBtnClose:			window.urlBase + '_lib/jquery-lightbox/images/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
			imageBlank:				window.urlBase + '_lib/jquery-lightbox/images/lightbox-blank.gif'				// (string) Path and the name of a blank image (one pixel)
		};
	}
	$('a[rel=\'dialog\']').lightBox(params);
	
	/***
	 * Image de background stretchée
	 **********************************/
	
	function resizeBg(bgImg) {
		var w, h, w0, h0, y;
		
		w = bgImg.attr('width');
		h = bgImg.attr('height');
		
		w0 = $(window).width();
		h0 =  h * w0 / w;
		
		y = 0;
		
		if (h0 < $(window).height()) {
			h0 = $(window).height();
			w0 = w * h0 / h;
		} else if (h0 > $(window).height()) {
			y = -(h0 - $(window).height()) / 2;
		}
		
		bgImg.attr('width', w0);	
		bgImg.attr('height', h0);
		bgImg.css({
			position: 'relative',
			top:      y
		});
	}
	
	(function() {
		var body = $('body');
		var delay = $('.delay');
		var src = body.css('background-image');
		var bg = $('<div id="bg"><img /></div>');
		var bgImg = $('img', bg);
		var matches = src.match(/^url\((?:"((?:"|[^"])*?)"|'((?:'|[^'])*?)'|(.*?))\)$/);
		var w, h, w0, h0;
		
		if (/* body.hasClass('test') && */ !body.hasClass('unify-edit')) {
			src = window.smsh && window.smsh.bgSrc || false;
		} else {
			src = matches && (matches[1] || matches[2] || matches[3]) || false;
		}
		
		if (src) {
			body.css('background-image', 'none');
			bg.css({
				position:'fixed',
				left:0,
				top:0,
				right:0,
				bottom:0,
				overflow:'hidden',
				height:'100%',
				width:'100%'
				//visibility:'hidden'
			});
			body.prepend(bg);
			
			if (/* body.hasClass('test') && */ !body.hasClass('unify-edit')) {
			
				delay.css({visibility:'hidden'});
				bgImg.css({opacity:0});
				body.addClass('bg-loaded');
			
				//body.css({opacity:0, visibility:'visible'});
				
				setTimeout(function() {
					//body.animate({opacity:1}, 800);
					bgImg.animate({opacity:1}, 800, function() {
						setTimeout(function() {
							delay.css({visibility:'visible'});
							//delay.animate({opacity:1}, 800);
						}, 500);
					});
				}, 1000);
				
				
				bgImg.load(function() {
					resizeBg(bgImg);
					
					//body.addClass('bg-loaded');
					//bg.css('visibility', '');
					
					$(window).resize(function() {
						resizeBg(bgImg);
					});
				});
				bgImg.attr('src', src);
				
			} else {
			
				//body.css({opacity:0, visibility:'visible'});
				
				setTimeout(function() {
					$('body').addClass('bg-loaded');
					//body.animate({opacity:1}, 800);
					//bgImg.animate({opacity:1}, 800);
				}, 1000);
				
				//bgImg.css({opacity:0});
				
				bgImg.load(function() {
					resizeBg(bgImg);
					
					//body.addClass('bg-loaded');
					//bg.css('visibility', '');
					
					$(window).resize(function() {
						resizeBg(bgImg);
					});
				});
				bgImg.attr('src', src);
				
			}
		}
	})();
	
	/*
	if ($('body').hasClass('certificat-cadeau')) {
		// Validation des champs
		$('#form').submit(function (event) {
			event.preventDefault();
			
			var sender = $('.sender :input:not(:radio,:checkbox, :hidden)', this);
			var recipient = $('.recipient :input:not(:radio,:checkbox, :hidden)', this);
			var certificateDelivery = $('.certificate-delivery :radio:checked');
			var shipping = $('.shipping :radio:checked');
			var postInvoice = $('.post-invoice :radio:checked');
			
			sender.filter(function() { 
				return $(this).val().trim() == '' ;
			});
			
			console.log(sender, recipient, certificateDelivery, shipping, postInvoice);
		});
	}
	*/
});
