( function () {
    if( document.getElementById && document.createElement ) {
        var h= document.getElementsByTagName("html")[0];
        h.className= (h.className+ ' js');
    }
} )();

BOX.setConfig('scroll', {
    container: '<div class="scrollContent">%content%</div>',
    simpleBar: '<div class="scrollBar" style="visibility:hidden;"><span class="face"></span></div>',
    fullBar: '<div class="scrollBar" style="visibility:hidden;"><span class="up"></span><span class="face"></span><span class="down"></span></div>'
}).setConfig('fauxFields', {
    checked: 'checked',
    focus: 'focus',
    checkedFocus: 'checkedFocus',
    selected: 'selected',
    hover: 'hover'
}).setConfig('fauxSelect', {
    container: '<div id="fauxSelect" style="left:-10000px;"></div>',
    mask: '<div id="fauxSelectMask" style="display:none;"></div>'
});

( function($) {
	var W = self;
   
    /* font-resizing ******************************************************** */
    var INCREASE= {
        "x-large":"x-large",
        "large":"x-large",
        "normal":"large",
        "small":"normal",
        "x-small":"small"
    }
    var DECREASE= {
        "x-large":"large",
        "large":"normal",
        "normal":"normal",
        "small":"normal",
        "x-small":"normal"
    }
    function changeFontSizeTo(dest){
        $("#content").get(0).className= dest;
		if($("#info").size()){
		    $("#info").get(0).className= dest;
		}
    }
    function changeFontSize(e, direction){
        var future= direction[$("#content").get(0).className||"normal"];
        changeFontSizeTo(future);
        e.preventDefault();
        e.stopPropagation();
    }
    function initFontResizing(){
        $("#increaseFontSize").click(function(e){
            changeFontSize(e, INCREASE);
            updateColumnsScrollSize();
        });
        $("#decreaseFontSize").click(function(e){
            changeFontSize(e, DECREASE)
            updateColumnsScrollSize();
        });
    }
    
    var popinOpen = false;
    function initPopinError(){
        $("#countries dl dt a[href*='#']").click(function() {
            if ( popinOpen == false ) {
                $('.toScroll').css('overflow', 'visible');
                $(this).parents('dl').find('dd').css('display', 'block');
                popinOpen = true;
            } else {
                $('dd').css('display', 'none');
                $(this).parents('dl').find('dd').css('display', 'block');
                popinOpen = true;
            }
            return false;
        });
        $('.close').click(function() {
            $(this).parents('dd').css('display', 'none');
            $('.toScroll').css('overflow', 'hidden');
            popinOpen = false;
        });
    }
    
    function writeFlash(){
        if($('#mainContent.howto #player').size()) {
            var fo = new FlashObject("../swf/player.swf", "player", "259", "207", "7", "#FFFFFF");

            fo.addParam("wmode", "transparent");
            fo.addParam("scale", "showall");
            fo.addParam("allowFullScreen","true");
            fo.addVariable("autoStart", false);
            fo.addVariable("loading_txt", "Chargement...");
            fo.addVariable("url_flv", "nutricia.flv");
            
            
            fo.setAttribute("redirectUrl","http://www.macromedia.com");
            fo.setAttribute("quality","high");
            fo.write("player");
        }
    }
    
    /* (;_;) un petit coup d'AOP... ***************************************** */
    function parasiteBOX () {
        if( !( BOX.SimpleScroll.prototype.old_initialize ) ) {
            BOX.SimpleScroll.prototype.old_initialize= BOX.SimpleScroll.prototype.initialize;
            BOX.SimpleScroll.prototype.initialize= function (datas) {
                this.old_initialize(datas);
                
                var that= this;
                var thread;
                function scrollParasited( that, e ) {
                    that.clickToPosition(e, $(that.sContainer).parent());
                }
                this.sContainer.mousedown(function(e) {
                    thread= window.setInterval(function(){
                        scrollParasited(that, e);
                    }, 100);
                });
                this.sContainer.mouseup(function(e) {
                    window.clearInterval(thread);
                });
            }
        }
        if( !( BOX.SimpleScroll.prototype.old_getContentOffset ) ){
            BOX.SimpleScroll.prototype.old_getContentOffset= BOX.SimpleScroll.prototype.getContentOffset;
            BOX.SimpleScroll.prototype.getContentOffset= function () {
                var t= this.old_getContentOffset();
                if( isNaN( t ) ){
                    return 0;
                }
                return t;
            }
        }
    }
    
    var scrolls2= [];
    window.scrolls2 = scrolls2;
    function initColumnScrolling () {		
		function appliScroll (O_objet) {
			if( O_objet.size() ) {
				if(O_objet.attr('id') == "info"){
					O_objet.wrapInner('<div class="toScroll" style="height:195px;"></div>');
				}
				else{
					O_objet.wrapInner('<div class="toScroll" style="height:'+O_objet.height()+'px;"></div>');
				}
	            O_objet.find(" > div.toScroll").each(
	                function (i, elm) {
	                    scrolls2[scrolls2.length]= new BOX.SimpleScroll({
	                        "target": this,
	                        "buttons": true
	                    });
	                    
	                    var I_hScrollbar = $(elm).height() - 20;
						$('.scrollBar', elm).css('height', I_hScrollbar+'px');
						
						scrolls2[scrolls2.length - 1].compute();
	                }
	            );
	        }
		}
		appliScroll($('#info'));
		appliScroll($('#countries'));
		appliScroll($('#mission .wrapper'));
    }
    function updateColumnsScrollSize () {
        $.each(scrolls2, function () {
           this.compute(); 
        });
    }
    
    function openWindow ( params ) {
        var t= "";
        t+= ( 'left='+params.left );
        t+= ( ',top='+params.top );
        t+= ( ',width='+params.width );
        t+= ( ',height='+params.height );
        t+= ( ',toolbar='+params.toolbar );
        t+= ( ',resizable='+params.resizable );
        t+= ( ',status='+params.status );
        t+= ( ',scrollbars='+params.scrollbars );
        
        window.open(
            params.url, 
            params.name,
            t
        );
    }
    
    function initPopupCredits () {
        var t= $("#credits");
        if( t.size() ){
            var params= {
                url: t.get(0).href,
                name: 'creditsWindow',
                width: 500,
                height: 500,
                toolbar: 0,
                resizable: 1,
                status: 0,
                scrollbars: 1
            }
            params.left= (screen.width- params.width)/ 2,
            params.top= (screen.height- params.height)/ 2,
        
            t.click(function (e) {
                openWindow(params);
                return false;
            });
        }
    }
	
	function initPopupLegals () {
        var t= $("#legals");
        if( t.size() ){
            var params= {
                url: t.get(0).href,
                name: 'legalsWindow',
                width: 500,
                height: 500,
                toolbar: 0,
                resizable: 1,
                status: 0,
                scrollbars: 1
            }
            params.left= (screen.width- params.width)/ 2,
            params.top= (screen.height- params.height)/ 2,
        
            t.click(function (e) {
                openWindow(params);
                return false;
            });
        }
    }
    
    function openWindow ( params ) {
        var t= "";
        t+= ( 'left='+params.left );
        t+= ( ',top='+params.top );
        t+= ( ',width='+params.width );
        t+= ( ',height='+params.height );
        t+= ( ',toolbar='+params.toolbar );
        t+= ( ',resizable='+params.resizable );
        t+= ( ',status='+params.status );
        t+= ( ',scrollbars='+params.scrollbars );
        
        window.open(
            params.url, 
            params.name,
            t
        );
    }
    

    popin = {
        init : function(params) {
            popin.popinMask = $('#popinMask');
            if (W.ie6) {
                popin.popinMask.dimensions = BOX.getDimensions();
                popin.popinMask.width(popin.popinMask.dimensions.viewportW + 'px');
                popin.popinMask.height(popin.popinMask.dimensions.viewportH + 'px');
            }
            popin.wrapper = $('#popin');
            popin.container = $('#popinContent');
        },
        close :function() {
            $('.closePopin').click(function(e) {
                e.preventDefault();
                popin.hide();
            });
            popin.popinMask.click(function() {
                popin.hide();
            });
        },
        show : function() {
            popin.popinMask.show()
            popin.close();            
            popin.wrapper.show();
        },
        hide : function() {
            popin.popinMask.hide();
            popin.wrapper.hide();
        }
    }
    
    /* load : lancement des inits ******************************************* */
	$(W).load( function() {
        parasiteBOX();
        if($("#increaseFontSize").get(0)){
            initFontResizing();
        }
        initColumnScrolling();
        initPopinError();
        if($('#credits').size()){
            initPopupCredits();
        }
		if($('#legals').size()){
            initPopupLegals();
        }
        $("#decreaseFontSize").click();
	});

	$(document).ready(function () {
		$("#imprimer").click(
			function () {
				window.print();
				return false;
			}
		);
        popin.init();
        $('#aside p.button a').click(function(e){
            e.preventDefault();
            popin.show();
        })
		writeFlash();
	});
    
})(jQuery);

