/**
 * @author spapp
 * @date: 
 */
var Voter = function(_conf){
	var cont;
    var _this = this;
    /**
     * alapértékek beállítása
     */
    this.version = '0.0.2';
    this.contener = _conf.contener;
    this.blankImg = _conf.blankImg || '../images/s.gif';
    this.iconCls = _conf.iconCls || 'v-icon';
    this.iconClsVote = _conf.iconClsVote || 'v-icon-vote';
    this.iconClsMove = _conf.iconClsMove || 'v-icon-vote';
	this.listeners = _conf.listeners || {};
	this.nyuuz = _conf.nyuuz || {};
    this.value = _conf.value || 0;
    this.number = _conf.number || 5;   
	/**
	 * ez a függvény készíti el a szavazót
	 */
    var render = function(){
		var conid;
        if (typeof _this.contener == 'string') {
            cont = Utilities.get(_this.contener);
        }
        else {
            cont = _this.contener;
        }
        
        for (var ii = 0; ii < _this.number; ii++) {
            var cls = _this.iconCls;
            
            if (ii < _this.value) {
                cls = _this.iconClsVote
            }
            var element = Utilities.createElement('img', {
                className: cls,
				border: 0,
                src: _this.blankImg,
                alt: ii + 1,
				id: _conf.contener+'_'+ii
            });
            
            Utilities.appendChild(cont, element);
            
            Utilities.addListener(element, 'mouseover', mouseOver);/*function(){
                mouseOver(_this);
            });*/
            Utilities.addListener(element, 'mouseout', mouseOut);/*function(){
                mouseOut(_this);
            });*/
            Utilities.addListener(element, 'click', mouseClick);/*function(){
                mouseClick(_this);
            });*/
			
        }
    }
    /**
     * @param {Object} _t
     * a szvazó onmouseover eljárása
     */
    var mouseOver = function(_t){
		//_t = _this;
		var _index;
		var c = cont.childNodes;

		if(c.length>_this.number){
			c = getImgElements(cont);
		}
	
		if(_t.target){
			_index=_t.target.alt;
		}else if(_t.srcElement){
			_index=_t.srcElement.alt;
		}

        for (var i = 0; i < c.length; i++) {	
            if (c[i].tagName == 'IMG') {
                if (i < _index) {
                    c[i].className = _this.iconClsVote;
                }
                else {
                    c[i].className = _this.iconCls;
                }
            }
        }
    }
	/**
 * @param {Object} name
 * @param {Object} tag
 */
	var getImgElements = function(_c){
	var tag = 'IMG';
    var found = 0;
    var elems = new Array();
    var alltags = _c.getElementsByTagName(tag);
    
    if (alltags) {
        for (i = 0; i < alltags.length; i++) {
            if (alltags[i].className == _this.iconClsVote || alltags[i].className == _this.iconCls ||alltags[i].className ==  _this.iconClsMove ) {
                elems[found++] = alltags[i];
            }
        }
    }
    return (elems);
}
    /**
     * a szavazó onmouseout eljárása
     */
    var mouseOut = function(){
        _this.refresh();
    }
    /**
     * @param {Object} _t
     * a szavazó onclick eljárása
     * itt történik meg a szvazat rögzítése, és az új érték kiírása
     */
    var mouseClick = function(_t){	
		var _index;
		var _id = cont.id;
		var c = cont.childNodes;
		
		if(_t.target){
			_index=_t.target.alt;
		}else if(_t.srcElement){
			_index=_t.srcElement.alt;
		}
		if(_this.listeners.click){
			_this.listeners.click(_this, _index);
		}
    }
    /**
     * frissíti a szavazót
     */
   	this.refresh = function(){
        var c = cont.childNodes
        for (i = 0; i < c.length; i++) {
            if (c[i].tagName == 'IMG') {
                if (i < _this.value) {
                    c[i].className = _this.iconClsVote;
                }
                else {
                    c[i].className = _this.iconCls;
                }
            }
        }
    }
    
    render();
    
    //return this;
};