/*
 * Author Krum Lozev
 * Script page is found at http://kik.dgd-bg.com,
 * there development version of this code can be found.
 * Use this code under the terms of GPL v3
 * and keep this note intact
 */

var velocity_max = 25;	/* pixels / time_interval */
var velocity_min = 1;	/* pixels / time_interval */
var acceleration_k = 1;
mazen_item = function ( obj, my_id ) {
	if ( !obj ) {
		return;
	}
	var velocity = 0, acceleration=0, motion_mid=0;;
	var object, id;
	var w, h, ratio;
	var w_cur, h_cur;
	var timer, busy = false;
	var id = my_id;
	var w = obj.width;
	var h = obj.height;
	var ratio = w / h;
	this.object = obj;
	this.objectSatelite = document.getElementById('MazenItemDescription_' + id);
	var w_cur = w;
	var h_cur = h;
	var wait4W = 0, wait4H = 0;
	this.resize = function ( to_w, to_h ) {
		if ( !(w_cur+h_cur) ) {
			w_cur = ratio;
			h_cur = 1;
			this.object.style.display = 'inline';
		}

		if ( ratio > to_w/to_h ) {
			perc = to_w / w_cur;
		} else {
			perc = to_h / h_cur;
		}

		h_cur = h_cur * perc;
		w_cur = w_cur * perc;
		if ( w_cur+h_cur <= 0 ) {
			this.object.style.display = 'none';
		} else {
			this.object.style.width  = Math.floor(w_cur) + 'px';
			this.object.style.height = Math.floor(h_cur) + 'px';
		}
	}
	this.getW = function () {
		return w_cur;
	}
	this.getH = function () {
		return h_cur;
	}
	this.CheckIfActive = function () {
		if (i_curly_mid == id) {
			this.object.className="active";
			if (this.objectSatelite)
				this.objectSatelite.style.display = 'inline';
		} else {
			this.object.className="";
			if (this.objectSatelite)
				this.objectSatelite.style.display = 'none';
		}
	}
	this.Move = function ( w_to, h_to ) {
		clearTimeout(timer);
		frame_ar = w_to/h_to;
		wait4W = (frame_ar<=ratio);
		wait4H = !wait4W;
		if ( w_cur >= w_to || h_cur >= h_to ) {
			acceleration = -1;
		} else {
			acceleration = 1;
		}
		velocity = acceleration*velocity_min;
		motion_mid = 0;
		if (wait4W)
			motion_mid = w_cur + ( acceleration * ( Math.abs(w_cur-w_to) * acceleration_k) );
		else
			motion_mid = h_cur + ( acceleration * ( Math.abs(h_cur-h_to) * acceleration_k) );

		this.Animate ( w_to, h_to );
	}
	this.Animate = function ( w_to, h_to ) {
		t=this;
		if (	( (wait4W && acceleration*(w_cur-w_to)>=0)
			||	  (wait4H && acceleration*(h_cur-h_to)>=0) )
		||	w_cur + h_cur <0 )
		{
			if ( w_to == 0 ||  h_to == 0 )
				t.resize ( w_to, h_to );
			acceleration = 0;
			velocity = 0;

			this.CheckIfActive();
		} else {
			v_plus = 0;
			tmp_a = acceleration;
			if ( ( motion_mid - acceleration * ( wait4W*w_cur + wait4H*h_cur ) ) <= 0 )
				tmp_a = -1 * tmp_a;
			if ( (acceleration*velocity-velocity_max) <= 0  && (acceleration*velocity-velocity_min) >= 0 )
				v_plus = tmp_a;
			velocity = velocity + (v_plus * acceleration_k);
			t.resize (
				(w_cur + velocity),
				(h_cur + velocity)
			);
			timer = setTimeout (
				'items[' +id+ '].Animate ( ' + w_to + ', ' + h_to + ' )',
				1000*trans_time
			);
		}
	}
	return this;
}