$(document).ready(function(){
	$('#submitForm_submit').click(function(){
		var name = $('#submitForm_name').val();
		var text = $('#submitForm_text').val();
		var reply = $('#submitForm_replyTo').val();
		if (text != '' && text.length < 130) {
			var url = 'update/twitter/';
			var data = {'name': name, 'text': text, 'reply': reply, 'type': 'post'};
			
			$.post(url, data, function(data){
				if (data.status == 404) alert('error occured');
				else if (data.status == 100) {
					//: 100 means form succesful submitted
					$('#submitForm_text').val('');
					$('#submitForm_replyTo_info').css('display', 'none').find('span').html();
					$('#submitForm_replyTo').val('0');
				}
			}, 'json');
		} else {
			alert('Etwas Text muss sein, zuviel sollte es aber auch nicht sein (130 Zeichen) :)');
		}
	});
	
	//: send via twitter form
	$('#twitterForm_submit').click(function(){
		var user = $('#twitterForm_user').val();
		var pw = $('#twitterForm_pw').val();
		var text = $('#twitterForm_text').val();
		var reply = $('#twitterForm_replyTo').val();
		if (text != '' && text.length < 130) {
			var url = 'update/twitter/';
			var data = {'user': user, 'pw': pw, 'text': text, 'reply': reply, 'type': 'twitter'};
			
			$.post(url, data, function(data){
				if (data.status == 403) alert('invalid credentials');
				else if (data.status == 404) alert('error occured');
				else if (data.status == 100) {
					//: 100 means form succesful submitted
					$('#twitterForm_text').val('');
					$('#twitterForm_replyTo_info').css('display', 'none').find('span').html();
					$('#twitterForm_replyTo').val('0');
				}
			}, 'json');
		} else {
			alert('Etwas Text muss sein, zuviel sollte es aber auch nicht sein (130 Zeichen) :)');
		}
	});
	
	
	//: reply anchors
	$('#submitForm_replyTo_info').css('display', 'none');
	$('#twitterForm_replyTo_info').css('display', 'none');
	
	//: forms
	$('#twitterForm').css('display', 'none');
	$('.twitterSwitch .twitter_no').addClass('active');
	
	$('.twitterSwitch .twitter_no').click(function(){
		$('#twitterForm').css('display', 'none');
		$('#submitForm').css('display', 'block');
		$('.twitterSwitch a').removeClass('active');
		$(this).addClass('active');
		return false;
	});
	$('.twitterSwitch .twitter_yes').click(function(){
		$('#twitterForm').css('display', 'block');
		$('#submitForm').css('display', 'none');
		$('.twitterSwitch a').removeClass('active');
		$(this).addClass('active');
		return false;
	});
	
	twitter_prepareLinks();
	listeners.push('twitter_prepareLinks');
});

function twitter_prepareLinks()
{
	$('a.replyTo').click(function(){
		var id = $(this).attr('id');
		id = id.replace(/replyTo-/, '');
		$('#submitForm_replyTo').val(id);
		$('#submitForm_replyTo_info').css('display', 'block').find('span').html('Antwort auf Eintrag ' + id);
		$('#twitterForm_replyTo').val(id);
		$('#twitterForm_replyTo_info').css('display', 'block').find('span').html('Antwort auf Eintrag ' + id);
		window.scrollTo(0,0);
		return false;
	});
	
	$('a.replyToRemove').click(function(){
		$('#submitForm_replyTo_info').css('display', 'none').html();
		$('#submitForm_replyTo').val('0');
		return false;
	});
	$('a.replyToRemoveTwitter').click(function(){
		$('#twitterForm_replyTo_info').css('display', 'none').html();
		$('#twitterForm_replyTo').val('0');
		return false;
	});
	
	
	//: admin funcs
	$('a.hidePost').click(function(){
		var id = $(this).attr('id');
		id = id.replace(/hide-/, '');
		var url = 'update/twitter/';
		var data = {'post_id': id, 'type': 'hide'};
		$.post(url, data, function(data){
			if (data.status == 100) {
				$('#post-' + id).css('display', 'none');
			}
		}, 'json');
		return false;
	});
	$('a.blacklistUserPost').click(function(){
		var id = $(this).attr('id');
		id = id.replace(/blacklist-user-/, '');
		var url = 'update/twitter/';
		var data = {'post_id': id, 'type': 'blockUser'};
		$.post(url, data, function(data){
			//: nothing to do
			//: @TODO: one might want to remove all posts of this user from view, but is that necessary?
			//: posts will vanish on next refresh, so ... nice to have, yet not necessary
		}, 'json');
		return false;
	});
	$('a.blacklistUserIP').click(function(){
		var id = $(this).attr('id');
		id = id.replace(/blacklist-ip-/, '');
		var url = 'update/twitter/';
		var data = {'post_id': id, 'type': 'blockIP'};
		$.post(url, data, function(data){

		}, 'json');
		return false;
	});
	
	/*$('a.userpost').click(function(){
		var id = $(this).attr('id');
		id = id.replace(/userpost-/, '');
		var url = 'update/twitter/';
		var data = {'post_id': id, 'type': 'userpost'};
		
		var pscroll = __getPageScroll();
		
		if ($('#userPost').is('div') == false) {
			var html = '<div id="userPost"><a href="#" id="userPost_close"></a><img src="img/loading.gif"></div>';
			$('body').append(html);
			$('#userPost').css('top', (pscroll[1] + 20) + 'px');
		} else {
			$('#userPost').html('<a href="#" id="userPost_close"></a><img src="img/loading.gif">').css('display', 'inline');
			$('#userPost').css('top', (pscroll[1] + 20) + 'px');
		}
		
		$('#userPost_close').click(function(){
			$('#userPost').css('display', 'none');
			return false;
		});
		$.post(url, data, function(data){
			if (data.status != 100) $('#userPost').remove();
			var html = '<a href="#" id="userPost_close"></a>' + data.content;
			$('#userPost').html(html);
			twitter_prepareLinks();
			$('#userPost_close').click(function(){
				$('#userPost').css('display', 'none');
				return false;
			});
		}, 'json');
		return false;
	});*/
}

function __getPageScroll(){
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}