// Important: sp = img Object for sprite (defined in header.php)

// Test if a li has a ul.children as children and add the class has-sub
function hasChildMenu(li) {
		if($('ul.children',li).length) {
			var firstLink = li.children('a:first-child');
			li.addClass('has-sub');
			$(sp).appendTo(firstLink);
		}
}



// Resize menu if window browser is too small and add arrows for items with sub menu or sub sub menu
function checkMenu(arr) {
	var nav = $('#access .menu'), allItems = $('li',nav).filter(function() { return !$(this).parent('ul').hasClass('children')}), newUL = $('<ul/>');
	
	// Compute first top position of menu item
	var pos = $(allItems[0]).offset().top, diff = false;
	
	// Test size and add arrows for sub menu			 
	allItems.each(function() {
		if(pos!=$(this).offset().top && diff==false) { diff = true;}
		if(arr) { hasChildMenu($(this)); }	
	})
				 
	// Add arrows for sub sub menu 
	var nav = $('#access .menu');
	$('ul.children li a',nav).filter(function() { return $(this).next().hasClass('children'); }).each(function() {
			$(sp).appendTo(this);
			$(this).parent().addClass('has-sub-sub');				 
	});
				 
	// If the dimension of the window is too small for the menu
	if(diff) 	 allItems.css('padding','0 6px').find('a').css('font-size','12px');
	else 		 allItems.css('padding','0 13px').find('a').css('font-size','13px');
}


//  For Affiliate form: return the 2 divs that correspond to an input (s = size of the input)
function get2Divs(idofinput,s) {
		var t = $(idofinput), parentPrev = t.parent().prev() ;
        if(s!=0) t.attr('size',s);
		// If there's an error, we return 3 divs
		if(parentPrev.hasClass('error')) {
                    var l = parentPrev.prev();
                    return $('<div/>').append(l).append(t.parent()).append(parentPrev);    
                }	
		else
        return $('<div/>').append(parentPrev).append(t.parent());	
}


// Onload
$(function() {
			
				// Stripes the tables
				$('tbody tr:odd').addClass('odd');
				
				
				// For big tables
				$('table').each(function() {
					if( $(this).width() > $('.hentry').width() ) {
				 		var tooBigHolder = $('<div style="width:'+$('.hentry').width().width()-20+'px; overflow:auto"/>');
				 		$(this).clone().appendTo(tooBigHolder);
				 		$(this).replaceWith(tooBigHolder);
				 	} else {
						$(this).css({'width':'100%'});	
					}
				 }); 
				 
				 // Resize menu 
				 checkMenu(true);
				 
				
				// Affiliate form 
				if($('.form2col').length) {
					$("<div class='rightCol'/>").append(get2Divs('#si_contact_ex_field2_2',0)).append(get2Divs('#si_contact_ex_field2_3',0)).append(get2Divs('#si_contact_ex_field2_4',20)).append(get2Divs('#si_contact_ex_field2_5',20)).append(get2Divs('#si_contact_ex_field2_6',10)).append(get2Divs('#si_contact_ex_field2_7',20)).appendTo('.form2col form');
					
					// Padding on Radios
					$('input[type=radio]').parent().css({'padding-left':'1em','font-size':'0.9em'});
					
					// Radios alignement
					$('#si_contact_ex_field2_8_1').parent().parent().css('margin-top','0.8em').find('br').eq(1).remove()
					$('#si_contact_ex_field2_9_1').parent().parent().css('margin','0.8em 0 1em 0').find('br:odd').remove()
				}


});
