/**
 * @author rysiu
 */  
    
var pokojowo = {

	login_available : 'ajax/loginAvailable/',   
	mail_available : 'ajax/mailAvailable/', 
	nickname_available : 'ajax/nicknameAvailable/',    
	vote_url: 'ajax/vote/', 
	voted : false,
	
    init: function(domain) 
    {
		this.url = domain;
    },
    
    $: function(id) 
    {
        return document.getElementById(id);
    },
	
	set_star : function(points)
	{
		__dir = 'images/';
		for(i = 1; i <= 6; i++)
		{
			__star = this.$('star'+i);
			__star.src = __dir + ( i <= points ? 'star_on.gif' : 'star_off.gif');
		}
		this.clear_timer();
	},

	clear_star : function()
	{
		this.timer = window.setTimeout("pokojowo.clear_star_real()", 400);
	},
	
	clear_star_real : function()
	{
		this.clear_stars();
	},
	
	clear_stars : function()
	{
		__dir = 'images/';
		for (i = 1; i <= 6; i++) {
			__star = this.$('star' + i);
			__star.src = __dir + 'star_off.gif';
		}
	},
	
	clear_timer : function()
	{
		if (this.timer) 
		{
			window.clearTimeout(this.timer);
			this.timer = null;
		}
	},
	
	vote : function(id_project, points)
	{
		$.post(
			this.url + this.vote_url,
			{ id_project : id_project, points : points },
  			function(data, textStatus)
			{
				if(textStatus == 'success')
				{
					if(data.success)
					{
						pokojowo.voted(data);
					}
					else
					{
						pokojowo.voted('fail');	
					}
				}
				else
				{
					pokojowo.voted('fail');
				}
  			}
  			, 'json');
	},
	
	voted : function (response)
	{
		if(response.success != 'false')
		{
			this.set_info('ok');
			this.$('score_info').innerHTML = response.score;
			this.voted = true;
		}
		else
		{
			this.set_info('fail');
		}
	},
	
	set_info : function (info)
	{
		switch (info) {
			case 'noscore':
				__info = 'Nie możesz oddać głosu.';
				break;
				
			case 'login':
				__info = 'Zaloguj się, aby oddać głos.';
				break;
				
			case 'own':
				__info = 'Nie można głosować na swoją pracę.';
				break;
				
			case 'voted':
				__info = 'Głos został już oddany.';
				break;
			
			case 'ok':
				__info = 'Dziękujemy za oddanie głosu.';
				break;
			
			case 'fail':
				__info = 'Wystąpił błąd, przepraszamy.';
				break;
				
			default:
				__info = 'Nie możesz oddać głosu.';
				break;
		}

		this.$('vote_info').innerHTML = '<strong>' + __info + '</strong>';
		
	},
	
	show : function(box, boxes, prefix)
	{
		active = 'a'
		for(i =1; i <= boxes; i++)
		{
			obj = this.$('b_' + prefix + '_' + i);
			this.$(prefix + '_' + i).className = 'hide';
			if(new RegExp('\\b'+active+'\\b').test(obj.className))
			{
				var rep=obj.className.match(' '+active) ? ' '+active : active;
				obj.className=obj.className.replace(rep,'');
			}
		}
		this.$(prefix + '_' + box).className = 'show';
		this.$('b_' + prefix + '_' + box).className += ' a';
	},
	
	check_answer : function (form, div)
	{
		this.$(div).className = form.value != 0 ? 'row hide' : 'row show';		
	},
	
	check_login : function(login)
	{
		this.$( 'login_ok'   ).className = 'hide';
		this.$( 'login_fail'   ).className = 'hide';
		this.$('login_loading').className = 'show';
		$.post(
			this.url + this.login_available, 
			{ login: login },
  			function(data, textStatus)
			{
				pokojowo.$('login_loading').className = 'hide';
				if(textStatus == 'success')
				{
					pokojowo.$( data.success == 'true' ? 'login_ok' : 'login_fail'   ).className = 'show';
				}
				else
				{
					pokojowo.$( 'login_fail'   ).className = 'show';
				}
  			}
  			, 'json');
	
	},
	
	check_mail : function(mail)
	{
		this.$( 'mail_ok' ).className = 'hide';
		this.$( 'mail_fail' ).className = 'hide';
		this.$( 'mail_loading') .className = 'show';
		$.post(
			this.url + this.mail_available, 
			{ email: mail },
  			function(data, textStatus)
			{
				pokojowo.$('mail_loading').className = 'hide';
				if(textStatus == 'success')
				{
					pokojowo.$( data.success == 'true' ? 'mail_ok' : 'mail_fail'   ).className = 'show';
				}
				else
				{
					pokojowo.$( 'mail_fail'   ).className = 'show';
				}
  			}
  			, 'json');
	
	},
	
	check_nickname : function(nickname)
	{
		this.$( 'nickname_ok' ).className = 'hide';
		this.$( 'nickname_fail' ).className = 'hide';
		this.$( 'nickname_loading') .className = 'show';
		$.post(
			this.url + this.nickname_available, 
			{ nickname: nickname },
  			function(data, textStatus)
			{
				pokojowo.$('nickname_loading').className = 'hide';
				if(textStatus == 'success')
				{
					pokojowo.$( data.success == 'true' ? 'nickname_ok' : 'nickname_fail'   ).className = 'show';
				}
				else
				{
					pokojowo.$( 'nickname_fail'   ).className = 'show';
				}
  			}
  			, 'json');
	
	}
}