/**
* Rating counter object
*/

// constructor
function oRatingCounter(objid, params){

	this._init(objid, params);

}

// prototype
oRatingCounter.prototype = {

	_ratSrcImgs: new Array(),
	_ratHvrImgs: new Array(),
	_ratFreeImgs: new Array(),
	_ratObj: new Array(),
	
	_rat_elem: '',
	_section : '',
	_elem_id : '',
	_user : null,
	_add_id: '',
	_ajx_href: '',
	_again_phrase: '',
	
	_callback_save : null,
	
	_init: function(objid, params){
		// getting object
		
		this._rat_elem = objid;
		this.add_params(params);
		
		if (!window['rating'])
			window['rating'] = [];
		window['rating'][window['rating'].length] = this;

		// trying to get rating object
		this._ratObj = getLikeElements(false, 'id', 'vt_pos['+this._add_id+']');
		
	},
	
	add_params : function(params){
	
		if ((typeof(params)).toLowerCase() == "array" || (typeof(params)).toLowerCase() == "object"){
			for (var i in params){
				if (typeof this['_'+i] != "null"){
					this['_'+i] = params[i];
				}
			}
		}
	
	},


	load: function(){
			
		var athis = this;
		var loaderTicket = portal.loader.create(this._rat_elem, {"mode" : "float"});
		
		var req = new JsHttpRequest();
		req.onreadystatechange = function (){
			if (req.readyState == 4){
							
				portal.loader.destroy(loaderTicket);
				var _phrase = '';
				if (req.responseJS.content.no_rat == 1 && req.responseJS.content.tout != undefined && typeof req.responseJS.content.ours_txt != 'undefined'){
					_phrase = athis._again_phrase.replace(/\[HOURS\]/, req.responseJS.content.tout);
					_phrase = _phrase.replace(/\[HOURS_TXT\]/, req.responseJS.content.ours_txt);
				}
				if (req.responseJS.content.error != false){
					_phrase += req.responseJS.content.error;
				}
				req.responseJS.content.content = req.responseJS.content.content.replace(/\[ADD_MSG\]/, _phrase);	
				$_(athis._rat_elem).innerHTML = req.responseJS.content.content;
				athis._ratObj = getLikeElements(false, 'id', 'vt_pos['+athis._add_id+']');
				athis._ratSrcImgs = req.responseJS.content.src_imgs;
				athis._ratHvrImgs = req.responseJS.content.hvr_imgs;
				athis._ratFreeImgs = req.responseJS.content.free_imgs;
				athis._user = req.responseJS.content.user;
			}
		};
		req.open('POST', athis._ajx_href, true);

		req.send({'objid': athis._add_id, 'section' : this._section, 'elem_id' : this._elem_id /*, 'again_phrase': athis._again_phrase*/});
	
	},


	showProgress: function(obj){
		
		if (this._ratSrcImgs.length == 0 || this._ratHvrImgs.length == 0) return false;		
	
		for (i=0;i<this._ratObj.length;i++){
			
			if (parseInt(obj.getAttribute('value')) > parseInt(this._ratObj[i].getAttribute('value'))){
				//this._ratObj[i].src=eval(this._ratHvrImgs[i]);
				//this._ratObj[i].className="vt-img png img-op-50";
				this._ratObj[i].className=this._ratHvrImgs[i]+" img-op-50";	
			} else {
				if (parseInt(obj.getAttribute('value')) == parseInt(this._ratObj[i].getAttribute('value'))){
					//this._ratObj[i].src=eval(this._ratHvrImgs[i]);
					//this._ratObj[i].className="vt-img png img-op-100";
					this._ratObj[i].className=this._ratHvrImgs[i]+" img-op-100";				
				} else {
					//this._ratObj[i].className="vt-img png img-op-50";
					//this._ratObj[i].src=eval(this.ratFeeeImgs[i]);
					this._ratObj[i].className=this._ratFreeImgs[i]+" img-op-50";
				}
				
			}
			
		}
	},
	
	showSource: function(){
		if (this._ratSrcImgs.length == 0 || this._ratHvrImgs.length == 0) return false;		
			
		for (i=0;i<this._ratObj.length;i++){
			//this._ratObj[i].src=eval(this._ratSrcImgs[i]);
			//this._ratObj[i].className="vt-img png";
			this._ratObj[i].className=this._ratSrcImgs[i];
		}
	},
	
	save: function(value, param){
			
		var athis = this;
		
		var err = 0;
		//var all_obj=getLikeElements(false, 'id', 'vt_pos['+this._add_id+']');
		// checking for input data
		/*if (obj.value > all_obj.length){
			err = 1;
		}*/
		
		// show popup for minus value
		if (value == '-1' && this._section == 'pg_catalog' && (typeof param == 'undefined' || (typeof param != 'undefined' && param != 'force'))){
		
			project.confirmMinusRating(value, this._user);
			return false;
		}
		
	
		if (err != 1){
			var req = new JsHttpRequest();
			var loaderTicket = portal.loader.create(this._rat_elem, {"mode" : "float"});
			
			req.onreadystatechange = function (){
	
				if (req.readyState == 4){
					portal.loader.destroy(loaderTicket);
					// updating content
					var _phrase = '';
					if (req.responseJS.content.no_rat == 1 && typeof req.responseJS.content.tout != 'undefined' && typeof req.responseJS.ours_txt != 'undefined'){
						_phrase = athis._again_phrase.replace(/\[HOURS\]/, req.responseJS.content.tout);
						_phrase = _phrase.replace(/\[HOURS_TXT\]/, req.responseJS.content.ours_txt);
					}
					
					req.responseJS.content.content = req.responseJS.content.content.replace(/\[ADD_MSG\]/, _phrase+req.responseJS.error);
					$_(athis._rat_elem).innerHTML = req.responseJS.content.content;
					athis._ratObj = getLikeElements(false, 'id', 'vt_pos['+athis._add_id+']');
					athis._ratSrcImgs = req.responseJS.content.src_imgs;
					athis._ratHvrImgs = req.responseJS.content.hvr_imgs;
					athis._ratFreeImgs = req.responseJS.content.free_imgs;
					athis._user = req.responseJS.content.user;
					
					if (typeof athis._callback_save == 'function'){
						athis._callback_save(req.responseJS);
					} else if (typeof project.rating.callback == 'function'){
					// checking for main rating container exists
						project.rating.callback(req.responseJS);
					}
				}
				
			};
		
			req.open('POST', athis._ajx_href, true);
			req.send({'value': value, 'save': 'yes', 'objid': athis._add_id, 'section' : this._section, 'elem_id' : this._elem_id /*, 'again_phrase': athis._again_phrase*/});
		}
	}

};
