/**
 * Load content XMLs and populate the page with them.
 * 
 * Using mootools v1.2.3, with Request (well... no more) and Selectors modules.
 * Using jQuery v1.3.2 for the XML parsing (for IE compatibility).
 */

/** Set the application environment ("development", "production") */
var app_env = 'production';

switch (app_env) {
case 'development':
	/** The base URL where XMLs are stored */
	var xmls_path = 'xml/';
	/** The complete proxy url, with the query string */
	var proxy_url = 'proxy.aspx?url=';
break;
case 'production':
	/** The base URL where XMLs are stored */
	// var xmls_path = 'http://beta.manythink.be/mseu_test/xml/';
	var xmls_path = 'http://www.microsofteurope.eu/com/xml/';
	/** The complete proxy url, with the query string */
	var proxy_url = 'xml.aspx?url=';
break;
}

/**
 * Load an XML through the proxy and pass the XML DOM object to a callback.
 * 
 * @see		xmls_path
 * @see		proxy_url
 * 
 * @param	url			the XML base name (e.g. "example.xml")
 * @param	callback	the callback function (will have the XML DOM object passed as the first argument)
 */
function loadXml(url, callback) {
	jQuery.get(proxy_url + xmls_path + url, {}, callback);
}

/**
 * Callback to build the sections with the content of sections.xml.
 * 
 * @param	xml		the XML DOM object
 */
function buildSections(xml) {
	try {
		xml = jQuery(xml);
		
		xml.find('section').each(function(i, section_xml) {
			buildSection(section_xml);
		});
	}
	catch (e) {
		// Consume the exception. Do nothing.
	}
}

/**
 * Sub-callback to build each section.
 * 
 * @see		buildSections
 * 
 * @param	xml		the XML DOM object for that section (the "section" node in the XML)
 */
function buildSection(xml) {
	xml = jQuery(xml);
	
	var section_id = xml.attr('id');
	
	// Header
	$$('#' + section_id + ' .content h2').set('html', xml.children('shorttitle').text());
	$$('#' + section_id + ' .content p').set('html', xml.children('shortdescription').text());
	$$('#' + section_id + ' .stories > img').set('src', xml.children('mediasintro').children('media').attr('fileName'));
	
	// Links
	try {
		if (xml.children('links').find('link').length == 0) throw "Link tag not found";
		xml.children('links').find('link').each(function(i, link) {
			link = jQuery(link);
			
			var section_index = i + 1;
			var id = section_id + section_index
			var url = link.find('url').text();
			var label = link.find('label').text();
			var a_options = (url.substr(0, 4) == 'http') ? 
				' target="_blank"' :
				' onclick="togLayer2(\'' + id + '\');return false;"';
			var li = new Element('li', {
				'html': '&gt; <a href="' + url + '"' + a_options + '>' + label + '</a>'
			});
			$$('#' + section_id + ' .content ul').grab(li);
		});
	}
	catch (e) {
		// Consume the exception, do nothing
	}

	// Contents
	try {
		if (xml.children('contents').find('case').length == 0) throw "Case tag not found";
		xml.children('contents').find('case').each(function(i, caze) {
			caze = jQuery(caze);
			
			var section_index = i + 1;

			// Title
			var title = caze.children('title').text();
			if (title) $$('#' + section_id + section_index + ' h4').set('html', title);
		
			// Content
			caze.children('chapitres').children('chapitre').each(function(c, chapitre) {
				chapitre = jQuery(chapitre);
				
				// Chapter title
				var title = chapitre.children('title').text();
				if (title) $$('#' + section_id + section_index + ' .text').grab(new Element('h5', {'html': title}));
			
				chapitre.children('chapitres').each(function(cc, chapitres) {
					chapitres = jQuery(chapitres);
					
					chapitres.children('chapitre').each(function(sc, souschapitre) {
						souschapitre = jQuery(souschapitre);
						
						// Subchapter title
						var url = souschapitre.children('title').children('url').text();
						var label = souschapitre.children('title').children('label').text();
						if (label) {
							var subtitle_content = url ? '<a href="' + url + '">' + label + '</a>' : label;
							var subtitle = new Element(url ? 'h6' : 'h6', {'html': subtitle_content});
							$$('#' + section_id + section_index + ' .text').grab(subtitle);
						}
						
						// Subchapter text
						var description = souschapitre.children('description').text();
						if (description) $$('#' + section_id + section_index + ' .text').grab(new Element('p', {'html': description}));
						
						// Subchapter links
						var links = souschapitre.children('links').children('link');
						if (links) {
							var lis = '';
							links.each(function(l, link) {
								link = jQuery(link);
								
								var url = link.find('url').text();
								var label = link.find('label').text();
								lis += '<li>&gt; <a href="' + url + '" target="_blank">' + label + '</a></li>';
							});
							var ul = new Element('ul', {'class': 'links', 'html': lis})
							$$('#' + section_id + section_index + ' .text').grab(ul);
						}
					});
				});
			
			});
		});
	}
	catch (e) {
		// Consume the exception, do nothing
	}

	// Stories
	try {
		// Create the <li> and fill the DOM for each story
		if (xml.children('stories').children('story').length == 0) throw "Story tag not found";
		xml.children('stories').children('story').each(function(i, story) {
			story = jQuery(story);
			
			var template = '' + 
				'<h6>{title}</h6>' + 
				'<p><a href="{url}" target="_blank">{description}</a></p>';
			var li = new Element('li', {'html': template.substitute({
				'title': story.find('title').text(), 
				'description': story.find('description').text(), 
				'url': story.find('url').text()
				}) 
			});
			$$('#' + section_id + ' .stories ul.box_stories').grab(li);
		});

		// Enable the sliding effect
		buildSlide(section_id + '_box_stories', section_id + '_stories_previous', section_id + '_stories_next');
	}
	catch (e) {
		// Hide the stories content block
		$$('#' + section_id + ' .stories_content').setStyle('display', 'none');
	}
	
	// Videos
	try {
		// Create the <li> for the video and fill the DOM
		if (xml.children('videos').children('video').length == 0) throw "Video tag not found";
		xml.children('videos').children('video').each(function(i, video) {
			video = jQuery(video);
			
			var template = '' + 
				'<div class="img">' + 
					'<img src="{poster}" alt="" />' +
					'<a href="{url}" class="picto_play" target="_blank"></a>' + 
				'</div>' + 
				'<div class="text">' + 
					'<h6>{title}</h6>' + 
					'<p><a href="{url}" target="_blank">{description}</a></p>' + 
				'</div>';

			var li = new Element('li', {'html': template.substitute({
				'title': video.children('title').text(), 
				'description': video.children('description').text(), 
				'url': video.children('url').text(), 
				'poster': video.children('poster').text()
				}) 
			});
			$$('#' + section_id + ' .videos ul.box_videos').grab(li);
		});

		// Enable the sliding effect
		buildSlide(section_id + '_box_videos', section_id + '_videos_previous', section_id + '_videos_next');
	}
	catch (e) {
		// Consume the exception, do nothing
	}

	// Magazines
	try {
		// Create the <li> for the magazine and fill the DOM
		if (xml.children('magazines').children('magazine').length == 0) throw "Magazine tag not found";
		xml.children('magazines').children('magazine').each(function(i, magazine) {
			magazine = jQuery(magazine);
			
			var template = '' + 
				'<div class="img">' + 
					'<a href="{url}" target="_blank"><img src="{poster}" alt="" /></a>' + 
				'</div>' + 
				'<div class="text">' + 
					'<h6>{title}</h6>' + 
					'<p><a href="{url}" target="_blank">{description}</a></p>' + 
				'</div>';
			var li = new Element('li', {'html': template.substitute({
				'title': magazine.children('title').text(), 
				'description': magazine.children('description').text(), 
				'url': magazine.children('url').text(), 
				'poster': magazine.children('poster').text()
				}) 
			});
			$$('#' + section_id + ' .magazine ul.box_magazine').grab(li);
		});

		// Enable the sliding effect
		buildSlide(section_id + '_box_magazine', section_id + '_magazine_previous', section_id + '_magazine_next');
	}
	catch (e) {
		// Consume the exception, do nothing
	}
	
}

/**
 * 
 */
function buildNews(xml) {
	try {
		xml = jQuery(xml);

		// Create the <li> and fill the DOM for each news
		if (xml.find('new').length == 0) throw "New tag not found";
		xml.find('new').each(function(i, news) {
			var template = '' + 
				'<h6><a href="{url}" target="_blank">{title}</a></h6>' + 
				'<p>{description}</p>' + 
				'<p><a href="{url}" target="_blank">{link_label}</a></p>';

			var li = new Element('li', {'html': template.substitute({
				'title': jQuery(news).find('title').text(), 
				'description': jQuery(news).find('description').text(), 
				'url': jQuery(news).find('link').attr('url'),
				'link_label': jQuery(news).find('link').text()
				}) 
			});
			$$('#news ul#box_news').grab(li);
		});

		// Enable the sliding effect
		buildSlide('box_news', 'news_previous', 'news_next');
	}
	catch (e) {
		// Consume the exception. Do nothing.
	}

}

/**
 * Initialize the sliding effect for one of the news, stories, videos or magazines blocks.
 */
function buildSlide(container, previous_container, next_container) {
	var nbr = $(container).getChildren('li').length;

	var items = [];
	for (var i = 0; i < nbr; i++) items.push(i);
	
	var slide = new noobSlide({
		box: $(container),
		items: items,
		addButtons: {
			previous: $(previous_container),
			next: $(next_container)
		}
	});
	
	if (nbr <= 1) {
		$(previous_container).setStyle('display', 'none');
		$(next_container).setStyle('display', 'none');
	}
}

/**
 * Load all XMLs on page load.
 */
window.addEvent('domready', function() {
	loadXml('sections.xml', buildSections);
	loadXml('news.xml', buildNews);
});
