/*
 * jQuery Color Animations
 * Copyright 2007 John Resig
 * Released under the MIT and GPL licenses.
 */

(function(jQuery){

	// We override the animation for all of these color styles
	jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){
		jQuery.fx.step[attr] = function(fx){
			if ( fx.state == 0 ) {
				fx.start = getColor( fx.elem, attr );
				fx.end = getRGB( fx.end );
			}

			fx.elem.style[attr] = "rgb(" + [
				Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
			].join(",") + ")";
		}
	});

	// Color Conversion functions from highlightFade
	// By Blair Mitchelmore
	// http://jquery.offput.ca/highlightFade/

	// Parse strings looking for color tuples [255,255,255]
	function getRGB(color) {
		var result;

		// Check if we're already dealing with an array of colors
		if ( color && color.constructor == Array && color.length == 3 )
			return color;

		// Look for rgb(num,num,num)
		if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
			return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];

		// Look for rgb(num%,num%,num%)
		if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
			return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];

		// Look for #a0b1c2
		if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
			return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];

		// Look for #fff
		if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
			return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];

		// Otherwise, we're most likely dealing with a named color
		return colors[jQuery.trim(color).toLowerCase()];
	}
	
	function getColor(elem, attr) {
		var color;

		do {
			color = jQuery.curCSS(elem, attr);

			// Keep going until we find an element that has color, or we hit the body
			if ( color != '' && color != 'transparent' || jQuery.nodeName(elem, "body") )
				break; 

			attr = "backgroundColor";
		} while ( elem = elem.parentNode );

		return getRGB(color);
	};
	
	// Some named colors to work with
	// From Interface by Stefan Petre
	// http://interface.eyecon.ro/

	var colors = {
		aqua:[0,255,255],
		azure:[240,255,255],
		beige:[245,245,220],
		black:[0,0,0],
		blue:[0,0,255],
		brown:[165,42,42],
		cyan:[0,255,255],
		darkblue:[0,0,139],
		darkcyan:[0,139,139],
		darkgrey:[169,169,169],
		darkgreen:[0,100,0],
		darkkhaki:[189,183,107],
		darkmagenta:[139,0,139],
		darkolivegreen:[85,107,47],
		darkorange:[255,140,0],
		darkorchid:[153,50,204],
		darkred:[139,0,0],
		darksalmon:[233,150,122],
		darkviolet:[148,0,211],
		fuchsia:[255,0,255],
		gold:[255,215,0],
		green:[0,128,0],
		indigo:[75,0,130],
		khaki:[240,230,140],
		lightblue:[173,216,230],
		lightcyan:[224,255,255],
		lightgreen:[144,238,144],
		lightgrey:[211,211,211],
		lightpink:[255,182,193],
		lightyellow:[255,255,224],
		lime:[0,255,0],
		magenta:[255,0,255],
		maroon:[128,0,0],
		navy:[0,0,128],
		olive:[128,128,0],
		orange:[255,165,0],
		pink:[255,192,203],
		purple:[128,0,128],
		violet:[128,0,128],
		red:[255,0,0],
		silver:[192,192,192],
		white:[255,255,255],
		yellow:[255,255,0]
	};
	
})(jQuery);


(function(jQuery){

var cH;
var mH;
var pP;


function appear(e){
	$('div', e).animate({ opacity: 1 }, 400);
	$('> a', e).animate({ color: '#FFFFFF' }, 400);
}

function disappear(e,d){
	$('> a', e).animate({ color: '#53534d' }, d);
	$('div', e).animate({ opacity: 0 }, d);
}

function deploy(e,d){
	//$('div', e).animate({ top: '0' }, 400);
	//$(e).animate({ height: cH+'px' }, 400);
	$(e).animate({ height: cH+'px', paddingTop: '0' }, 400);
}

function collapse(e){
	if ($(e).height() > mH) {
		disappear(e,400);
	} else {
		disappear(e,200);
	}
	//$(e).animate({ height: mH+'px' }, 400);
	//$('div', e).animate({ top: 32+'px' }, 400);
	$(e).animate({ height: mH+'px', paddingTop: '32px' }, 400);
}

function oclosePopin(){
	$('a.closePopin').fadeIn(300);
}

function cclosePopin(){
	$('a.closePopin').fadeOut(100);
}

function displayPopin(data) {
	$('div.popin').html(data['zone1']);
	$('div.popin').css({'display':'block','top':pP+'px','marginLeft':'1px','width':'0%','height':'148px'});
	$('div.popin .mea').css({'width':'947px','height':'473px'});
	$('div.popin img').css({'cursor':'pointer'});
	$('div.popin a.closePopin').fadeOut(1);	
	$('div.popin').animate({'width':'316px','height':'473px'},300, function() {
		$('div.popin').animate({'width':'947px'},300, function() {
			oclosePopin();
		})
	});
	$('a.closePopin').click(function () {
		closePopin();
		return false;
	});
	$('div.popin img').click(function () {
		closePopin();
	});			
}

function closePopin(e){
	cclosePopin();
	closePLink();
	$('div.popin').animate({'width':'316px'},300,function () {
		$('div.popin').animate({'width':'0','height':'0'},300,function () {
			$('div.popin').css({'display':'none'});
			if (e) {
				var slpos = e.lastIndexOf('/');
				var turl = e.substr(0,slpos) + 'js/' + e.substr((slpos+1));
				var jso = $.getJSON(turl, function(data) {
					displayPopin(data); 
				})
			} else {
				$('div.main').data({'curDisp':''});
				$('div#conThu').animate({'opacity':1},300);
			}
		})
	});
}

function selName(a,b) {
	var url = $(a).attr('href');
	var slpos = url.lastIndexOf('/');
	turl = url.substr(0,slpos) + 'js/' + url.substr((slpos+1));
	dopP(a,b);
	var ourl = $('div.main').data('curDisp');
	if (ourl != url) {
		closePLink();
		if (ourl != 'undefined' && ourl != '') {
			closePopin(url);
		} else {
			var jso = $.getJSON(turl, function(data) {
				displayPopin(data); 
			})
		}
		var link = (b) ? $(a):$('.ttH4').parent().find('ul > li > a[href$='+url+']');
		link.hover(function() {
			$(this).css({'background':'#c50f50'});
		}, function () {
			$(this).css({'background':'#c50f50'});
		});
		link.css({'background':'#c50f50'});
		$('div.main').data({'curDisp':url});
	}
	$('div#conThu').animate({'opacity':.3},300);
}

function dopP(a,b) {
	var id = (b) ? $(a).attr('id').split('nl')[1]:$(a).attr('id').split('th')[1];
	var diff = thu.length - id;
	if (diff - (diff%6) >= 12) {
		pP = $('#th'+id).position()['top'];
	} else {
		pP = $('#th'+(thu.length - 12)).position()['top'];
	}
}

function closePLink (f) {
	var urlT;
	var off;
	if (f && $('div.main').data('curDisp') != '') {
		urlT = $('div.corps a[href$='+$('div.main').data('curDisp')+']');
		off = {'background':'transparent','color':'#53534d'};
	} else {
		urlT = $('.ttH4').parent().find('ul > li > a[href$='+$('div.main').data('curDisp')+']');
		off = {'background':'transparent'};
	}
	urlT.css(off);
	urlT.hover(function() {
		$(this).css({'background':'#c50f50','color':'#FFFFFF'});
	}, function () {
		$(this).css(off);
	});
}

function displayDetails(a) {
	closePLink(true);
	if ($(window).scrollTop() > $('.detail').position()['top']) $("html, body").animate({ scrollTop: $('.detail').position()['top'] }, "slow");
	var url = $(a).attr('href');
	var jso = $.getJSON(url, function(data) {
		$('div.detail').html(data['zone1']);
	})
	$('div.main').data({'curDisp':url});	
	$(a).css({'background':'#c50f50','color':'#FFFFFF'});
	$(a).hover(function() {
		$(this).css({'background':'#c50f50','color':'#FFFFFF'});
	}, function () {
		$(this).css({'background':'#c50f50','color':'#FFFFFF'});
	});	
}

var thu;

$(function(){
	cH = $('div.main').attr('offsetHeight');
	mH = $('ul.nav').height() - 25;
	var lis = $('ul.nav > li');
	thu = $('a.thumbs');
	if (thu.length > 18) {
		var li = Math.ceil(thu.length/6);
		cH = thu.height() * li + $('.trombinoscope').position()['top'] + (li-1) + 5;
	}
	var anam = $('div.listAssocie');
	var snam = $('ul.onglets > li > a.RarrowGrey');
	var names = $('.ttH4').parent().find('ul > li > a');
	$('div.main').data({'curDisp':''});

	$('div.detail').css({'display': 'block', 'float':'left','width':'316px','border-right':'1px dotted #AAAAAA','height':'90%','background':'#FFFFFF'});
	
	$('ul.nav > li > ul').css({'display': 'block', 'position':'absolute','top':'50px'});
	lis.css({'height':mH+'px', 'paddingTop':'32px', 'overflow':'hidden'});
	
    var libg = document.createElement('div');
	libg.className = 'bgli';
	lis.prepend(libg);
	$('div.bgli').css({'display': 'block', 'position':'relative','zIndex':'-1', 'top':'0', 'left':'1px', 'width':'100%', 'height':'1500px', 'marginBottom':'-1500px', 'backgroundColor':'#746964', 'opacity':0});
	$('ul.nav li ul').css({'marginLeft': '1px'});

	$('ul.nav > li > a').css({'position': 'absolute', 'top':'0'});
	
	$('ul.nav > li > a').click(function(e) {
		$(this).parent().click();
		return false;
	});
	
	lis.click(function(e) {
		deploy(this);
	}).mouseenter(function() {
		var e = this;
		var t = setTimeout(function() {
			appear(e);
			}, 100);
		$(this).data('delay', t);
	}).mouseleave(function() {
		clearTimeout($(this).data('delay'));
		collapse(this);
	});	
	
	thu.mouseenter(function() {
		$('img:nth-child(2)',this).fadeOut(300);
	}).mouseleave(function() {
		$('img:nth-child(2)',this).fadeIn(300);
	}).click(function() {
		selName(this);
		return false;
	});	

	anam.mouseenter(function() {
		clearTimeout($('div.main').data('dsnam'));
		$('div.listAssocie').fadeIn(300);
	}).mouseleave(function() {
		var t = setTimeout( function() {
			$('div.listAssocie').fadeOut(300);
			}, 400);
		$('div.main').data('dsnam', t);	
	});
	
	snam.mouseenter(function() {
		clearTimeout($('div.main').data('dsnam'));
		$('div.listAssocie').fadeIn(300);
	}).mouseleave(function() {
		var t = setTimeout( function() {
			$('div.listAssocie').fadeOut(300);
			}, 400);
		$('div.main').data('dsnam', t);	
	});
	
	names.click(function() {
		selName(this,true);
		$('div.listAssocie').fadeOut(300);
		return false;
	});
	
	var chre = document.location.href;
	if (chre.substr(chre.lastIndexOf('/') + 1) == 'avocats') {
		var dets = $('div.corps a');
		dets.click(function() {
			displayDetails(this);
			return false;
		});
	}
	
	if (document.getElementById('contactForm')) {
		$('#contactForm').validationEngine();
		$('ul.onglets > li > a').click(function(e) {
			if ($('h2.ttLine').text() != this.text) {
				$('ul.onglets > li > a').css({'color':'#494840 !important'});
				$(this).css({'color':'#C50F50 !important'});
				var cc = $(this).parent().index() - 1;
				var cs = (cc == 1) ? 0:1;
				$('div.corps > div > div:eq('+cc+')').toggleClass('tpush');
				$('div.corps > div > div:eq('+cs+')').toggleClass('tpush');
				$('h2.ttLine').text(this.text);
				$('#office').attr('value',this.text)
			}
		})
		$('#subm').click(function(e) {
			doEmail($('#contactForm').validationEngine('validate'));
		})
	}
	
	function doEmail(s) {
		if(s) {
			$('#contactForm').submit();
		}
	}

	
	if ($('.hpush').length > 0) {
		var iteml = $('.vpush').length;
		var pushes = $('div.corps > div');
		var pages = Math.floor(pushes.length/iteml);
		pages += (pushes.length%iteml > 0) ? 1:0;
		$('div.corps').append('<p id="pagerLine" class="size4of6"></p>');
		for (i = 0; i < pages; i++) {
			$('#pagerLine').append('<div class="pager" id="'+i+'pager">'+(i+1)+'</div>');
		}
		$('#0pager').addClass('actpager');
		
		$('.pager').click(function(e) {
			if (!$(this).hasClass('actpager')) {
				$('.actpager').toggleClass('actpager');
				$(this).toggleClass('actpager');
				var nd = iteml * parseInt(this.id,10);
				$('.vpush').toggleClass('hpush').toggleClass('vpush');
				if (nd > 0) {
					$('div.corps > div:gt('+(nd-1)+'):lt('+(iteml)+')').toggleClass('vpush').toggleClass('hpush');
				} else {
					$('div.corps > div:lt('+(iteml)+')').toggleClass('vpush').toggleClass('hpush');
				}
			}
		})
	}
	
	var ihm, ipm;
	
	function createMapConteneur(a) {
		if (a) {
			$('body').append('<div id="' + a + '"></div>');
			ihm = $('h2.TpaddingLg').offset();
			$('#'+a).css({'position':'absolute','top':ihm.top+'px','left':'-11111px','opacity':'0','width':'631px','height':'260px','border':'1px solid #C3C3C3'});
		} else {
			$('body').append('<div id="mapCanvas"></div>');
			ipm = $('.corps img').offset();
			$('#mapCanvas').css({'position':'absolute','top':ipm.top+'px','left':'-11111px','opacity':'0','width':'473px','height':'404px','border':'1px solid #C3C3C3'});
		}
	}
	
	function doShmap(a) {
		if (a) {
			shmap = $('#'+a);
			var targ = (a == 'smap1') ? $('#mapParis'):$('#mapLyon');
		} else if ($('.showmapParis').length > 0) {
			shmap = $('.showmapParis');
			var targ = $('#mapCanvas');
		} else {
			shmap = $('.showmapLyon');
			var targ = $('#mapCanvas');
		}
		genMap(a);
		if (a) addClose(targ);
		shmap.click(function () {
			var pos = targ.position();
			if (pos.left < 0) {
				if (ipm) {
					targ.css({'left':(ipm.left+316)+'px'});
				} else {
					targ.css({'left':ihm.left+'px'});
				}
				targ.animate({ opacity: 1 }, 300);
				if (!a) $(this).text('Masquer plan')
			} else {
				if (!a) $(this).text('Voir plan');
				targ.animate({ opacity: 0 }, 300, function() {
					targ.css({'left':'-11111px'});
				});
			}
			return false;
		});
	}

	function addClose(a) {
		a.append('<div class="closeMap">Fermer le plan</div>');
		$('.closeMap').css({'position':'absolute','bottom':'-1px','left':'-1px','font-size':'12px','padding':'5px 10px 7px 10px','cursor':'pointer','color':'#FFFFFF','background':'#636363','z-index':'10000'});
		$('.closeMap').click(function () {
			a.animate({ opacity: 0 }, 300, function() {
				a.css({'left':'-11111px'});
			});
		});
	}
	
	function genMap(a) {
		var targ;
		if (a == 'smap1' || $('.showmapParis').length > 0) {
			centerPoint = new google.maps.LatLng(48.87209672724078, 2.3294878005981445);
			targ = (a == 'smap1') ? "mapParis":"mapCanvas";
		} else if (a == 'smap2' || $('.showmapLyon').length > 0) {
			centerPoint = new google.maps.LatLng(45.761723, 4.847114);
			targ = (a == 'smap2') ? "mapLyon":"mapCanvas";
		} else {
			centerPoint = new google.maps.LatLng(48.87209672724078, 2.3294878005981445);
			targ = "mapCanvas";
		}
		var mapOptions = { zoom: 16, center: centerPoint, mapTypeId: google.maps.MapTypeId.ROADMAP };
		var map = new google.maps.Map(document.getElementById(targ), mapOptions);
		var marker = new google.maps.Marker({
			position: centerPoint,
			title:"Fromond-Briens"
		});
		marker.setMap(map);
	}
	
	if ($('.showmapParis').length > 0) {	
		createMapConteneur();
		doShmap()
	} else if ($('.showmapLyon').length > 0) {
		createMapConteneur();
		doShmap()
	}
	
	
	if ($('.showmap').length > 0) {
		createMapConteneur('mapParis');
		doShmap('smap1')
		createMapConteneur('mapLyon');
		doShmap('smap2')
	}	
	
});

})(jQuery);


