login = 'thatsnotalogin';

$(document).ready(function() {
	
	$("form[name=comment]").submit(function() {
		$(this).ajaxSubmit({
			beforeSubmit: function(before) {
				$('div.comments div.form input[type=text]').attr('disabled',true);
				$('div.comments div.form input[type=text]').after(' <img src="http://cdn.betacie.com/ootweet/images/loader-input.gif" />');
			},
			success: function(html) {
				if (html == 'flood') {
					$('div.comments div.form img').remove();
					$('div.comments div.form input[type=text]').after('<span class="flood"> Don\'t flood!</span>');
					setTimeout("$('div.comments div.form span.flood').fadeOut();",3000);
					setTimeout("$('div.comments div.form input[type=text]').attr('disabled',null);",3000);
				} else {
					/*$('div.comments div.form').slideUp(function() {*/
						$('div.comments div.list').slideDown();
						$('div.comments div.form img').remove();
						$('div.comments div.form input[type=text]').attr('disabled',null);
						$('div.comments div.form input[type=text]').val('');
						$('div.comments div.list div.new:last').html(html).slideDown();
						anchor = $('div.comments div.list div.new:last a').attr('name');
						parts = anchor.toString().split("-");
						$('div.comments div.list div.new:last').after('<div class="new"></div>');
						commentHL_Click(parts[1]);
					/*});*/
				}
			}
		});
		return false;
	});
	
	$("form[name=comment] b").click(function() {
		html = $(this).html();
		if (html == '@anonymous' && login != 'thatsnotalogin') {
			$(this).html(login);
			$("form[name=comment] input[name=anonymous]").attr('value','false');
		} else if (html != '@anonymous') {
			login = html;
			$(this).html('@anonymous');
			$("form[name=comment] input[name=anonymous]").attr('value','true');
		}
	});
	
	$("div.desc h2.editable").click(function() {
		if ($("div.desc h2 input").length == 0) {
			$(this).html('<input type="text" name="titre" value="'+$(this).html()+'" />');
			$("div.desc h2 input").focus();
			if ($("div.desc h2 input").attr('value') == 'Click to insert a title.') {
				$("div.desc h2 input").attr('value','');
			}
			$("div.desc h2 input").blur(function() {
				titreMaj();
			});
			jQuery(document).bind('keydown','return',function() {
				titreMaj();
			});
		}
	});
	
	catClickable();
	
	commentHL_URL();
	favorites_URL();

});

function catClickable() {

	$("div.desc h3 span.cat.clickable").click(function() {
		id = $('div.desc').attr('id');
		$(this).after('<span class="cat clash select" onclick="addCat(\''+id+'\',\'clash\');">clash</span><span class="cat fail select" onclick="addCat(\''+id+'\',\'fail\');">fail</span><span class="cat joke select" onclick="addCat(\''+id+'\',\'joke\');">joke</span><span class="cat misc select" onclick="addCat(\''+id+'\',\'misc\');">misc</span>').remove();
	});

}

function addCat(id,cat) {

	$('div.desc h3 span.cat').remove();
	$('div.desc h3').prepend('<span class="cat '+cat+' clickable">'+cat+'</span>');
	catClickable();

	$.post('/ajax/cat.php',{'id':id,'cat':cat});

}

function vote(type,id) {

	$('.votes#'+type+id+' a.count').attr('onclick','');
	count = parseInt($('.votes#'+type+id+' a.count span.c').html());
	count = count + 1;
	$('.votes#'+type+id+' a.count span').hide();
	
	$.post('/ajax/vote.php',{'type':type,'ref_id':id},function() {

		$('.votes#'+type+id+' a.count span.c').html(count);
		$('.votes#'+type+id+' a.count span').fadeIn('slow');
		$('.votes#'+type+id+' a.count').addClass('voted');

	});

}

function tweetStar(id) {

	$('.tweet#id'+id+' .actions img.star').attr('src','http://cdn.betacie.com/ootweet/images/loader-star.gif');

	$.post('/ajax/favorite.php',{'id':id},function(html) {
		if (html == 'N') {
			$.facebox('Sign in with Twitter first!');
		} else if (html == '0') {
			$('.tweet#id'+id+' .actions img.star').attr('src','http://cdn.betacie.com/ootweet/images/star.png');
		} else if (html == '1') {
			$('.tweet#id'+id+' .actions img.star').attr('src','http://cdn.betacie.com/ootweet/images/star_full.png');
		}
	});

}

function favorites_URL() {

	var url = document.location;
	if (url == 'http://ootweet.com/import#favorites') {
		importFavorites(1);
	}

}

function importFavorites(page) {

	$('ul.favorites li').remove();
	$('ul.favorites').append('<li><img src="http://cdn.betacie.com/ootweet/images/loader-fav.gif" /></li>');

	$.get('/ajax/favimport.php?page='+page,function(xml) {
	
		$('ul.favorites li').remove();
	
		nombre = 0;
	
		$('status',xml).each(function() {
			$('ul.favorites').append('<li><b>@'+$('login',this).text()+'</b> '+$('text',this).text()+' <a href="/import/'+$('id',this).text()+'">Import this tweet</a></li>');
			nombre = nombre + 1;
		});
		
		prevpage = page - 1;
		nextpage = page + 1;
		
		if (nombre == 20) {
			$('ul.favorites').append('<li class="search"><a href="#favorites" onclick="importFavorites('+nextpage+');">Next 20 tweets&hellip;</a></li>');
		}

		if (page > 1) {
			$('ul.favorites').append('<li class="search"><a href="#favorites" onclick="importFavorites('+prevpage+');">Previous 20 tweets&hellip;</a></li>');
		}
	
	});

}

function commentHL_URL() {

	var anchorValue;
	var url = document.location;
	var strippedUrl = url.toString().split("#");
	if (strippedUrl.length > 1) {
		anchorValue = strippedUrl[1];
		var parts = anchorValue.toString().split("-");
		if (parts[0] == 'comment') {
			$('div.comment#comment'+parts[1]).addClass('highlight');
		}
	}

}

function commentHL_Click(id) {

	document.location.href = '#comment-'+id;
	$('div.comment').removeClass('highlight');
	$('div.comment#comment'+id).addClass('highlight');

}

function importStatus(id,login) {

	$('div.comments').html('<h2>Importing&hellip;</h2><div class="import_box"><p><img src="http://cdn.betacie.com/ootweet/images/loader.gif" /></p></div>');

	$.post('/ajax/import.php',{'type':'status','id':id,'login':login},function(html) {
	
		$('div.comments').html(html);
		
		if ($('div.comments a.url').length > 0) {
			setTimeout('document.location.href = "'+$('div.comments a.url').attr('href')+'";',3000);
		}
	
	});

}

function importDiscussion(id,login) {

	$('div.comments').html('<h2>Importing&hellip;</h2><div class="import_box"><p><img src="http://cdn.betacie.com/ootweet/images/loader.gif" /></p></div>');

	$.post('/ajax/import.php',{'type':'discussion','id':id,'login':login},function(html) {
	
		$('div.import div.new').html(html).slideDown();
		
		$('div.comments').html('<h2>Now, it\'s your turn.</h2><div class="import_box"><p>We did the maximum we could to import the discussion.</p><p>You can also import other tweets to add them in the discussion with the form below:</p><form name="add" action="/ajax/add.php" method="post"><p><input type="text" name="url" value="http://twitter.com/" onclick="if(this.value==\'http://twitter.com/\'){this.value=\'\';}" maxlength="140" /> <a href="javascript:;" onclick="$(\'form[name=add]\').submit();">Add</a></p></form><ul><li class="end"><a href="javascript:;" onclick="createDiscussion();">I\'m done, let\'s create the discussion page.</a></li></ul></div><p>If you\'re not happy with that, you can always <a href="/import">go back</a>.</p>');
		
		formAdd();
	
	});

}

function formAdd() {

	$("form[name=add]").submit(function() {
		$(this).ajaxSubmit({
			beforeSubmit: function(before) {
				$('div.comments form input[type=text]').attr('disabled',true);
				$('div.comments form input[type=text]').after(' <img src="http://cdn.betacie.com/ootweet/images/loader-input.gif" />');
			},
			success: function(html) {
				if (html == 'notfound') {
					
				} else if (html == 'error') {
					
				} else {
					$('div.comments form input[type=text]').attr('value','http://twitter.com/');
					$('div.comments form input[type=text]').attr('disabled',false);
					$('div.comments form img').remove();
					id_new = parseInt($('div.tweet',html).attr('id'));
					id_go = 0;
					stop = false;
					$('div.tweet').each(function() {
						id = parseInt($(this).attr('id'));
						if (id_new == id) {
							stop = true;
						}
						if (id_new > id) {
							id_go = id;
						}
					});
					if (stop == false) {
						if (id_go == 0) {
							if ($('div.tweet:first').length == 0) {
								$('div.import').html(html);
							} else {
								$('div.tweet:first').before(html);
							}
							$('div.new').slideDown();
							return false;
						} else {
							$('div.tweet').each(function() {
								id = parseInt($(this).attr('id'));
								if (id == id_go) {
									$(this).after(html);
									$('div.new').slideDown();
									return false;
								}
							});
						}
					}
				}
			}
		});
		return false;
	});

}

function createDiscussion() {

	ids = Array();
	i = 0;

	$('div.tweet').each(function() {
		id = $(this).attr('id');
		ids[i] = id;
		i++;
	});
	
	$('div.comments').html('<h2>Importing&hellip;</h2><div class="import_box"><p><img src="http://cdn.betacie.com/ootweet/images/loader.gif" /></p></div>');
	
	$.post('/ajax/import.php',{'type':'discussion_create','ids[]':ids},function(html) {
	
		$('div.comments').html(html);
		
		if ($('div.comments a.url').length > 0) {
			setTimeout('document.location.href = "'+$('div.comments a.url').attr('href')+'";',3000);
		}
	
	});

}

function editDiscussion(did) {

	ids = Array();
	i = 0;

	$('div.tweet').each(function() {
		id = $(this).attr('id');
		ids[i] = id;
		i++;
	});
	
	$('div.comments').html('<h2>Editing&hellip;</h2><div class="import_box"><p><img src="http://cdn.betacie.com/ootweet/images/loader.gif" /></p></div>');
	
	$.post('/ajax/import.php',{'type':'discussion_edit','id':did,'ids[]':ids},function(html) {
	
		$('div.comments').html(html);
		
		if ($('div.comments a.url').length > 0) {
			setTimeout('document.location.href = "'+$('div.comments a.url').attr('href')+'";',3000);
		}
	
	});

}

function recoverTweet() {

	$('div.comments').html('<h2>Recovering&hellip;</h2><div class="import_box"><p><img src="http://cdn.betacie.com/ootweet/images/loader.gif" /></p></div>');
	
	$.post('/ajax/recover.php',{},function(html) {
	
		$('div.comments').html(html);
		
		if ($('div.comments a.url').length > 0) {
			setTimeout('document.location.href = "'+$('div.comments a.url').attr('href')+'";',3000);
		}
	
	});

}

function setSelRange(inputEl, selStart, selEnd) { 
	if (inputEl.setSelectionRange) { 
		inputEl.focus(); 
		inputEl.setSelectionRange(selStart, selEnd); 
	} else if (inputEl.createTextRange) { 
		var range = inputEl.createTextRange(); 
		range.collapse(true); 
		range.moveEnd('character', selEnd); 
		range.moveStart('character', selStart); 
		range.select(); 
	}
}

function deleteTweet(id) {

	$('.tweet#'+id).slideUp(function() {
		$(this).remove();
	});

}

function titreMaj() {
	
	html = $("div.desc h2 input").val();
	
	$("div.desc h2 img").remove();
	$("div.desc h2 input").attr('disabled',true);
	$("div.desc h2 input").after(' <img src="http://cdn.betacie.com/ootweet/images/loader-input.gif" />');
		
	$.post('/ajax/titre.php',{'id':$("div.desc").attr('id'),'titre':html},function(html) {
		
		$("div.desc h2").html(html);
		
	});

}

function tweetIt(type,refid,etape) {

	$('p.bottom#tweet').html('<img src="http://cdn.betacie.com/ootweet/images/loader-blue.gif" />');
	
	$.post('/ajax/tweetit.php',{'type':type,'ref_id':refid,'etape':etape},function(html) {
	
		$('p.bottom#tweet').html(html);
	
	});

}
