/* Author: Mathias Lavaert */

$(document).ready(function() {
	//Messages initializeren
	if(typeof(initMessages) == 'function') {
		initMessages();
	}
	
	if(typeof(smartphoneSize) == 'function') {
		smartphoneSize();
	}
	
	$("#sendMessageButton").click(function() {
		validateForm();
	});
	
	$("#message").bind('keypress', function(e)
	{
		 if(e.keyCode == 13)
		 {
			 validateForm();
		 }
	});
	
	function validateForm() {
		var errors = false;
		if($("#name").val().length == 0) {
			$("#name").addClass("overflow");
			$("#name").keyup(function() {
				$(this).removeClass("overflow");
			});
			errors = true;
		}
		if($("#message").val().length == 0) {
			$("#message").addClass("overflow");
			errors = true;
		}
		if(!errors)
			$("#messageFormForm").submit();
	}


	// Favorieten highlighten
	FavoriteController.highlightFavorites();
		
	// Header rotation
	var rotate = rotateHeader();
	setInterval(function(){ rotate(); }, 5500);

	// Animatie navigatie
	$("nav>ul>li>a").mouseenter(function() {
		$(this).animate({
			paddingLeft: 25 
		}, 200);
	}).mouseleave(function() {
		$(this).clearQueue().animate({
			paddingLeft: 10
		}, 200);
	});

	// Animatie menu's aan de zijkant
	$('div.left ul li a, div.right ul li a').mouseenter(function() {
		$(this).animate({
			backgroundColor : '#222',
			color : '#EEE'
		}, 200);
	}).mouseleave(function() {
		$(this).clearQueue().animate({
			backgroundColor: '#DDD',
			color : '#222'
		}, 200);
	});
	
	
	$('#filter-button').toggle(function() {
		var unselectedItems = $('div.middle table tr:not(.selected) td');
		var selectedItems   = $('div.middle table tr.selected');
		$('div.middle .error').remove();
		
		if (selectedItems.size() === 0) {
			$('div.middle').prepend('<div class="error">' +
									'<p>You haven\'t selected any pilots...</p>' +
									'</div>');
		} else {
			unselectedItems.addClass('hidden');
			$('tr.selected').removeClass('selected');
			$(this).html($(this).html().replace('Favorites', 'Remove filter'));
		}
			
	}, function() {
		$('td.hidden').removeClass('hidden');
		$(this).html($(this).html().replace('Remove filter', 'Favorites'));
		FavoriteController.highlightFavorites();
	});
	
	// Weergeven messageformulier
	$('span#message-button').toggle(function() {
		$('div#messageform').fadeIn('fast');
	}, function() {
		$('div#messageform').fadeOut('fast');
		$('#message').val('');
		$('#name').val('');
		$('#counter').text('141');
	});

	// Counter voor lengte van het bericht
	$('textarea#message').keypress(function() {
		var chars = $(this).val().length;

		if (chars > 141) {
			$(this).addClass('overflow');
		} else {
			$(this).removeClass('overflow');
		}

		$('span#counter').text(141 - $(this).val().length);
	});
	
});

function rotateHeader() {
	var images = [
					BASE_URL+'theme/img/header_tsjoen2.jpg',
					BASE_URL+'theme/img/header_romain.jpg',
					BASE_URL+'theme/img/header_tsjoen.jpg'
				 ];

	var index  = 0;
	var header = $('header');
	
	// Closure!
	return function() {		 
		if (index >= images.length)
			index = 0;
			
		header.css("background-image", "url(" + images[index] + ")");
		index++;
	}
}

// EventController
var Dashboard = {
	
	loadEvents: function() {
		// Scope veranderd in de functie
		var _this = this;
		
		// Rijen van de tabel
		$('div.middle table tr').bind('click', _this.toggleFavorite(this));


	}
}

// Favorieten toevoegen en verwijderen
	$("div.middle table tr").click(function() {
		if (!($(this).hasClass('selected'))) {
			// Selected toevoegen
			$(this).addClass('selected');

			// Toevoegen aan favorieten
			FavoriteController.addFavorite($(this).find('td.nr').text());
		} else {
			// Selectie verwijderen
			$(this).removeClass('selected');
			var favorites = getCookie('tacFavorites');

			// Favoriet verwijderen
			FavoriteController.removeFavorite($(this).find('td.nr').text());
		}
	});


var FavoriteController = {

	cookieName : 'tacFavorites',

	addFavorite: function(favoriteNumber) {
		var favorites = getCookie(this.cookieName);

		if (favorites === null) {
			favorites = '';
		}

		// Add favorite to the string
		favorites += favoriteNumber + ';';

		// SaveChanges
		setCookie(this.cookieName, favorites);
	},

	removeFavorite: function(favoriteNumber) {
		var favorites = getCookie(this.cookieName);

		if (favorites === null) {
			favorites = '';
		}
  
		// Remove favorite from the string
		favorites = favorites.replace(favoriteNumber + ';', '');

		// SaveChanges
		setCookie(this.cookieName, favorites);
	},

	highlightFavorites: function() {
		var favorites = getCookie(this.cookieName);

		if (favorites) {
			var f = favorites.split(';');

			$('td.nr').each(function() {
				var n = $(this).text();

				for (var i = 0; i < f.length; i++) {
					if (n === f[i]) {
						$(this).closest('tr').addClass('selected');
					}
				}
			});
		}
	}
};

function messagesTopScroll() {
	$(window).scroll(function () {
		if ($(window).height() + $(window).scrollTop() > ($(document).height()-10)) {
			nextMessages();
		} 
	});		
	
	if ($(window).height() + $(window).scrollTop() > ($(document).height()-10)) {
		nextMessages();
	}
}

	
