// Global varibles
var _timer;
var _isCommentView;
var _MAX_HEIGHT = 200; //400

// ===============================================================
// = On Load =====================================================
// ===============================================================

$(function(){
	InitializeWeeklyBlocks();
	InitializeReadMore();
});

// Display weekly footers and add click action
function InitializeWeeklyBlocks(){
	$('#picture-footer').show();
	$('#picture-change-link').click(function() { ChangePicture(); });
	$('#picture-change-link').css('cursor', 'pointer');
	$('#quote-footer').show();
	$('#quote-change-link').click(function() { ChangeQuote(); });
	$('#quote-change-link').css('cursor', 'pointer');
}

// Collapse news content as soon as height exceeds _MAX_HEIGHT
function InitializeReadMore(){
	_isCommentView = $('#comments').length;
	$('.news').each(function(){
		var newsBody = $(this).children('.news-body');
		if(!_isCommentView && newsBody.height() > _MAX_HEIGHT)
		{
			$(this).children('.footer-readmore').show();
			newsBody.css('overflow', 'hidden');
			newsBody.height(_MAX_HEIGHT);
		}
		else
		{
			$(this).children('.footer-readmore').hide();
		}
	});
	
	$('.readmore-link').toggle(function(){
		$(this).parent().parent().children('.news-body').css('height', 'auto');
		$(this).text('... read less');
	}, function(){
		$(this).parent().parent().children('.news-body').height(_MAX_HEIGHT);
		$(this).text('read more ...');
	});
}

// ===============================================================
// = Weekly Picture Ajax Functions ===============================
// ===============================================================

// Function called when user click on change link from quote block
function ChangePicture(){

	SetChangeLinkEnabled('picture', false);

	// Ajax configuration and call
	var xmlHttp = getHttpObject();
	var responseXMLRequest = R_ChangePicture;
	var url = "http://g.leuridan.free.fr/blog/xml/xPicture.php";
	sendHttpPost(xmlHttp, url, null, responseXMLRequest);
}

// Callback from ajax call
function R_ChangePicture(xmlHttp){
	if ((xmlHttp.readyState == 4) && (xmlHttp.status == 200)){

		var docXML = xmlHttp.responseXML;
		var id = docXML.getElementsByTagName('serial')[0].firstChild.nodeValue;
		var pictureUrl = docXML.getElementsByTagName('pictureUrl')[0].firstChild.nodeValue;
		
		$('#xmlPicture').fadeOut(500);
		_timer = setTimeout(function() { SetPicture(pictureUrl); }, 500);
	}
}

// Delay picture change
function SetPicture(pictureUrl){
	clearTimeout(_timer);
	$('#xmlPicture').attr('src', pictureUrl);
	$('#xmlPicture').fadeIn(500);
	SetChangeLinkEnabled('picture', true);
}

// ===============================================================
// = Weekly Quote Ajax Functions =================================
// ===============================================================

// Function called when user click on change link from quote block
function ChangeQuote(){

	SetChangeLinkEnabled('quote', false);

	// Ajax configuration and call
	var xmlHttp = getHttpObject();
	var responseXMLRequest = R_ChangeQuote;
	var url = "http://g.leuridan.free.fr/blog/xml/xQuote.php";
	sendHttpPost(xmlHttp, url, null, responseXMLRequest);
}

// Callback from ajax call
function R_ChangeQuote(xmlHttp){
	if ((xmlHttp.readyState == 4) && (xmlHttp.status == 200)){

		var docXML = xmlHttp.responseXML;
		var id = docXML.getElementsByTagName('serial')[0].firstChild.nodeValue;
		var quoteText = docXML.getElementsByTagName('quotetext')[0].firstChild.nodeValue;
		
		$('#xmlQuote').html(quoteText);
		SetChangeLinkEnabled('quote', true);
	}
}

// ===============================================================
// = Misc Functions ==============================================
// ===============================================================

// Switch footer elements visibilily
function SetChangeLinkEnabled(element, isEnabled){
	if(isEnabled)
	{
		$('#'+ element +'-change-link').show();
		$('#'+ element +'-loading').hide();
	}
	else
	{
		$('#'+ element +'-change-link').hide();
		$('#'+ element +'-loading').show();
	}
}