$(document).ready(function() {
	//fetch language parameter
	var langParameter = window.location.pathname.substring(1,3);

	//this bit AJAX-loads the pages using the newsfeed.php
	$('div.newsDiv').html('<img src=http://www.magenta-legal.com/_img/loader.gif>');
	$('div#news1').load('http://www.magenta-legal.com/newsfeed.php?category=actualites&descr=1&max=3&l=' + langParameter);
	$('div#news2').load('http://www.magenta-legal.com/newsfeed.php?category=publications&descr=1&max=3&l=' + langParameter);
	
	$('a#news1Link').attr("href","http://www.magenta-legal.com/blog/category/actualites/");
	$('a#news2Link').attr("href","http://www.magenta-legal.com/blog/category/publications/");
	
	// those bits of code generate the glowing effect on the menu
	// as well as the slideshow on the homepage
	$('div.menu_item:not(div.selected)').hover(function() {
		$(this).animate( { backgroundColor: '#ed417c' }, 'slow');
	}, function() {
		$(this).animate( { backgroundColor: '#5f7179' }, 'slow');
	});
	
	//this one bit manages the slideshow on the homepage
	//$('#imgHomeWrapper').cycle({ 
	//    fx:      'fade', 
	//    speed:  3000 
	//});
	
	// this bit of code creates an automatic summary using 
	// the page's named anchors
	var anchorArray = new Array();
	var anchorIndex = "<li>";
	var i = 0;
	$("h2 a[name]").each(function() {
		anchorArray[i] = "<a href=#" + this.name + ">" + this.innerHTML + "</a>";
		i++;
	});
	anchorIndex = anchorIndex + anchorArray.join(" | </li><li>");
	$("#index").append(anchorIndex);
	
	// SMOOTH Scrolling Code
	$('a[href*=#]').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;
	    	}
	    }
	});
	
	// the below snippet adds the back-to-top-anchor links
	$("h2:not(.avocatName)").each(function() {
		$(this).append('<a href="#topOfContent" class="miniTop">&uarr;&nbsp; TOP</a>');
	});
	// and then binds the experimental smoothscrolling to them
	$("a.miniTop").click(function() {
		$('html,body').animate({scrollTop: 425}, 1000);
		return false;
	});
	
	//link fader code
	$("a.footerLink, siteMap a, ul.summary li a, ul#categories li a").hover(function(){
		$(this).animate( { color: '#ed417c' }, '35000');
	}, function() {
		$(this).animate( { color: '#5F7179' }, '35000');
	});

	//Adds a button within a span 
	$("ul.infoContacts").append('<li class="avocatBTN">Bio &raquo;</li>');
	
	//hide and shows in the AVOCATS page
	$("div.execSummary").hide();
	$("li.avocatBTN").click(function() {
		if(langParameter == "fr") {
			if($(this).html() == "Bio » Masquer") {
				$(this).html("Bio &raquo;");
			} else {
				$(this).html("Bio &raquo; Masquer");
			}
		}
		if(langParameter == "us") {
			if($(this).html() == "Bio » Hide") {
				$(this).html("Bio &raquo;");
			} else {
				$(this).html("Bio &raquo; Hide");
			}
		}
		$(this).parent().parent().parent().children(".execSummary").slideToggle();
	});
	
	//this one here circumvents IE's issue with LI:hover
	$("li.avocatBTN").hover(function(){
		$(this).css("background-color","pink").css("cursor","pointer").css("color","#ed417C");	
	}, function() {
		$(this).css("background-color","#DFDFDF").css("cursor","default").css("color","#4F4F5F");	
	});	
	
	//Toggle recrutment span visibility
	$("span#recrutement").hide();
	$("a#recrut").click(function(){
		$("span#recrutement").slideToggle();
		return false;
	});

	//loads news into the newsHomeWrapper when on Homepage
	//remaining issue, this is still loaded even when div is not here
	$('div#newsHomeWrapper').html('<img src=http://www.magenta-legal.com/_img/loader.gif>');
	$('div#newsHomeWrapper').load('http://www.magenta-legal.com/newsfeed.php?descr=0&max=12').css('float','right').css('width','278px').css('padding','10px');
});

