var $ = jQuery.noConflict();

var gSubscribeSubmit = true;

$( window ).ready( function() {
	
	// Préchargement des images du menu en rollover
	for( var i = 1; i <= 5; i++ ) {
		var img = new Image();
		$( img ).load();
		$( img ).attr( 'src', gMenuItems[ i ].onSrc );
	}
	
	$( '#sidebar .level1' ).mouseenter( function() {
		if( $( '#' + $( this ).attr( 'id' ) + '_items' ).length > 0 ) {
			if( $( '#' + $( this ).attr( 'id' ) + '_items' ).css( 'display' ) != 'block' ) {
				$( '.level2_box' ).hide( 2 );
				$( '#' + $( this ).attr( 'id' ) + '_items' ).slideDown();
			}
		}
	} );	
} );

var gCurrentMenuItem = null;

$( window ).load( function() {
	$( '.menu_item' ).mouseenter( function() {

		$( '#sub' + $( this ).attr( 'id' ) ).width( $( this ).width() );
		
		if( gCurrentMenuItem != null ) {
			$( gCurrentMenuItem ).attr( 'src', gMenuItems[ $( gCurrentMenuItem ).attr( 'index' ) ].offSrc );
		}
		
		var gCurrentMenuItem = this;
		
		// Changement de la source de l'image au rollover
		$( this ).attr( 'src', gMenuItems[ $( this ).attr( 'index' ) ].onSrc );
		
		// On masque tous les soumenus sauf si le bon est affiché
		if( $( '#sub' + $( this ).attr( 'id' ) ).css( 'display' ) != 'block' ) {
			$( '.submenu_item' ).hide( 2 );
		
			// On place le sousmenu en hauteur
			$( '#sub' + $( this ).attr( 'id' ) ).css( 'top', ( $( this ).offset().top + $( this ).height() ) );

			// on place le sous menu en largeur
			/*
			if( ( $( this ).offset().left + $( '#sub' + $( this ).attr( 'id' ) ).width() ) < ( $( '#header_top' ).offset().left + $( '#header_top' ).width() ) ) {
				$( '#sub' + $( this ).attr( 'id' ) ).css( 'left', $( this ).offset().left );
			} else {
				$( '#sub' + $( this ).attr( 'id' ) ).css( 'left', ( $( this ).offset().left - ( $( '#sub' + $( this ).attr( 'id' ) ).width() - $( this ).width() ) ) );
			}
			*/
			$( '#sub' + $( this ).attr( 'id' ) ).css( 'left', $( this ).offset().left );
			
		}
			
		$( '#sub' + $( this ).attr( 'id' ) ).show( 2, function() {
			
			// On affiche la première recette s'il y en a
			$( this ).find( '.forward' ).hide();
			if( $( this ).find( '.forward' ).length > 0 ) {
				$( this ).find( '.submenu_container' ).width( $( this ).find( '.submenu_container' ).width() );
				$( this ).find( '.submenu_container' ).css( 'float', 'left' );
				
				$( this ).width( $( this ).find( '.submenu_container' ).outerWidth() + $( this ).find( '.forward_container' ).outerWidth() );
				
				$( this ).find( '.forward:first' ).show();
			}
			
			
			$( this ).mouseenter( function() {
				$( gCurrentMenuItem ).attr( 'src', gMenuItems[ $( gCurrentMenuItem ).attr( 'index' ) ].onSrc );
				$( this ).mouseleave( function() {
					$( this ).unbind( 'mouseleave' );
					$( gCurrentMenuItem ).attr( 'src', gMenuItems[ $( gCurrentMenuItem ).attr( 'index' ) ].offSrc );
					$( this ).hide( 2 )
				} );
			} );
		} );
	} );
	
	$( '.menu_item' ).mouseleave( function() {
		$( this ).attr( 'src', gMenuItems[ $( this ).attr( 'index' ) ].offSrc );
	} );
	
	$( '#header_menu' ).mouseleave( function() {
		$( '.submenu_item' ).hide( 2 );
	} );
	
	$( '.switchOnFocus' ).each( function() {
		if( $( this ).val() == '' ) {
			$( this ).val( $( this ).attr( 'rel' ) );
		} else if( $( this ).val() != $( this ).attr( 'rel' ) ) {
			$( this ).css( 'color', '#000000' );
		}
	} );
	
	$( '.switchOnFocus' ).focus( function() {
		$( this ).css( 'color', '#000000' );
		if( $( this ).val() == $( this ).attr( 'rel' ) ) {
			$( this ).val( '' );
		}
	} );
	
	$( '.switchOnFocus' ).blur( function() {
		if( $( this ).val() == '' ) {
			$( this ).css( 'color', '#a6a6a3' );
			$( this ).val( $( this ).attr( 'rel' ) );
		}
	} );
	
	$( '.recipe_menu' ).mouseenter( function() {
		var id = $( this ).attr( 'id' ).substr( $( this ).attr( 'id' ).indexOf( '_' ) );
		
		if( $( '#forward' + id ).length > 0 ) {
			$( '.forward' ).hide();
			$( '#forward' + id ).show();
		}
	} );
	
	$( '.testimonial_expander' ).click( function() {
		
		if( $( this ).parent().next( '.testimonial_expand' ).is( ':visible' ) ) {
			if( ! $( this ).parent().next( '.testimonial_expand' ).is( ':animated' ) ) {
				$( '.testimonial_expand' ).slideUp();
				$( '.up' ).removeClass( 'up' );
			}
		} else {
			$( '.testimonial_expand' ).not( $( this ).parent().next( '.testimonial_expand' ) ).slideUp();
			$( '.up' ).not( $( this ).find( 'div' ) ).removeClass( 'up' );
			
			$( this ).parent().next( '.testimonial_expand' ).slideDown();
			$( this ).find( 'div' ).addClass( 'up' );
		}
		
		return false;
	} );
	
} );

function displayFollowUs() {
	$( '#follow_us' ).slideToggle();
	$( '.submenu_item' ).hide( 2 );
}

function toggleFrmSelect( id, selected ) {
	if( typeof( selected ) == 'undefined' ) {
		if( $( '#' + id + '_list' ).css( 'display' ) != 'block' ) {
			$( '.frm_select_list' ).hide();
			var count = 0;
			$( '#' + id + '_list a' ).each( function() {
				if( $( this ).css( 'display' ) == 'block' ) {
					count += 1;
				}
			} );
			if( count < 4 ) {
				$( '.frm_select_list_scroll' ).height( count * 25 );
			} else {
				$( '.frm_select_list_scroll' ).height( 100 );
			}
			$( '#' + id + '_list' ).slideDown();
		} else {
			$( '#' + id + '_list' ).slideUp();
		}
	}
}

function frmSelectedValue( id, value, label, depend, selected ) {
	if( depend != null ) {
		$( '.' + depend ).hide();
		$( '.' + id + '_' + value ).show();
	}
	$( '#' + id ).val( value );
	$( '#' + id + '_value' ).html( label );
	$( '#' + id + '_value' ).css( 'color', '#000000' );
	toggleFrmSelect( id, selected );
}

function displayFacebookFeed( templateURI ) {
	$.ajax( {
	  url: templateURI + 'ajax.fb-feed.php',
	  dataType:  'json',
	  success: function( data ) {
	  	if( data.date ) {
	  		$( '#fb_home_feed_date' ).html( data.date );
	  	}
	  	
	  	if( data.msg ) {
	  		$( '#fb_home_feed_message' ).html( data.msg );
	  	}
	  	
	  	$( '#fb_home_feed_loader' ).hide();
	  }
	} );
}

function subscribeNewsletter( templateURI, idMail, defaultVal, extra ) {
	if( gSubscribeSubmit ) {
		gSubscribeSubmit = false;
		
		$.ajax( {
			url: templateURI + 'ajax.subscribe.php',
			type: 'POST',
			dataType: 'json',
			data: { subscriber: $( '#' + idMail ).val(), defaultVal: defaultVal },
			success: function( data ) {
				if( data.error ) {
					alert( data.error );
				} else {
					alert( data.msg );
					sendTag( ( typeof( extra ) != 'undefined' ? extra : '' ) + '/inscrivez-vous-confirmation' );
				}
				gSubscribeSubmit = true;
			}
		} );
	}
	
	return false;
}
