(function($){

	$(function() {

/*
 * @section Coins ronds dans IE
******************************************************************************/

		if ( $.browser.msie ) {
			$('#utilitaires ul').corner('bottom round 8px')
			$('#nav li').corner('round 7px')
			$('.template-accueil .colonnes .col3 ul').corner('round 3px cc:#E1E3DE')
			$('.menu-pages ul li a, body.template-statique #sidebar .boutons ul li a, .template-tarifs-tableau th .bouton a').corner('round 5px');
			$('body.template-statique #data .contentlist .nav a').corner('left 4px');
			$('body.template-compte-login .bouton a').corner('round 5px');
		}

/*
 * @section Bouton "imprimer"
******************************************************************************/
		$('.imprimer, .imprimer a').click(function() {
			window.print();
			return false;
		});

/*
 * @section Bouton "mon compte"
******************************************************************************/
		/*
		$('#acces .compte > a').click(function() {
			var $li = $(this).parent();
			var a = $li.children('a')[0];
			$li.children('div').css( 'top', a.offsetHeight );
			$li.toggleClass('active');
			if ( $li.is('.active') ) {
				$li.find('li').eq(0).find('input').each(function() {
					this.focus();
				});
			}
			/*
			var $li = $(this).parent();
			var $div = $li.children('div');
			if ( !$li.is('.active') ) {
				$div.hide();
				$li.addClass('active');
				$div.slideDown(300);
			} else {
				$div.slideUp(300, function() {
					$li.removeClass('active');
				});
			}
			
			return false;
		});
		*/
/*
 * @section Catalogue, grille à cellules égales
******************************************************************************/
		$('body.template-catalogue-complet .grid li:even').each(function() {
			var $this = $(this);
			var $next = $this.next();
			if ( $next.length ) {
				var h = $this.height();
				var h2 = $next.height();
				if ( h > h2 ) {
					var newh = h+'px';
					if ( $.browser.msie && $.browser.version == 6 ) {
						$next.css({'height': newh}).find('a').css({'height': newh});
					}
					$next.css({'min-height': newh}).find('a').css({'min-height': newh});
				}
			}
		}).css('clear', 'both').addClass('impair');
		
		$('body.template-catalogue-complet .grid li:odd').each(function() {
			var $this = $(this);
			var $prev = $this.prev();
			if ( $prev.length ) {
				var h = $this.height();
				var h2 = $prev.height();
				if ( h > h2 ) {
					var newh = h+'px';
					if ( $.browser.msie && $.browser.version == 6 ) {
						$prev.css({'height': newh}).find('a').css({'height': newh});
					}
					$prev.css({'min-height': newh}).find('a').css({'min-height': newh});
				}
			}
		});

/*
 * @section Catalogue, résultats
******************************************************************************/

		$('.template-catalogue-resultats table tr td').click(function() {
			var href = $(this).parent().find('td.app a').attr('href');
			window.location = href;
		});

/*
 * @section Catalogue, zoom image
******************************************************************************/

		var zoomImage = {
			init : function() {
				$('body').append('<div id="overlay"><div id="overlay-fg"><ul class="fg-nav"><li class="prev"><a href="javascript://">Image précédente</a></li><li class="next"><a href="javascript://">Image suivante</a></li></ul><div class="fg-inner groupe"><p class="fermer"><a href="javascript://"><img src="/img/interface/btn-catalogue-overlay-fermer.gif" alt="Fermer" /></a></p><h1></h1><div class="image"></div><div class="text"></div></div></div><div id="overlay-bg"></div></div>');
				$('#overlay-bg').css('opacity', 0.85);
				$('.images .viewport a').click(function() {
					zoomImage.show( $(this).parent().parent() );
					return false;
				});
				$('#overlay-fg .fermer a').click(function() {
					zoomImage.close();
				});

				$(document).keyup(function(e){
					keycode = (e == null) ? event.keyCode : e.which;
					if (keycode == 27 && $('#overlay').is(':visible')) { // close
						zoomImage.close();
					}
				});


			},
			close : function() {
				$('#overlay').hide();
			},
			show : function( $li ) {
				$('#overlay-fg').show();

				var preload = new Image();
				var src = $li.find('.thumb a').attr('href');
				preload.onload = function() {
					preload.onload = null;
					$('#overlay-fg').find('.image').html('<img src="'+src+'" alt="" />');
					$('#overlay-fg h1').html( $li.find('h3').html() );
					$('#overlay-fg .text').html( $li.find('.text').html() );

					if ( $.browser.msie && $.browser.version == 6 ) {
						window.scrollTo(0,1);
					}

					$('#overlay:hidden').show().find('#overlay-fg').css('top', ($(window).scrollTop() + 60)+'px') ;

					if ( $.browser.msie && $.browser.version == 6 && window.DD_belatedPNG ) {
						DD_belatedPNG.fix('body.template-catalogue-fiche #overlay ul.fg-nav a');
					}


				}
				preload.src = src;

				$('#overlay-fg .fg-nav a').unbind('click');
				if ( $li.next().length ) {
					$('#overlay-fg .fg-nav .next a').removeClass('inactive').click(function() {
						zoomImage.show( $li.next() );
					});
				} else {
					$('#overlay-fg .fg-nav .next a').addClass('inactive');
				}
				if ( $li.prev().length ) {
					$('#overlay-fg .fg-nav .prev a').removeClass('inactive').click(function() {
						zoomImage.show( $li.prev() );
					});
				} else {
					$('#overlay-fg .fg-nav .prev a').addClass('inactive');
				}

			}
		}

		if ( $('.template-catalogue-fiche').length ) {
			zoomImage.init();
		}

/*
 * @section Démos accueil
******************************************************************************/

		var ixOverlay = {

			callback : {
				init : function(){},
				show : function(){},
				hide : function(){}
			},

			init : function( html, callbacks ) {
				ixOverlay.callbacks = callbacks;
				$('body').append('<div id="overlay"><div id="overlay-fg">'+html+'</div><div id="overlay-bg"></div></div>');
				$('#overlay-bg').css('opacity', 0.85);
				$(document).keyup(function(e){
					keycode = (e == null) ? event.keyCode : e.which;
					if (keycode == 27 && $('#overlay').is(':visible')) { // close
						ixOverlay.hide( );
					}
				});
				$('#overlay .fermer a').click(function() {
					ixOverlay.hide();
				})
				if ( this.callbacks.init ) { this.callbacks.init(); }
			},

			show : function() {
				$('#overlay').show();
				if ( $.browser.msie && $.browser.version <= 6 ) { window.scrollTo(0,1); }
				$('#overlay-fg').css('top', ($(window).scrollTop() + 60)+'px') ;
				if ( this.callbacks.show ) { this.callbacks.show(); }
			},

			hide : function() {
				$('#overlay').hide();
				if ( this.callbacks.hide ) { this.callbacks.hide(); }
			},

			toggle : function() {
				if ( $('#overlay').is(':visible') ) {
					this.hide();
				} else {
					this.show();
				}
			}

		};

		function afficherVideo( e, swf, largeur, hauteur ) {
			var swf = '/swf/'+swf;
			e.html( '<object type="application/x-shockwave-flash" data="'+swf+'" width="'+largeur+'" height="'+hauteur+'"><param name="movie" value="'+swf+'" /></object>' );
		}


		if ( $('body').is('.template-accueil') ) {
			var html = '<div class="fermer"><a href="javascript://">Fermer</a></div><div class="videos"><div class="video" style="display: none;"></div><div class="video1"><a href=""><strong><em>Démo</em> Express <span>(1 minute)</span></strong></a></div><div class="video2"><a href=""><strong><em>Démo</em> Détaillée <span>(5 minutes)</span></strong></a><div></div></div></div>';
			ixOverlay.init( html,
				{
					init : function() {
								$('#overlay .videos a').click(function() {
									$('#overlay .video').show().siblings().hide();
									if ( $(this).parent().is('.video1') ) {
										afficherVideo( $('#overlay .video'), 'demo-express.swf', 900, 500 );
									} else {
										afficherVideo( $('#overlay .video'), 'demo-detaillee.swf', 900, 500 );
									}
									return false;
								});
							},
					hide : function() {
						$('#overlay .video').html('').hide().siblings().show();
					}

				}
			);

			$('li.demo a').click(function() {
				ixOverlay.show();
				return false;
			});

		}

		if ( $('#sidebar .video').length ) {
			var html = '<div class="fermer"><a href="javascript://">Fermer</a></div><div class="video"></div>';
			ixOverlay.init( html,
				{
					init : function() {
						$('#overlay').addClass('video');
					},
					show : function() {
						afficherVideo( $('#overlay .video'), 'demo-express.swf', 900, 500 );
						$('#overlay .video').show();
					},
					hide : function() {
						$('#overlay .video').html('').hide().siblings().show();
					}

				}
			);

			$('#contenu div.video a').click(function() {
				ixOverlay.show();
				return false;
			});

		}


/*
 * @section Catalogue, slide images
******************************************************************************/

		var imagesPosition = 0;
		$('.images .nav a').click(function() {
			if ( $(this).parent().is('.inactive') ) { return false; }
			if ( $(this).parent().is('.next') ) {
				imagesPosition++;
				imagesNaviguer();
			} else {
				imagesPosition--;
				imagesNaviguer(-1);
			}
		});

		function imagesNaviguer( ) {
			var newleft = imagesPosition * -88;
			$('.images .viewport ul').stop().animate({
				left: newleft+'px'
			},{
				duration: 400
			});
			imagesUpdateNav();
		}

		function imagesUpdateNav() {
			if ( imagesPosition >= $('.images .viewport ul li').length-3 ) {
				$('.images .nav li.next').addClass('inactive').css('opacity', 0.5);
			} else {
				$('.images .nav li.next').removeClass('inactive').css('opacity', 1);
			}

			if ( imagesPosition == 0 ) {
				$('.images .nav li.prev').addClass('inactive').css('opacity', 0.5);
			} else {
				$('.images .nav li.prev').removeClass('inactive').css('opacity', 1);
			}
		}
		imagesUpdateNav();

/*
 * @section Sélecteur d'image
******************************************************************************/

	if ( $('div.visualisateur-images').length ) {

		$('div.visualisateur-images').each(function() {

			var $self = $(this);
			updateEvents();

			$(this).data('position', 0);
		
			var $p = $(this).find('p');

			$(this).find('ul a').click(function() {

				$self.data('position', $(this).parent().parent().find('li').index( this.parentNode ) );

				var meta = $.metadata.get(this);
				var medium = meta.medium;
				var large = $(this).attr('href');
				var preload = new Image;
				var $this = $(this);
				preload.onload = function() {
					preload.onload = null;
					$p.find('a').remove();
					
					$this.parent().parent().find('li').each(function() {
						if ( $(this).find('a').attr('href') == large ) {
							$p.append('<a rel="group" style="width: '+preload.width+'px; height: '+preload.height+'px; margin: 0 auto;" href="'+large+'"><span></span><img src="'+medium+'" alt="" /></a>');
						} else {
							$p.append('<a href="'+$(this).find('a').attr('href')+'" rel="group"></a>');
						}
					})
					
					updateNav();
					updateEvents();
				};
				preload.src = medium;
				return false;
			});

			function updateNav() {
				$self.find('li').eq( $self.data('position') ).css('opacity', 1).siblings().css('opacity', 0.3);
			}

			function updateEvents() {
				$self.find('p a').fancybox({
					overlayOpacity : 0.85,
					'zoomSpeedIn': 300,
					'zoomSpeedOut': 300,
					'padding' : 0
				});
			}

			updateNav();

		});


	}


/*
 * @section Listes de contenu
******************************************************************************/

		$('.contentlist').addClass('contentlist-etroit');
		var $contentlist_nav = $('<ul class="nav"></ul>');
		var contentlist_compteur = 0;
		$('.contentlist .items .item').each(function() {
			$contentlist_nav.append('<li'+(contentlist_compteur == 0?' class="active"':'')+'><a href="#'+$(this).attr('id')+'">'+$(this).find('h3').html()+'</a></li>');
			contentlist_compteur++;
		}).eq(0).siblings().hide();

		$('.contentlist').prepend( $contentlist_nav );
		$('.contentlist .nav a').click(function() {
			var hash = $(this).attr('href').replace(/^.+#/,'#');
			$( hash ).show().siblings().hide();
			$(this).parent().addClass('active').siblings().removeClass('active');
			
			if ( $.browser.msie && $.browser.version <= 6 ) {
				$('.lezard-outer').html('');
				$('.lezard-outer').prepend('<div id="lezard-large" class="'+hash.replace(/#/,'')+'"></div>');
				//alert( $('#lezard-large').attr('class') );
				//var node = $('#lezard-large');
				//DD_belatedPNG.fix( node[0] );
			} else {
				$('#lezard-large').attr('class', hash.replace(/#/,'') );
			}
			
			return false;
		});

/*
 * @section Formulaire d'adhésion
******************************************************************************/

		$('body.template-formulaire').find('input, select').focus(function() {
			$(this).addClass('active').parents('fieldset').addClass('active');
		}).blur(function() {
			$(this).removeClass('active').parents('fieldset').removeClass('active');
		}).end().find('.convention div').hide().prev().find('a').click(function() {
			$(this).parent().next().slideToggle();
		}).parent().next().find('a').click(function() {

			$(this).parent().parent().slideUp().prev().find('input').attr('checked', 'checked');
		});
	});

})(jQuery);