/**
 * @author iPrior
 */
var avatarTwist = {
    interval: 7500,
    timer: false,
    elementID: 'twister_td',
    index: false,
    requestURL: '/json/avatars.json',
    arrayJSON: false,
    template: new Template('<div><a href="/users/#{name}/" onmouseover="Tip(\'#{info}\')" onmouseout="UnTip()"><img src="/img/ramka_100x100.png" style="background: transparent url(#{avatar}) no-repeat scroll center center; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; width: 110px; height: 110px;"></a><span href="/users/#{name}/">#{name}</span><input id="twister_id" value="#{id}" type="hidden"><a class="back" href=" javascript: void(0);" onclick="avatarTwist.Back()"><em><< back</em></a> <a class="next" href=" javascript: void(0);" onclick="avatarTwist.Next()"><em>next >></em></a></div>'),
    Start: function(){
        if ($(this.elementID)) {
            this.Machine();
        }
        else {
            setTimeout('avatarTwist.Start()', 1000);
        }
        return;
    },
    Machine: function(){
        if (!this.arrayJSON) {
            this.RequestJSON();
        }
        else {
            var show = {
                id: this.arrayJSON[this.index].id,
                name: this.arrayJSON[this.index].name,
                info: this.arrayJSON[this.index].info,
                avatar: this.arrayJSON[this.index].avatar
            };
			this.setCookie(this.arrayJSON[this.index].id);
            $(this.elementID).update(this.template.evaluate(show));
            if (!this.timer) 
                this.timer = setInterval('avatarTwist.Next()', this.interval)
        }
        return;
    },
    RequestJSON: function(){
        new Ajax.Request(this.requestURL, {
            onFailure: function(){
                setTimeout('avatarTwist.RequestJSON()', 1000);
            },
            onComplete: function(transport){
                avatarTwist.arrayJSON = transport.responseText.evalJSON();
                if (avatarTwist.getCookie()) {
                    for (var i = 0; i < avatarTwist.arrayJSON.length; i++) {
                        if (avatarTwist.arrayJSON[i].id == avatarTwist.getCookie()) {
                            avatarTwist.index = i+1;
							break;
                        }
                    }
                }
                avatarTwist.QueryIndex();
                avatarTwist.Machine();
            }
        });
    },
    QueryIndex: function(){
        if (this.index == false || (this.index + 1) >= this.arrayJSON.length) {
            this.index = 0;
        }
    },
    Next: function(){
        if (this.timer) {
            clearInterval(this.timer);
            this.timer = false;
        }
        this.index++;
        if (this.index >= this.arrayJSON.length) 
            this.index = 0;
        this.Machine();
    },
    Back: function(){
        if (this.timer) {
            clearInterval(this.timer);
            this.timer = false;
        }
        this.index--;
        if (this.index < 0) 
            this.index = (this.arrayJSON.length - 1);
        this.Machine();
    },
    setCookie: function(id){
        var value = escape(id);
        var name = 'avatarTwist';
        document.cookie = name + "=" + escape(value) + "; path=/";
    },
    getCookie: function(){
        {
            var name = 'avatarTwist';
            cookie_name = name + "=";
            cookie_length = document.cookie.length;
            cookie_begin = 0;
            while (cookie_begin < cookie_length) {
                value_begin = cookie_begin + cookie_name.length;
                if (document.cookie.substring(cookie_begin, value_begin) == cookie_name) {
                    var value_end = document.cookie.indexOf(";", value_begin);
                    if (value_end == -1) {
                        value_end = cookie_length;
                    }
                    return unescape(document.cookie.substring(value_begin, value_end));
                }
                cookie_begin = document.cookie.indexOf(" ", cookie_begin) + 1;
                if (cookie_begin == 0) {
                    break;
                }
            }
            return null;
        }
    }
};
avatarTwist.Start();
