var feedNo = 0;
var notes = {
	'search-input' : 'Suchen',
	'gb-text' : 'Was geht so?',
	'ins-ahner' : 'Wird geahnt von',
	'ins-story' : 'Story',
	'music-author' : 'Künstler',
	'music-title' : 'Name des Tracks',
	'music-album' : 'Aus dem Album',
	'music-youtube' : 'Link zum Youtube-Video',
	'music-comment' : 'Dein Kommentar',
	'special-title' : 'Titel',
	'special-content' : 'Link',
	'special-text' : 'Beschreibung',
	'film-title' : 'Name des Films',
	'film-author' : 'Regisseur',
	'film-cast' : 'Schauspieler',
	'film-text' : 'Story',
	'buch-title' : 'Titel des Buchs',
	'buch-author' : 'Autor',
	'buch-origtitle' : 'Original-Titel',
	'buch-content' : 'Zusammenfassung / Inhalt',
	'buch-meinung' : 'Meine Meinung'
}

$(document).ready(function() {
	// Startseite:
  $('#login-name').focus();
  
  // Messages:
  $('.msg').click(function() { toggleMessages($(this).attr('id')); });
  $('#compose').click(function() { $('#compose-msg').slideToggle('fast'); });
  
  // Home, Messages, Profile:
  $('.resizable').live('click', function() { $(this).autoResize(); });
  
  // Profile: 
	$('#my_data, #my_photo, #my_password').click(function() {		
		$('.profile_fieldset, .done').hide();
		$('.profile-links a').removeClass('active');
		$('#'+$(this).attr('id')).addClass('active');
		$('#'+$(this).attr('id')+'_edit').fadeIn('slow');
	});  
  
  // Home: 
  $('#show-types').change(function() {
    window.clearTimeout(scrolling);
    $('#feed').html('');
    feedNo = 0;
    scrolling = window.setTimeout('endlessScroll('+$(this).val()+')', 250);
    $('#options-box').fadeOut();
  });
  $('#subnav a').click(function() {
  	var id = $(this).attr('id');
		$('#subnav a').removeClass('active');
		$('#'+id).addClass('active');
  	$('#f_type').val(id);
  	$('.feed-type:visible').hide();
  	prepareInputs();
		$('#type'+id).show();
		return false;
  });
  $('.feed-post').submit(function() {	$('input[type=text], input[type=radio], select, textarea').not(':visible').attr('name',''); });
	$('.smileys-link').click(function() {
		if($('.smileys-box').is(':visible')) {
			$('.smileys-box').fadeOut();
		} else {
			$.get('home.php?action=loadSmileys', function(data) {
				$('.smileys-box').css('background', '#fff');
				$('.smileys-box').html(data);
				$('.insertSmiley').click(function() { $('textarea[name=f_text]:visible').insertAtCaretPos($(this).attr('title')); $('.smileys-box').fadeOut(); });				
			});
			$('.smileys-box').fadeIn();
		}
		return false;
	});
	
	$('.like').live('click', function() { comment('like', $(this).attr('id')); return false; });
	$('.dislike').live('click', function() { comment('dislike', $(this).attr('id')); return false; });
	$('.delete').live('click', function() { comment('delete', $(this).attr('id')); return false; });
	$('.submit-comment').live('click', function() {
	  feedid = $(this).parents('.feed').attr('id');
	  $(this).attr('disabled', true);
	  $.post('home.php', { "action": "comment", "feedid": feedid, "type": "comment", "text": $('#comment-'+feedid).val() },	function(data) {
	  	$('#newcomments'+feedid).append(data).fadeIn();
	  	$('#comment-'+feedid).val('');
	  });
    $(this).attr('disabled', false);
    $('.comment .resizable').css('height', '40px');
	  return false;
	});	
	$('.toggle-comments').live('click', function() { 
		var comments = $('#'+$(this).parents('.feed').attr('id')+' .comments'); 
		if(comments.is(':visible')) comments.fadeOut(); else comments.fadeIn();
		return false;
	});
	$('.video').live('click', function() {
		var id = $(this).attr('id');
		var origlink = $('#'+id+' .video-shot').attr('id');
		$('#'+id+' .play-button').hide();
		$('#'+id).css({ 'width': '670px', 'height': '503px' });
		$.get('home.php?action=loadVideo&video='+origlink, function(data) {
			$('#'+id).html(data);
		});
	});
  prepareInputs();
});

function comment(type, feedid) {
	$.get('home.php?action=comment&feedid='+feedid+'&type='+type, function(data) {
    $('#opinions-'+feedid).html(data);
	});
}

var loading = false;

function endlessScroll(type) {
  if((($(document).height() - $(window).scrollTop() - $(window).height()) < 800) && !loading) {
    loadItems(feedNo, type);
    feedNo += 20;
  }
  scrolling = window.setTimeout('endlessScroll('+type+')', 250);
}

function loadItems(feedNo, type) {
	loading = true;
  $.ajax({
  	cache: false,
    url: 'home.php?action=loadFeeds&type='+type+'&feedNo='+feedNo,
    success: function(data) {	
    	$('#feed').append(data).show();
    	loading = false;
    	resizeImages();
    }
  });
}

function toggleMessages(id) {
	var msg = $('#content-'+id);
	if(msg.is(':visible')) {
		msg.slideUp('fast');
		$('#'+id).removeClass('msg-open');
	} else {
		$('.msg-content:visible').slideUp('fast');
		$('.msg').removeClass('msg-open');
		$('#'+id).addClass('msg-open');
		msg.slideToggle('fast');
		if($('#'+id).hasClass('msg-unread')) {
			$.get('messages.php', {'action': 'markRead', 'msg_id': id}, function(return_id) {
				if(return_id!=0) $('#'+return_id).removeClass('msg-unread');
			});
		}
	}
}

function prepareInputs() {
  $('.feed-type input, .feed-type textarea, .search-input').each(function() {
  	$(this).val(notes[$(this).attr('id')]);
  	$(this).addClass('input-note');
  	$(this).focus(function() { if($(this).val()==notes[$(this).attr('id')]) { $(this).removeClass('input-note'); $(this).val(''); } });
  	$(this).blur(function() { if($(this).val()=='') { $(this).addClass('input-note'); $(this).val(notes[$(this).attr('id')]); } });
  });
}

function resizeImages() {
	var maxWidth = 664;
	$('.feedcontent img').each(function() {
		if($(this).width() > maxWidth) {
			var newWidth = maxWidth;
			var newHeight = ($(this).height() / $(this).width()) * maxWidth;
			$(this).attr('width', newWidth);
			$(this).attr('height', newHeight);
		}
	});
}

