/* Объект для работы с интерьером */
window.interier = {
	'current': 1,
	'stack': window.interier_stack,
	'count': 1,
	'img': null,

	'next': function() {
		if (this.current < this.count)
			this.change(1);
	},

	'prev': function() {
		if (this.current > 1)
			this.change(-1);
	},

	'change': function(dir) {
		this.current += dir;
		this.img.setAttribute('src', '/img/interiers/'+window.group+'_'+this.current+'i.jpg');

		if (this.stack[this.current].x > 0) {
			if (String(this.img.parentNode.nodeName).toLowerCase() == 'a')
				this.img.parentNode.setAttribute('href', '/img/interiers/'+window.group+'_'+this.current+'.jpg');
			else {
				var a = create_element(this.img.parentNode, 'a', {'href': '/img/interiers/'+window.group+'_'+this.current+'.jpg', 'target': '_blank'});
				this.img.parentNode.removeChild(this.img);
				a.appendChild(this.img);
			}
		}
		else if (String(this.img.parentNode.nodeName).toLowerCase() == 'a')
			this.img.parentNode.removeAttribute('href');
		
		$('interier-prev').className = (this.current > 1)?'':'disabled';
		$('interier-next').className = (this.current < this.count)?'':'disabled';
	},

	'start': function() {
		this.img = $('interier-image');
		
		//for (this.count in this.stack) {
	  if (this.count > 1)
			$('interier-next').className = '';
        
	}
}


/* Изменяет адрес страницы */
function relocate(firstonly) {
	var url = '/'+window.type_url;

	if ($('selector-list1').value)
		url += '/'+$('selector-list1').value;

	if (!firstonly && $('selector-list2') && $('selector-list2').value)
		url += '/'+$('selector-list2').value;
	
	window.location = url;
}


/* Объект для перелистывания итемов селекта */
window.lister = {
	'lister': null,

	'start': function() {
		this.lister = $('selector-list2')?$('selector-list2'):$('selector-list1');
		if (this.lister == null)
			return;
		this.visibility();
	},

	'visibility': function() {
		var prev = (this.lister.selectedIndex > 0)?'prev':'prev_none';
		$('item-prev').setAttribute('src', '/img/'+prev+'.png');
		var next = (this.lister.selectedIndex+1 < this.lister.length)?'next':'next_none';
		$('item-next').setAttribute('src', '/img/'+next+'.png');
	},
	
	'prev': function() {
		if (this.lister && this.lister.selectedIndex > 0) {
			this.lister.selectedIndex--;
			relocate();
		}
	},

	'next': function() {
		if (this.lister && this.lister.selectedIndex+1 < this.lister.length) {
			this.lister.selectedIndex++;
			relocate();
		}
	}
}



/* Объект для показа увеличенного изображения */
window.big_image = {
	'obj': null,
	
	'show': function(item) {
		if (!window.big_image_stack)
			return true;

			var iwidth = parseInt(window.big_image_stack[item].x, 10);
			if (iwidth == 0)
				return false;

			var iheight = parseInt(window.big_image_stack[item].y, 10);
			if (iwidth > document.body.clientWidth || iheight > document.body.clientHeight)
				return true;

			var bi = document.getElementById('bigimage');
			if (bi == null) {
				bi = document.createElement('img');
				document.body.appendChild(bi);
				bi.id = 'bigimage';
				bi.style.position = 'absolute';
				bi.style.cursor = 'pointer';
				bi.style.border = '1px solid #dedfde';
				bi.setAttribute('title', 'Щелкните левой кнопкой мыши чтобы убрать изображение');
				bi.onclick = close_big_image;
				bi.style.display = 'none';
			}

			
			var scrl = (typeof(window.scrollY) == 'undefined') ? document.body.parentNode.scrollTop : window.scrollY;
			bi.style.top = scrl + Math.round(document.body.clientHeight - iheight)/2 + 'px';
			var lefter = (iwidth > 750) ? 0 : 100;
			bi.style.left = Math.round((document.body.clientWidth-iwidth)/2) + lefter + 'px';
			bi.setAttribute('src', '/img/gif.gif');
			bi.setAttribute('width', iwidth);
			bi.setAttribute('height', iheight);
			bi.style.display = 'block';
			bi.setAttribute('src', '/img/items/'+Math.floor(item/1000)+'/'+item+'.jpg');
		return false;
	},
	
	'hide': function() {
	
	},

	'create': function() {

	}
}

window.description = {
	'obj': null,
	
	'show': function(item, evnt) {
		if (!evnt)
			evnt = window.event;
		if (window.big_image_stack[item].description == '')
			return;

		var b = this.get_info_block();
		b.innerHTML = window.big_image_stack[item].description;
		b.style.left = Math.round(document.body.clientWidth/2) - 200 + 'px';
		var scrl = (typeof(window.scrollY) == 'undefined') ? document.body.parentNode.scrollTop : window.scrollY;
		b.style.top = evnt.clientY + scrl + 5 + 'px';
		b.style.display = 'block';
	},

	'hide': function() {
		this.get_info_block().style.display = 'none';
	},
	'get_info_block': function() {
		var b = document.getElementById('infoblock');
		if (!b) {
			b = document.createElement('div');
			b.setAttribute('id', 'infoblock');
			document.body.appendChild(b);
		}
		return b;
	}
}


/* Запускается при запуске страницы */
function catalog() {
	window.interier.start();
	window.lister.start();
}




function close_big_image() {
	document.getElementById('bigimage').style.display = 'none';
}