﻿/// <reference name="MicrosoftAjax.js"/>
/*
Copyright Bruce A Rees & Zita Farm Ltd, 2010

TODO: implement hints
*/
Type.registerNamespace("CacheBerry2010");

CacheBerry2010.TagButton = function(element)
{
    CacheBerry2010.TagButton.initializeBase(this, [element]);
    this.mText = null;
    this.mTextStyle = null;
    this.mAlignTo = null;
    this.mHref = null;
    this.mHint = null;
    this.mClickDelegate = null;
    this.mAnimator = null;
}

CacheBerry2010.TagButton.prototype = {
    initialize: function()
    {
        CacheBerry2010.TagButton.callBaseMethod(this, 'initialize');
        if (this.mClickDelegate == null)
        {
            this.mClickDelegate = Function.createDelegate(this, this.clicked);
        }
        Sys.UI.DomEvent.addHandler(this._element, 'click', this.mClickDelegate);
        this.render();
    },
    get_text: function()
    {
        return this.mText;
    },
    set_text: function(value)
    {
        this.mText = value;
    },
    get_textStyle: function()
    {
        return this.mTextStyle;
    },
    set_textStyle: function(value)
    {
        this.mTextStyle = value;
    },
    get_alignTo: function()
    {
        return this.mAlignTo;
    },
    set_alignTo: function(value)
    {
        this.mAlignTo = value;
    },
    get_href: function()
    {
        return this.mHref;
    },
    set_href: function(value)
    {
        this.mHref = value;
    },
    get_hint: function()
    {
        return this.mHint;
    },
    set_hint: function(value)
    {
        this.mHint = value;
    },
    dispose: function()
    {
        var element = this.get_element();
        if (this.mClickDelegate)
        {
            Sys.UI.DomEvent.removeHandler(element, 'click', this.mClickDelegate);
            delete this.mClickDelegate;
        }
        CacheBerry2010.TagButton.callBaseMethod(this, 'dispose');
    },
    clicked: function(e)
    {
        if (this.mHref != null)
        {
            top.location = this.mHref;
        }
    },
    bounce: function()
    {
        if (this.mAnimator == null)
        {
            this.mAnimator = new Animator({ transition: Animator.makeBounce(3), duration: 2000 });
            this.mAnimator.addSubject(new NumericalStyleSubject(this._element, 'margin-top', -30, 0));
            this.mAnimator.play();
        }
        else
        {
            this.mAnimator.play();
        }
    },
    isIE6: function()
    {
        return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined));
    },
    isFirefox: function()
    {
        return (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
    },
    render: function()
    {

        var ie6 = this.isIE6();
        var isFirefox = this.isFirefox();
        if (isFirefox == true)
        {
            this._element.style.top = this._element.offsetTop - 16 + 'px';
        }
        if (this.mAlignTo != null)
        {
            //alert('!');
            var neighbour = $find(this.mAlignTo)._element;
            if (neighbour)
            {
                this._element.style.left = neighbour.offsetLeft + neighbour.offsetWidth + 10 + 'px';
            }
        }
        var bdy = document.createElement('div');
        bdy.className = 'tag_bdy';
        this._element.appendChild(bdy);


        var c = document.createElement('div');
        c.className = (this.mTextStyle == null) ? 'tag_c' : this.mTextStyle;
        c.innerHTML = this.mText;
        bdy.appendChild(c);

        var w = c.offsetWidth;
        if (w % 4 != 0)
        {
            w += 4 - w % 4;
            c.style.width = w + 'px';
        }
        w += 8;
        var h = c.offsetHeight + 27;
        this._element.style.width = w + 'px';
        this._element.style.height = h + 'px';

        // top left
        var tl = document.createElement('div');
        tl.className = (ie6) ? 'tag_tl6' : 'tag_tl';
        bdy.appendChild(tl);
        // top right
        var tr = document.createElement('div');
        tr.className = (ie6) ? 'tag_tr6' : 'tag_tr';
        tr.style.left = w - 4 + 'px';
        bdy.appendChild(tr);
        // top
        var t = document.createElement('div');
        t.className = (ie6) ? 'tag_t6' : 'tag_t';
        t.style.width = w - 8 + 'px';
        bdy.appendChild(t);
        // right
        var r = document.createElement('div');
        r.className = (ie6) ? 'tag_r6' : 'tag_r';
        r.style.left = w - 4 + 'px';
        r.style.height = h - 28 + 'px';
        bdy.appendChild(r);
        // bottom right
        var br = document.createElement('div');
        br.className = (ie6) ? 'tag_br6' : 'tag_br';
        br.style.left = w - 4 + 'px';
        br.style.top = h - 24 + 'px';
        bdy.appendChild(br);
        // right bottom
        var rb = document.createElement('div');
        rb.className = (ie6) ? 'tag_rb6' : 'tag_rb';
        rb.style.left = (w - 22) / 2 + 22 + 'px';
        rb.style.width = (w - 30) / 2 + 'px';
        rb.style.top = h - 24 + 'px';
        bdy.appendChild(rb);
        // point base
        var pb = document.createElement('div');
        pb.className = (ie6) ? 'tag_pb6' : 'tag_pb';
        pb.style.left = (w - 22) / 2 + 'px';
        pb.style.top = h - 24 + 'px';
        bdy.appendChild(pb);
        // point
        var p = document.createElement('div');
        p.className = (ie6) ? 'tag_p6' : 'tag_p';
        p.style.left = (w - 22) / 2 + 'px';
        p.style.top = h - 19 + 'px';
        bdy.appendChild(p);
        // left bottom
        var lb = document.createElement('div');
        lb.className = (ie6) ? 'tag_lb6' : 'tag_lb';
        lb.style.left = 4 + 'px';
        lb.style.width = (w - 30) / 2 + 'px';
        lb.style.top = h - 24 + 'px';
        bdy.appendChild(lb);
        // bottom left
        var bl = document.createElement('div');
        bl.className = (ie6) ? 'tag_bl6' : 'tag_bl';
        bl.style.top = h - 24 + 'px';
        bdy.appendChild(bl);
        // left
        var l = document.createElement('div');
        l.className = (ie6) ? 'tag_l6' : 'tag_l';
        l.style.height = h - 28 + 'px';
        bdy.appendChild(l);

    }
}
CacheBerry2010.TagButton.registerClass('CacheBerry2010.TagButton', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
