/* bRate Star Widget Thing
 *
 *** bRate.create(string elementID, string type, string b32id);
 * creates a rating widget that is interactable for voting.
 *
 *** bRate.create(string elementID, string type, string b32id, boolean static, double value, integer count);
 * creates a rating widget that sits there doing nothing but displaying what it is told.
 * because of this the type and id are mostly irrlevent however still needed for generating
 * the codes and stuff. the id still needs to be unique if using multiple times on the same page.
 */

var bRate = {
	create: function(element,type,id,static,value,count) {
		element = bCommon.jqid(element,true);
		
		if(!static) static = false;
		if(!value) value = 0;
		if(!count) count = 0;
		
		var a = 0;
		var blob = null;
		var star = null;
		var lid = 'rate-' + type + '-' + id;
		var starid = '';
		
		blob = jQuery.create('div',{'id':lid,'alt':value + ':' + count});
		
		jQuery(element).html(blob);
		
		if(!static) {
			jQuery.get(
				ajaxURL('/a/rate-get.api'),
				{ 'type':type,'id':id },
				function(json) {
				
					blob.attr('alt',json.value + ':' + json.count);
					
					if(json.allow) {
						blob
							.mouseenter(function(){
								bRate.updateRateMode(lid);
							})
							.mouseleave(function(){
								bRate.updateDisplayMode(lid);
							});
					}
				
					bRate.updateDisplayMode(lid);			
					return;
				},'json'
			);
		} else {
			bRate.updateDisplayMode(lid);
		}

		return;
	},
	
	updateRateMode: function(lid) {
//		document.title = 'rate mode';
		lid = bCommon.jqid(lid,true);
		var blob = jQuery(lid);
		var star = null;
		var starid = '';
		
		blob.empty();
		
		var a = 1;
		for(a = 1; a <= 5; a++) {
			starid = bCommon.jqid(lid,false) + '-star-on-' + a;
			star = jQuery.create('img',{'class':'brate-star-active','id':starid,'src':'/share/gfx/star-empty.png'});
			
			star
				.bind('mouseover',function(){
					var cur = parseInt(jQuery(this).attr('id').replace(bCommon.jqid(lid,false) + '-star-on-',''));
					for(var b = 1; b <= 5; b++) {
						if(b <= cur) { jQuery(lid + '-star-on-' + b).attr('src','/share/gfx/star-hover.png'); }
						else { jQuery(lid + '-star-on-' + b).attr('src','/share/gfx/star-empty.png'); }
					}
				})
				.bind('click',function(){
					var cur = parseInt(jQuery(this).attr('id').replace(bCommon.jqid(lid,false) + '-star-on-',''));
					var info = lid.split('-');
					
					jQuery('.brate-star-active').unbind('click');
					
					jQuery.post(
						ajaxURL('/ai/rate-submit.api'),
						{ 'type':info[1],'id':info[2],'value':cur },
						function(json) {
						
							jQuery(lid).attr('alt',json.value + ':' + json.count);
							jQuery(lid).unbind('mouseenter mouseleave');
							bRate.updateDisplayMode(lid);
						
						},'json'
					);
					
					return;					
				});
			
			blob.append(star);
		}

		
		blob.append('<br />Rate&nbsp;This!&nbsp;');
				
		return;
	},
	
	updateDisplayMode: function(lid) {
//		document.title = 'display mode';
		lid = bCommon.jqid(lid,true);
		var blob = jQuery(lid);
		var data = blob.attr('alt').split(':');
		var value = parseFloat(data[0]);
		var count = parseInt(data[1]);

		var star = null;
		var starid = '';
		var starsrc = '';
		
		blob.empty();

		var a = 1;
		for(a = 1; a <= 5; a++) {
			if(a <= value) { starsrc = '/share/gfx/star-active.png'; }
			else if(a > value && a < value + 1) { starsrc = '/share/gfx/star-half.png'; }
			else { starsrc = '/share/gfx/star-empty.png'; }
			
			starid = bCommon.jqid(lid,false) + '-star-' + a;
			
			star = jQuery.create('img',{'id':starid,'src':starsrc});
			
			blob.append(star);
		}
		
		blob.append('<br />' + value + ' / ' + count + '&nbsp;rating' + ((count == 1)?(''):('s')) + '&nbsp;');
		
		return;	
	}
};
