///////////////////////////////////////OPEN IN NEW WINDOW

/**
 * ouovre le lien dans une nouvelle fenêtre
 *
 */
function openInNewWindow ( e ) {

    Event . stop ( e ) ;

    var href = Event . element ( e ) . href ;

    if ( href == undefined ) {

	href = Event . element ( e ) . up ( 'a' ) . href ;

    }

    window . open ( href ) ;
}

/**
 * initialise l'ouverture dans une autre fenêtre
 *
 */
function initOpenInNewWindow ( ) {

    $$ ( '.openInNewWindow' ) . each ( function  ( e ) {
	Event . observe ( $ ( e ) , 'click', openInNewWindow )
    } ) ;
}

///////////////////////////////////////FOCUS FIRST ELEMENT

/**
 * initFocusFirstElement
 */
function initFocusFirstElement ( ) {
 	
    var forms = document . getElementsByTagName ( 'form' ) ;
 	
    for ( var i = 0 ; i < forms . length ; i++) {
    
	forms [ i ] . focusFirstElement ( ) ;
    }
 	
}

///////////////////////////////////////SOUS MENUS GAUCHE

/**
 * observeEventMenu
 */
function observeEventMenu ( ) {
 	
    $$ ( '.leftMenuHead' ) . each ( function ( e ) {
 		
	if ( ! $ ( e ) . up ( 'li' )  . hasClassName ( 'active' ) ) {
 		
	    $ ( e ) . nextSiblings ( ) . each ( function ( el ) {
		$ ( el ) . hide ( )
	    } ) ;
	}
	 		
	Event . observe ( $ ( e ), 'mouseover', openSubMenu ) ;
    } ) ;

    Event . observe( $ ( 'principal' ), 'mouseover', closeAllSubMenus ) ;

    Event . observe( $ ( 'upMenuLinkMore' ), 'click', function ( event ) {
	Event . stop ( event )
    } ) ;
    
    Event . observe( $ ( 'upMenuLinkMore' ), 'mouseover', function ( ) {

	$ ( 'subUpMenuMoreUl' ) . show ( ) ;

	$ ( 'upMenuLinkMore' ) . up ( 'li' ) . siblings() . each (function (sibling){

	    Event . observe( $ ( sibling ) . down ('a') , 'mouseover', function ( ) {

		$ ( 'subUpMenuMoreUl' ) . hide ( ) ;
	    } ) ;
	});

//	Event . observe( $ ( 'principal' ) , 'mouseover', function ( ) {
//
//	    $ ( 'subUpMenuMoreUl' ) . hide ( ) ;
//	} ) ;
    } ) ;

}

/**
 * openSubMenu 
 * @param {event}  e  
 */
function openSubMenu ( e ) {
  
    //Event . stop ( e ) ;
  
    var link = Event . element ( e ) ;

    var ul = link . up ( 'li' ) . down ( 'ul' ) ;

    var left = link . positionedOffset ( ) . left + 135 ;
    var top = link . positionedOffset ( ) . top ;
  	
    if ( $ ( ul ) && ! $ ( ul ) . visible ( ) ) {
  	
	$ ( ul ) . setStyle ( {
	    position: 'absolute',
	    width: '240px',
	    left: left+'px',
	    top: top+'px',
	    zIndex: '1000',
	    padding : '3px 4px 3px 5px'
	} );
		
	//	new Effect . BlindDown ( $ ( ul ), {
	//	    duration : 0.3
	//	} ) ;

	$ ( ul ) . show () ;
  		
    }

    closeOtherSubMenus ( $ ( link ) . id ) ;
  	
}
  
/**
* closeOtherSubMenu
*/
function closeOtherSubMenus ( openId ) {
  	
    $$ ( '.leftMenuHead' ) . each ( function ( e ) {
 	 	
	if ( $ ( e ) . id != openId && $ ( e ) . siblings ( ) . length != 0 ) {
 		
	    $ ( e ) . up ( 'li' ) . down ( 'ul' ) . hide ( ) ;
	}
    } ) ;
}

/**
*closeAllSubMenus
*/
function closeAllSubMenus () {

    $$ ( '.leftMenuHead' ) . each ( function ( e ) {

	if ( $ ( e ) . up ( 'li' ) . down ( 'ul' ) ) {
    
	    $ ( e ) . up ( 'li' ) . down ( 'ul' ) . hide ( ) ;
	}
    } ) ;
}
 
/**
* observeEventWhoIsHover
*/
function observeEventWhoIsHover() {

    $$ ( '.menuLinkHover' ) . each ( function ( e ) {
	$ ( e ) . up ( 'fieldset' ) . addClassName ( 'fieldsetHover' ) ;
    } ) ;
}

///////////////////////////////////////CLEAR ON FOCUS

/**
*
*/
function initClearOnFocus ( ) {

    $$ ( '.clearOnFocus' ) . each ( function ( e ) {

	Event . observe ( $ ( e ), 'focus', clearOnFocus ) ;
    } )
}

/**
*
*/
function clearOnFocus ( e ) {

    var element = Event . element ( e ) ;

    $ ( element ) . clear ( ) ;
}

///////////////////////////////////////TEXTES DES DETAILS CATALOGUE

function initTextDetailsCatalog ( ) {

    $$ ( '.textDetailsItem' ) . each ( function ( el ) {

	Event . observe ( $ ( el ), 'click', textDetailsCatalog ) ;
    } );
}

function textDetailsCatalog ( el ) {

    Event . stop ( el ) ;

    var elementId = Event . element ( el ) . id ;
    var text = elementId .gsub ( 'menu-textDetailsId_', '' ) ;

    var productId = $F ( 'productId' );

    var data = $H ( {
	'module' : 'catalog',
	'controller' : 'ajax',
	'action' : 'ajaxtextdetailscatalog',
	'text' : text,
	'productId' : productId
    } ) . toQueryString ( ) ;

    new Ajax . Updater ( 'detailsTextDiv', 'ajaxtextdetailscatalog', {
	
	postBody: data,
	onCreate: function ( ) {
	    
	    $ ( 'ajaxIndicatorDiv' ) . show () ;
	},
	onSuccess: function (  ) {

	    $$ ( '.textDetailsItem' ) . each ( function ( el ) {

		$ ( el ) . removeClassName( 'active' ) ;
	    } );

	    $ ( elementId ) . addClassName( 'active' ) ;

	    $ ( 'ajaxIndicatorDiv' ) . hide () ;
	}
    } );
}

///////////////////////////////////////AJOUT AU PANIER

function initAddToCaddie () {

    if ( $ ( 'addToCaddieSubmit' ) ) {

	Event . observe( $ ( 'addToCaddieSubmit' ), 'click', function ( event ) {

	    Event . stop ( event ) ;
	    checkDispo ( null ) ;
	} ) ;
    }

    $$ ( '.addToCaddieButton' ) . each ( function ( el ) {

	var id = $ ( el ) . id .gsub ( 'addToCaddieSubmit' , '' ) ;

	Event . observe( $ ( el ), 'click', function ( event ) {
	    
	    Event . stop ( event ) ;
	    checkDispo ( id ) ;
	} ) ;
    } ) ;
}

function checkDispo ( id ) {

    //    var packagingId = $F ( 'addToCaddie' ) ;
    //    var quantity = $F ( 'quantity' ) ;

    var packagingId = $ ( 'addToCaddie' + id ) . value ;
    var quantity = $ ( 'quantity' + id ) . value ;

    var dataForCheckDispo = $H ( {
	'module' : 'catalog',
	'controller' : 'ajax',
	'action' : 'ajaxcheckdispo',
	'packagingId' : packagingId,
	'quantity' : quantity
    } ) . toQueryString ( ) ;

    if ( ! checkNumeric ( quantity, false ) || quantity == '' ) {

	$ ( 'addToCaddieMessage' + id ) . update ( 'Vous devez entrer un nombre entier !' ) ;
	new Effect . Pulsate ( 'addToCaddieMessage' + id ) ;
    }
    else {
	
	new Ajax . Request ( 'ajaxcheckdispo' , {
	    
	    postBody : dataForCheckDispo ,
	    onCreate : function () {

		$ ( 'ajaxIndicatorDiv' ) . show () ;
	    },
	    onSuccess : function ( requester ) {

		$ ( 'ajaxIndicatorDiv' ) . hide () ;

		var responseArray = eval ( requester . responseJSON ) ;

		if ( responseArray['response'] == 1 ) {

		    addToCaddie ( id ) ;
		}
		else {
		    
		    $ ( 'addToCaddieMessage' + id ) . update ( responseArray['text'] ) ;

		    Event . observe( $ ( 'confirmQuantityOk' ), 'click', function () { addToCaddie ( id ) } ) ;

		    Event . observe( $ ( 'confirmQuantityNo' ), 'click', function () {
			
			$ ( 'addToCaddieMessage' + id ) . update ( '' ) ;
		    } ) ;
		}
	    }
	} );
    }
}

function addToCaddie ( id ) {
    
    var addToCaddie = $ ( 'addToCaddie' + id ) . value ;
    var quantity = $ ( 'quantity' + id ) . value ;

    var data = $H ( {
	'module' : 'catalog',
	'controller' : 'ajax',
	'action' : 'ajaxaddtocaddie',
	'addToCaddie' : addToCaddie,
	'quantity' : quantity
    } ) . toQueryString ( ) ;

    new Ajax . Request ( 'ajaxaddtocaddie', {

	postBody : data,
	onCreate : function () {

	    $ ( 'ajaxIndicatorDiv' ) . show () ;
	},
	onSuccess : function ( requester ) {

	    var responseArray = eval ( requester . responseJSON ) ;

	    $ ( 'ajaxIndicatorDiv' ) . hide () ;
	    $ ( 'addToCaddieMessage' + id ) . update ( 'Le produit a été ajouté au panier' ) ;
	    new Effect . Pulsate ( 'addToCaddieMessage' + id ) ;

	    if ( $ ( 'totalInCaddie' ) . empty () ) {

		$ ( 'totalInCaddie' ) . update ( responseArray [ 'totalInCaddie' ] )
	    }

	    else {

		var totalInCaddie = parseInt ( $ ( 'totalInCaddie' ) . innerHTML . gsub ( '(', '' ) . gsub ( ')', '' ) ) ;

		totalInCaddie += parseInt ( quantity ) ;

		$ ( 'totalInCaddie' ) . update ( responseArray [ 'totalInCaddie' ] ) ;

		new Effect . Highlight ( 'totalInCaddie' , {
		    startcolor : '#ff006d' ,
		    duration : 3
		} ) ;
	    }
	}
    });
}

///////////////////////////////////////MISC

function checkNumeric ( variable, zeroAllowed ) {

    var pattern ;

    if ( false == zeroAllowed ) {

	pattern = '^[1-9][0-9]*$' ;
    } else {

	pattern = '^[0-9]+$' ;
    }

    var exp = new RegExp ( pattern , 'g' ) ;
    return exp . test ( variable ) ;
}

///////////////////////////////////////MISE À JOUR DES QUANTITÉS CADDIE

function initUpdateCaddieQuantity () {
    
    $$ ( '.caddieQuantitySubmit' ) . each ( function ( el ) {

	Event . observe( $ ( el ), 'click', updateCaddieQuantity ) ;
    } ) ;
}

function updateCaddieQuantity ( event ) {

    Event . stop ( event ) ;

    var caddieLine = Event . element ( event ) . id .gsub( 'quantitySubmit', '') ;

    var quantity = $F ( 'quantity' + caddieLine ) ;

    var data = $H ( {
	'module' : 'caddie',
	'controller' : 'ajax',
	'action' : 'ajaxupdatecaddiequantity',
	'caddieLine' : caddieLine,
	'quantity' : quantity
    } ) . toQueryString ( ) ;

    if ( ! checkNumeric ( quantity, false ) || quantity == '' ) {

	$ ( 'generalMessage' ) . update ( 'Vous devez entrer un nombre entier !' ) ;
    }
    else {

	$ ( 'generalMessage' ) . update ( '' ) ;

	new Ajax . Request ( 'ajaxupdatecaddiequantity', {

	    postBody : data,
	    onCreate : function () {

		$ ( 'ajaxIndicatorDiv' ) . show () ;
	    },
	    onSuccess : function ( requester ) {

		$ ( 'ajaxIndicatorDiv' ) . hide () ;

		var responseArray = eval ( requester . responseJSON ) ;

		$ ( 'amountCell_' + caddieLine ) . update ( responseArray [ 'newLineAmount' ] ) ;
		$ ( 'totalAmountCell' ) . update ( responseArray [ 'totalAmount' ] ) ;
		//		$ ( 'francoAlertFieldset' ) . update ( '<legend>Livraison offerte</legend>' + responseArray [ 'francoAlert' ] ) ;
		$ ( 'quantityAlertDiv' ) . update ( responseArray [ 'displayQuantityAlert' ] ) ;
		$ ( 'stockCaddieDiv' + caddieLine ) . update ( responseArray [ 'dispo' ] ) ;
		$ ( 'unitPriceCell_' + caddieLine ) . update ( responseArray [ 'price' ] ) ;
		$ ( 'totalInCaddie' ) . update ( responseArray [ 'totalInCaddie' ] ) ;

		new Effect . Highlight ( 'totalInCaddie' , {
		    startcolor : '#ff3300' ,
		    duration : 3
		} ) ;
		new Effect . Highlight ( 'amountCell_' + caddieLine , {
		    startcolor : '#ff3300' ,
		    duration : 1
		} ) ;
		new Effect . Highlight ( 'unitPriceCell_' + caddieLine , {
		    startcolor : '#ff3300' ,
		    duration : 1
		} ) ;
		new Effect . Highlight ( 'stockCaddieDiv' + caddieLine , {
		    startcolor : '#ff3300' ,
		    duration : 1
		} ) ;
		new Effect . Highlight ( 'totalAmountCell' , {
		    startcolor : '#ff3300' ,
		    duration : 1
		} ) ;
		new Effect . Highlight ( 'francoAlertFieldset' , {
		    startcolor : '#ff3300' ,
		    duration : 1
		} ) ;
	    }
	} ) ;
    }
}

///////////////////////////////////////IDENTIFICATION EN COLONNE

function initColumnLogin () {

    if ( $ ( 'loginColumnSubmit' ) ) {

	Event . observe ( $ ( 'loginColumnSubmit' ), 'click', columnLogin );
    }
}

function columnLogin ( event ) {
    
    Event . stop ( event ) ;

    var mail = $F ( 'loginColumnMail' ) ;
    var pass = $F ( 'loginColumnPass' ) ;

    var data = $H ( {
	'module' : 'default',
	'controller' : 'ajax',
	'action' : 'ajaxcolumnlogin',
	'mail' : mail,
	'pass' : pass
    } ) . toQueryString ( ) ;

    if ( mail == '' ) {

	$ ( 'generalMessage' ) . update ( 'Vous devez fournir une adresse email' ) ;
    }
    else if ( pass == '' ) {

	$ ( 'generalMessage' ) . update ( 'Vous devez fournir un mot de passe' ) ;
    }
    else {

	$ ( 'generalMessage' ) . update ( '' ) ;

	new Ajax . Request ( 'ajaxcolumnlogin', {

	    postBody : data,
	    onCreate : function () {

		$ ( 'ajaxIndicatorDiv' ) . show () ;
	    },
	    onComplete : function ( requester ) {

		$ ( 'ajaxIndicatorDiv' ) . hide () ;

		var responseArray = eval ( requester . responseJSON ) ;

		$ ( 'generalMessage' ) . update ( responseArray [ 'message' ] ) ;

		if ( responseArray [ 'code' ] == 1 ) {

		    $ ( 'loginFrameDiv' ) . update ( responseArray [ 'view' ] ) ;
		    
		    Event . observe ( $ ( 'wrongIdentityLink' ), 'click', columnLogout );
		}
	    }
	} ) ;
    }
}

function initColumnLogout () {

    if ( $ ( 'wrongIdentityLink' ) ) {

	Event . observe ( $ ( 'wrongIdentityLink' ), 'click', columnLogout );
    }
}

function columnLogout ( event ) {

    Event . stop ( event ) ;

    var data = $H ( {
	'module' : 'default',
	'controller' : 'ajax',
	'action' : 'ajaxcolumnlogout'
    } ) . toQueryString ( ) ;


    $ ( 'generalMessage' ) . update ( '' ) ;

    new Ajax . Request ( 'ajaxcolumnlogout', {

	postBody : data,
	onCreate : function () {

	    $ ( 'ajaxIndicatorDiv' ) . show () ;
	},
	onComplete : function ( requester ) {

	    $ ( 'ajaxIndicatorDiv' ) . hide () ;

	    var responseArray = eval ( requester . responseJSON ) ;

	    $ ( 'generalMessage' ) . update ( 'Vous êtes déconnecté' ) ;

	    $ ( 'loginFrameDiv' ) . update ( responseArray [ 'view' ] ) ;
	}
    } ) ;
}

///////////////////////////////////////CATÉGORIES NON CLIQUABLES

function initUnclickableCategories () {

    $$ ( '.leftMenuCatalog' ) . each ( function ( el ) {

	Event . observe( $ ( el ), 'click', unclickableCategories ) ;
    } );
}

function unclickableCategories ( event ) {

    Event . stop ( event ) ;
}

///////////////////////////////////////GRANDE IMAGE CATALOGUE

function initDisplayBigImage () {

    $$ ( '.enlargeLink' ) . each ( function ( el ) {

	Event . observe( $ ( el ), 'click', displayBigImage ) ;
    } ) ;
}

function displayBigImage ( el ) {

    Event . stop ( el ) ;

    var element = Event . element ( el ) ;

    var href = element . href ;

    $ ( 'bigCatalogImage' ) . update ( '<img alt="" src="' + href + '" />' ) ;

    new Effect . Appear ( 'blackCache' , {

	duration : 0.5,
	from : 0,
	to : 0.7,
	afterFinish : function () {

	    new Effect . Appear ( 'bigCatalogImageDiv' ) ;
	    
	    Event . observe ( $ ( 'bigCatalogImageClose' ), 'click', function () {
		
		new Effect . Fade ( 'bigCatalogImageDiv', {

		    duration : 0.5,
		    afterFinish : function () {

			$ ( 'bigCatalogImage' ) . update ( '' ) ;

			new Effect . Fade ( 'blackCache', {

			    duration: 0.5,
			    from: 0.7,
			    to: 0
			} ) ;
		    }
		} ) ;
	    } ) ;
	}
    } ) ;
}

///////////////////////////////////////OBSERVERS

function initNewsletterSubscription () {

    if ( $ ( 'newsletterSubmit' ) ) {

	Event . observe( $ ( 'newsletterSubmit' ) , 'click', newsletterSubscription ) ;
    }
}

function newsletterSubscription ( event ) {

    Event . stop ( event ) ;

    var mail = $F ( 'newsletterInput' ) ;

    var data = $H ( {
	'module' : 'default',
	'controller' : 'ajax',
	'action' : 'ajaxnewsletter',
	'mail' : mail
    } ) . toQueryString ( ) ;

    new Ajax . Request ( 'ajaxnewsletter', {

	postBody : data,
	onCreate : function () {

	    $ ( 'ajaxIndicatorDiv' ) . show () ;
	},
	onComplete : function ( requester ) {

	    $ ( 'ajaxIndicatorDiv' ) . hide () ;

	    var responseArray = eval ( requester . responseJSON ) ;

	    $ ( 'generalMessage' ) . update ( responseArray [ 'response' ] ) ;
	}
    } ) ;

}

///////////////////////////////////////SCROLLING HOMEPAGE IMAGES

function initNewComers () {

    if ( $ ( 'newComersDiv' ) && $ ( 'newComerImage_1' ) ) {

	var imagesArray = new Array () ;
	
	var i = 0 ;
	
	$$ ( '.newComerImage' ) . each ( function ( el ) {
	    
	    imagesArray [ i ] = $ ( el ) . getDimensions () . height ;
	    i = i + 1 ;
	} ) ;

	imagesArray . sort () ;
	
	var maxHeight = imagesArray . last () ;

	$ ( 'newComersDiv' ) . setStyle ( {
	    'height' : maxHeight + 'px'
	} ) ;

	var updater = new Ajax . PeriodicalUpdater ( 'scrollingHidden' , 'newComers' , {

	    method : 'post' ,

	    postBody : $H ( {
		'module' : 'default',
		'controller' : 'ajax',
		'action' : 'newComers',
		'displayed' : getDisplayedImage()
	    } ) . toQueryString ( ) ,

	    frequency : 4 ,

	    onSuccess : function (responders) {

		var toDisplay = responders . responseText ;

		if ( $ ( 'newComerImage_' + getDisplayedImage() ) ) {

		    new Effect . Fade ( 'newComerImage_' + getDisplayedImage () , {

			afterFinish : function () {

			    new Effect . Appear ( 'newComerImage_' + toDisplay ) ;
			}
		    } ) ;

		    new Effect . Fade ( 'newComerLegend_' + getDisplayedImage () , {

			afterFinish : function () {

			    new Effect . Appear ( 'newComerLegend_' + toDisplay ) ;
			}
		    } ) ;
		}
		else {

		    new Effect . Appear ( 'newComerImage_' + toDisplay ) ;
		    new Effect . Appear ( 'newComerLegend_' + toDisplay ) ;
		}

		$ ( 'scrollingHidden' ) . update ( toDisplay ) ;
	
		updater . options . postBody = $H ( {
		    'module' : 'default',
		    'controller' : 'ajax',
		    'action' : 'newComers',
		    'displayed' : getDisplayedImage ( )
		} ) . toQueryString ( ) ;
	    }
	});
    }
}

function getDisplayedImage() {

    var displayed=$ ( 'scrollingHidden' ) . innerHTML;

    return displayed;
}

///////////////////////////////////////HELP

function initHelpFieldsetToggle () {

    if ( $ ( 'helpPageDiv' ) ) {

	$$ ( '.helpTopic' ) . each ( function ( topic ) {

	    $ ( topic ) . hide () ;
	} );
	
	$$ ( '.clickableLegend' ) . each ( function ( legend ) {
	    
	    var id = $ ( legend ) . id . gsub ( 'helpLegend_' , '' ) ;

	    Event . observe( $ ( legend ), 'click', function () {

		new Effect . toggle ( 'helpTopic_' + id, 'blind' , {
		    duration : 0.5
		} ) ;
	    } )
	} );
    }
}

///////////////////////////////////////OBSERVERS

/**
* observateurs d'événements
*/
//Event . observe ( window, 'load', initFocusFirstElement ) ;
Event . observe ( window, 'load', observeEventWhoIsHover ) ;//mise en route du gestionnaire d'evenements qui est actif
Event . observe ( window, 'load', observeEventMenu ) ;//mise en route du gestionnaire d'evenements menu
Event . observe ( window, 'load', initClearOnFocus ) ;
Event . observe ( window, 'load', initTextDetailsCatalog ) ;
Event . observe ( window, 'load', initAddToCaddie ) ;
Event . observe ( window, 'load', initUpdateCaddieQuantity ) ;
Event . observe ( window, 'load', initColumnLogin ) ;
Event . observe ( window, 'load', initColumnLogout ) ;
Event . observe ( window, 'load', initUnclickableCategories ) ;
Event . observe ( window, 'load', initDisplayBigImage ) ;
Event . observe ( window, 'load', initOpenInNewWindow, false ) ;
Event . observe ( window, 'load', initNewsletterSubscription, false ) ;
Event . observe ( window, 'load', initHelpFieldsetToggle, false ) ;
Event . observe ( window, 'load', initNewComers, false ) ;
