
var GLOB = {
    set: function (namespace, keys) {
        if (!GLOB[namespace]) {
            GLOB[namespace] = keys;
        } else {
            for (key in keys) {
                GLOB[namespace][key] = keys[key];
            }
        }
    },
    get: function (namespace, key) {
        if (!GLOB[namespace]) {
            return;
        }
        if (!key) {
            return GLOB[namespace];
        }
        return GLOB[namespace][key];
    }
};

/*global $,document*/
function updateShowAllTr()
{
	if( $('.jsHidenTr').length>0 )
	{
		$('.jsShowAllTr').show();
	}
	else
	{
		$('.jsShowAllTr').hide();
	}
}

$(document).ready(function(){
	$('#hd_m li').mouseover(function(){
		$(this).find('.under').show();
			$(this).find('>.subunder').show();
		});
	$('#hd_m li').mouseleave(function(){
		$(this).find('.under').hide();
		$(this).find('>.subunder').hide();
	});
	
	$('.jsPoplanguages').click(function(){
		$('#poplanguages').toggle().siblings('.popup').hide(); $(this).blur();
		return false;
	})
	
	$('.jsPoppopothers').click(function(){
		$('#popothers').toggle().siblings('.popup').hide(); $(this).blur();
		return false;
	})
	
    $(".jsautoempty")
	   .blur(function(event){
			if(!$(this).attr('value'))
				{$(this).css('color','#999'); $(this).attr('value',$(this).attr('alt')); }
		})
	   .focus(function(event){
			if($(this).attr('value')===$(this).attr('alt'))
				{ $(this).attr('value',''); $(this).css('color',''); }
		})
		.each(function(){
			if(!$(this).attr('value') || $(this).attr('value')===$(this).attr('alt') )
				{$(this).css('color','#999'); $(this).attr('value',$(this).attr('alt'));  }
		})
	;
	$('.jsautofocus').focus();
	$('.jsonly').show();
	$('.jshide').hide();
	
	$('.jsautosubmit').live("click",function(){
		$(this).parents('form').submit();
	});
	
	$('.jsHideTr').live("click",function(){
		$(this).parents('tr').addClass('jsHidenTr').hide();
		updateShowAllTr();
	});
	
	$('.jsShowAllTr').live("click",function(){
		$('tr.jsHidenTr').removeClass('jsHidenTr').show();
		$('.jsfilters .dd_c a.sel').removeClass('sel');
		$('.jsfilters .dd_c a.jsfilter_all').addClass('sel');
		$('.jsfilters .dd_c select.jsproductselect').val('');
		updateShowAllTr();
	});
	
	$('.jscheckdate').blur(function(){
		var date = $(this).val().replace(/\//g,'-');
		date = date.split('-');
		if( $(this).val() =='' )
		{
			$(this).parents('dd').removeClass('witherror');
		}
		else if (date[0]>999 && date[1]<13 && date[1]>0 && date[2]>0 && date[2]<32  )
		{
			$(this).val( date.join('-')  );
			$(this).parents('dd').removeClass('witherror');
		}
		else if (date[2]>999 && date[1]<13 && date[1]>0 && date[0]>0 && date[0]<32  )
		{
			$(this).val( date[2] + '-' + date[1] + '-' + date[0] );
			$(this).parents('dd').removeClass('witherror');
		}
		else
		{
			$(this).parents('dd').addClass('witherror');
		}
	});
	
	$('.jstonotactive').live("click",function(){
		$(this).parents('ul').removeClass('onlyactive').addClass('onlyone');
		return false;
	});
	
	$('.jsdown')
		.parents('dd')
		.addClass('jswithdown');
	$('.jswithdown')
		.live('mouseover',function(){
			$(this).addClass('jsdownsel').find('.jsdown').show();
		})
		.live('mouseout',function(){
			$(this).removeClass('jsdownsel').find('.jsdown').hide();
		})
		;
	$('.jswithsaved')
		.append('<div class="jssaved">&nbsp;</div>').find('.jssaved').each(function(){
			var t=setTimeout("$('.jssaved').fadeOut();",1500)
		});
	$('.jswithsavederror')
		.append('<div class="jssavederror">&nbsp;</div>').find('.jssavederror').each(function(){
			var t=setTimeout("$('.jssavederror').fadeOut();",1500)
		});
	
	updateShowAllTr();
	
	$('.equalHeights').equalHeights();
	
	$('.jsajaxtab a').live('click',function(){
		var datatab = $(this).parents('li').addClass('sel').siblings('li').removeClass('sel').parents('.menutab').siblings('.datatab');
		datatab.html('<div class="loading">&nbsp;</div>')
			.load( $(this).attr('href') + ' #' + datatab.attr('id'))
			;
		return false;
	})
	;
	
	
	function autocheckform()
	{
		$(this).find('.witherror').removeClass('witherror');
		$(this).find('.witherrorlistnotempty').removeClass('witherrorlistnotempty');
		$(this).find('.autocheck_error').remove();
		var nb_errors = 0;
		$(this).find('.jsnotempty').each(function(){
			if( $(this).val()==''  )
			{
				$(this).after(' <span class="autocheck_error"><span>Erreur !</span></span>');
				$(this).parents('dd').addClass('witherror');
				nb_errors++;
			}
		})
		
		$(this).find('.listnotempty').each(function(){
			if(!$(this).siblings('.selector').find('input[type=checkbox]:checked').length)
			{
				$(this).addClass('witherrorlistnotempty');
				$(this).parents('dd').addClass('witherror');
				nb_errors++;
			}
		})
		
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		$(this).find('.jscheckmail').each(function(){
			if( !filter.test( $(this).val())  )
			{
				$(this).after(' <span class="autocheck_error"><span>Erreur !</span></span>');
				$(this).parents('dd').addClass('witherror');
				nb_errors++;
			}
		})
		if(nb_errors)
			return false;
	}
	
	$('.jsnotempty, .jscheckmail, .listnotempty').each(function(){
		$(this).siblings('label').append(' <span class="mandatory" title="' + GLOB.get("field_mandatory") + '"><span>oblig.</span></span>');
		$(this).siblings('label').addClass('mandatory');
		$(this).parents('form').submit(autocheckform);
		
	});

	$(function() {
	  
		//cache the ticker
		var ticker = $("#ticker");
		  
		//wrap dt:dd pairs in divs
		ticker.children().filter("dt").each(function() {
		  
		  var dt = $(this),
		    container = $("<div>");
		  
		  dt.next().appendTo(container);
		  dt.prependTo(container);
		  
		  container.appendTo(ticker);
		});
				
		//hide the scrollbar
		ticker.css("overflow", "hidden");
		
		//animator function
		function animator(currentItem) {
		    
		  //work out new anim duration
		  var distance = currentItem.height();
			duration = (distance + parseInt(currentItem.css("marginTop"))) / 0.025;

		  //animate the first child of the ticker
		  currentItem.animate({ marginTop: -distance }, duration, "linear", function() {
		    
			//move current item to the bottom
			currentItem.appendTo(currentItem.parent()).css("marginTop", 0);

			//recurse
			animator(currentItem.parent().children(":first"));
			
		  }); 
		};
		
		//start the ticker
		animator(ticker.children(":first"));
				
		//set mouseenter
		ticker.mouseenter(function() {
		  
		  //stop current animation
		  ticker.children().stop();
		  
		});
		
		//set mouseleave
		ticker.mouseleave(function() {
		          
          //resume animation
		  animator(ticker.children(":first"));
		  
		});
	  });
	
});




/*-------------------------------------------------------------------- 
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Description: Compares the heights or widths of the top-level children of a provided element 
 		and sets their min-height to the tallest height (or width to widest width). Sets in em units 
 		by default if pxToEm() method is available.
 * Dependencies: jQuery library, pxToEm method	(article: 
		http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/)							  
 * Usage Example: $(element).equalHeights();
  		Optional: to set min-height in px, pass a true argument: $(element).equalHeights(true);
 * Version: 2.0, 08.01.2008
--------------------------------------------------------------------*/

$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().not('.notEqualHeights').each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
		$(this).children().not('.notEqualHeights').css({'min-height': currentTallest}); 
	});
	return this;
};