$.noConflict();
jQuery.fn.outer = function() { return jQuery(jQuery('<div></div>').html(this.clone())).html(); }

/** Placeholder support for non-HTML5 browsers **/
function initPlaceHolders() {
	if (!('placeholder' in document.createElement('input'))) {
		jQuery('input[type=password]').attr('placeholder', jQuery('input[type=password]').eq(0).attr('placeholder'));
		var items = jQuery('input[type=text], input[type=email], input[type=password], textarea');
		items.each(function() {
			if ((jQuery(this).val() == '') && (jQuery(this).attr('placeholder'))) {
				jQuery(this).val(jQuery(this).attr('placeholder'));
				jQuery(this).addClass('placeholder');
			}
		});
		items.live('focus', function() {
			if (jQuery(this).val() == jQuery(this).attr('placeholder')) {
				jQuery(this).val('');
				jQuery(this).removeClass('placeholder');
			}
		});
		items.live('blur', function() {
			if ((jQuery(this).val() == '') && (jQuery(this).attr('placeholder'))) {
				jQuery(this).val(jQuery(this).attr('placeholder'));
				jQuery(this).addClass('placeholder');
			}
		});
		jQuery('form').bind('submit', function() {
			jQuery(this).find('input[type=text], input[type=password], input[type=email], textarea').each(function() {
				if (jQuery(this).val() == jQuery(this).attr('placeholder')) {
					jQuery(this).val('');
					jQuery(this).removeClass('placeholder');
				}
			});
		});
	}
}

function slidePhotos() {
	var cont = jQuery('div#photos div div');
	var px = '-=202';
	var sel = 'a[href] + a';
	if (cont.children('a[href] + a + a').length == 0) {
		px = '-105px';
		if (jQuery.browser.msie) px = '-102px';
		sel = 'a:first-child + a';
	}
	cont.animate({ 'left': px }, 1000, function() {
		cont.children(sel).attr('href', function() {
			cont.children('a').removeAttr('href');
			return jQuery(this).attr('data-link');
		});
	});
}

var n, limit = 1, nb = 0;
function slideNews() {
	if (nb == 0) {
		jQuery.ajax({
			type: 'POST',
			url: 'ajax/nb_news.php',
			async: false,
			success: function(data) { nb = data; }
		});
	}
	limit++;
	if (limit >= nb) limit = 0;
	jQuery('div#actu_right div:first-child + div').animate({ 'top': '-=92px' }, 'slow', 'linear');
	jQuery('div#actu_right div:first-child').animate({ 'top': '-=92px' }, 'slow', 'linear', function() {
		jQuery(this).remove();
		jQuery.ajax({
			type: 'POST',
			url: 'ajax/news.php',
			data: ({ limit: limit }),
			async: false,
			success: function(data) {
				jQuery('div#actu_right div').after(data);
				jQuery('div#actu_right div + div').fadeIn();
			}
		});
	});
}

jQuery(document).ready(function() {
	if (jQuery.browser.mozilla) jQuery("form").attr("autocomplete", "off");
	jQuery('select').mouseleave(function(event) { event.stopPropagation(); });
	if (typeof console === "undefined") console = { log: function() {} };
	initPlaceHolders();
	if (jQuery('a.gallery').length > 0) jQuery('a.gallery').lightBox();
	if ((jQuery('div#left + div').length > 0) && (jQuery('div#corps').height() > jQuery('div#left + div').outerHeight())) jQuery('div#left + div').height(jQuery('div#corps').height()-40);
	if (jQuery('div#actu_right div').length == 2) n = setInterval("slideNews()", 6000);
	jQuery('fieldset#clubs select').bind('change', function() { window.location = window.location+"?id="+jQuery(this).val(); });
	if (jQuery('a.gallery').length > 0) { jQuery('a.gallery').last().children('img').bind('load', function() { jQuery('a.gallery img').fadeIn(); }); }
	
	// Photo gals slide
	if (jQuery('div#photos').length > 0) {
		var cont = jQuery('div#photos div div');
		cont.prepend(cont.children('a:last-child').outer());
		cont.append(cont.children('a:first-child + a').outer());
		cont.children('a:first-child + a').attr('href', function() { return jQuery(this).attr('data-link'); });
		setInterval('slidePhotos()', 4000);
	}
	
	// Events positioning
	if (jQuery('div#agenda div div div#mark').length > 0) {
		jQuery('div#agenda > div > div').css('top', function() {
			var item = jQuery(this).children('div');
			var mark = item.index(item.filter('#mark'));
			if (mark > 0) {
				jQuery('div#agenda > a').not(':last-child').show();
				if (mark >= (item.length - 4)) {
					mark = item.length - 4;
					jQuery('div#agenda > a:last-child').hide();
				}
			}
			return (-1 * (item.outerHeight(true)) * mark);
		});
	}
	
	// Events scrolling
	jQuery('div#agenda > a').bind('click', function() {
		var link = jQuery(this);
		var h_item = link.siblings('div').children('div').children('div').outerHeight(true);
		var down = link.is(':last-child');
		jQuery('div#agenda > div > div').animate({ 'top': ((down) ? '-' : '+')+'='+h_item+'px' }, 'linear', function() {
			link.siblings('a').show();
			if (((!down) && ((Math.abs(parseInt(jQuery(this).css('top')))) <= 0)) || ((down) && (((h_item*4)+(Math.abs(parseInt(jQuery(this).css('top'))))) >= (jQuery(this).height())))) link.hide();
		});
	});
	
	// MacOS + Firefox hack
	if ((jQuery.client.os == "Mac") && (jQuery.browser.mozilla)) {
		jQuery('div#actu_left a, div#actu_right div a, div#left div a, div.actu_zoom a').css('padding', '3px 3px 0px');
		jQuery('div#actu_left img, div#actu_right div img').css('margin-top', '11px');
		jQuery('div.cache').css('top', '68px');
		jQuery('div.cache.bottom').css('top', '208px');
		jQuery('div.cache.left, div.cache.right').css('top', '80px');
		jQuery('div#agenda > a + a').css('top', '233px');
	}
});
