﻿/**
* Masque Layout Library 0.0.1.0
* Copyright(c) 2008, 青岛领创网络科技有限公司
* @author Lookingon Team
*/

/// <reference path="Jquery/jquery.js" />
/// <reference path="MBase.js" />

//使用说明，必须保证控件加载完成后再调用类进行操作。
if (!Masque.L) {
    Masque.L = {};
}

Masque.L.ImageRating = Class();
Masque.L.ImageRating.extend({
    initialize: function(RatingBox, options) {
        this.option = {
            Filter: "img", //图片筛选器
            LightImage: "/images/Rating1.jpg", //变亮时的图片。
            DarkImage: "/images/Rating0.jpg", //变暗时的图片
            Outer: "",
            DefaultText: "",
            Event: "mouseover",
            TitleFormat: "",
            Reselect: true,
            OnSelect: null,
            SelectedIndex: -1,
            IsApprise: false
        };
        this.RatingBox = $(RatingBox);
        this.option.DarkImage = this.RatingBox.find(this.option.Filter).eq(0).attr("src");
        this.SetOption(options);
        if (!this.option.DefaultText || this.option.DefaultText == "") {
            if (this.option.Outer) {
                this.option.DefaultText = $(this.option.Outer).html();
            }
        }

        this.__bindEvents();
        this.__setState();
        if (this.option.SelectedIndex > -1) {
            var i = this.option.SelectedIndex;
            this.option.SelectedIndex = -1;
            this.____Hover(i);
            this.option.SelectedIndex = i;
        }
    },
    __bindEvents: function(first) {
        var event;
        var me = this;
        var sts = this.RatingBox.find(this.option.Filter);
        if (this.option.Event.toLowerCase() == "mouseover") {
            sts.bind("mouseover", me, me.__EventHover).bind("mouseout", me, me.__EventClearHover);
        }
        sts.bind("click", me, me.__EventSelect);
    },
    __setState: function() {
        var me = this;
        var test = ["非常不满意", "不满意", "一般", "满意", "非常满意"];
        this.RatingBox.find(this.option.Filter).each(function(index, el) {
            var e = $(el);

            if (me.option.TitleFormat != "") {
                if (me.option.IsApprise) {
                    e.attr("title", me.option.TitleFormat.replace('{0}', test[index]));
                }
                else {
                    e.attr("title", me.option.TitleFormat.replace('{0}', (index + 1) + '星'));
                }

                //e.attr("title", me.option.TitleFormat.replace('{0}', index + 1));

            }
            e.attr("src", me.option.DarkImage);
        });
        this.__SetOuter(this.option.DefaultText);

    },
    __EventSelect: function(handler) {

        var rating = handler.data;

        if (rating.option.Reselect != true && rating.option.SelectedIndex >= 0)
            return;
        rating.option.SelectedIndex = -1;
        var index = rating.RatingBox.find(rating.option.Filter).index(this);

        rating.____Hover(index);
        rating.option.SelectedIndex = index;
        if (rating.option.OnSelect != null) {
            rating.option.OnSelect(index, rating);
        }
        if (rating.option.IsApprise) {//评价页面
            rating.RatingBox.parent().attr("style", "width:auto;");
        }

    },
    __SetOuter: function(str) {
        if (this.option.Outer) {
            $(this.option.Outer).html(str);
        }

    },
    ____Hover: function(index) {
        if (this.option.SelectedIndex >= 0)
        { return; }


        if (isNaN(index))
            index = -1;
        var me = this;
        this.RatingBox.find(this.option.Filter).each(function(idx, el) {
            e = $(el);
            if (idx <= index) {
                e.attr("src", me.option.LightImage);
                if (idx == index) {
                    me.__SetOuter(e.attr("title"));
                }
            }
            else
                e.attr("src", me.option.DarkImage);
        });
        if (index < 0) {
            this.__SetOuter(this.option.DefaultText);
        }
    },
    __EventHover: function(handler) {
        var rating = handler.data;
        var index = rating.RatingBox.find(rating.option.Filter).index(this);

        rating.____Hover(index);

        if (rating.option.IsApprise) {//评价页面
            rating.RatingBox.parent().attr("style", "width:auto;");
        }
    },
    __EventClearHover: function(handler) {
        var rating = handler.data;
        rating.____Hover(rating.option.SelectedIndex);
        if (rating.option.SelectedIndex < 0) {
            if (rating.option.IsApprise) {//评价页面
                rating.RatingBox.parent().removeAttr("style");
            }
        }

    }, Hover: function(index) {
        this.____Hovert(index);
    },
    Reset: function(option) {

        this.SelectIndex == -1;
        this.Hover(-1);
    },
    Value: function() {
        return this.option.SelectedIndex + 1;
    }
});
