var url = "/";

$(document).ready(function() {
	resetLanguageSwitch();
	resetMenuActions();
	resetContent();
	resetMenuStyle();
	
	tweetList = $('#tweetsContent li');
	if (tweetList.length > 0 && $('#noTweets').length == 0) {
		$("#tweetsContent").jCarouselLite({
			auto: 5000,
			speed: 1000,
			vertical: true,
			visible: 1
		});
	}
});

function switchLanguage() {
	currentContentID = $('.selectedOption');
	currentContentID = (currentContentID.length > 0) ? currentContentID.attr('id').replace('menuItem', '') : '0';
	
	$.ajax({
			url:	url + "ajaxRequests.php",
			type:   "POST",
			data:   "action=switchLanguage&currentContentID=" + currentContentID,
			dataType:"json",
			success: function (data) { 
						/* print new menu*/
						$('#menu').html(data.menuText);
						
						/* switch the language*/
						if (language == 'ro') {
							language = 'en';
							$('#languages').html('<a id="languageChange" href="switchLanguage.php">RO</a> / <span id="selectedLanguage">EN</span>');
							
						} else {
							language = 'ro';
							$('#languages').html('<span id="selectedLanguage">RO</span> / <a id="languageChange" href="switchLanguage.php">EN</a>');
						}
						resetLanguageSwitch();
						
						$('#contentColumn').html(data.newText);
						resetMenuActions();
						resetMenuStyle();
						$('#menuItem' + data.newID).removeClass('unselectedOption').addClass('selectedOption');
						
						try{
						pageTracker._trackPageview("/" + language);
						} catch(err) {}
					}
			});
}

function getContent(id) {
	itemID = id.replace('menuItem', '');
	
	$.ajax({
			url:		url + "ajaxRequests.php",
			type:   	"POST",
			data:   	"action=getContent&itemID=" + itemID,
			dataType:	"json",
			success: 	function (data) {
							
							if ($(window).width() > 999) {
								contentDiv = $('#contentColumn');
								contentDiv.html('').addClass('hidden');
								contentDiv.html(data.text).show( 'slide', {}, 700, callback );
								$('body').animate({ backgroundColor: "#" + data.backgroundColor }, 500);
							} else {
								$('#mobileContentColumn').remove();
								$('#' + id).parent().append('<div id="mobileContentColumn"></div>');
								contentDiv = $('#mobileContentColumn');
								contentDiv.html('').addClass('hidden');
								contentDiv.html(data.text).show( 'slide', {}, 700, function() {	/*if ($(window).width() < 1000) { window.location.hash='item' + itemID;}*/}	);
								$('body').animate({ backgroundColor: "#" + data.backgroundColor }, 500);
							}
							
							resetMenuStyle();
							$('#' + id).removeClass('unselectedOption').addClass('selectedOption');
							
							try {
							pageTracker._trackPageview("/" + language + "/" + $('#' + id).html());
							} catch(err) {}
						}
			});
}

function callback() {}

function resetLanguageSwitch() {
	$('#languageChange').bind('click', function() { switchLanguage(); return false; } );
}

function resetContent() {
	$('#contentColumn').html('').addClass('hidden');
}

function resetMenuActions() {
	$('.menuItem').each(
		function() {
			$(this).bind('click', function() { getContent(this.id); return false;} );
		}
	);
}

function resetBackgroundColor(animate) {
	if (animate)
		$('body').animate({ backgroundColor: "#996633" }, 500);
	else
		$('body').css('background', '#996633');
}

function resetMenuStyle() {
	$('.menuItem').removeClass('selectedOption');
	$('.menuItem').addClass('unselectedOption');
}
