/**
 * requires prototype.js .
 **/

function calc_abs_left(e)
{
	//var left = 206;
	var left = 240;
/*
	do {
		trace(e.tagName + ": " + e.offsetLeft);
		var n = e.offsetLeft;
		if ( typeof n == "number" )
			left += n;
	} while ( e = e.parentNode );
*/
	return left;
}

	function favmark(id, uniqid, itemtype, entity_id, entity_type)
	{
		this.itemid = uniqid;
		this.itemtype = itemtype;
		this.entity_id = entity_id;
		this.entity_type = entity_type;

		this.favimg = $(id);
		this.img_offsetX = null;

		this.star_images = [
			'http://img.mogue.jp/img/bg/mark/stars0.gif',
			'http://img.mogue.jp/img/bg/mark/stars1.gif',
			'http://img.mogue.jp/img/bg/mark/stars2.gif',
			'http://img.mogue.jp/img/bg/mark/stars3.gif'
		];

		this.stars = 0;

		this.favcountid = 'favcount';
		this.initial_count = Number(initial_favcount);
		this.initial_state = Number(initial_marker_stat);

		for (var i = 0; i < this.star_images.length; i++) {
			if ( this.star_images[i] == this.favimg.src ) {
				this.stars = i;
				break;
			}
		}

		this.on_dblclick_listener = this.on_dblclick.bindAsEventListener(this);
		this.on_mousedown_listener = this.on_mousedown.bindAsEventListener(this);
		Event.observe( this.favimg, 'mousedown', this.on_mousedown_listener, true );
		Event.observe( this.favimg, 'dblclick', this.on_dblclick_listener, true );
	}

	favmark.prototype.on_dblclick = function (e)
	{
		this.change_star( 0 );
	}

	favmark.prototype.on_mousedown = function (e)
	{
		if ( this.img_offsetX == null )
			this.img_offsetX = ( calc_abs_left(this.favimg) );

		this.on_mousemove_lister = this.on_mousemove.bindAsEventListener(this);
		this.on_mouseup_listner = this.on_mouseup.bindAsEventListener(this);
		
		Event.observe( window, 'mousemove', this.on_mousemove_lister, true );
		Event.observe( window, 'mouseup', this.on_mouseup_listner, true );

		this.update(e);

		Event.stop(e);
		return false;
	}

	favmark.prototype.on_mouseout = function (e)
	{
		this.update(e);
	}
	favmark.prototype.on_mousemove = function (e)
	{
		this.update(e);
	}

	favmark.prototype.on_mouseup = function (e)
	{
		Event.stopObserving( window, 'mousemove', this.on_mousemove_lister, true );
		Event.stopObserving( window, 'mouseup', this.on_mousemove_lister, true );
	}

	favmark.prototype.update = function (e)
	{
		var x = e.clientX - this.img_offsetX;

		var w = this.favimg.offsetWidth;

		var n = 3;

		     if ( x < ( (w / 3) * 0) ) n = 0;
		else if ( x < ( (w / 3) * 1) ) n = 1;
		else if ( x < ( (w / 3) * 2) ) n = 2;

		this.change_star( n );
	}

	favmark.prototype.change_star = function (n)
	{

		this.favimg.src = this.star_images[n];
		this.stars = n;
		
		// send update request actully if mark is not changed for 200ms.
		if ( this.timerid != null) {
			window.clearTimeout( this.timerid );
		}
		this.timerid = window.setTimeout( this.send_update_request.bind(this), 200 );
		
	}

	favmark.prototype.step = function (e)
	{
		var n = this.stars;
		n = (n + 1) % this.star_images.length;

		this.change_star( n );
	}

	favmark.prototype.send_update_request = function ()
	{
		window.clearTimeout( this.timerid );

		var pars = "_set" + "&" + ( $H( {
									flag:			this.stars,
									itemid:			this.itemid,
									itemtype:		this.itemtype,
									entity_id:		this.entity_id,
									entity_type:	this.entity_type,
									cred:			cred,
									rand:		Math.floor(Math.random() * 65536)
								} ) .collect( function (a, b) {
									return a.key + "=" + encodeURIComponent(a.value)
								}) ) . join("&");
	
		var request_url = "/api/fav/";

		var aj = new Ajax.Request( request_url, {
								method: 'get',
								onComplete: this.onComplete.bind(this),
								parameters: pars
								} );
	}

	favmark.prototype.onComplete = function (req)
	{
			var dom = req.responseXML.documentElement;
			var res = xml2js(dom);

			if (res.errorcode == 0) {
				this.is_marked = res.flag;
			}
			
			this.change_appearance(this.faviconid, this.stat_string_elementid);

			//if (this.callback_onchange)
				//this.callback_onchange(res.flag);
	
	}

	favmark.prototype.change_appearance = function (imageid, statid)
	{
		var me = $(statid);

		var count_elem = $(this.favcountid);
		var count = this.initial_count;
		count -= Number(this.initial_state);
		count += Number(this.is_marked);

		if (count) {
			Element.show('favcount_positive');
			Element.hide('favcount_zero');
			Element.show(count_elem);
			count_elem.innerHTML = count;
		} else {
			count_elem.innerHTML = '';
			Element.hide('favcount_positive');
			Element.show('favcount_zero');
			Element.hide(count_elem);
		}
	}

window.push_request_sender_singleton = null;
function push_request_sender (itemtype, uniqid) {

	this.itemtype = itemtype;
	this.uniqid = uniqid;

	

}

push_request_sender.prototype.push_this = function ()
{
	if ( this.timerid != null) {
		window.clearTimeout( this.timerid );
	} else {

		var popularity = $("popularity_point");
		var pop_positive = $("popularity_positive");
		var pop_zero = $("popularity_zero");
		var popcount = parseInt(popularity.innerHTML);

		if ( ! popcount ) {
			Element.show( pop_positive );
			Element.hide( pop_zero );
			popularity.innerHTML = 1;
		} else {
			popularity.innerHTML = popcount + 1;
		}
	}

	this.timerid = window.setTimeout( this.send_request.bind(this), 400 );
}
push_request_sender.prototype.send_request = function ()
{
		window.clearTimeout( this.timerid );
		this.timerid = null;

		var pars = "_push" + "&" + ( $H( {
									uniqid:			this.uniqid,
									itemtype:		this.itemtype,
									cred:			cred,
									rand:		Math.floor(Math.random() * 65536)
								} ) .collect( function (a, b) {
									return a.key + "=" + encodeURIComponent(a.value)
								}) ) . join("&");
	
		var request_url = "/api/fav/";
	
		var aj = new Ajax.Request( request_url, {
								method: 'get',
								parameters: pars
								} );
}

function push_this (itemtype, uniqid)
{
	if ( window.push_request_sender_singleton == null )
		window.push_request_sender_singleton = new push_request_sender(itemtype, uniqid);
	window.push_request_sender_singleton.push_this();
}
