// adobe awful functions
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i = 0; i < changeImages.arguments.length; i += 2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i + 1];
		}
	}
}
var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		carterh_halifax = newImage("carterh_halifax.png");
		carterh_manchester = newImage("carterh_manchester.png");
		preloadFlag = true;
	}
}

// Dropdown navigation menu
var obj = null;
function checkHover() {
	if (obj) {
		obj.find('ul.menu').fadeOut('fast');
	}
}

$(document).ready(function() {

	// queue & wait
	$.fn.wait = function(time, type) {
		time = time || 1000;
		type = type || "fx";
		return this.queue(type, function() {
			var self = this;
			setTimeout(function() {
				$(self).dequeue();
			}, time);
		});
	};


	// Overlabel function : auto label positioning into inputs
	$.fn.overlabel = function() {
		this.each(function() {
			var label = $(this);
			$("#" + (this.htmlFor || label.attr('for') || "ID-NOT-FOUND"))
			.focus(function() { label.css("text-indent", "-1000px"); })
			.blur(function() { this.value || label.css("text-indent", "0px"); })
			.trigger("focus").trigger("blur")
			.length &&
				label.addClass("overlabel-apply");
		});
	}

	// adapt content (or any block to a proper height)
	$.fn.contentHeight = function(target, minHeight) {
		target = (target) ? target : $('#content');
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if ($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		return this.each(function() {
			tallest = tallest + 7;
			target.height(tallest);
		});
		$('#callback').css({
			'position': 'absolute',
			'bottom': 0,
			'right': 37
		});
	}
	// adapt content (or any block to a proper height) with smoothness
	$.fn.contentHeightAnimated = function(target, minHeight) {
		target = (target) ? target : $('#content');
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if ($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		return this.each(function() {
			tallest = tallest + 7;
			target.animate({ 'height': tallest }, 250);
		});
		$('#callback').css({
			'position': 'absolute',
			'bottom': 0,
			'right': 37
		});
	}

	// navigation links
	$('#navlinks > li').hover(function() {
		if (obj) {
			obj.find('ul.menu').fadeOut('fast');
			obj = null;
		}
		$(this).addClass('hover');
		$(this).find('ul.menu:not(:animated)').fadeIn('fast');
	}, function() {
		$(this).removeClass('hover');
		obj = $(this);
		checkHover();
	});

	$('body #content .panel').contentHeight();

	// apply overlabels
	$("#contact label").overlabel();
	$("#callback label").overlabel();

	// Display contact form
	if ($.browser.msie) { // Internet Explorer (6, 7 and above...)
		$('a[href$=#form-contact]').livequery('click', function() {
			$('#contact').toggle();
			if ($('#sidemenu li#sa a').hasClass("active") && !$(this).parent('li#sa').length) {  // not clicking the sidenav link
				$('body:not(.expand-sidebar) #sidebar').animate({
					width: "24"
				}, { queue: false, duration: 500 });
			}
			$('#sidemenu li#sa a').toggleClass('active');

			return false;
		});
	}
	else { // Modern Browsers : full effects support
		$("a[href$=#form-contact]").livequery('click', function() {
			if ($('#sidemenu li#sa a').hasClass("active")) {
				$('#sidemenu li#sa a').removeClass("active")
				$('#contact').fadeOut("800");

				if (!$(this).parent('li#sa').length) { // not clicking the sidenav link
					$('body:not(.expand-sidebar) #sidebar').animate({
						width: "24",
						opacity: "0.5"
					}, { queue: false, duration: 500 });
				}
			}
			else {
				$('#sidemenu li#sa a').addClass("active");
				$("#contact").fadeIn("800", function() {
					if ($(this).hasClass("virgin")) {
						$(this).removeClass("virgin");
						$("#contact select").selectbox();
					}
				});
			}
			return false;
		});
	}

	// display callback form
	if ($.browser.msie) { // Internet Explorer (6, 7 and above...)
		$('a[href$=#callback-form]').livequery('click', function() {
			$('#callback #callback-form').toggle();
			$('#callback-link').toggleClass('active');
			return false;
		});
	}
	else { // Modern Browsers : full effects support
		$("a[href=#callback-form]").livequery('click', function() {
			if ($('a#callback-link').hasClass("active")) {
				$('a#callback-link').removeClass("active");
				$("#callback-form").fadeOut("800");
			}
			else {
				$('a#callback-link').addClass("active");
				$("#callback-form").fadeIn("800", function() {
					if ($('#callback-form').hasClass("virgin")) {
						$('#callback-form').removeClass("virgin");
						$("#callback select").selectbox();
					}
				});
			}
			return false;
		});
	}


	// apply opacity 1/2 to sidebar when collapsed
	if (!$.browser.msie) { // Terrible support of animations + PNG bg in IE
		$('body:not(.expand-sidebar) #sidebar').css({ 'opacity': '0.50' });
	}


	// Slide sidebar menu (not on homepage)
	$('body:not(.expand-sidebar) #sidemenu').hover(function() {
		// hover
		if ($.browser.msie) {
			$('#sidebar').animate({
				width: "200"
			}, { queue: false, duration: 500 });
		} else {
			$('#sidebar').animate({
				width: "200",
				opacity: 1
			}, { queue: false, duration: 500 });
		}
	}, function() {
		// mouse out
		if ($('#sidemenu li#sa a.active').length != 0) { // has contact form popped up ?
			return false;
		}
		else { // yes it has
			if ($.browser.msie) {
				$('#sidebar').animate({
					width: "24"
				}, { queue: false, duration: 500 });
			} else {
				$('#sidebar').animate({
					width: "24",
					opacity: "0.5"
				}, { queue: false, duration: 500 });
			}
		}
	});


	// Side content menu
	$('#content-menu li a').each(function() {
		$(this).attr('title', $(this).text());

	});
	$('#content-menu li a').each(function() {
		$(this).attr('color', 'color: ' + $(this).css('color'));
	});
	$('#content-menu li.fi ul li a').attr('style', 'color: #3AB9E2');

	$('#content-menu li a').hover(function() {
		if (!$(this).hasClass('active'))
			$(this).prepend('› ');
	}, function() {
		if (!$(this).hasClass('active'))
			$(this).html($(this).attr('title'));
	});
	$('body:not(.rh.nos-offres-emploi) #content a[href^=#]').not('[href*=form]').click(function() {
		// note : #links cannot contain "form"

		// Fading content in/out
		var _a = $(this).attr('href');

		if (_a != '#void' && !$(this).hasClass('active')) {

			$('#content-menu li a').each(function() {
				$(this).html($(this).attr('title'));
				$(this).removeClass('active');
			});
			$('#content-menu li.fi ul li a').attr('style', 'color: #3AB9E2');
			var _color = ($('#content-menu li a[href$=' + _a + ']').attr('color')) ? ($('#content-menu li a[href$=' + _a + ']').attr('color')) : 'color: #3AB9E2';
			$('#content-menu li a[href$=' + _a + ']').attr('style', _color).addClass('active').prepend('› ');

			// toggling h1's when fading content 
			if ($('h1').size() > 1) { // we have multiple h1s
				var _h1 = _a.substring(1).split('#');
				if ($('h1[class$=' + _h1 + ']').length) { // h1 related to content exists
					$('h1').fadeOut('normal');
					$('h1[class$=' + _h1 + ']').fadeIn('normal');
				}
				else {
					$('h1:gt(0)').fadeOut('normal');
					$('h1:first').fadeIn('normal');
				}
			}
			if ($.browser.msie) // Internet Explorer : basic toggle
				$('div.panel').hide();
			else // modern browsers : smooth toggle
				$('div.panel').fadeOut("slow");
			$('div.panel').removeClass('active');
			if ($.browser.msie) // Internet Explorer : basic toggle
				$(_a).show();
			else  // modern browsers : smooth toggle
				$(_a).fadeIn("slow", function() { $('#content .panel').contentHeight(); });

			$(_a).addClass('active');
		}
		return false;
	});

	var _path = document.location.hash.substring(1);
	$('h1').hide();
	if (_path && _path != 'void') {
		$('#content-menu a[href$=' + _path + ']').addClass('active').prepend('› ');
		$('div.panel').hide();
		var _panel = _path.substring(0).split('#');

		$('body:not(.rh) #content-content .panel[id=' + _panel + ']').show().addClass('active');
		if ($('h1').size() > 1 && $('h1[class$=' + _panel + ']').length)
			$('h1[class=' + _panel + ']').show();
		else
			$('h1:eq(0)').show();
	}
	else {
		$('body:not(.rh) #content-content .panel:eq(0)').show();
		$('h1:eq(0)').show();
	}
	if (!$('#content-menu li a.active').length) {
		$('#content-menu li.fi.active>a:not(.active)').addClass('active').prepend('› ');
	}


	getNodeValue = function(data, nodeName) {
		var value = '';

		var node = $(data).find(nodeName);
		if (node.length != 0) {
			var value = node.text();
			if (value.indexOf('<![CDATA[') != -1) {
				var pattern = /^<!\[CDATA\[(.*)]]>$/;
				pattern.exec(value);
				value = RegExp.$1;
			}
		}
		return jQuery.trim(value);
	}

	// Callback & contact forms
	$('form#form-callback, form#form-contact').each(function() {
		$(this).submit(function() {
			$(this).parent('div').find('div.wait').remove();

			$(this).parent('div').prepend($('<div class="wait">Traitement en cours</div>').click(function() { $(this).hide(); }));

			var param = {};
			$(this).find(':input').each(function() {
				param[$(this).attr('name')] = $(this).val();
			});

			$.post('contact.aspx', param, function(data) {

				var selector = getNodeValue(data, 'selector');
				var message = getNodeValue(data, 'message');

				if (message != '') {
					// Message update
					$('div:has(' + selector + ') div.wait').html($(message)).show();
				}
			});

			return false;
		});
	});

	$('a[href$=.pdf]').attr('target', '_blank');

	$('#content-content li').each(function() {
		if ($(this).find('a').size() > 0)
			$(this).addClass('fleche');
	});
	if ($.browser.msie && ($.browser.version < 7) && !window.XMLHttpRequest) { // Internet Explorer 6
		DD_belatedPNG.fix('#content-content .transimg'); // PNG Fix
		DD_belatedPNG.fix('#content .transimg'); // PNG Fix

	}
	$("#form-contact input[name=url]").val(document.location);

	// Popup Thank - Fermeture
	$('a[href$=#form-thank]').livequery('click', function() { $('#popupThank').toggle(); });


	// $("a#toggleNewsletter").click(function() {
	// 		$("#form-contact input[name=newsletter]").val( Math.abs( ( $("#form-contact input[name=newsletter]").val() == '' ? 0 : $("#form-contact input[name=newsletter]").val() ) - 1) );
	// 	});
});

