UserInfo.BEGIN = 'begin';
UserInfo.SUCCESS = 'success';
UserInfo.ERROR = 'error';

function UserInfo(){
	this.timerId = 0;
	this.timeout = 1;
	this.userName = '';
	this.userId = 0;
	this.state = UserInfo.BEGIN;
	
	this.isOpen = false;
	this.OPEN_HEIGHT = 85;
}

var userInfo = new UserInfo();

userInfo.click = function(){
	var userNameInput = document.getElementById('userNameInput');
	userNameInput.value = '';
	userNameInput.style.color = 'black';
}

userInfo.submit = function() {
	this.state = UserInfo.BEGIN;
	this.UI = document.getElementById('innerUserInfo');
	this.userNameValue = document.getElementById('userNameInput').value.replace(/^\s+|\s+$/g, '');
	
	if (!this.userNameValue || this.userNameValue == '')
		return;
	
	
	if (!this.isOpen){
		this.startAnimate();
	}
	
	var q = new ETSYQuery(2,'/users/',this.userNameValue,['Profile(avatar_id)','Shops(listing_active_count)'],{fields:'login_name,user_id,creation_tsz'});
	q.callbackStr = 'userInfo.callback1';
	q.query();
	
}

userInfo.getUserName = function(){
	return this.userName;

}

userInfo.getUserId = function(){
	return this.userId;

}

userInfo.setUserName = function(userName){
	var userNameInput = document.getElementById('userNameInput');
	userNameInput.style.color = 'black';
	userNameInput.value = userName;
	this.submit();

}

userInfo.callback1 = function(response) {
        
	if (response.error){
		this.state = UserInfo.ERROR;
		this.errorCode = response.error; 
		this.userName = "";
		this.userId = 0;
		userInfo.done();
		return;
	}
			
	this.results = response.results[0];
	this.userName = this.results.login_name;
	this.userId = this.results.user_id;
	
	var q = new ETSYQuery(2,'/users/' + this.userId + '/favored-by',null,null,{limit:1,offset:0});
	q.callbackStr = 'userInfo.callback2';
	q.query();	
		
}

userInfo.callback2 = function(response){
	if (response.error){
		this.heartCount = 0;
	} else {
		this.heartCount = response.count
	}
	
	this.state = UserInfo.SUCCESS;
	etsyOauth.backFromEtsyLogin();
	userInfo.done();
}

userInfo.done = function(){
	if (this.isOpen && userInfo.state != UserInfo.BEGIN && etsyGoogle.isProfileReady()){
		this.drawUserInfo();
		etsyGoogle.setSelect();
		etsyOauth.setSelect();
		controller.userInputDone();
	}
}


userInfo.drawUserInfo = function(){
	var info = '';
	
	if (this.state == UserInfo.SUCCESS){
	   info = '<img src="http://ny-image1.etsy.com/iusa_75x75.' + this.results.Profile.avatar_id + '.jpg" />';
	   info += '<h1>' + userInfo.userName  + '</h1>';
	   this.joinedDate = new Date(this.results.creation_tsz * 1000);
	   info += '<h2>Joined: ' + this.joinedDate.toDateString() + '</h2>';
	   //info += '<h2>Last Updated: ' + (new Date(this.results.Shops[0].last_updated_tsz * 1000)).toDateString() + '</h2>';
	   info += '<h2>Number of Listings: ' + this.results.Shops[0].listing_active_count + '</h2>';
	   info += '<h2>Shop Hearts: ' + this.heartCount + '</h2>';
	} else {
		info = '<div class="error"><h1>' + this.userNameValue + ' does not appear to be a valid Etsy username</h1></div>';
	}

	this.UI.innerHTML = info;
	

}

userInfo.startAnimate = function(){
	this.timerId = setInterval('userInfo.runAnimate()', this.timeout);

}

userInfo.runAnimate = function () {
	if (this.UI.offsetHeight >= this.OPEN_HEIGHT){
		clearInterval(this.timerId);
		this.isOpen = true;
		this.done();
		return;
	}
	this.UI.style.height = (this.UI.offsetHeight + 2) + 'px';

}


