﻿/* -------------- Прокрутка картинок -------------- */

(function($) {
	$.GalleryList = function(o) {
		$.extend(this, {
			inner: null,
			left: null,
			right: null,
			list: null,
			timer: null,
			interval: 1, // ms
			step: 10, // px
			required: ['inner', 'left', 'right'],
			move: false,
			one: false,
			classname: 'visibl_on',
			onclass: 'td.img_bord_on'
		}, o || {});
		this.init();
	}
	$.GalleryList.prototype = {
		init: function() {
			var i, p, t = this;
			try {
				for (i = this.required.length; i--;)
					if ( !(p = this[this.required[i]]) )
						throw "Init: required property " + this.required[i];
					else this[this.required[i]] = $(p);
			} catch(e) { this.log(e); return; }
			this.right
				.mousedown(function(e) { t.move = 'rightMove'; t.startMove(); e.preventDefault(); })
				.click(function(e) { e.preventDefault(); });
			this.left
				.mousedown(function(e) { t.move = 'leftMove'; t.startMove(); e.preventDefault(); })
				.click(function(e) { e.preventDefault(); });
			$(document).mouseup(function() { t.move = false; clearInterval(t.timer); });
			$(window).resize(function() { t.checkMove(); });
			setTimeout( function() {
				if ($(t.inner).find(t.onclass).length) t.inner.scrollLeft(Math.max(0, $(t.inner).find(t.onclass)[0].offsetLeft - $(t.inner).width() / 2));
				t.checkMove();
			}, 10 ); // ie fix
			
			if (t.list) {
				$(t.list).mouseover(function () {
					$(t.list).removeClass('on');
					$(this).addClass('on');
				});
				$(t.list).mouseleave(function () {
					$(t.list).removeClass('on');
					$(this).removeClass('on');
				});
			}
		},
		startMove: function() {
			var t = this;
			if (t.move) {
				t[t.move]();
				t.timer = setInterval(function() { t[t.move](); }, t.interval);
			}
		},
		rightMove: function() {
			this.inner.scrollLeft( this.inner.scrollLeft() + this.step );
			this.checkMove();
		},
		leftMove: function() {
			this.inner.scrollLeft( this.inner.scrollLeft() - this.step );
			this.checkMove();
		},
		checkMove: function() {
			this.left.toggleClass(this.classname, this.inner.scrollLeft() > 0);
			this.right.toggleClass(this.classname, this.inner.scrollLeft() + this.inner.width() < this.inner[0].scrollWidth);
		},
		log: function() { if ( window.console && window.console.log ) console.log(arguments); }
	}
})(jQuery);


