/**
 * @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();
    $("a.lightbox, a:has(img.lightbox)").attr('rel', 'shadowbox[main]');
	Shadowbox.init({
	    handleUnsupported:  "link",
	    autoplayMovies:     true,
		players: ["img", "flv", "qt", "iframe"]
	});


	
	// init thumbnails5
    if (jQuery.support.opacity) {
//    	$("p.thumbnails5 a img, div.thumbnails5 a img").hide();
    	$("p.thumbnails5 a, div.thumbnails5 a").cycle();
    }

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

	
	
	/*
	 * 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');
	});



});

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);
			}
		}
	}
}
