/* Ten */
if (typeof(Ten) == 'undefined') {
    Ten = {};
}
Ten.NAME = 'Ten';
Ten.VERSION = 0.19;

/* Ten.Class */
Ten.Class = function(klass, prototype) {
    if (klass && klass.initialize) {
    var c = klass.initialize;
    } else if(klass && klass.base) {
        var c = function() { return klass.base[0].apply(this, arguments) };
    } else {
    var c = function() {};
    }
    c.prototype = prototype || {};
    c.prototype.constructor = c;
    Ten.Class.inherit(c, klass);
    if (klass && klass.base) {
        for (var i = 0;  i < klass.base.length; i++) {
        var parent = klass.base[i];
            if (i == 0) {
                c.SUPER = parent;
                c.prototype.SUPER = parent.prototype;
            }
            Ten.Class.inherit(c, parent);
            Ten.Class.inherit(c.prototype, parent.prototype);
        }
    }
    return c;
}
Ten.Class.inherit = function(child,parent) {
    for (var prop in parent) {
        if (typeof(child[prop]) != 'undefined' || prop == 'initialize') continue;
        child[prop] = parent[prop];
    }
}

/* Ten.JSONP */
Ten.JSONP = new Ten.Class({
    initialize: function(uri,obj,method) {
        if (Ten.JSONP.Callbacks.length) {
            setTimeout(function() {new Ten.JSONP(uri,obj,method)}, 500);
            return;
        }
        var del = uri.match(/\?/) ? '&' : '?';
        uri += del + 'callback=Ten.JSONP.callback';
        if (!uri.match(/timestamp=/)) {
            uri += '&' + encodeURI(new Date());
        }
        if (typeof(obj) == 'function' && typeof(method) == 'undefined') {
            obj = {callback: obj};
            method = 'callback';
        }
        if (obj && method) Ten.JSONP.addCallback(obj,method);
        this.script = document.createElement('script');
        this.script.src = uri;
        this.script.type = 'text/javascript';
        document.getElementsByTagName('head')[0].appendChild(this.script);
    },
    addCallback: function(obj,method) {
        Ten.JSONP.Callbacks.push({object: obj, method: method});
    },
    callback: function(args) {
        // alert('callback called');
        var cbs = Ten.JSONP.Callbacks;
        for (var i = 0; i < cbs.length; i++) {
            var cb = cbs[i];
            cb.object[cb.method].call(cb.object, args);
        }
        Ten.JSONP.Callbacks = [];
    },
    MaxBytes: 1800,
    Callbacks: []
});

/*
// Nicotag.Vote .* classes //
*/
if (typeof(Nicotag) == 'undefined') {
    Nicotag = {};
}
if (typeof(Nicotag.Vote) == 'undefined') {
    Nicotag.Vote = {};
}
Nicotag.Vote.VERSION = 0.1;

/* Nicotag.Vote.Init */
Nicotag.Vote.Init = new Ten.Class({
    initialize : function(id, node) {
    	var elems = document.getElementById(id).getElementsByTagName(node);
    	if (Prototype.Browser.IE) {
    		for (var i = 0; i < elems.length; i++) {
    			var element = elems[i];
        		element.onmouseover = function() {
					this.className += " over";
				}
				element.onmouseout = function() {
					this.className = this.className.replace(" over", "");
				}
	    	}
    	}
    }
});

/* Nicotag.Vote.Vote */
Nicotag.Vote.Vote = new Ten.Class({
    initialize : function(type,tid) {
    	var v = Nicotag.Vote.Vote;
    	v.type = type;
    	v.tid = tid;
    	v.contaner = document.getElementById('tag' + tid);
		v.updateRate();
    },
    updateRate: function() {
    	var uri = Nicotag.Vote.BaseURL + 'updtagrate?type=' + this.type + '&vtid=' + this.tid;
        new Ten.JSONP(uri, this, 'receiveResult');
    },
    receiveResult: function(res) {
    	if (res.error) {
    		this.contaner.childNodes[1].innerHTML = '<img src="/images/icon_good_false.gif" alt=""><img src="/images/icon_bad_false.gif" alt=""><span>Rank:' + res.rank + '</span>';
    		this.contaner.className = this.contaner.className.replace(" over", "");
    		//alert(res.error);
    		return false;
    	}
    	var a = this.contaner.childNodes[0];
    	var r = this.contaner.childNodes[1].childNodes[2];
    	with (a.style) {
        	fontSize = this.calFontSize(res.rank) + '%';
        	fontWeight = this.calFontWeight(res.rank);
        	color = 'rgb(' + this.calFontColor(res.rank) + ')';
        }
        r.innerHTML = 'Rank:' + res.rank;
    },
    calFontSize: function(rank) {
		var max = 300;
		var min = 60;
    	var size = rank*4+100;
    	if (size < min) {
    		return min;
    	} else if (size > max) {
    		return max;
    	} else {
    		return size;
    	}
    },
    calFontColor: function(rank) {
		var max = 230;
		var min = 0;
    	var color = rank*-8;
    	if (color < min) {
    		color = min;
    	} else if (color > max) {
    		color = max;
    	}
    	return color+','+color +','+color;
    },
    calFontWeight: function(rank) {
    	if (rank*4 > 50) {
    		return 'bold';
    	} else {
    		return 'normal';
    	}
    }
});

Nicotag.Vote.BaseURL = 'http://www.nicotag.jp/api/';

