(function($) {
    
    var W = this.window, D = W.document, DE = D.documentElement;
    if(!DE || !D.getElementById || !D.getElementsByTagName || !D.createElement) {
        return;
    }
    
    DE.id = 'js';

    /**
    * @section       Top Menu
    */
    var topMenu = {
        init: function() {
            $('#topMenu li').hover(function(){
                topMenu.openSubMenu($(this));
            }, function(){
                topMenu.closeSubMenu($(this));
            });
        },
        openSubMenu: function(item) {
            item.toggleClass('on');
            var subMenu = item.find('ul:first');
            if (subMenu.is(":hidden")) {
                subMenu.slideDown(100);
            }
        },
        closeSubMenu: function(item) {
            $(item).toggleClass('on');
            var subMenu = $(item).find('ul:first');
            subMenu.slideUp(100);
        }
    };
    
    /**
    * @section       Main Menu
    */
    var mainMenu = {
        init: function() {
            $('#mainMenu ul').each(function() {
                /* Largeur du ul égale au minimum à celle du li parent */
                var liParentWidth = $(this).parent().width();
                if (liParentWidth != 0) {
                    $(this).css('minWidth', liParentWidth);
                }
                /* Largeur du li enfant du ul égale à celle du ul parent */
                var ulParentWidth = $(this).width();
                if (ulParentWidth != 0) {
                    $(this).children().css('width', ulParentWidth);
                }
            });

            $('#mainMenu li').hover(function(){
                mainMenu.openSubMenu($(this));
            }, function(){
                mainMenu.closeSubMenu($(this));
            });
        },
        openSubMenu: function(item) {
            item.toggleClass('on');
            var subMenu = item.find('ul:first');
            if (subMenu.is(":hidden")) {
                //subMenu.slideDown(100);
                subMenu.show();
            }
        },
        closeSubMenu: function(item) {
            $(item).toggleClass('on');
            var subMenu = $(item).find('ul:first');
            //subMenu.slideUp(100);
            subMenu.hide();
        }
    };
    
    /**
    * @section       Forms fields default values
    */
    var formFieldsDefaultValues = {
        init: function() {
            if ($('#search').val() == '') {
                $('#search').val(l10n['searchKeywords']);
            }
            $('#search').focus( function(){
                if ($(this).val() == l10n['searchKeywords']) {
                    $(this).val('');
                }
            }); 
        }
    };
    
    /**
    * @section       Carousels
    */
    var carousels = {
        init: function(){
            box.dom('div.carousel').each(function(i, elm) {
                if (this.id == "partnersCarousel") {
                    box.ui('carousel').create({
                        element: elm,
                        horizontal: true,
                        display: 4, // nombre d'items affiche
                        duration:600, // temps des transitions (donc animation) 
                        paginate:true
                    });
                } else if (this.id == "referencesCarousel") {
                    box.ui('carousel').create({
                        element: elm,
                        horizontal: true,
                        display: 4, 
                        duration:600,
                        paginate:true 
                    });
                } else {
                    box.ui('carousel').create({
                        element: elm,
                        horizontal: true,
                        display: 1, 
                        duration:600
                    }); 
                }
            });
        }
    };
    
    /**
    * @section       Tabs
    */
    var tabs = {
        init: function(){
            if (window.location.hash.length) {
                $("#content .tabContent").hide();
                $(window.location.hash).show();
                $("#content .tabs a").each(function() {
                    if ($(this).attr('href') == window.location.hash) {
                        $("#content .tabs a").removeClass("on");
                        $(this).addClass("on");
                    }
                });
            }
            if ($("#content .tabs").length) {
                $("#content .tabs a").click(function() {
                    $("#content .tabs a").removeClass("on");
                    $(this).addClass("on");
                    $("#content .tabContent").hide();
                    $($(this).attr("href")).show();
                    return false;
                });
            }
            /* World map */
            if ($("#world").length) {
                $("#world map area").click(function() {
                    $("#content .tabContent").hide();
                    var area = $(this).attr("href");
                    $($(this).attr("href")).show();
                    $("#content .tabs a").each(function() {
                        if ($(this).attr('href') == area) {
                            $("#content .tabs a").removeClass("on");
                            $(this).addClass("on");
                        }
                    });
                    return false;
                });
            }
        }
    };
    
    /**
    * @section       Print button
    * @note          Open the print nav window on click
    */
    var print = {
        init: function() {
            if ($('a.print').length > 0) {
                $('a.print').click ( function () {
                    window.print();
                    return false;
                });
            }
        }
    };
    
    $(document).ready(function () {
        
        formFieldsDefaultValues.init();
        
        mainMenu.init();
        
        topMenu.init();
        
        carousels.init();
        
        tabs.init();
        
        print.init();
        
        //$("img").noContext();
		
		// home scrollable
		if ($('#wrapper').hasClass('home') && $('#scrollable').length) {
			box.ui('scroll').create({
				element: '#scrollable',
				buttons: false
			}); 
			window.setTimeout(function() {
				box.ui('scroll.example').compute();
			}, 200);
		}
        
    });
    
})(jQuery);

// jQuery Right-Click Plugin
//
// Version 1.01
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 20 December 2008
//
// Visit http://abeautifulsite.net/notebook/68 for more information
//
// Usage:
//
//		// Capture right click
//		$("#selector").rightClick( function(e) {
//			// Do something
//		});
//		
//		// Capture right mouse down
//		$("#selector").rightMouseDown( function(e) {
//			// Do something
//		});
//		
//		// Capture right mouseup
//		$("#selector").rightMouseUp( function(e) {
//			// Do something
//		});
//		
//		// Disable context menu on an element
//		$("#selector").noContext();
// 
// History:
//
//		1.01 - Updated (20 December 2008)
//		     - References to 'this' now work the same way as other jQuery plugins, thus
//		       the el parameter has been deprecated.  Use this or $(this) instead
//		     - The mouse event is now passed to the callback function
//		     - Changed license to GNU GPL
//
//		1.00 - Released (13 May 2008)
//
// License:
// 
// This plugin is dual-licensed under the GNU General Public License and the MIT License
// and is copyright 2008 A Beautiful Site, LLC. 
//
if(jQuery) (function(){
	
	$.extend($.fn, {
		
		rightClick: function(handler) {
			$(this).each( function() {
				$(this).mousedown( function(e) {
					var evt = e;
					$(this).mouseup( function() {
						$(this).unbind('mouseup');
						if( evt.button == 2 ) {
							handler.call( $(this), evt );
							return false;
						} else {
							return true;
						}
					});
				});
				$(this)[0].oncontextmenu = function() {
					return false;
				}
			});
			return $(this);
		},		
		
		rightMouseDown: function(handler) {
			$(this).each( function() {
				$(this).mousedown( function(e) {
					if( e.button == 2 ) {
						handler.call( $(this), e );
						return false;
					} else {
						return true;
					}
				});
				$(this)[0].oncontextmenu = function() {
					return false;
				}
			});
			return $(this);
		},
		
		rightMouseUp: function(handler) {
			$(this).each( function() {
				$(this).mouseup( function(e) {
					if( e.button == 2 ) {
						handler.call( $(this), e );
						return false;
					} else {
						return true;
					}
				});
				$(this)[0].oncontextmenu = function() {
					return false;
				}
			});
			return $(this);
		},
		
		noContext: function() {
			$(this).each( function() {
				$(this)[0].oncontextmenu = function() {
					return false;
				}
			});
			return $(this);
		}
		
	});
	
})(jQuery);	

