function $(o){
    return ((typeof(o) == 'object' ? o : document).getElementById(o));
}

function $S(o){
    return ($(o).style);
}

function agent(v){
    return (Math.max(navigator.userAgent.toLowerCase().indexOf(v), 0));
}

function abPos(o){
    var o = (typeof(o) == 'object' ? o : $(o)), z = {
        X: 0,
        Y: 0
    };
    while (o != null) {
        z.X += o.offsetLeft;
        z.Y += o.offsetTop;
        o = o.offsetParent;
    };
    return (z);
}

function XY(e, v){
    var o = agent('msie') ? {
        'X': event.clientX + document.body.scrollLeft,
        'Y': event.clientY + document.body.scrollTop
    } : {
        'X': e.pageX,
        'Y': e.pageY
    };
    return (v ? o[v] : o);
}

var scripts = new Array();
scripts[0] = "Poor";
scripts[1] = "Average";
scripts[2] = "Good";
scripts[3] = "Excellent";
scripts[4] = "Awesome";
scripts[5] = "Good";
scripts[6] = "Very Good";
scripts[7] = "Great";
scripts[8] = "Excellent";
scripts[9] = "Super Excellent";
var numberofstars = 5;
var imagewidth = 17;
var starswidth = numberofstars * imagewidth;
function UpdateRate(data, n){
    $('lcrate' + n).innerHTML = data;
    alert('Thanks for voting!');
}

function ajax(url, vars, callbackFunction, cbParam1){
    var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
    request.open("POST", url, true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.onreadystatechange = function(){
        if (request.readyState == 4 && request.status == 200) {
            if (request.responseText) {
                callbackFunction(request.responseText, cbParam1);
            }
        }
    };
    request.send(vars);
}

function getRank(oX){
    var index = oX / starswidth * numberofstars;
    var num = new Number(index);
    var str = new String(num.toString());
    if (index == numberofstars) {
        index = index - 1;
    }
    else {
        if (str.length == 1) {
            index = index - 1;
        }
        else {
            if (index == 10) {
                alert(str);
            }
            index = Math.floor(index);
        }
    }
    return (index);
}

var star = {
    'cur': function(e, o){
        if (star.stop) {
            star.stop = 0;
            document.onmousemove = function(e){
                var n = star.num;
                starswidth = numberofstars * imagewidth;
                var p = abPos($('star' + n)), x = XY(e), oX = x.X - p.X, oY = x.Y - p.Y;
                star.num = o.id.substr(4);
                if (oX < 1 || oX > starswidth || oY < 0 || oY > 19) {
                    star.stop = 1;
                    star.revert();
                }
                else {
                    $S('starCur' + n).width = oX + 'px';
                    $S('starUser' + n).color = '#111';
                    $('starUser' + n).innerHTML = scripts[getRank(oX)];
                }
            };
        }
    },
    'update': function(e, o){
        var n = star.num, p = abPos($('star' + n));
        x = XY(e), oX = x.X - p.X, oY = x.Y - p.Y;
        star.num = o.id.substr(4);
        starswidth = numberofstars * imagewidth;
        n = o.id.substr(4);
        if (oX < 1 || oX > starswidth || oY < 0 || oY > 19) {
            star.stop = 1;
            star.revert();
        }
        else {
            $('starCur' + n).innerHTML = getRank(oX);
            $('starCur' + n).title = oX;
            ajax('http://www.netsalon.com/desktopmodules/LiveContent/rate.aspx?vote=' + oX + '&uid=' + n, '', UpdateRate, n);
        }
    },
    'revert': function(){
        var n = star.num, v = parseInt($('starCur' + n).title);
        starswidth = numberofstars * imagewidth;
        $S('starCur' + n).width = Math.round((v / (100 / numberofstars)) * imagewidth) + 'px';
        $('starUser' + n).style.color = '#888';
        $('starUser' + n).innerHTML = '&nbsp;';
        document.onmousemove = '';
    },
    'stop': 1,
    'num': 0
};
