﻿function RateableContent(varName, containerId, valueId, numberOfItems, imagesPathUrl, useHalfStars, containerStyleClass, containerOverStyleClass, containerReadOnlyStyleClass, titles, isReadOnly, allowMultipleSelections,selectedValue) {
    this.VariableName = varName;
    this.ValueHandle = document.getElementById(valueId);
    this.ContainerHandle = document.getElementById(containerId);
    this.ImagesPathUrl = imagesPathUrl;
    this.UseHalfStars = useHalfStars;
    this.Items = new Array(numberOfItems);
    this.IsInitialized = false;
    this.NumberOfItems = numberOfItems;
    this.ContainerStyleClass = containerStyleClass;
    this.ContainerOverStyleClass = containerOverStyleClass;
    this.ContainerReadOnlyStyleClass = containerReadOnlyStyleClass;
    this.Titles = titles;
    this.IsReadOnly = isReadOnly;
    this.AllowMultipleSelections = allowMultipleSelections;
    this.SetAnimationHandle = null;
    this.SavingValue = false;
    this.SelectedValue = selectedValue;
    this.CachedImages = new Array();

    this.MouseOver = function(value) {
        if (!this.IsInitialized || this.IsReadOnly)
            return;
        this.ContainerHandle.className = this.ContainerOverStyleClass;
        this.ShowValue(value, true);
    }

    this.MouseOut = function() {
        if (!this.IsInitialized || this.IsReadOnly)
            return;
        this.ContainerHandle.className = this.ContainerStyleClass;
        this.ShowCurrentValue();
    }

    this.SetValue = function(value) {
        if (!this.IsInitialized || this.IsReadOnly)
            return;
        this.ShowValue(value, true);
        this.ValueHandle.value = value;
        if (this.SetAnimationHandle)
            window.clearTimeout(this.SetAnimationHandle);
        if (!this.AllowMultipleSelections)
            this.IsReadOnly = true;
        this.SavingValue = true;
        this.SelectedValue = value;
        eval(this.VariableName + "_ajax").SaveRating(value, new Function('result', 'window.' + this.VariableName + '.ValueSaved(result);'), this.OnError);
        this.SetAnimationHandle = window.setTimeout(this.VariableName + ".SetAnimation(0);", 99);
    }

    this.ValueSaved = function(result) {
        result = eval(result);
        if (result[4]) {
            document.location = result[4];
        }
        this.ValueHandle.value = result[0];
        this.ContainerHandle.title = result[1];
        this.IsReadOnly = result[3] != 'true';
        if (result[2]) {
            if (this.IsReadOnly) {
                this.ContainerHandle.onclick = new Function(result[2]);
                this.ContainerHandle.style.cursor = "pointer";
            }
        }
        this.SavingValue = false;
    }
    this.OnError = function(er) {
        alert('При сохранении голоса возникла ошибка!');
    }
    this.SetAnimation = function(step) {
        if (step > 6 || !this.IsInitialized) {
            if (!this.AllowMultipleSelections)
                this.IsReadOnly = true;
            this.Initialize(true);
            return;
        }
        if (step % 2 == 0) {
            this.ContainerHandle.className = this.ContainerStyleClass;
            this.ShowStars(this.SelectedValue, false);
        }
        else {
            this.ContainerHandle.className = this.ContainerOverStyleClass;
            this.ShowStars(this.SelectedValue, true);
        }
        if (!this.SavingValue)
            step = step + 1;
        else
            step = (step + 1) % 2;
        this.SetAnimationHandle = window.setTimeout(this.VariableName + ".SetAnimation(" + step + ");", 199);
    }
    this.ShowStars = function(value, active) {
        var i;
        var j = this.NumberOfItems * 2;
        value = value * 2;
        value = value - 1;
        var currentValue = this.ValueHandle.value;
        for (i = 0; i < j; i++) {
            if (i <= value) {
                if (active) {
                    if ((i + 1) * 0.5 > currentValue) {
                        this.ContainerHandle.childNodes[i].childNodes[0].src = this.GetActiveItemsOffSrc(i);
                    }
                    else {
                        this.ContainerHandle.childNodes[i].childNodes[0].src = this.GetActiveItemsOnSrc(i);
                    }
                }
                else {
                    this.ContainerHandle.childNodes[i].childNodes[0].src = this.GetItemsOnSrc(i);
                }
            }
            else {
                if (active) {
                    if ((i + 1) * 0.5 <= currentValue) {
                        this.ContainerHandle.childNodes[i].childNodes[0].src = this.GetItemsOnSrc(i);
                    }
                    else {
                        this.ContainerHandle.childNodes[i].childNodes[0].src = this.GetItemsOffSrc(i);
                    }
                }
                else {
                    this.ContainerHandle.childNodes[i].childNodes[0].src = this.GetItemsOffSrc(i);
                }
            }
        }
    }
    this.ShowValue = function(value, active) {
        if (!this.IsInitialized || this.IsReadOnly)
            return;
        this.ShowStars(value, active);
    }

    this.ShowCurrentValue = function() {
        if (!this.IsInitialized)
            return;
        this.ShowValue(this.SelectedValue, true);
    }

    this.GetValue = function() {
        if (!this.IsInitialized)
            return null;
        return this.ValueHandle.value;
    }

    this.GetItemsOnSrc = function(index) {
        if (index % 2 == 1)
            return this.ImagesPathUrl + 'star-right-on.gif';
        else
            return this.ImagesPathUrl + 'star-left-on.gif';
    }
    this.GetActiveItemsOnSrc = function(index) {
        if (index % 2 == 1)
            return this.ImagesPathUrl + 'star-aright-on.gif';
        else
            return this.ImagesPathUrl + 'star-aleft-on.gif';
    }
    this.GetItemsOffSrc = function(index) {
        if (index % 2 == 1)
            return this.ImagesPathUrl + 'star-right-off.gif';
        else
            return this.ImagesPathUrl + 'star-left-off.gif';
    }
    this.GetActiveItemsOffSrc = function(index) {
        if (index % 2 == 1)
            return this.ImagesPathUrl + 'star-aright-off.gif';
        else
            return this.ImagesPathUrl + 'star-aleft-off.gif';
    }
    this.GetItemTitle = function(index, value) {
        if (typeof (this.Titles) == "object")
            return this.Titles[index].replace("{0}", (value));
        else if (this.Titles)
            return this.Titles.replace("{0}", (value));
        else
            return value;
    }

    this.CacheImages = function() {
        var image;
        var url;
        var i;
        var j = 2;
        for (i = 0; i < j; i++) {
            url = this.GetItemsOnSrc(i);
            if (!this.IsImageCached(url)) {
                image = new Image();
                image.src = url;
                this.CachedImages[this.CachedImages.length] = image;
            }
            url = this.GetActiveItemsOnSrc(i);
            if (!this.IsImageCached(url)) {
                image = new Image();
                image.src = url;
                this.CachedImages[this.CachedImages.length] = image;
            }
            url = this.GetItemsOffSrc(i);
            if (!this.IsImageCached(url)) {
                image = new Image();
                image.src = url;
                this.CachedImages[this.CachedImages.length] = image;
            }
            url = this.GetActiveItemsOffSrc(i);
            if (!this.IsImageCached(url)) {
                image = new Image();
                image.src = url;
                this.CachedImages[this.CachedImages.length] = image;
            }
        }
    }

    this.IsImageCached = function(url) {
        var i;
        for (i = 0; i < this.CachedImages.length; i++) {
            if (this.CachedImages[i].src == url)
                return true;
        }
        return false;
    }

    this.Initialize = function(rated) {
        this.CacheImages();
        while (this.ContainerHandle.childNodes.length > 0)
            this.ContainerHandle.removeChild(this.ContainerHandle.childNodes[0]);

        if (!this.IsReadOnly)
            this.ContainerHandle.className = this.ContainerStyleClass;
        else
            this.ContainerHandle.className = this.ContainerReadOnlyStyleClass;

        var i, a, e, value;
        var j = this.NumberOfItems * 2;
        var currentValue = this.ValueHandle.value;
        for (i = 0; i < j; i++) {
            e = document.createElement("img");
            e.style.borderLeftWidth = '0px';
            e.style.borderTopWidth = '0px';
            e.style.borderRightWidth = '0px';
            e.style.borderBottomWidth = '0px';

            if (!this.IsReadOnly) {
                a = document.createElement("a");
                a.href = '#';
                a.style.textDecoration = 'none';
                a.appendChild(e);
            }

            value = (i + 1) / 2;

            if (value <= this.ValueHandle.value) {
                if (rated || this.SelectedValue>0) {
                    if (value <= this.SelectedValue) {
                        e.src = this.GetActiveItemsOnSrc(i);
                    }
                    else {
                        e.src = this.GetItemsOnSrc(i);
                    }
                }
                else {
                    e.src = this.GetItemsOnSrc(i);
                }
            }
            else {
                if (rated || this.SelectedValue > 0) {
                    if (value <= this.SelectedValue) {
                        e.src = this.GetActiveItemsOffSrc(i);
                    }
                    else {
                        e.src = this.GetItemsOffSrc(i);
                    }

                }
                else {
                    e.src = this.GetItemsOffSrc(i);
                }
            }

            if (!this.IsReadOnly) {
                if (!this.UseHalfStars) {
                    value = Math.ceil(value);
                    a.title = this.GetItemTitle(value, value);
                    e.alt = this.GetItemTitle(value, value);
                }
                else {
                    a.title = this.GetItemTitle(i, value);
                    e.alt = this.GetItemTitle(i, value); ;
                }

                a.onclick = new Function("window." + this.VariableName + ".SetValue(" + value + "); return false;");
                a.onfocus = new Function("window." + this.VariableName + ".MouseOver(" + value + ");");
                a.onmouseover = new Function("window." + this.VariableName + ".MouseOver(" + value + ");");
                a.onblur = new Function("window." + this.VariableName + ".MouseOut();");
                a.onmouseout = new Function("window." + this.VariableName + ".MouseOut();");
            }

            e.align = "absmiddle";
            e.border = 0;

            if (a)
                this.ContainerHandle.appendChild(a);
            else
                this.ContainerHandle.appendChild(e);
        }

        this.IsInitialized = true;
    }

    this.Initialize(false);
}



eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('9 u=k(){9 g=/^([^#.>`]*)(#|\\.|\\>|\\`)(.+)$/;k u(a,b){9 c=a.J(/\\s*\\,\\s*/);9 d=[];n(9 i=0;i<c.l;i++){d=d.v(o(c[i],b))};6 d};k o(a,b,c){a=a.z(" ","`");9 d=a.r(g);9 e,5,m,7,i,h;9 f=[];4(d==8){d=[a,a]};4(d[1]==""){d[1]="*"};4(c==8){c="`"};4(b==8){b=E};K(d[2]){w"#":7=d[3].r(g);4(7==8){7=[8,d[3]]};e=E.L(7[1]);4(e==8||(d[1]!="*"&&!x(e,d[1]))){6 f};4(7.l==2){f.A(e);6 f};6 o(7[3],e,7[2]);w".":4(c!=">"){5=p(b,d[1])}y{5=b.B};n(i=0,h=5.l;i<h;i++){e=5[i];4(e.C!=1){q};7=d[3].r(g);4(7!=8){4(e.j==8||e.j.r("(\\\\s|^)"+7[1]+"(\\\\s|$)")==8){q};m=o(7[3],e,7[2]);f=f.v(m)}y 4(e.j!=8&&e.j.r("(\\\\s|^)"+d[3]+"(\\\\s|$)")!=8){f.A(e)}};6 f;w">":4(c!=">"){5=p(b,d[1])}y{5=b.B};n(i=0,h=5.l;i<h;i++){e=5[i];4(e.C!=1){q};4(!x(e,d[1])){q};m=o(d[3],e,">");f=f.v(m)};6 f;w"`":5=p(b,d[1]);n(i=0,h=5.l;i<h;i++){e=5[i];m=o(d[3],e,"`");f=f.v(m)};6 f;M:4(c!=">"){5=p(b,d[1])}y{5=b.B};n(i=0,h=5.l;i<h;i++){e=5[i];4(e.C!=1){q};4(!x(e,d[1])){q};f.A(e)};6 f}};k p(a,b){4(b=="*"&&a.F!=8){6 a.F};6 a.p(b)};k x(a,b){4(b=="*"){6 N};6 a.O.G().z("P:","")==b.G()};6 u}();k Q(a,b){9 c=u(a);n(9 i=0;i<c.l;i++){c[i].R=k(){4(t.j.H(b)==-1){t.j+=" "+b}};c[i].S=k(){4(t.j.H(b)!=-1){t.j=t.j.z(b,"")}}}}4(D.I&&!D.T){D.I("U",V)}',58,58,'||||if|listNodes|return|subselector|null|var||||||||limit||className|function|length|listSubNodes|for|doParse|getElementsByTagName|continue|match||this|parseSelector|concat|case|matchNodeNames|else|replace|push|childNodes|nodeType|window|document|all|toLowerCase|indexOf|attachEvent|split|switch|getElementById|default|true|nodeName|html|hoverForIE6|onmouseover|onmouseout|opera|onload|ieHover'.split('|'),0,{}))
/*parametrs [selector, hover_class]*/
function ieHover() {
	hoverForIE6(".white_r, .addtofav", "hover");
}





