/**
 * @author florent global variables
 */
var hl = 825; // height limits : after that, we display large lightbox image
var hl2 = 1025; // height limits : after that, we display larger lightbox
				// image!!
var ns = null; // new size : resized images if window allow us to do so

try {
    if (searchFieldDefault == null) 
    	searchFieldDefault = 'Recherche';
} 
catch (e) {
	searchFieldDefault = 'Recherche';
}



$(document).ready(function() {

	// we declare the javascript activated!! party on! :-)
	$("body").addClass("jsOn");
	
	// we addapt the size of lightbox image so it is as big as possible.
	lightboxAdaptLargeImageSize();
	
	// init lightbox : here fancybox
	$("a.lightbox, a:has(img.lightbox)").fancybox();

	// init search
	$('#search input.text').focus().keydown(function() {
		if ( $(this).attr('value') == searchFieldDefault) {
			$(this).attr('value', '');
		}
	});

	
	/* products main page */
	if ($('#products-page').length>0) {
		for ( var i = 0; i < $('div.subpages-list.wvisual ul').length; i++) {
	
			/* creates the cycle */
			$('div.subpages-list.wvisual ul:eq(' + i + ')').parent().after('<div class="navigation"><a class="prev" id="prev'+i+'"><</a><span class="pager" id="pager'+i+'"/><a class="next" id="next'+i+'">></a></div>');		
			$('div.subpages-list.wvisual ul:eq(' + i + ')').cycle({
				timeout		: 6000, 
				pager		: '#pager'+i,
				next		: '#next'+i,
				prev		: '#prev'+i,
				delay		: -750*i
			});	
			
			/* adds click and hover on paragraph */
			/* tricks */ $('div.subpages-list.wvisual:eq(' + i + ') ul').parent().nextAll('p:first').addClass('');		
			$('div.subpages-list.wvisual:eq(' + i + ') ul').parent().nextAll('p:first').css({
				'cursor' : 'pointer'
			}).click(function(){
				window.location = $(this).prevAll('h2:first').find('a').attr('href');		
			}).hover(function(){
				$(this).addClass('selected');
			},function(){
				$(this).removeClass('selected');
			});
			/* synchronise title and paragraph */
			$('div.subpages-list.wvisual:eq(' + i + ') ul').parent().prevAll('h2:first').hover(function(){
				$(this).nextAll('p:first').addClass('selected');
			},function(){
				$(this).nextAll('p:first').removeClass('selected');
			});		
		}
	}
	
	/*
	 * subpages list whole link process
	 */
	$('.subpages-list.wdesc .item').css({
		'cursor' : 'pointer'
	}).click(function() {
		window.location = $(this).find('h2 a').attr('href');
	}).hover(function(){
		$(this).addClass('selected');
	},function(){
		$(this).removeClass('selected');
	});


	/*
	 * search results whole link process
	 */
	$('body.family-search #content ol li').css({
		'cursor' : 'pointer'
	}).click(function() {
		window.location = $(this).find('h2 a').attr('href');
	}).hover(function(){
		$(this).addClass('selected');
	},function(){
		$(this).removeClass('selected');
	});


	/*
	 * activate forms
	 */

	// displays a nice text in each field
	for (var i = 0; i < $('.family-products #content .formItem').length; i++) {
		var lab = $('.family-products #content .formItem:eq('+i+')').find('label').text();
		$('.family-products #content .formItem:eq('+i+')').find('.formField').val(lab);
	}
	
	/*

	// clear field if first click on email field
	var isFirst2 = true;
	$('.family-products #content .formItem.email .formField').keydown(function() {
		if (isFirst2) {
			$(this).attr('value', '');
			isFirst2 = false;
		}
	});

	// clear field if first click on email nom
	var isFirst3 = true;
	$('.family-products #content .formItem:eq("2") .formField').keydown(function() {
		if (isFirst3) {
			$(this).attr('value', '');
			isFirst3 = false;
		}
	});
	*/
	
	
	// faire ça pour le troisième et puis aussi vérifier en submit... 


	/*
	 * PAGES DISPLAY EFFECTS
	 */
	if (jQuery.support.opacity) {	 
		if ($('body.family-home').length>0) {
			// hide page items
			$('#body, #header, #localnav, #footer .designby').hide();
			$('#topnav').css( {
				'marginTop' : '-35px'
			});
			$('#content').css({'opacity': 0});		
			$('#footer').hide().css( {
				'marginTop' : '100px',
				'height' : '0'
			});
	
			// show everything bits by bits
			$('#body').fadeIn(10, function() {
				$("#header").slideDown(250, function() {
					$("#topnav").show().animate( {
						"marginTop" : "0"
					}, 350, function() {
						$("#localnav").slideDown();
						$('#content').fadeTo('normal', 1, function(){
							$("#footer").show().animate( {
								'marginTop' : '0',
								'height' : '100px'
							}, 500, function() {
								$("#footer .designby").fadeIn('slow');
							});
						});
					});
				});
			});					
		} else {
			$('#content, #localnav').css({'opacity': 0});		
			$('#content').fadeTo('normal', 1, function(){
				$('#localnav').fadeTo('slow', 1);
			});
		}
	} 
});

function lightboxAdaptLargeImageSize() {
	/*
	 * if window is tall enough, we change src of lightbox images so it adapts
	 * to large screens
	 */
	ns = 620;
	if ($(window).height() >= hl) {
		ns = 800;
		if ($(window).height() >= hl2) {
			ns = 1000;
		}
	}
	/*
	 * change src of lightbox image
	 */
	for ( var i = 0; i < $('a.lightbox').length; i++) {
		// lightboxImageSouce : /dynimages/620/files/images/photo.jpg : for
		// normal fckeditor text lightbox link
		// or :
		// /artistes/residents/patrick-chappert-gaujal/tableaux/chappert-rouge.jpg
		// : lightbox link from gallery
		// $('body').append('video? : '+$('a.lightbox:eq(' + i +
		// ')').parent().hasClass('video')+'<br/>');
		var lightboxImageSouce = $('a.lightbox:eq(' + i + ')').attr('href');
		if (lightboxImageSouce.indexOf('.com') > 0)
			lightboxImageSouce = lightboxImageSouce
					.substring(lightboxImageSouce.indexOf('.com') + 4);

		/*
		 * since videos are will handelded by the lightbox pluggin, we change
		 * links to the no-script video page for the real video source (.flv or
		 * .mov)
		 */
		if ($('a.lightbox:eq(' + i + ')').parent().hasClass('video')
				|| $('a.lightbox:eq(' + i + ')').attr('href').indexOf('.mov') > 0
				|| $('a.lightbox:eq(' + i + ')').attr('href').indexOf('.flv') > 0) {
			if (lightboxImageSouce.indexOf('/files/_galleries') < 0)
				lightboxImageSouce = '/files/_galleries' + lightboxImageSouce;
			$('a.lightbox:eq(' + i + ')').attr('href', lightboxImageSouce);
		} else {
			if (lightboxImageSouce.indexOf('.jpg') > 0
					|| lightboxImageSouce.indexOf('.JPG') > 0
					|| lightboxImageSouce.indexOf('.jpeg') > 0
					|| lightboxImageSouce.indexOf('.JPEG') > 0
					|| lightboxImageSouce.indexOf('.png') > 0
					|| lightboxImageSouce.indexOf('.gif') > 0) {
				if (lightboxImageSouce.indexOf('/dynimages/') == 0) {
					lightboxImageSouce = lightboxImageSouce
							.substring(lightboxImageSouce
									.indexOf('/dynimages/') + 11);
					lightboxImageSouce = lightboxImageSouce
							.substring(lightboxImageSouce.indexOf('/'));
				} else {
					// case of the gallery link to image page -> we call the
					// direct large image
					lightboxImageSouce = '/files/_galleries' + lightboxImageSouce;
				}
				// place new source for current lightbox link
				$('a.lightbox:eq(' + i + ')').attr('href',
						'/dynimages/' + ns + lightboxImageSouce);
			}
		}
	}
}
