var userInfo = {};

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

userInfo.init = function() {
	this.timerId = 0;
	this.timeout = 1;
	this.userName = '';
	this.userId = 0;
	this.state = userInfo.BEGIN;
	this.isOpen = false;
	this.OPEN_HEIGHT = 85;
	this.UI = document.getElementById('innerUserInfo');
	this.userNameInput = document.getElementById('userNameInput');
	
	if (controller.entry == controller.ETSY){
		return;
	}	
	else if (controller.params.user){
		this.setUserName(controller.params.user);
	}
	else{
		controller.userInputDone();
	}
	
};


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

userInfo.opts = {includes:['Profile(avatar_id)','Shops(listing_active_count,num_favorers)'],params:{fields:'login_name,user_id,creation_tsz'},callbackStr:'userInfo.callback1'};

userInfo.submit = function() {
	this.state = userInfo.BEGIN;
	
			
	this.userNameValue = this.userNameInput.value.replace(/^\s+|\s+$/g, '');
	
	if (!this.userNameValue || this.userNameValue == '')
		return;
	
	
	if (!this.isOpen){
		this.startAnimate();
	}
	
	this.opts.input = this.userNameValue;
	this.opts.entry = 'public';
	var q = new EQuery('/users/',this.opts);
	q.query();
	
};

userInfo.privateSubmit = function(){
	this.state = userInfo.BEGIN;
	
	if (!this.isOpen){
		this.startAnimate();
	}
	
	this.opts.input = '__SELF__';
	this.opts.entry = 'private';
	var q = new EQuery('/users/',this.opts);
	q.query();
}

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

};

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

};

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

};

userInfo.callback1 = function(response) {
        
	this.results = response.results[0];
	
	if (!response.ok){
		this.state = userInfo.ERROR;
		this.error = response.error; 
	} 
	else if (!this.results.Shops[0]){
		this.state = userInfo.ERROR;
		this.error = "'" + this.results.login_name + "' is not a seller";
	}
	else {
		this.userName = this.results.login_name;
		this.userId = this.results.user_id;
		this.state = userInfo.SUCCESS;
	}
	
	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>Number of Listings: ' + this.results.Shops[0].listing_active_count + '</h2>';
	   info += '<h2>Shop Hearts: ' + this.results.Shops[0].num_favorers + '</h2>';
	} else {
		this.userName = "";
		this.userId = 0;
		info = '<div class="error"><h1>' + this.error + '</h1></div>';
	}

	this.UI.innerHTML = info;
	
	this.userNameInput.value = userInfo.userName;
	this.userNameInput.style.color = 'black';
	
};

userInfo.startAnimate = function(){
	this.timerId = setInterval(function(){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';

};


