$j(document).ready(function(){
	window.setTimeout('fix_content_height()', 200);
	window.setTimeout('fix_right_height()', 200);
});

/* FIX CONTENT HEIGHT */
function fix_content_height() {
	main_height = $j('#main').height();
	content_margin = $j('#content').css('margin-top');
	
	content_margin_length = content_margin.length;
	content_margin_int = parseFloat(content_margin.substring(0, content_margin_length-2));
	
	$j('#content .wrapper').height(main_height - content_margin_int);
}

/* FIX RIGHT COLUMN HEIGHT */
function fix_right_height() {
	main_height = $j('#main').height();
	right_margin = $j('#right').css('margin-top');
	
	right_margin_length = right_margin.length;
	right_margin_int = parseFloat(right_margin.substring(0, right_margin_length-2));
	
	$j('#right .wrapper').height(main_height - right_margin_int);
}

Event.observe(window, 'load', function() {
	
	/* COLOR BACKGROUNDS IN LISTS WITH RADIO BUTTONS */
	if ($('info')) {
		var inputs = $('info').select('input');
		inputs.each(function(item) {
			item.observe('click', function(event) {
				
				ancestors = this.ancestors();
				
				for(i = 0; i < inputs.length; i++) {
					current_ancestors = inputs[i].ancestors();
					
					current_names = current_ancestors[0].classNames();
					current_names = current_names.toString();
					current_names = current_names.split(' ');
					
					// Other items in list
					for(j = 0; j < current_names.length; j++) {
						if(position = strpos(current_names[j], '_act'))
						{
							tmp_names = current_names[j];
							// Remove active classname ...
							current_ancestors[0].removeClassName(current_names[j]);
							// ... and replace with regular classname
							current_ancestors[0].addClassName(tmp_names.substring(0, position));
						}
					}
				}
				
				// Selected item in list
				tmp_names = ancestors[0].classNames().toString();;
				ancestors[0].writeAttribute('class', '');
				ancestors[0].addClassName(tmp_names+'_act');
			});
			/*
			new Ajax.Updater(target, "/members/portfolio_get_form/<?= $data['Member']['alias'] ?>/"+id, {
			method: 'post',
			evalScripts: true,
			onLoading:function() {
			show_status();
			Element.update($("ajax_replace_status"), "<div class='growl_message'>Loading portfolio picture ...</div>")
			},
			onComplete:function() {
			Element.update($("ajax_replace_status"), "<div class='growl_message'>Profile portfolio loaded</div>");
			checker();
			hide_status();
			}
			});
			*/
		});
	}
	
	var margin = 0;
	
	/* TOGGLE WELCOME BAR ON HOMEPAGE */
	if ($('toggle_welcome')) {
		$('toggle_welcome').observe('click', function(event) {
			
			switch(this.hasClassName('open'))
			{
				case true:
					message_height = $('welcome_message').getHeight();
					margin = $('left').getStyle('margin-top');
					margin_length = margin.length;
					margin_int = parseFloat(margin.substring(0, margin_length-2));
					
					this.removeClassName('open');
					this.addClassName('closed');
					this.update('Ouvrir');
					$('welcome_message').addClassName('none');
					
					result = margin_int + message_height + 2 + 'px';
				break;
				
				case false:
					this.removeClassName('closed');
					this.addClassName('open');
					this.update('Fermer');
					$('welcome_message').removeClassName('none');
					
					result = margin;
				break;
			}
			
			$('left').setStyle({
				marginTop: result
			});
		});
	}
	
	/* TOGGLE ISLANDS BAR ON HOMEPAGE */
	if ($('toggle_shortcuts')) {
		$('toggle_shortcuts').observe('click', function(event) {
			ancestors = this.ancestors();
			switch(ancestors[1].hasClassName('open'))
			{
				case true:
					ancestors[1].removeClassName('open');
					ancestors[1].addClassName('closed');
					this.update('Open');
				break;
				
				case false:
					ancestors[1].removeClassName('closed');
					ancestors[1].addClassName('open');
					this.update('Close');
				break;
			}
		});
	}
});

function strpos( haystack, needle, offset){
    var i = haystack.indexOf( needle, offset ); // returns -1
    return i >= 0 ? i : false;
}