//(jQuery.noConflict())(function($){
$(document).ready(function(){	
	$("ul > li:last-child").addClass("last");
	$("ul > li:first-child").addClass("first");
	
    /**********************************/
    /** Background animation          */
    /**********************************/
	if($.browser.msie)
	{
		$(".menu-slide a.slide:not(.current)").css({'background-position-x': '-499px'}).hover(
				function(){
					var left = -499 + $(this).outerWidth();
					$(this).stop().animate({'background-position-x': left+'px'}, 250);
				},
				function(){
					$(this).stop().animate({'background-position-x': '-499px'}, 200, function(){
						$(this).css({'background-position-x': '-499px'});
					});
				}
		);
		
		$("#menu-main a:not(.current)").css({'background-position-x': '-250px'}).hover(
				function(){
					var left = -250 + $(this).outerWidth();
					$(this).stop().animate({'background-position-x': left+'px'}, 250);
				},
				function(){
					$(this).stop().animate({'background-position-x': '-250px'}, 200, function(){
						$(this).css({'background-position-x': '-250px'});
					});
				}
		);
	}
	else
	{
		$(".menu-slide a.slide:not(.current)").css({backgroundPosition: '-499px 1px'}).hover(
				function(){
					var left = -499 + $(this).outerWidth();
					$(this).stop().animate({backgroundPosition: left+'px 1px'}, 250);
				},
				function(){
					$(this).stop().animate({backgroundPosition: '-499px 1px'}, 200, function(){
						$(this).css({backgroundPosition: '-499px 1px'});
					});
				}
		);
		
		$("#menu-main a:not(.current)").css({backgroundPosition: '-250px 1px'}).hover(
				function(){
					var left = -250 + $(this).outerWidth();
					$(this).stop().animate({backgroundPosition: left+'px 1px'}, 250);
				},
				function(){
					$(this).stop().animate({backgroundPosition: '-250px 1px'}, 200, function(){
						$(this).css({backgroundPosition: '-250px 1px'});
					});
				}
		);
	}
	
    /**********************************/
    /** Smooth Scroll                 */
    /**********************************/
    $("a[href*=#]:not(.state)").click(function(){
      if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
              && location.hostname == this.hostname) {
                  var $target = $(this.hash);
                  $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
                  if ($target.length) {
                      var targetOffset = $target.offset().top;
                      $('html,body').animate({scrollTop: targetOffset}, 1000);
                     
                      return false;
                  }
              }       
    });
	
	//change display for small screens
	$(window).resize(function() {
		setSizes();
	});
	
	//set sizes at initialization;
	setSizes();
	
	/**
	 * Fermer le formulaire
	 */
	$("FORM .closebutton > .ui-icon-circle-close").click(function(){
		$(this).parents("FORM").remove();
	});
});

//function to set sizes of elements
function setSizes()
{
	if(jQuery(window).width() <= 1024)
	{
		jQuery("body").addClass('is1024');
	}
	else
	{
		jQuery("body").removeClass('is1024');
	}
	
	//bottom
	var newWidth = jQuery("#content-bottom").width() - jQuery("#content-bottom-left").outerWidth() - 85;
	if(newWidth < jQuery("#content-bottom-right").css('min-width'))
	{
		jQuery("#content-bottom-right").width(jQuery("#content-bottom-right").css('min-width'));
	}
	else if(newWidth > jQuery("#content-bottom-right").css('max-width'))
	{
		jQuery("#content-bottom-right").width(jQuery("#content-bottom-right").css('max-width'));
	}
	else
	{
		jQuery("#content-bottom-right").width(newWidth);
	}
	

	//footer separation  maxPercent ^ (((currentWidth / minWidth) - 1) * 2)
	//940px => 1%
	//1460 => 5%
	var maxPercent = 5;
	var minWidth = parseInt(jQuery(".wrapper-content").css('min-width'));
	var curWidth = parseInt(jQuery(".wrapper-content").width());
	newWidth = parseInt(Math.pow(maxPercent, ((curWidth / minWidth) - 1) * 2));
	jQuery("#copyright").css({'margin-right': newWidth+"%"});
	
	//home bottom buttons
	if(is_home)
	{
		newWidth = parseInt((jQuery("#content-bottom-right").width() - 30) / 2, 10);
		jQuery(".home-button").width(newWidth - (jQuery(".home-button").outerWidth() - jQuery(".home-button").width()));
	}
	else
	{
		if(jQuery("#type-ligne").length > 0)
		{
			var contentHeight = jQuery("#type-content").outerHeight();
			var contentBottom = jQuery("#type-content").offset().top + contentHeight + 20;
			var lineTop = jQuery("#type-ligne").offset().top - parseInt(jQuery("#type-ligne").css('margin-top'), 10);
			var rightHeight = jQuery("#type-right").outerHeight();
			var rightBottom = jQuery("#type-right").offset().top + rightHeight;
			
			var bottom =  100;
			var minHeight = 30;
			
			if(contentBottom < (rightBottom-bottom-minHeight))
			{
				var newHeight = rightBottom - bottom - contentBottom;
				//var newTop = (lineTop - contentBottom);
				var newTop = 130 - newHeight; //default height is 130
				if(newTop > 0)
				{
					newTop = 145 + Math.abs(newTop); //default margin is 145
				}
				else
				{
					newTop = 145 - Math.abs(newTop);
				}
				jQuery("#type-ligne").css({'margin-top': newTop+'px', 'height': newHeight+'px', 'border-left': '2px solid #FFFFFF'});
			}
			else
			{
				jQuery("#type-ligne").css({'margin-top': '145px', 'border-left': 'none'}); //default margin is 145
			}
			
			//quick fix for ie7
			if(jQuery.browser.msie && (jQuery.browser.version == '7.0'))
			{
				jQuery("#type-ligne").css({'margin-left': '-'+(jQuery("#type-content").outerWidth()-45)+'px' });
			}
		}
	}
}


/**
 * jQuery Background Position Animate Fix (for firefox)
 * @author Alexander Farkas
 * v. 1.02
 */
(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);


function format_amount(elem, lang) {
	var numbers = /[0-9]/g;
	var matches = elem.val().match(numbers);
	var new_number = '';
	if(!(matches == null))
	{
		for(var i=0; i<matches.length; i++)
		{
			new_number += String(matches[i]);
		}
	}
	else
	{
		new_number = 0;
	}
	
	var formatted = '$ '+new_number;
	if(lang == 'fr')
	{
		formatted = new_number+' $';
	}
	elem.val(formatted);
}

/**
 * Function used to autofill a field with a default value and put back this value whan field is empty
 * 
 * @param id element id
 * @param v element value
 */
function autoFill(id, v){
	jQuery(id).attr({ value: v }).focus(function(){
		if(jQuery(this).val()==v){
			jQuery(this).val("");
		}
	}).blur(function(){
		if(jQuery(this).val()==""){
			jQuery(this).val(v);
		}
	});
}



