/**
 * Used for menu drop down
 * [make sure jquery is included first]
 */
$(document).ready(function () { 
	var badBrowser = (/MSIE ((5\.5)|6|7)/.test(navigator.userAgent) && navigator.platform == "Win32");
	
	/*
	$("SELECT").each(function(){
		if(!$(this).parent().hasClass("innopole-calendar") )// && ($(this).attr('onchange') == ""))
		{
			$(this).selectbox();
		}
	});
	//fix background-position in IE7- 
	if(badBrowser)
	{
		$(".selectbox").each(function(){
			var newpos = $(this).width() + 3;
			$(this).css('background-position', newpos+'px center');
		});
	}
	*/
	
/*** Menus ***/
    $("ul.sf-menu").supersubs({ 
        minWidth:    20,                                // minimum width of sub-menus in em units 
        maxWidth:    27,                                // maximum width of sub-menus in em units 
        extraWidth:  0                                  // extra width can ensure lines don't sometimes turn over 
                                                        // due to slight rounding differences and font-family 
    }).superfish({ 
        delay:       400,                               // delay on mouseout 
        animation:   {opacity:'show',height:'show'},    // fade-in and slide-down animation 
        speed:       'fast',                            // faster animation speed 
        autoArrows:  false,                             // disable generation of arrow mark-up 
        dropShadows: false                              // disable drop shadows 
    }); 
    
	$("ul > li:last-child").addClass("last");
	//$("ul > li:first-child").addClass("first");
    
    //Verify position of each sub-menus
    $(".sf-menu ul").each(function(){

    	//get container position and width to calculate maximum value from left of the screen
    	var containerWidth = $(".header").width();
    	var containerLeft = $(".header").offset().left + parseInt($(".header").css("padding-left"), 10) + 
    		parseInt($(".header").css("margin-left"), 10) + parseInt($(".header").css("borderLeftWidth"), 10); 
    	var max_left = containerWidth + containerLeft;
    	
    	//element must be visible to get position and size
    	var parent = $(this).parent().parent(); //get ul parent
    	var parent_visible = parent.is(':visible');
    	if(!parent_visible) //third level
    	{
    		parent.show();
    	}
    	var is_visible = $(this).is(':visible');
    	if(!is_visible)
    	{
    		$(this).show();
    	}

    	var elemEndPos = $(this).width() + $(this).offset().left;
    	var newLeft = 0;
    	if(elemEndPos > max_left)
    	{
    		//calculate and set new left value
    		if(!parent_visible) //third level
    		{
    			//revertedDisplay is only used on second level.
    			if(!$(this).hasClass("revertedDisplay")) //if parent not already reverted
    			{
    				//revert parent
    				parent.addClass("revertedDisplay");
    				var parentNewLeft = -parent.width() + parent.parent().width(); 
    				parent.css("left", parentNewLeft);
    			}
    			newLeft = -$(this).width();
    		}
    		else //second level
    		{
    			$(this).addClass("revertedDisplay");
    			newLeft = -$(this).width() + $(this).parent().width(); 
    		}
    		$(this).css("left", newLeft);
    	}

    	if(!is_visible)
    	{
    		$(this).hide();
    	}
    	if(!parent_visible)
    	{
    		parent.hide();
    	}
    });
    
/*** Content ***/
    //APPLY CUSTOM STYLES
    //New way for a real even odd by table (when more than 1 table in the page)
    $("TABLE.table_4_columns_odd_grey TR").each(function(){Innopole_RealEvenOdd($(this), "grey");});
    $("TABLE.table_3_columns_odd_grey TR").each(function(){Innopole_RealEvenOdd($(this), "grey");});
    $("TABLE.table_2_columns_odd_grey TR").each(function(){Innopole_RealEvenOdd($(this), "grey");});
    $("TABLE.table_4_columns_odd_green TR").each(function(){Innopole_RealEvenOdd($(this), "green");});
    $("TABLE.table_3_columns_odd_green TR").each(function(){Innopole_RealEvenOdd($(this), "green");});
    $("TABLE.table_2_columns_odd_green TR").each(function(){Innopole_RealEvenOdd($(this), "green");});
    
    var doIt = true;
    function Innopole_RealEvenOdd(elem, the_class)
    {
    	if((elem.parent().get(0).tagName == "TBODY") || (elem.parent().get(0).tagName == "TABLE"))
    	{
	    	if(doIt)
	    	{
	    		elem.addClass(the_class);
	    		doIt = false;
	    	}
	    	else
	    	{
	    		doIt = true;
	    	}
    	}
    	else
    	{
    		doIt = true;
    	}
    }
    
    /** Table du formulaire **/ 
    
    $("#batiments-listing tr:nth-child(odd)").addClass("green");
    $("#batiments-listing tr:nth-child(even)").addClass('');
   
    $("#bat-info tr:nth-child(odd)").addClass("green");
    $("#bat-info tr:nth-child(even)").addClass('');
    
    $("#tab-dispo tr:nth-child(odd)").addClass("green");
    $("#tab-dispo tr:nth-child(even)").addClass('');
    
    $("#tab-super tr:nth-child(odd)").addClass("green");
    $("#tab-super tr:nth-child(even)").addClass('');
   
    $("#tab-supp tr:nth-child(odd)").addClass("green");
    $("#tab-supp tr:nth-child(even)").addClass('');
    
    
    //imitate button behaviour
	$("div.button-style-link").mouseover(function(){
		$(this).addClass("over");
	}).mouseout(function(){
		$(this).removeClass("over");
	});
	   
/*** Sidebars ***/
	//Fix for IE5.5 or 6 or 7
    //Make sure this is the first one of the sidebars
	if (badBrowser) {
		$('UL LI H2').each(function() {
			if($.trim($(this).html()) == "")
			{
				$(this).css("display","none");
			}
		});
	}
	
	//To put 2 ads with small height side by side
	var prev_item = null;
	$("UL LI.Advman_Widget IFRAME").each(function() {
		if($(this).width() <= ($(this).parent().width()/2))
		{
			if(prev_item != null)
			{
				prev_item.addClass("sidebar-item-left");
				$(this).parent().addClass("sidebar-item-left");
			}
			else
			{
				prev_item = $(this).parent();
			}
		}
	});
	
	/*** Display text (using sweet-justice) ***/
	if (!badBrowser) {
		$("P").each(function(){
			var p_text = $(this).stripTags().replace(/&nbsp;/gi, ' ');;
			var numWords =  $.trim(p_text).split(' ').length;
			if(numWords < 10)
			{
				$(this).addClass("no-justify");
			}
			//$(this).addClass("sweet-justice");
		});
		$("LI").each(function(){
			if($(this).css("text-align") == "justify")
			{
				var p_text = $(this).stripTags().replace(/&nbsp;/gi, ' ');;
				var numWords =  $.trim(p_text).split(' ').length;
				if(numWords < 10)
				{
					$(this).addClass("no-justify");
				}
				//$(this).addClass("sweet-justice");
			}
		});
		$("DIV").each(function(){
			if($(this).css("text-align") == "justify")
			{
				var p_text = $(this).stripTags().replace(/&nbsp;/gi, ' ');;
				var numWords =  $.trim(p_text).split(' ').length;
				if(numWords < 10)
				{
					$(this).addClass("no-justify");
				}
				//$(this).addClass("sweet-justice");
			}
		});
	}

/*** Anchor bug fix ***/
	fixAnchorBug();
	
/*** Fix long titles ***/
	$("H1").each(function(){
		var p_text = $(this).stripTags().replace(/&nbsp;/gi, ' ');;
		var numWords =  $.trim(p_text).split(' ').length;
		if(numWords > 6)
		{
			$(this).addClass("long-h1");
		}
	});
	
	$("H2.section-menu-title").each(function(){
		var p_text = $(this).stripTags().replace(/&nbsp;/gi, ' ');;
		if($.trim(p_text).length > 25)
		{
			$(this).addClass("long-h2");
		}
	});
	
/*** FIX justify for tag A ***/
	$(".column-center A").each(function(){
		var p_text = $(this).stripTags().replace(/&nbsp;/gi, ' ');;
		var numWords =  $.trim(p_text).split(' ').length;
		if((numWords <= 6)
				&& !$(this).hasClass("sector-quick-access") 
				&& ($(this).html() == $(this).stripTags())
				&& ($(this).parent().get(0).tagName != "H1")
				&& ($(this).parent().get(0).tagName != "H2")
				&& ($(this).parent().get(0).tagName != "H3")
				&& ($(this).parent().get(0).tagName != "H4")
				&& ($(this).parent().get(0).tagName != "H5")
				&& ($(this).parent().get(0).tagName != "H6")
				&& !$(this).parent().hasClass("title")
				&& !$(this).hasClass("events")
				&& !$(this).hasClass("comm_presse")
			)
		{
			var text = $(this).html().replace(/\s/gi, '&nbsp;');;
			$(this).html(text);
		}
	});
	
});

jQuery.fn.stripTags = function() { 
	return this.html().replace(/<\/?[^>]+>/gi, ''); 
};

/**
 * Function to hack display when anchor.. Inserts <br clear="all" /> before
 * 
 * @return
 */
function fixAnchorBug(){
	var url = window.location.hash;
	if(url.indexOf("#") != -1)
	{
		$("H1:first").before("<br clear='all'/>");
	}
}

/**
 * Function to apply specific class to all cells in a table row
 * 
 * @param trElement
 * @param className
 */
function applyClass(trElement, className) {
	trElement.addClass(className);
}

/**
 * Function used to autofill a field with a default value and put back this value whan field is empty
 * 
 * @param id element id
 * @param v element value
 */
function autoFill(id, v){
	$(id).attr({ value: v }).focus(function(){
		if($(this).val()==v){
			$(this).val("");
		}
	}).blur(function(){
		if($(this).val()==""){
			$(this).val(v);
		}
	});
}

/**
 * Function to redirect to the link value in a combobox (select)
 * 
 * @param id combobox id
 * @return
 */
function gotoLink(id) {
	window.location = $(id).val();
}

