/**
 * Add/remove favorites
 */

function hijackFavorites(){
	var clack = function(e){
		var _this = $(this);
		_this.parent().css({
			overflow: 'hidden'
		});
		_this.animate({
			top: '123px'
		}, 50);
		
		var target = _this.attr('href') + '/1';
		$.ajax({
			url: target,
			data: null,
			dataType: 'json',
			success: function(data){
				if (data.success) {
					if (document.location.href.substring(document.location.href.length-2) == 'ik') {
						_this.parent().fadeOut(250, function() {
							$(this).remove();
						});
					} else {
						if (_this.hasClass('remove')) {
							Garp.flashMessage('De favoriet is verwijderd');
							_this.removeClass('remove').addClass('save').text('bewaar');
						} else {
							Garp.flashMessage('De favoriet is opgeslagen');
							_this.removeClass('save').addClass('remove').text('verwijder');
						}
					}
				} else {
					Garp.flashMessage('Error: de favoriet kon niet bewerkt worden');
				}
				
				_this.animate({
					top: '108px'
				}, 50, function(){
					_this.parent().css({
						overflow: 'visible'
					});
				});
			},
			error: function(){
				location.href = _this.attr('href');
			}
		});
		e.preventDefault();
		return false;
	};
	$('a.save, a.remove').unbind('click', clack);
	$('a.save, a.remove').click(clack);
}

/**
 * Thumbs anination
 */
function animateThumbs() {
	$('.source-thumb>a:nth-child(1)').mouseenter(function() {
		$(this).stop().animate( { 
			paddingRight : '7px',
			duration: 'fast',
			easing: 'swing' } );
	}).mouseleave(function() {
		$(this).stop().animate( { 
			paddingRight : '0px',
			duration: 'fast',
			easing: 'swing' } );
	});
}

$(document).ready(function() {

	animateThumbs();	
	hijackFavorites();
	
	if($('#profiel').length < 0){
		return;
	}
	
	$('#profiel').hide();
	$('#profiel').after('<p class="link"><a href="#" id="wijzig-profiel">Wijzig profiel</a></p>');
	
	$('.toggle').each(function(){
		if ( !$(this).is(':checked') ) {
			$(this).parents('label').next().remove();
		}
	});
			
	$('.toggle').click(function(){
		if ( $(this).attr('checked') ) {
			$(this).parents('label').after('<label for="'+$(this).attr('id')+'-input"><input type="text" id="'+$(this).attr('id')+'-input" name="'+$(this).attr('name')+'"/></label>');
		} else {
			$(this).parents('label').next().remove();
		}
	});
	
	$('a.showHiddenThumbs').click(function(){
		$(this).fadeOut(null, function(){
			$(this).remove();
		});
		$('.hidden-thumb').fadeIn();
		return false;
	});
	
	$('#wijzig-profiel').click(function(){
		$(this).parent().toggleClass('align-right');
		$('#profiel').slideToggle('medium');
		$(this).text() === 'Wijzig profiel' ? $(this).text('Annuleren') : $(this).text('Wijzig profiel');
		return false;
	});
});

// GA api docs:	  _trackEvent(category, action, opt_label, opt_value)
// _gaq.push(['_trackEvent', 'cat', 'act', 'label', 'value');
(function(){
	$('.trackLink').click(function(){
		var track = $(this).attr('rel').split('|');
		_gaq.push(['_trackEvent', 'lesson_download', track[0], track[1]]);
	});
})();
