jQuery(document).ready(function() {

	jQuery("a[rel='article_tab']").click(function() { return swapArticle.swapContent(this.href); });
	swapArticle.swapContent(document.location.hash);

});

var swapArticle            = {};
swapArticle.section        = '';
swapArticle.processing     = false;
swapArticle.loadedSections = {texte:true};
swapArticle.ressources     = {};

/* +-----------------------------------------------
   | swapContent()
   +----------------------------------------------- */
swapArticle.swapContent = function(link) {

	// do nothing if already processing
	if (!swapArticle.processing) {

		// start processing
		swapArticle.processing = true;

		// get id
		var e  = link.split('#');
		var id = e[e.length-1]; 
		id = (id == 'texte' || !(swapArticle.ressources[id])) ? 'texte' : id;

		// if clicked on something different
		if (id != swapArticle.section) {

			// if first time
			swapArticle.section = (swapArticle.section) ? swapArticle.section : 'texte';

			// swap tab look
			jQuery('#tab_'+swapArticle.section).parent().parent().removeClass('selected');
			jQuery('#tab_'+id).parent().parent().addClass('selected');
				
			// swap content
			jQuery('#nouv_'+swapArticle.section).hide();
			jQuery('#nouv_'+id).show();
			
			// gossage de pub
			// ICI

			// set current section
			swapArticle.section = id;

			// if not loaded
			if (!swapArticle.loadedSections[swapArticle.section]) {
		
		        separateur = '?';
				if (swapArticle.section == 'video')
				{
					separateur = '&';
				}

				jQuery.get(swapArticle.ressources[swapArticle.section]+separateur+new Date().getTime(), function(data){

					jQuery('#nouv_'+swapArticle.section).html(data);
					swapArticle.loadedSections[id] = true;
					swapArticle.processing = false;
				});

			// if loaded stop processing
			} else {
				swapArticle.processing = false;
			}

		// stop processing
		} else {
			swapArticle.processing = false;
		}
	}

	return true;
}


/* +-----------------------------------------------
   | setRessources()
   +----------------------------------------------- */
swapArticle.setRessources = function(data) {
	swapArticle.ressources = data;
	return false;
}


var commentPNmber = 0;
function swapComments(monUrl)
{
	jQuery.get(monUrl, function(data){
					jQuery('#comment_liste').html(data);
				});

	//return false;
}


function submitComment(form) {
   
	var url = form.action+'?story_id='+escape(form.story_id.value)+'&author='+escape(form.author.value)+'&email='+escape(form.email.value)+'&comment='+escape(form.comment.value);

	jQuery.get(url, function(data) {

		var elements = data.split('|');
		var state = (Number(elements[0])) ? 'success' : 'error'; 
	
		var field = elements[1];
		var message = elements[2];

		if (state == 'error')
		{
			var err_fields = field.split(',');

			// on colore en rose les champs en erreur
			for(var i=0;i<err_fields.length;i++){
				jQuery('#'+err_fields[i]).addClass('error');
			}

			jQuery('#message_error').html(message).show('fast');
		}
		else // on affiche le message de confirmation
		{
			jQuery('#message_error').hide();

			jQuery(form).hide('fast');
			//jQuery('#loading_succes').show('normal');
			//jQuery('#loading_succes').hide();
			jQuery('#message_succes').addClass(state).html(message).show('fast');
		}
	});

	return false;
}
