
var Tape1 = {	

	postLoaded	: 0,

	tapeId	: 'bottom-tape',
	step		: 1,
	steps	: [1, 1],
	width	: 540,
	
	cid		: 0,
	id			: 0,
	pages	: 0,

	locked	: false,

	right : function()
	{
		if (Tape1.locked)
		{
			return false;
		}

		if (this.step < this.pages)
		{
			Tape1.locked = true;

			this.step += 1;
			this._next();
		}
	},

	set_ : function(pages, cid, id)
	{
		this.pages	= pages;
		this.cid			= cid;
		this.id			= id;

		Tape1.steps		= [1, 1];
		Tape1.step		= 1;

	}, 

	left : function()
	{
		if (Tape1.locked)
		{
			return false;
		}

		if (this.step > 1)
		{
			Tape1.locked = true;

			this.step -= 1;
			$('#bottom-tape_ul').animate({left:(-(this.step-1)*351)}, 800, function () {
				Tape1.locked = false;
			});
		}
	},


	_next_cb : function()
	{
//debugger;
		$('#bottom-tape_ul').animate({left:(-(this.step-1)*351)}, 800, function () {
				Tape1.locked = false;
			});

		this.steps[this.step] = 1;
	},

	_next : function()
	{

		if (this.steps[this.step])
		{
			this._next_cb();
		}
		else
		{
			$.get('/x-entity.galery.grid.aspx?categoryID='+Tape1.cid+'&goodid='+Tape1.id+'&p=' + Tape1.step, function (data) {
				
				Tape1.width += 400;

				$('#bottom-tape_ul').width(Tape1.width).createAppend(
						'div', {className: 'divd'}, data
					);
			
				Tape1._next_cb();

			})
		}

	}

}

var Tape = {	

	speedHoriz:			100, // 1/x
	speedVert:			100, // 1/x

	boxTop:				new Array(),
	boxLeft:				new Array(),
	boxTimer:				new Array(),
	boxWidth:			new Array(),
	boxBodyWidth:	new Array(),
	boxHeight:			new Array(),
	boxBodyHeight:	new Array(),
	postLoaded	:		0,
	obHide			:		false,

	runHoriz : function(id, direction, stop)	// direction == 1 -- right
	{
		this.postLoad();

		pid = this.getPrimaryId(id);
		if (!direction) direction = 0;
		if (!stop) stop = 0;

		//-
		if (!this.boxLeft[pid]) this.boxLeft[pid] = 0;
		this.boxWidth[pid]			= gE(pid + "_ul").clientWidth;
		this.boxBodyWidth[pid]	= gE(pid + "_body").clientWidth;
		//-
//gE("123123").innerHTML = Math.abs(this.boxLeft[pid]);		
		var f = false;
		f =  !direction ? Math.abs(this.boxLeft[pid])>this.boxWidth[pid]-this.boxBodyWidth[pid] : this.boxLeft[pid] == 0;

		if(f || stop)
		{
			clearTimeout(this.boxTimer[pid]);
			return;
		}

		if (direction == 1)
		{
			var delta = Math.round(Math.abs(this.boxLeft[pid])/10);		
			delta = delta == 0 ? 1 : delta;
			this.boxLeft[pid] += delta;
		}
		else
		{
			var delta = Math.round((this.boxWidth[pid]-this.boxBodyWidth[pid]-Math.abs(this.boxLeft[pid]))/10);		
			if (ua != "IE") delta = this.boxWidth[pid]-this.boxBodyWidth[pid]-Math.abs(this.boxLeft[pid]) != 0 && delta == 0 ? 1 : delta;
			this.boxLeft[pid] -= delta;
		}

		$("#" + pid + "_ul").css({left:this.boxLeft[pid]+"px"});
		this.boxTimer[pid] = setTimeout("Tape.runHoriz('" + id + "', " + direction + ", " + stop + ")", this.speedHoriz - Math.round((1/delta) * 40));
	},

	reset : function (norez)
	{

		if (!this.obHide && !norez)
		{
			$('#order-box').show();
			if ($.browser.msie) {$('#order-box').attr('class', 'obop1')} else {$('#order-box').css({opacity:1})};
			$('#order-box-form').show();
		}
		else
		{
		//	$('#order-box-form').hide();
		}

		$('#order-box-ext').hide();
		
		Img.nosub = false;
		
		this.postLoaded	= 0;
		this.boxLeft			= new Array();
		this.boxTop			= new Array();
		this.boxLeft			= new Array();
		this.boxTimer		= new Array();
		this.boxWidth		= new Array();
		this.boxBodyWidth = new Array();
		this.boxHeight		= new Array();
		this.boxBodyHeight = new Array();

		Tape.step				= 1;
		Tape.steps			= [1, 1];


	},

	showImg : function (id)
	{
	//	debugger;
		$(id).animate({'opacity': 'show'}, 'slow');
	},

	postLoad : function ()
	{

		if (!this.postLoaded)
		{
			jQuery.each($('img[@name="post-load"]'), function()
			{
				$(this).hide();
				$(this).attr('src', $(this).attr('alt'));
			});

			this.postLoaded = 1;
		}
		
	},

	runVert : function(id, direction, stop) // direction == 1 -- down
	{
		pid = this.getPrimaryId(id);
		if (!direction) direction = 0;
		if (!stop) stop = 0;

		//-
		if (!this.boxTop[pid]) this.boxTop[pid] = 0;
		this.boxHeight[pid]			= gE(pid + "_ul").clientHeight;
		this.boxBodyHeight[pid]	= gE(pid + "_body").clientHeight;
		//-
//gE("123123").innerHTML = this.boxTop[pid];		
		var f = false;
		f =  !direction ? Math.abs(this.boxTop[pid])>this.boxHeight[pid]-this.boxBodyHeight[pid] : this.boxTop[pid] == 0;

		if(f || stop)
		{
			clearTimeout(this.boxTimer[pid]);
			return;
		}

		if (direction == 1)
		{
			var delta = Math.round(Math.abs(this.boxTop[pid])/10);		
			delta = delta == 0 ? 1 : delta;
			this.boxTop[pid] += 5;
		}
		else
		{
			var delta = Math.round((this.boxHeight[pid]-this.boxBodyHeight[pid]-Math.abs(this.boxTop[pid]))/10);		
			/*if (ua != "IE")*/ delta = this.boxHeight[pid]-this.boxBodyHeight[pid]-Math.abs(this.boxTop[pid]) != 0 && delta == 0 ? 1 : delta;
			this.boxTop[pid] -= 5;
		}

		$("#" + pid + "_ul").css({top:this.boxTop[pid]+"px"});
		this.boxTimer[pid] = setTimeout("Tape.runVert('" + id + "', " + direction + ", " + stop + ")", 15/* this.speedVert - Math.round((1/delta) * 40)*/);
	},

	runLeft : function(id)
	{
		pid = this.getPrimaryId(id);
		this.runHoriz(id);
	},

	runLeftStop : function(id)
	{
		pid = this.getPrimaryId(id);
		this.runHoriz(id, 0, 1);
	},

	runRight : function(id)
	{
		pid = this.getPrimaryId(id);
		this.runHoriz(id, 1);
	},

	runRightStop : function(id)
	{
		pid = this.getPrimaryId(id);
		this.runHoriz(id, 1, 1);
	},

	runUp : function(id)
	{
		pid = this.getPrimaryId(id);
		this.runVert(id);
	},

	runUpStop : function(id)
	{
		pid = this.getPrimaryId(id);
		this.runVert(id, 0, 1);
	},

	runDown : function(id)
	{
		pid = this.getPrimaryId(id);
		this.runVert(id, 1);
	},

	runDownStop : function(id)
	{
		pid = this.getPrimaryId(id);
		this.runVert(id, 1, 1);
	},

	getPrimaryId: function(id)
	{
		var re = new RegExp("\_[^_]+$","ig");
		id = id.replace(re, '');
		
		return id;
	}
}


