var autostart = 1;
var pgs = new Object();

window.onload=function(){
	new GP();
	for (a in pgs) pgs[a].autoWidth();
}

$(window).resize(function(){
	for (a in pgs) pgs[a].defaultWidth();
	for (a in pgs) pgs[a].autoWidth();
});


function GP(containerId, jumpSize) {
	this.initArrows = function() {
		var B = this.sliderGrab.position().left;
		var A = this.sliderGrab.position().left + this.sliderGrab.width() - this.sliderContainer.width();
		this.prev.removeClass().addClass("al_" + ((B<0) ? "e" : "d"));
		this.next.removeClass().addClass("ar_" + ((A>0) ? "e" : "d"));
	}
	this.moveStop = function(){ this.course=0; }
	this.moveStart = function(A) {
		this.course = A;
		this.moveStep();
	}
	this.moveStep = function() {
		if(this.course != 0) {
			this.step(this.course);
			window.setTimeout(function() { gp_.moveStep() }, 300);
		}
	}
	this.step = function(A) {
		var B = 0;
		if(A > 0)
		{
			var E;
			E = this.sliderGrab.position().left + this.sliderGrab.width() - this.sliderContainer.width();
			if(E > 0)
			{
				var C = E / this.jump;
				if(C > A)
				{
					C = A
				}
				else
				{
					this.moveStop();
					this.next.removeClass().addClass("ar_d");
				}
				this.sliderGrab.css('left', this.sliderGrab.position().left + ( - 1 * C * this.jump) + "px");
				A = A - C;
				this.prev.removeClass().addClass("al_e");
			}
		}
		else
		{
			if(A < 0)
			{
				var E;
				E = this.sliderGrab.position().left;
				if(E < 0)
				{
					var C = E / this.jump;
					if(C < A)
					{
						C = A
					}
					else
					{
						this.moveStop();
						this.prev.removeClass().addClass("al_d");
					}
					this.sliderGrab.css('left', this.sliderGrab.position().left - (C * this.jump) + "px");
					var D = this.sliderGrab.width() - Math.abs(this.sliderGrab.position().left);
					if (D >= this.sliderContainer.width())
					{
						this.next.removeClass().addClass("ar_e");
					}
					A = A - C;
				}
			}
		}
	}
	this.defaultWidth = function() {
		$(this.globContainer).css('width','655px');
		this.sliderContainer.css('width','575px');
	}
	this.autoWidth = function() {
		if (this.sliderWidth < $("#content").width() - 80) {
			this.sliderNeedWidth = this.sliderWidth;
		} else {
			this.sliderNeedWidth = $("#content").width() - 80;
		}
		if (this.sliderNeedWidth) {
			$(this.globContainer).css('width',this.sliderNeedWidth+80+'px');
			this.sliderContainer.css('width',this.sliderNeedWidth+'px');
			this.initArrows();
		}
	}
	this.jump = jumpSize - 0 > 0 ? jumpSize - 0 : 50;
	if ((containerId == '') || (!$('#'+containerId).length)) {
		if (!autostart) return 0;
		if ($("div.photo_oth").length) {
			this.globContainer = "div.photo_oth";
		}
	} else {
		this.globContainer = "#"+containerId;
	}
	if (!this.globContainer) return 0;
	$(this.globContainer).mouseup(function(){gp_.moveStop()});
	this.sliderContainer = $(this.globContainer + " > div.container");
	this.sliderGrab = $(this.globContainer + " table.list");
	this.sliderWidth = this.sliderGrab.width();

	this.prev = $(this.globContainer + " > div.al_d");
	this.next = $(this.globContainer + " > div.ar_d");
	this.initArrows();
	this.prev.mousedown(function(){gp_.moveStart(-1)});
	this.next.mousedown(function(){gp_.moveStart(1)});
	var gp_ = this;
	this.sliderGrab.mousewheel( function(event, delta) {
		if (delta != 0) gp_.step(delta);
		event.stopPropagation();
		event.preventDefault();
	});
	autostart = 0;
	pgs[containerId] = this;
//	this.autoWidth();
}