/*------------------------------------------------------------------------------
    JS Document (https://developer.mozilla.org/en/JavaScript)

    project:    POSTE IMMO
    created:    2009-11-16
    author:     flobou

    summary:    MAJX_CORE
                CONSTANTES
                UTILITIES
                WINDOW.ONLOAD
                EXAMPLE
								PRINT_BUTTON
								TEXT_SIZE
								INPUTLABEL
								GO_TO_BY_SCROLL
								FOOTER_PANEL
								FLASH_HOME
								TABS_STYLE

----------------------------------------------------------------------------- */

/*  =CONSTANTES
----------------------------------------------------------------------------- */
var d = document;
var w = window;
var modem = {};

majx.set({
    firebuglite : {
        active : false,                  // disable (false) in production environment
        url : 'js/firebug-lite-modem.js'
    },
	inputs : {                           // gets label value and puts it inside text field
        name : '.inside-label'
    },
	printButton : {                      // enable PRINT button with javascript
        elm : '#print-bt',
        txtPrint : 'Imprimer'
    },
	textSize : {                         // enable TEXT SIZE button with javascript
        elm : '#text-size-bt',
        txtTextSize : 'Taille du texte'
    },
	footerPanel : {                      // toggle slide footer mini site map
        elm     : '#panel-bt',
        button  : '#panel-bt button',
        textOn  : 'Ouvrir le mini-plan',
        textOff : 'Refermer le mini-plan',
        panel   : '#site-map-panel'
    },
	searchPanel : {                      // toggle slide search panel
        button  : '.search-panel .precise-bt button',
        panel   : '.search-panel .form-1'
    }
	
	
});


/*  =UTILITIES
----------------------------------------------------------------------------- */
var log = function(x) {
    if (typeof console != 'undefined') {
        console.log(x);
    }
};

var debuger = function(){
    if (majx.config.firebuglite.active && typeof console == 'undefined') {
        d.write('<script src="../js/'+majx.config.firebuglite.url+'" type="text/javascript"></script>');
    }
}();


/*  =WINDOW.ONLOAD
----------------------------------------------------------------------------- */
jQuery(d).ready(function(){

    // Call Functions
	modem.printButton();                            // handle print button
	modem.textSizeButton();                         // handle text size button
	modem.inputLabel();                             // gets label value and puts it inside text field
	modem.footerPanel();                            // slide toggle footer site map
	modem.searchPanel();                            // slide toggle fine search
	modem.setTabsStyle();                           // add last tab for style...
	modem.mediaBox();								// mediatheque thumbs over for ie...
	modem.popinKeyNav();                            // go next or previous popin using keyboard
	
	popin1 = new majx.popin(majx.config.popin1);
	
    if (jQuery.browser.msie && jQuery.browser.version == 6) {
        // IE 6 FUNCTIONS ONLY
        
    }

});


/*  =PRINT_BUTTON
----------------------------------------------------------------------------- */
modem.printButton = function() {
    var elm = majx.config.printButton.elm;
    var txtPrint = majx.config.printButton.txtPrint;

    var launch = function() {
        var zone = jQuery(elm);
        zone.each(function(){
            jQuery(this).append('<p><a href="javascript:window.print();">'+txtPrint+'</a></p>');
        });
    };

    var init = function() {
        if (jQuery(elm)[0]) {
            launch();
        }
    }();
}


/*  =TEXT_SIZE
----------------------------------------------------------------------------- */
modem.textSizeButton = function() {
    var elm = majx.config.textSize.elm;
    var txtTextSize = majx.config.textSize.txtTextSize;
	
    var launch = function() {
        var zone = jQuery(elm);
        zone.each(function(){
            jQuery(this).append( txtTextSize
								+'<button title="augmenter" class="increase">+</button>'
								+'<button title="diminuer" class="decrease">-</button>');
        });
    };

    var init = function() {
        if (jQuery(elm)[0]) {
            launch();
        }
    }();
	
	// Increase Font Size
	$(".increase").click(function(){
		var currentFontSize = $('#content').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*1.1;
		$('#content').css('font-size', newFontSize);
		return false;
	});
	// Decrease Font Size
	$(".decrease").click(function(){
		var currentFontSize = $('#content').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*0.9;
		$('#content').css('font-size', newFontSize);
		return false;
	});
}


/*  =INPUTLABEL
----------------------------------------------------------------------------- */
modem.inputLabel = function() {
    var inputs = jQuery(majx.config.inputs.name);
    
    var setField = function(current) {
		var prevLabel = current.parentNode.getElementsByTagName('label')[0];
        if (prevLabel) {
            var title = prevLabel.innerHTML;
			prevLabel.className = 'off';
        }
        current.value = title;
        current.state = 0;
    };

    var init = function() {
        inputs.each(function() {
            jQuery(this).keypress(function() {
                this.state = 1;
            });
            jQuery(this).focus(function() {
                if (this.state != 1) {
                    this.value = '';
                }
            });
            jQuery(this).blur(function() {
                if (this.state == 0 || this.value == '') {
                    setField(this);
                }
            });
            setField(this);
        });
    }();
}


/*  =GO_TO_BY_SCROLL
----------------------------------------------------------------------------- */
function goToByScroll(id){
	jQuery('html,body').animate({scrollTop: jQuery(id).offset().top},'slow');
}


/*  =FOOTER_PANEL
----------------------------------------------------------------------------- */
modem.footerPanel = function() {
	var button = majx.config.footerPanel.button;
	var panel = majx.config.footerPanel.panel;
	var textOn = majx.config.footerPanel.textOn;
	var textOff = majx.config.footerPanel.textOff;
	var elm = majx.config.footerPanel.elm;
	
	
	jQuery("#site-map-panel").css("display","none");
	
	
	var launch = function() {
        var zone = jQuery(elm);
        zone.each(function(){
            jQuery(this).html("<button type='button'>Ouvrir le mini-plan</button>");
        });
    };

    var init = function() {
        if (jQuery(elm)[0]) {
            launch();
        }
    }();
	
	
    jQuery(button).click(function(){
		
		var current = jQuery(this);
		var thepanel = jQuery(panel);
		
		if(current.attr('class') == "on") {
			current.removeClass("on");
			thepanel.slideUp();
			current.text(textOn);
		} else {
			current.addClass("on");
			thepanel.slideDown();
			goToByScroll(button);			
			current.text(textOff);		
		}
	});
};


/*  =SEARCH_PANEL
----------------------------------------------------------------------------- */
modem.searchPanel = function() {
	var button = majx.config.searchPanel.button;
	var panel = majx.config.searchPanel.panel;
	var thepanel = jQuery(panel);
	
	if( jQuery(button).attr('class') != "on" ) { // base.css ?
		thepanel.css("display","none");
	}
	
	
    jQuery(button).click(function(){
		var current = jQuery(this);
		if(current.attr('class') == "on") {
			current.removeClass("on");
			thepanel.slideUp();
		} else {
			current.addClass("on");
			thepanel.slideDown();	
		}
	});
};


/*  =TABS_STYLE
----------------------------------------------------------------------------- */
modem.setTabsStyle = function() {
	var tabsUl = jQuery('#tabs > ul');
	var tabsLi = jQuery('#tabs > ul > li');
	var ulWidth  = parseInt(tabsUl.width() ,10);
	var itemsWidth = 0;
	
	var lisWidth = tabsLi.each(function(){
		itemsWidth = itemsWidth + parseInt(this.offsetWidth ,10); 
	});	
	nItemWidth = ulWidth - itemsWidth - 24;	
	if (nItemWidth > 0) {
		tabsUl.append('<li class="last" style="width:'+nItemWidth+'px;"></li>');
	}
	
};


/*  =MEDIA_BOX
----------------------------------------------------------------------------- */
modem.mediaBox = function() {
	/*alert("touc");
	var thumb = jQuery("#box-mediatheque ul li a");

	thumb.hover(
		var current = jQuery(this);
		function () {
			thumb.css("z-index", "10");
			current.css("z-index", "11");
		}, 
		function () {
			
			
		}
    );
*/
};


/*  =POPIN_KEYNAV
----------------------------------------------------------------------------- */


modem.popinKeyNav = function() {

    var keyboardHandle = function(e) {
		if (document.getElementById('prev'))
	        var linkPrev = document.getElementById('prev').href;
    	
		if (document.getElementById('next'))
			var linkNext = document.getElementById('next').href;
        
        var options = {
            inner:true
        };

        if (e.keyCode == 37 && linkPrev) {
            popin1.openPopin(linkPrev, options);
        }
        else if (e.keyCode == 39 && linkNext) {
            popin1.openPopin(linkNext, options);
        }
    };
    
    jQuery(d).bind('keyup',function(e){
        if (document.getElementById('popin')) {
            if (e.keyCode == 37 || e.keyCode == 39) {
                keyboardHandle(e);
            }
        }//if
    });
    
};






/* END */


