$("document").ready (function () {
		
	
	
	$("#home_img").hide ();
	$("#home_img").fadeIn (3500, function () {
		
		setTimeout (function () {
			this.location.href = 'projets';
			
		}, 2000);
		
	});
	
	// DOM manipulation 
	// hide scrollbar
	$("#gallery").css ({
		'overflow' 	: 'hidden', 
		'height'	: 612}
	);
	// draw arrows
	//$("#main_container").prepend ("<div id='arrow_next' style='position:absolute; margin-left: 970px; margin-top:330px'>&gt;</div>\n");
	//$("#main_container").prepend ("<div id='arrow_prev' style='position:absolute; margin-left:-20px; margin-top:330px'>&lt;</div>\n");
	
	
	$("#gallery").prepend ("<div id='arrow_prev' style='background-image:url(/img/prev.png); border:solid black 0px; position:absolute; z-index:2; width:40px; height:615px;'><div id='prev_transp' style='position:absolute; top:-8px;  width:40px; height:606px'></div><div style='position:absolute; margin-left:0px; width:20px;  margin-top:292px;font-size:13px; text-align:center'>&lt;</div></div>\n");
	
	$("#gallery").prepend ("<div id='arrow_next' style='background-image:url(/img/next.png); border:solid black 0px; position:absolute; top:-8px; z-index:2; width:40px; height:606px; margin-left:920px;'><div id='next_transp' style='position:absolute;  width:40px; height:615px; '></div><div style='margin-left:20px;  position:absolute; width:20px; text-align:center; margin-top:292px;font-size:13px; '>&nbsp;&gt;</div></div>\n");
	
	href = this.location.href;
	if (href.indexOf ('/projets/') != -1 || href.indexOf ('/about_ar') != -1 || href.indexOf ('/contact') != -1)
	{
		$("#gallery").after ("<div id='caption'><div>");
	}
	
	
	

	$('#prev_transp').css('opacity', 0.65);
	$('#next_transp').css ('opacity', 0.65);

	// init the system 
	$.initSlides 			();
	$.initNouvelles 		();
	$.initProjetsMenus 		();		

	endUrl = "";
	if ($("#back")!= undefined) {

		if ($("#back").attr ('href') != null)
			endUrl = $("#back").attr ('href');
	
		if (window.page_calling != undefined && window.page_calling == 'news') 
			endUrl = '/news';
	}

	// 2. the functions to dispatch
	options = {
		nextFn 	: function () {$.slider.next ()},
		prevFn 	: function () {$.slider.prev ()}, 
		nextEl 	: $('#arrow_next'),
		prevEl 	: $('#arrow_prev'), 
		counter : $.slider, 
		endUrl	: endUrl,
		margin 	: 47
		
	}
	
	// 3. the jQuery plugin magic!
	if ($.slider != null) {
		
		$('#gallery').navigation (options);
	}

	else {
		$("#arrow_next").hide ();
		$("#arrow_prev").hide ();
	}
})


function selectNum (id) {
	$("#counter").find ('a').removeClass ('selected');
	$("#"+id).addClass ('selected');
}


// little tool
function updateCounter () {
    if ($.slider != null) {
		crtNum = $.slider.getCrtNum ();
		count = $.slider.getCount ();	
		
		str = "";
				
		for (i=0; i<count; i++) {
			dNum = i+1;
			str += "<a id='goto_"+i+"' href='#' onclick='$.slider.goto ("+i+"); selectNum(\"goto_"+i+"\"); return false'>&nbsp;&nbsp;"+dNum+"</a>";
		}
		
		$("#counter").html (str);	
		$("#goto_"+crtNum).addClass ('selected');
	}	
}

function updateArrows () {

	updateCounter ();

	if ($.slider != null) {
		if (!$.slider.isNext ()) {
			//$("#arrow_next").html ('&nbsp;');
			$("#next").css ('opacity', 0.4);
		}
		else {
			//$("#arrow_next").html ('&gt;');	
			$("#next").css ('opacity', 1);
		} 
		
		if (!$.slider.isPrev ()) {
			//$("#arrow_prev").html ('&nbsp;');
			$("#prev").css ('opacity', 0.4);
		}
		else { 
			//$("#arrow_prev").html ('&lt;');
			$("#prev").css ('opacity', 1);
		}
	}
}



// Loop through DOM and find elements with 'slide' class to build slide list
jQuery.initSlides = function () {
	var countSlides = $(".slide").length;
	
	var w = $('#gallery_content > table').width();
	
	//if (countSlides > 0 && w > 960) {
	if (countSlides > 0 && w > 0) {
	
		var slides = new Array ();
		coords = 0;
		i = 0;
		
		$(".slide").each (function () {
			// remove the eventual margin-left of elements
			margin_left = parseFloat($(this).css ('margin-left').replace("px", ""));
			margin_left_div = parseFloat($(this).find ('div').css('margin-left').replace ('px', ''));
			
			if (isNaN(margin_left)) 	{margin_left = 0;}
			if (isNaN(margin_left_div)) {margin_left_div = 0;}
						
			coordsMargin = coords + margin_left + margin_left_div;

			slides [i] = new Slide (coordsMargin, $("#caption > div"), $(this).find ('.slide_caption'));
			coords += $(this).width ();
			i ++;
		})
		$.slider = new Slider (slides, $("#gallery_content"), function () {updateArrows ();});
		
		//$("#nav").prepend ("<div class='gallery_nav'><span id='left'><a href='' onclick='return false' id='prev'><</a></span> <span id='counter'>1/10</span> <span id='right'><a href='' onclick='return false' id='next'>></a></span></div>");
		$("#nav").prepend ("<div class='gallery_nav'></span> <span id='counter'>1/10</span></div>");
		
		updateCounter ();
		updateArrows ();

		// set event listeners
		$("#next").click (function () {
			$.slider.next ();
			updateCounter ();
		})
		$("#prev").click (function () {
			$.slider.prev ();
			updateCounter ();
		})
		
	}
}


// loop through DOM and init nouvelles list with '.nouvelle' elements
jQuery.initNouvelles = function () {
	var countNouvelles = $('.nouvelle').length;
	if (countNouvelles > 0) {
		
		var nouvelles = new Array ();
		
		i = 0;
		
		$('.nouvelle_infos').each (function () {
			
			h = $(this).height();
			
			hCoord = $("#gallery").height () - h + 3;
			
			$(this).css ('margin-top', hCoord);
			
		})
		
		
		// $('.nouvelle').each (function () {
		// 			nouvelles [i] = new Nouvelle ($(this).find('a'), $(this).find ('.texte_nouvelle'));
		// 			i++;
		// 		})
	}
}



// loop through DOM and init nouvelles list with '.nouvelle' elements
jQuery.initProjetsMenus = function () {
	var countProjets = $('.projet_menu_item').length;
	if (countProjets > 0) {
		
		var projets = new Array ();
		
		i = 0;
		$('.projet_menu_item').each (function () {
			projets [i] = new Projet ($(this).find ('a'), $(this).find ('.projet_caption'), $("#title"));
			i++;
		})
	}
}



