<?xml version="1.0"?>
<!--
To do: 
 * clean up the startup model
-->

<!DOCTYPE dialog SYSTEM "chrome://foxmarks/locale/foxmarks.dtd">
<bindings xmlns="http://www.mozilla.org/xbl"
    xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    xmlns:xbl="http://www.mozilla.org/xbl">
    <binding id="xmarks-review-stars">
        <!-- content, property, method and event descriptions go here -->
        <content>
            <xul:vbox style="margin: 0px 0px" >
            <xul:box style="background: transparent url(chrome://foxmarks/skin/images/rating_stars.png) 0px 0px no-repeat; width: 75px; height: 15px; cursor: pointer;" />
                <xul:spacer flex="1" />
            </xul:vbox>
        </content>
        <handlers>
            <handler event="click" button="0">
                <![CDATA[
                if (!this.disabled) {
                    this.rating = this.starIndex(event);
                }
                if (!this.disabled || this.clickable) {
                    var handler = this.getAttribute("onstarsclick");
                    if (handler != "") {
                        var fn = new Function("event", handler);
                        fn.apply(this, [event]);
                    }
                }
                ]]>
            </handler>
            <handler event="mouseover">
                <![CDATA[
                this.mouseposition = this.starIndex(event);
                ]]>
            </handler>
            <handler event="mousemove">
                <![CDATA[
                this.mouseposition = this.starIndex(event);
                ]]>
            </handler>
            <handler event="mouseout">
                <![CDATA[
                this.mouseposition = 0;
                ]]>
            </handler>
        </handlers>
        <implementation>
            <property name="mouseposition">
                <getter>
                    return Number(this.getAttribute('mouseposition') || 0, 10);
                </getter>
                <setter>
                    <![CDATA[
                    var v = Number(String(val), 10);
                    if (isNaN(v) || v < 0 || v > 5) {
                        return this.getAttribute('mouseposition');
                    }
                    this.setAttribute('mouseposition', v);
                    if (!this.disabled) {
                        this.stars = v ? v : this.rating;
                    }
                    return v;
                    ]]>
                </setter>
            </property>
            <method name="starIndex">
                <parameter name="event" />
                <body>
                    return Math.floor((event.clientX - this.boxObject.x) / 
                        15) + 1;
                </body>
            </method>
            <method name="redraw">
                <body>
                    <![CDATA[
                    var img = document.getAnonymousNodes(this)[0].childNodes[0];
                    var offset = 32 * this.stars + 
                        ((this.usermode || (!this.disabled && 
                                    this.mouseposition)) ? 180 : 0); 
                    img.setAttribute("style", "background: transparent url(chrome://foxmarks/skin/images/rating_stars.png) 0px " + String(-offset) + "px no-repeat; width: 75px; height: 15px; cursor: pointer;");
                    ]]>
                </body>
            </method>
            <property name="rating">
                <getter>
                    return this.getAttribute('rating');
                </getter>
                <setter>
                    <![CDATA[
                    var v = Number(String(val), 10);
                    if (isNaN(v) || v < 0 || v > 5) {
                        return this.getAttribute('rating');
                    }
                    this.setAttribute('rating', v);
                    if (!this.mouseposition || this.disabled) {
                        this.stars = v;
                    }
                    return v;
                    ]]>
                </setter>
            </property>
            <property name="stars">
                <getter>
                    return this.getAttribute('stars');
                </getter>
                <setter>
                    <![CDATA[
                    var v = Number(val);
                    if (isNaN(v) || v < 0 || v > 5) {
                        return this.stars;
                    }
                    v = Math.round(v * 2) / 2.0;
                    this.setAttribute('stars', v);
                    this.redraw();
                    var controls = this.getAttribute('controls');
                    if (controls) {
                        document.getElementById(controls).stars = v;
                    }
                    return v;
                    ]]>
                </setter>
            </property>
            <property name="disabled">
                <getter>
                    return this.hasAttribute('disabled');
                </getter>
                <setter>
                    if (val) {
                        this.setAttribute('disabled', true);
                    } else {
                        this.removeAttribute('disabled');
                    }
                    this.redraw();
                </setter>
            </property>
            <property name="clickable">
                <getter>
                    return this.hasAttribute('clickable');
                </getter>
                <setter>
                    if (val) {
                        this.setAttribute('clickable', true);
                    } else {
                        this.removeAttribute('clickable');
                    }
                </setter>
            </property>
            <property name="usermode">
                <getter>
                    return this.hasAttribute('usermode');
                </getter>
                <setter>
                    if (val) {
                        this.setAttribute('usermode', true);
                    } else {
                        this.removeAttribute('usermode');
                    }
                    this.redraw();
                    return this.usermode;
                </setter>
            </property>
        </implementation>
    </binding>

    <binding id="xmarks-review-status">
        <content>
            <xul:hbox >
                <xul:label style="margin: 2px 0 0 6px; padding: 0;" />
                <xul:label class="text-link xmarks-review-link" 
                    style="margin: 2px 0 0; padding: 0; 
                        cursor: pointer !important;" 
                    xbl:inherits="href" href="http://www.xmarks.com/" />
                <xul:label style="margin: 2px 0 0; padding: 0;" />
                <xul:spacer flex="100" />
            </xul:hbox>
        </content>
        <implementation>
            <constructor>
                <![CDATA[
                if (this.hasAttribute('iRated') && 
                        this.getAttribute('iRated') != 'true') {
                    this.iRated = false;
                }
                this.updateText();
                ]]>
            </constructor>
            <method name="updateText">
                <body>
                    <![CDATA[
                    var n = Xmarks.Bundle().GetStringFromName;
                    var f = Xmarks.Bundle().formatStringFromName;

                    var str = "";

                    if (this.numratings > 0 && !this.iRated) {
                        str = f("reviews.otherrating", [this.numratings], 1);
                    } else if (this.numratings <= 1 && this.iRated) {
                        str = n("reviews.yourrating");
                    } else if (this.numratings > 1 && this.iRated) {
                        str = f("reviews.bothrating", [this.numratings], 1);
                    } else {
                        str = n("reviews.notyetrated");
                    }
                    var strs = str.split("|");
                    strs.push("");
                    strs.push("");
                    var box = document.getAnonymousNodes(this)[0];
                    box.childNodes[0].value = strs[0];
                    box.childNodes[1].value = strs[1];
                    box.childNodes[2].value = strs[2];
                    ]]>
                </body>
            </method>
            <property name="numratings">
                <getter>
                    return Number(this.getAttribute('numratings') || 0);
                </getter>
                <setter>
                    <![CDATA[
                    var v = Math.floor(Number(val));
                    if (isNaN(v) || v < 0) {
                        return this.numratings;
                    }
                    this.setAttribute('numratings', ''+v);
                    this.updateText();
                    return v;
                    ]]>
                </setter>
            </property>
            <property name="iRated"
                onget="return this.hasAttribute('iRated');">
                <setter>
                    <![CDATA[
                    var v = val && val != 'false';
                    if (v) {
                        this.setAttribute('iRated', 'true');
                    } else {
                        this.removeAttribute('iRated');
                    }
                    this.updateText();
                    return v;
                    ]]>
                </setter>
            </property>
            <property name="url">
                <setter>
                    <![CDATA[
                    var url = val;
                    var label = document.getAnonymousNodes(this)[0].
                        childNodes[1];
                    url = url.replace(/^http:\/\//i, "");
                    var href = Xmarks.gSettings.httpProtocol + 
                        Xmarks.gSettings.driftHost + "/site?url=" + 
                        encodeURIComponent(url) + "&cid=" +
                        this.getAttribute("cid") + 
                        "&product=xmarks&anchor=r";
                    label.href = href;
                    ]]>
                </setter>
            </property>
        </implementation>
    </binding>

    <binding id="xmarks-review">
        <content>
            <xul:vbox flex="1">
                <xul:hbox>
                    <xul:xmarks-review-stars xbl:inherits="rating,controls"
                        onstarsclick="document.getBindingParent(this).clicked();"/>
                    <xul:xmarks-review-status cid="xmfx.bd.review"
                        xbl:inherits="iRated,numratings,href,cid"/>
                </xul:hbox>
                <xul:hbox hidden="true">
                    <xul:xmarks-review-stars disabled="true" usermode="false" />
                    <xul:xmarks-review-status cid="xmfx.sbs.review" />
                </xul:hbox>
                <xul:textbox collapsed="true" multiline="true" rows="3"
                    xbl:inherits="value=reviewText" 
                    class="menulist-editable-box"
                    emptytext= "&reviews.emptytext;"
                    oninput="document.getBindingParent(this).changed = true" />
                    <xul:checkbox collapsed="false" 
                        label="&reviews.facebookshare;" />
            </xul:vbox>
        </content>
        <implementation>
            <property name="url" >
                <getter>
                    return this.getAttribute('url');
                </getter>
                <setter>
                    this.setAttribute('url', val);
                    this.status.url = val;
                </setter>
            </property>
            <field name="url_id" />
            <field name="fb_session" />
            <field name="changed" />
            <field name="title" />
            <field name="description" />
            <property name="stars">
                <getter>
                    var ann = document.getAnonymousNodes(this)[0];
                    return ann.childNodes[0].childNodes[0];
                </getter>
            </property>
            <property name="status">
                <getter>
                    var ann = document.getAnonymousNodes(this)[0];
                    return ann.childNodes[0].childNodes[1];
                </getter>
            </property>
            <property name="alt_row">
                <getter>
                    var ann = document.getAnonymousNodes(this)[0];
                    return ann.childNodes[1];
                </getter>
            </property>
            <property name="alt_stars">
                <getter>
                    return this.alt_row.childNodes[0];
                </getter>
            </property>
            <property name="alt_status">
                <getter>
                    return this.alt_row.childNodes[1];
                </getter>
            </property>
            <property name="textbox">
                <getter>
                    var ann = document.getAnonymousNodes(this)[0];
                    return ann.childNodes[2];
                </getter>
            </property>
            <property name="post_to_fb">
                <getter>
                    var ann = document.getAnonymousNodes(this)[0];
                    return ann.childNodes[3];
                </getter>
            </property>
            <property name="rating">
                <getter>
                    return this.stars.rating;
                </getter>
                <setter>
                    this.stars.rating = val;
                    if (this.stars.rating == val) {
                        this.status.iRated = true;
                        this.textbox.collapsed = false;
                        this.changed = true;
                    }
                    return this.stars.rating;
                </setter>
            </property>
            <property name="numratings">
                <setter>
                    this.status.numratings = val;
                </setter>
            </property>
            <property name="body">
                <getter>
                    return this.textbox.value;
                </getter>
                <setter>
                    this.textbox.value = val;
                    return val;
                </setter>
            </property>
            <property name="average">
                <setter>
                    this.alt_stars.rating = val;
                </setter>
            </property>
            <method name="clicked">
                <body>
                    this.stars.usermode = true;
                    this.status.iRated = true;
                    this.textbox.collapsed = false;
                    this.post_to_fb.collapsed = false;
                    this.changed = true;
                </body>
            </method>
            <method name="initialize">
                <parameter name="review" />
                <parameter name="title" />
                <parameter name="description" />
                <parameter name="fb_session" />
                <parameter name="post_to_fb" />
                <body>
                    <![CDATA[
                    this.status.numratings = review.numratings;
                    this.status.iRated = review.rating && review.rating != 0;
                    this.status.url = review.url;
                    this.stars.rating = review.rating || review.average || 0;
                    this.textbox.value = review.body || "";
                    this.textbox.collapsed =  !review.rating;
                    this.post_to_fb.collapsed = !review.rating;
                    this.changed = false;
                    this.url = review.url;
                    this.url_id = review.url_id;
                    this.title = title;
                    this.description = description;
                    this.stars.usermode = this.status.iRated;
                    if (this.status.iRated && this.status.numratings > 0) {
                        this.status.numratings -= 1;
                    }
                    this.fb_session = fb_session;
                    this.post_to_fb.checked = post_to_fb;
                    ]]>
                </body>
            </method>
        </implementation>
    </binding>
</bindings>

