//Votes javascript file
$(document).ready(function() {
    $(".emptystar, .fullstar").hover(
        function() {
            var pageType = getItemId(this, 0);
            var articleId = getItemId(this, 1);
            var rateTxt;
            if(this.className == "emptystar") this.className = "fullstar";
            switch(this.title){
                case '1' : rateTxt = "Слабо"; break;
                case '2' : rateTxt = "Нищо специално"; break;
                case '3' : rateTxt = "Добре"; break;
                case '4' : rateTxt = "Много добре"; break;
                case '5' : rateTxt = "Страхотно"; break;
                default : rateTxt = "Неопределено";
            }
            
           
                for(var i = this.title; i >= 1; i--) {
                        var itemId = pageType + "_" + articleId + "_rate_" + i;
                    document.getElementById(itemId).className = "fullstar";
                }
            
            var tmpSpanId = "vote_label_" + articleId;
            var spanElm = document.createElement("div");
            spanElm.id = tmpSpanId;
            spanElm.className = "rateLabel";
            spanElm.appendChild(document.createTextNode(rateTxt));
            this.parentNode.appendChild(spanElm);
            var lastNode = last_child(this.parentNode);
            $(lastNode).before(spanElm);
        },
        function() {
            var pageType = getItemId(this, 0);
            var articleId = getItemId(this, 1);
            var rateVal = $("#main_article_rate_" + articleId).html();
            var tmpSpanId = "#vote_label_" + articleId;
            $(tmpSpanId).remove();
            this.className = "emptystar";
            
            for(var i = 5; i >= 1; i--) {
                        var itemId = pageType + "_" + articleId + "_rate_" + i;
                  
                if(i <= rateVal) {
                    document.getElementById(itemId).className = "fullstar";
                }
                else {
                    document.getElementById(itemId).className = "emptystar" ;
                }
            }
            
        }
    )
    
    $(".emptystar, .fullstar").click(function() {
        var itemType = getItemId(this, 0);
        var articleId = getItemId(this, 1);
        var voteResult = getItemId(this, 3);
        
        $.ajax({
            cache : false,
            type : "get",
            url : "remote.php",
            data : "action=vote&article=" + articleId + "&vote=" + voteResult + "&key=" + itemType,
            success : function(msg) {
                var result = eval(msg);
                if(result.result) {
                    $("#main_article_rate_" + articleId).text(result.rate);
                    $("#main_article_voters_" + articleId).text(result.voters);
                    alert("Благодарим Ви, че гласувахте!");
                }
                else {
                    alert("Вие сте гласували или не сте регистриран!");
                }
            }
        })
        
    })
    
})
