/*
 * Script used to support display
 * Copyright fredericback.com, 2007
 *
 */
var default_location_re = /\/index\...\.(aspx|shtml)$/;

$(document).ready(function() {

	/* Illustrateur section specific borders.
	 * This needs to be first because wrapInner destroy and recreates
	 * the nodes (but not the js hooks associated with them).
	 */
	$('body.illustrateur').find('#content').wrapInner('<div class="wrapper clearfix"></div>')
	$('body.illustrateur').find('#content').wrapInner('<div class="wrapper clearfix"></div>').children().border('type_01-');
	$('body.illustrateur').find('.medias > .wrapper').border('type_01-');
	$('body.illustrateur').find('.media > .wrapper').border('type_02-');

	$('body.artiste').find('#content').wrapInner('<div class="wrapper clearfix"></div>').children().border('type_03-');
	$('.border').wrapInner('<div class="wrapper clearfix"></div>').children().border();

	/* Here we want a li "bullet" that looks like a tilde.
	 * Using a bg-image instead would have cost several images
	 * because of the different color schemes.
	 */
	$('#menu ul ul li').prepend('~&nbsp;');

	/* Create needed div for the boxes to display right.
	 * Moving backward allows us to have "inner boxes".
	 */
	var boxes = $('.box');
	for (var i = boxes.size() -1; i >= 0; i--) {
		$(boxes.get(i)).customcorners2();
	}

	/* Image mouseovers are done adding
	 * a "mouseover" class to the image node.
	 */
	$('img.mouseover').add('input[@type="image"].mouseover').hover(function() {
		this.src = this.src.replace(/-off\./, '-on.');
	}, function() {
		this.src = this.src.replace(/-on\./, '-off.');
	});

	/* A "cleardefaultvalue" class on an input
	 * is an hint that the default value in
	 * the input should be cleared when the
	 * user clicks in.
	 */
	$('input.cleardefaultvalue').each(function() {
		var orig_value = this.value;
		$(this).click(function() {
			if (this.value == orig_value) {
				this.value = '';
			}
		});
		$(this).blur(function() {
			if (this.value == '') {
				this.value = orig_value;
			}
		});
	});

	/* adding the language slide effect.
	 *
     * choices is removed until at least two choices is available.
     *
	$('#languages').hover(function() {
		$('#languages span.choices').show();
	}, function() {
		$('#languages span.choices').hide();
	});
	$('#languages span.choices').append('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
	$('#languages span.choices').hide();
     *
     */
	$('#languages a.selected').prepend('&nbsp;+&nbsp;|&nbsp;');
	$('#languages a.selected').click(function() {
		window.location = window.location.href.replace(/\...\./, '.'+this.hreflang+'.');
		return false;
	});

	/* Automaticaly adds "selected" class on
	 * menu based on the current location of the file.
	 */
	$('#sitenav a').add('#menu a').each(function() {
		var loc1 = location.href.replace(default_location_re, '');
		var loc2_re = (this.className == 'defaultpage') ? /\/[\w-]+\...\.(aspx|shtml)$/ : default_location_re;
		var loc2 = this.href.replace(loc2_re, '');

		if (loc1.indexOf(loc2) >= 0) {
			$(this).addClass('selected');
			// refactor...
			var e = $(this).parent().parent().prev().get(0);
			if (e.nodeName == 'A') {
				$(e).addClass('selected');
			}
		}
	});

	/* hides submenu that are not in the current section.
	 */
	$('#menu ul ul').each(function() {
		if ($('a.selected', $(this).parent()).size() == 0) {
			$(this).css('display', 'none');
		}
	});

	/* adds the link to the media block
	 * and not just the anchor.
	 */
	$('.media').each(function() {
		var ref = $('a', this).attr('href');
		if (location.href.indexOf(ref) >= 0) {
			$(this).addClass('selected');
		}
		$(this).click(function() {
			window.location = ref;
		})
	});

	/* Sections tab navigation, to use it respect
	 * the following layout : .sections .section h2
	 */
	$('.tabbed.sections').each(function() {
		var current;
		var separator = '&nbsp; |&nbsp;';
		var section_nav = $(document.createElement('p')).addClass('subsections').addClass('navigation');

		if ($(this).children('.section').size() > 1) {;
			$(this).prepend(section_nav);
		}

		$(this).children('.section').each(function(i) {

			// adding links sub navigation
			var links_nav = $(document.createElement('p')).addClass('links').addClass('navigation');
			$(this).find('a').each(function() {
				var l = $(this).clone();
				l.html( ' ' + l.html().replace(/\s/g, '&nbsp;') );
				links_nav.append(l).append(separator);
			}).end().append(links_nav);

			// adding tab navigation anchor
			var a_text = $(this).children('h2').text();
			if (a_text == '' /* || $('body.cineaste').size() == 1 */ ) {
				a_text = i + 1;
			}
			var a = $( document.createElement('a') ).html( a_text );
			a.html( a.html().replace(/\s/g, '&nbsp;') ); // keep the link on its own line.

			var section = $(this);
			var click_handler = function() {
				if (current) {
					current.hide()._anchor.removeClass('selected');
				}
				current = section;
				current.show()._anchor.addClass('selected');
			}
			section._anchor = a;
			section_nav.append(a).append(separator);
			a.click(click_handler);

			// hides by default, except the first
			if (i == 0) {
				click_handler();
			} else {
				$(this).hide();
			}

		});
	});

});


