var sourceObj = new Object;
var targetObj = new Object;
var relationship = new Object;
targetObj.value = target;
$(document).ready(function(){
	$("#container").show();
	// validate source
	if (source != '') {
		$.jsonp({
			url: "http://twitter.com/users/" + source + ".json?count=1&callback=?",
			success: function(info){
				sourceObj.info = info;
			},
			error: function(){
				if (sourceObj.info == null) {
					$("#container").append("Screen name not valid");
					$("#container").fadeOut(3000, function(){ window.location='http://www.followingmeornot.net';});
				}
			}
		});
	};
			
	// source focus/blur
	$("#source").focus(function(){
		if ($(this).attr('value') == 'ducban') {
			$(this).attr('value', '');
			$(this).attr('style','color:#000');
		};
		$("#errorBox").fadeOut("fast");
	});
	$("#source").blur(function(){
		if ($(this).attr('value') == '') {
			$(this).attr('value', 'ducban');
			$(this).attr('style','color:#ccc');
		};
	});
	
	// target focus/blur
	$("#target").focus(function(){
		if ($(this).attr('value') == 'mrnhim') {
			$(this).attr('value', '');
			$(this).attr('style','color:#000');
		};
		$("#errorBox").fadeOut("fast");
	});
	$("#target").blur(function(){
		if ($(this).attr('value') == '') {
			$(this).attr('value', 'mrnhim');
			$(this).attr('style','color:#ccc');
		};
	});
	
	// ornot event
	$("#ornot").click(function(){
		sourceObj = new Object;
		sourceObj.value = $("#source").attr('value');
		if (targetObj.value == '') {
			$.jsonp({
				url: "http://twitter.com/users/" + sourceObj.value + ".json?count=1&callback=?",
				success: function(info){
					sourceObj.info = info;
					$("#content").fadeOut(300, function(){$("#userBox").fadeIn(700);});
				},
				error: function(){
					if (sourceObj.info == null) {
						$("#errorMsg").html("Screen name not valid");
						$("#errorBox").fadeIn("fast");
					}
				}
			});
		}else{
			$("#followingform").submit();
		}
	});
	
	// GO event
	$("#go").click(function(){
		targetObj = new Object;
		targetObj.value = $("#target").attr('value');
		if (targetObj.value == sourceObj.value) {
			$("#errorMsg").html("Oops. You're checking yourself?");
			$("#errorBox").fadeIn("fast");
			return false;
		};
		if (source != '') {
			$.jsonp({
				url: "http://twitter.com/users/" + source + ".json?count=1&callback=?",
				success: function(info){
					sourceObj.info = info;
				},
				error: function(){
					if (sourceObj.info == null) {
						$("#container").append("Screen name not valid");
						$("#container").fadeOut(3000, function(){ window.location='http://www.followingmeornot.net';});
					}
				}
			});
		};
		$.jsonp({
			url: "http://twitter.com/users/" + targetObj.value + ".json?count=1&callback=?",
			success: function(info){
				targetObj.info = info;
				$("#hiddenTarget").attr('value', targetObj.value);
				$("#followingform").attr('action', 'http://' + sourceObj.value + '.followingmeornot.net');
				$("#followingform").submit();
			},
			error: function(){
				if (targetObj.info == null) {
					$("#errorMsg").html("Screen name not valid");
					$("#errorBox").fadeIn("fast");
				}
			}
		});
	});
	
	
	// auto resize
	$("input:text").bind("mousemove keypress keyup keydown change click", function(){
		txtLen = $(this).attr('value').length;
		if (txtLen <= 10) {
			$(this).css("width", 150);
		}else{
			$(this).css("width", (txtLen - 10)*20 + 150);
		}
	});
	
	// form submit preventation
	$("#followingform").submit(function() {
		if (targetObj.value == '') {
			sourceObj = new Object;
			sourceObj.value = $("#source").attr('value');
			$.jsonp({
				url: "http://twitter.com/users/" + sourceObj.value + ".json?count=1&callback=?",
				success: function(info){
					sourceObj.info = info;
					$("#content").fadeOut(300, function(){$("#userBox").fadeIn(700);});
				},
				error: function(){
					if (sourceObj.info == null) {
						$("#errorMsg").html("Screen name not valid");
						$("#errorBox").fadeIn("fast");
					}
				}
			});
		}else{
			$("#followingform").attr('action', 'http://' + $("#source").attr('value') + '.followingmeornot.net');
			return true;
		}
		return false;
    });	
    
	$("#result").ready(function(){
		if ((target != '')&&(source != '')) {
			if (target  == source) {
				$("#result").html("Oops. You're checking yourself!");
				$("#again").show();
				return false;
			};
			$.jsonp({
				url: "http://twitter.com/friendships/show.json?source_screen_name=" + source + "&target_screen_name=" + target + "&callback=?",
				success: function(info){
					relationship = info;
					if (relationship['relationship']['target']['followed_by']) {
						$("#result").html("Yup. <a id='he' href='http://twitter.com/" + source + "'>" + source + "</a> is following <a id='you' href='http://twitter.com/" + target + "'>you</a>");
					}else{
						$("#result").html("No. " + source + " is not following you");
					}
					$("#again").show();
				},
				error: function(){
					if (relationship == null) {
						$("#errorMsg").html("Request failed. Please, try again.");
						$("#errorBox").fadeIn("fast");
					}
				}
			});	
		}
	});	
	
	
	
});

