var rootImgPath = '/data/'
function portfolioCycle(){
	var initSize = $('.cycle').children('img:first').outerHeight() + "px"
	$('.cycle') 
		.after('<div id="cycleOutput">','<div id="cycleNav">') 
		.cycle({ 
			fx:     'fade', 
			speed:  'normal', 
			timeout: 0, 
			sync:0,
			speed:  'slow',
			fxDelay: 1000,
			height: initSize,
			pager:  '#cycleNav',
			before:  onBefore,
			between : switchFit,
    		after:   onAfter
		}).bind('click',function(){createFancy()});
}

function onBefore() {
	$('.diapo').animate({width:$(this).width()+6 })
	$('.cycle').animate({height: $(this).height()+2,width:$(this).width()+2 })} 
function switchFit() { 
}
function onAfter() { 
	$('#cycleOutput').html('<p>' + this.alt + '</p>'); 
}

function createFancy(){
	var origine = $('.cycle')
	var currentId = $('#cycleNav').find('.activeSlide')
	var id = $('#cycleNav a').index(currentId)

	var img = $('.cycle').find('img')
	var imgTab = new Array();
	img.each(function(i){
		var imgPath = $(this).attr('src');
		var imgURL = imgPath.substring(imgPath.lastIndexOf('/')+3,imgPath.length)
		var imgFullURL = rootImgPath + imgURL
		imgTab.push({'href':imgFullURL,'title':$(this).attr('alt')})
		})
	$.fancybox(imgTab,{'titleShow':true,'titlePosition':'inside','type' : 'image','transitionIn':'elastic', 'transitionOut':'elastic','orig':origine,'index':id}); 
}


function checkMail(){
	var el = $('#mel');
	var dom = $(el).attr('href').substring(11,$(el).attr('href').length);
	var title = $(el).attr('title');
	
	var mailtoLnk = "mailto:" + title + "@" + dom;
	$(el).attr('href',mailtoLnk);
	$(el).empty()
	$(el).append(title + "@" + dom)
	}
	
function initFancyBox(){	
	$("#mentions").fancybox({'titleShow':false});
	$("#credits").fancybox({'titleShow':false});
	}

var entrySelected
function initNav(){			   
	var locationTab = window.location.href.split("/")
	
	var location = "/"+locationTab[locationTab.length-1]
	
	var refLocation = locationTab[locationTab.length-2]
	if(refLocation == "reference") location ="/references-communication-comsurterre.php"
	if(location.indexOf("?")>0)location = "/clermont-paris-contact-comsurterre.php"
	
	var current = $('a[href$="'+location+'"]')
	if(current.length < 1 )clearNav();
	else{
	  current.parent().addClass("current");
	  current.addClass("current");
	}

	}
	
function clearNav(){
	if(!entrySelected){
		var nav = $('#nav')
		var emptyEntry = '<li class="current" style="position:absolute;left:3px;"></li>'
		$(emptyEntry).prependTo(nav)
		}
	}

function extendsLava(){
	var nav = $('#nav')
	var entry = $(nav).find('.current')
	if($(entry).length !=0 ){	
		var itemBg = '<li id="itemBg"><div class="left"></div></li>'
		$(itemBg).appendTo(nav)
		$('#itemBg').css({"left":$('.back').css('left'), "width" : $('.back').css('width')})
		}
	}
	
$(document).ready(function(){
	initNav()
	portfolioCycle()
	initFancyBox()
	if($(".contact").length !=0) checkMail()
			
			if(!browserIE6){
			 $(function() {
				$("#nav").lavaLamp({
					fx: "easeOutQuint",
					speed: 700
					});
				});
			}
			extendsLava()
	})
