// Determine variables.
var oTweets = '';
var iCurrentTweet = -1;
var oHeaders = '';
var iCurrentHeader = -1;

// When document is loaded.
$(document).ready(function() {

	// Ajax call to retrieve latest headers.
	$.ajax({
		url: '/content/ajax-get-headers',
		type: 'post',
		success: function(sResponse) {

			// Translate response json string to object.
			oHeaders = eval('(' + sResponse + ')');

			// Find current one.
			if (typeof iCurrentHeaderId != 'undefined') {
				iCurrentHeader = (iCurrentHeaderId - 1);
				if (typeof oHeaders[iCurrentHeader] == 'undefined') {
					iCurrentHeader = -1;
				}
			} else {
				iCurrentHeader = -1;
			}

			// Display tweets.
			showHeader();
			setInterval(hideHeader, 10000);

		}
	});

	// Ajax call to retrieve latest tweets.
	$.ajax({
		url: '/content/ajax-get-tweets',
		type: 'post',
		success: function(sResponse) {

			// Translate response json string to object.
			oTweets = eval('(' + sResponse + ')');

			// Display tweets.
			showTweet();
			setInterval(showTweet, 10000);

		}
	});

	// Click on submenu item.
	$('#header ul#submenu li a').mousedown(function() {

		$(this).css('background', 'url(/images/website/60.png) center 0 no-repeat');

	});

	// Click on folder item in menu.
	$('.menuFolder').click(function() {

		// Get id.
		var iFolderId = $(this).attr('id').substr(6, 10);

		// Show submenu.
		if ($('#submenu' + iFolderId).css('display') == 'none') {

			// Retrieve height of submenu.
			var iHeight = $('#submenu' + iFolderId).css('height');

			// Set submenu height to 0.
			$('#submenu' + iFolderId).css('height', '0px');
			$('#submenu' + iFolderId).css('padding-top', '0px');
			$('#submenu' + iFolderId).css('padding-bottom', '0px');

			// Make folder active.
			$('#folder' + iFolderId).parent().addClass('active');

			// Show and animate to height.
			$('#submenu' + iFolderId).css('display', 'block');
			$('#submenu' + iFolderId).animate({
				height: [iHeight, 'swing'],
				'padding-top': '16px',
				'padding-bottom': '16px'
			}, 1000);

		// Hide submenu.
		} else {

			// Retrieve height of submenu.
			var iHeight = $('#submenu' + iFolderId).css('height');

			// Show and animate to height.
			$('#submenu' + iFolderId).animate({
				height:  ['0px', 'swing'],
				'padding-top': '0px',
				'padding-bottom': '0px'
			}, 1000, function() {
				$('#submenu' + iFolderId).css('display', 'none');
				$('#submenu' + iFolderId).css('height', iHeight);
				$('#folder' + iFolderId).parent().removeClass('active');
			});

		}
		
	});

});

/* Display function. */
function showTweet() {

	// Add to tweet count.
	iCurrentTweet++;

	// Check if tweet exists, else go back to 0.
	if (typeof oTweets[iCurrentTweet] == 'undefined') {
		iCurrentTweet = 0;
	}

	// Replace tweet.
	$('#tweetFrom').html('@' + oTweets[iCurrentTweet].user + ':');
	$('#tweetContent').html(oTweets[iCurrentTweet].text);
	$('#tweetDetails').html(oTweets[iCurrentTweet].ago + ' - <a href="http://twitter.com/intent/tweet?in_reply_to=' + oTweets[iCurrentTweet].id + '" target="_blank">reply</a> - <a href="http://twitter.com/intent/retweet?tweet_id=' + oTweets[iCurrentTweet].id + '" target="_blank">retweet</a>');
	var iImageHeight = $('#headerAnimation img').height();

}

/* Display function. */
function hideHeader() {

	// Replace tweet.
	$('#headerAnimation h1').fadeOut(300, function() {
		$('#headerAnimation h2').fadeOut(300, function() {
			$('#headerAnimation p').fadeOut(300, function() {
				$('#headerAnimation img').animate({'margin-top': '323px' }, 500, function() {
					setTimeout('showHeader()', 1000);
				});
			});
		});
	});

}

/* Display function. */
function showHeader() {

	// Add to tweet count.
	iCurrentHeader++;

	// Check if tweet exists, else go back to 0.
	if (typeof oHeaders[iCurrentHeader] == 'undefined') {
		iCurrentHeader = 0;
	}

	// Place outside box.
	$('#headerAnimation h1').css('margin-left', '-100px');
	$('#headerAnimation h1').css('display', 'block');
	$('#headerAnimation h2').css('margin-left', '702px');
	$('#headerAnimation h2').css('display', 'block');
	$('#headerAnimation p').css('display', 'none');

	// Replace header details.
	$('#headerAnimation img').attr('src', oHeaders[iCurrentHeader].image_src);
	$('#headerAnimation h1').html(oHeaders[iCurrentHeader].title);
	$('#headerAnimation h2').html(oHeaders[iCurrentHeader].subtitle);
	$('#headerAnimation p').html(oHeaders[iCurrentHeader].description + '<div class="clear"></div><a href="' + oHeaders[iCurrentHeader].button_url + '" class="btn" title="meer informatie">meer informatie</a>');
	var iImageHeight = $('#headerAnimation img').height();
	if (iImageHeight < 35) {
		iImageHeight = iInitialHeight;
	}

	// Animate.
	$('#headerAnimation h1').animate({'margin-left': "44px"}, 700);
	$('#headerAnimation h2').animate({'margin-left': "44px"}, 700, function() {
		$('#headerAnimation p').fadeIn(700, function() {
			var iTop = 323 - iImageHeight;
			var sTop = iTop + 'px';
			$('#headerAnimation img').animate({'margin-top': sTop }, 700);
		});
	});
	
	// Ajax call to save current header in session.
	$.ajax({
		url: '/content/ajax-save-header?iShowreelId=' + iCurrentHeader,
		type: 'post',
		success: function(sResponse) {
		}
	});

}

