function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}

function GetAverage(_modul, _id, _index){
    url = '?c=mdOceny-cmPobierzSrednia';
    new Ajax(url, {
        method: 'post',
        data: Object.toQueryString({
            modul: _modul,
            id: _id,
            index: _index
        }),
        update: $('tresc_'+_index)
    }).request();
}
function GetVoteCount(_modul, _id, _tag){
     url = 'PobierzIloscGlosow.php';
    new Ajax(url, {
        method: 'post',
        data: Object.toQueryString({
            modul: _modul,
            id: _id
        }),
        update: $(_tag)
    }).request();
}
/***
 * _modul: nazwa modulu z ktorego jest wysylane zadanie
 * _id: identyfikator tresci która podlega ocenie
 * _ocena: bez komentarza:)
 * _inxdex: pozycja na stronie(ta z for'a)
 *
 * return: aktualizuje tag wyswietlajacy ilosc oddanych glosow
 *         wyswietla srednia glosow na panelu z gwiazdkami
 */
function AddVote(_modul, _id, _ocena, _index){
	if(getCookie(_modul+_id) == ""){
		setCookie(_modul+_id,'addVote', 30);
        /*
        document.getElementById('tresc_'+_index).removeAttribute("onmouseout");
		
		for (var i = 1; i <= 5; i++) {
                        //alert(_modul+ "_"+_index + "_" + i);
			document.getElementById("tresc_"+_index + "_" + i).removeAttribute("onmouseover");
			document.getElementById("tresc_"+_index + "_" + i).removeAttribute("onclick");
		*/
		}
		url = '?c=mdOceny-cmDodajOcene';
		new Ajax(url, {
			method: 'post',
			data: Object.toQueryString({
				modul: _modul,
				id: _id,
				ocena: _ocena
			}),
			update: $("kontener_glosowania_"+_index)
                        
		}).request();

}

function setAVG(_index, _value){
	tabi[_index] = _value;
}
var tabi = new  Array('.$iLiczbaWynikow.');

function getImage(star_id, srednia){
	var a = new Array(3);
	a[0] = '/Zasoby/Grafiki/Vote/star-fill-all.png';
	a[1] = '/Zasoby/Grafiki/Vote/star-fill-half.png'; 
	a[2] = '/Zasoby/Grafiki/Vote/star-fill-none.png';
	var out = '';
	var calkowita = Math.floor(srednia);
	var ulamek = srednia - calkowita;
	
	if (ulamek > 0.5){
		if (ulamek > 0.75)
			{
				ulamek = 0.0;
			    calkowita++;
			}
		else
			ulamek = 0.5;
	}
	else {
		if(ulamek > 0.25)
			ulamek = 0.5;
		else ulamek = 0.0;
	}			
	
	if(star_id <= calkowita){
		out = a[0];
	}
	else {
		if(calkowita+ulamek+1 > star_id)
			out = a[1];
		else out = a[2];
	}
	
	return out;
}

function ChangePicture(element){
				
                var indexSelected = parseInt(element.charAt(element.length-1));
				var i;
				var tmp = element.substr(0, element.length-1);
				for(i = 1; i<=indexSelected; i++){					
					document.getElementById(tmp + i).src = "/Zasoby/Grafiki/Vote/star-fill-all.png";
				}
				if(i<=5){
					for(i; i<=5; i++)
						document.getElementById(tmp + i).src = "/Zasoby/Grafiki/Vote/star-fill-none.png";	 
				}
            }
			
function RestoreImageStatus(element, i){
	/*TODO: poprawić odczytywanie z elementu indeksu
	 * 
	 */
				var position = element.indexOf('_');
				var index = element.substring(position+1, element.length);
				//var index = element.charAt(element.length -1);
				for(var i = 1; i<=5; i++){
					document.getElementById(element+'_'+i).src = getImage(i, tabi[index]);
				}
			}	
