function get_inner(dimension)
{
	return navigator.appName == 'Microsoft Internet Explorer' ? document.body['client' + dimension] : window['inner' + dimension];
}

function set_opacity(element, opacity)
{
	if(navigator.appName == 'Microsoft Internet Explorer')
		element.style.filter = 'alpha(opacity:' + opacity * 100 + ')';
	else
		element.style.opacity = opacity;
}

function vis()
{
	if(arguments.length != 0)
	{
		var pic = document.getElementById('pictures');
		var urls = arguments;
		var i = 0;
		var timer;
		var opacity = 1;
		pic.style.background = 'url(' + urls[i] + ') no-repeat';
		var loop = function()
		{
			timer = setInterval(function()
			{
				set_opacity(pic, opacity.toFixed(2));
				if(opacity.toFixed(2) == 0)
				{
					clearInterval(timer);
					if(++i == urls.length)
						i = 0;
					pic.style.background = 'url(' + urls[i] + ') no-repeat';
					timer = setInterval(function()
					{
						set_opacity(pic, opacity.toFixed(2));
						if(opacity.toFixed(2) == 1)
						{
							clearInterval(timer);
							setTimeout(loop, 5000);
						}
						opacity += .05;
					}, 50);
				}
				opacity -= .05;
			}, 50);
		}
		setTimeout(loop, 5000);
	}
}

function Scroll()
{
	this.inc = 1;

	this.setInc = function(inc)
	{
		this.inc = inc;
	}

	this.addPic = function()
	{
		var xhr = new XMLHttpRequest();
		xhr.open('POST', '?product', true);
		xhr.send();
		pics_scroll.innerHTML += xhr.responseText;
	}

	this.scroll = function()
	{
		this.addPic();
		var i = 0;
		var obj = this;
		var timer = setInterval(function()
		{
			if(i != 212)
			{
				pics.scrollLeft = i;
				i += obj.inc;
			}
			else
			{
				clearInterval(timer);
				for(var j=4; j<pics_scroll.children.length; i++)
					pics_scroll.removeChild(pics_scroll.firstChild);
				pics.scrollLeft = 0;
				obj.scroll();
			}
		}, 16);
	}

	var pics = document.getElementById('products');
	var pics_scroll = pics.getElementsByTagName('div')[0];
	pics_scroll.innerHTML = '';
	for(var i=0; i<4; i++)
		this.addPic();

	this.scroll();
}

function Image2(elm, url)
{
	var elm = elm.getElementsByTagName('img')[0];
	var pic = document.createElement('img');
	var timer = null;
	var opacity = 0;
	pic.setAttribute('class', 'full');
	pic.setAttribute('src', url);
	document.getElementById('content').appendChild(pic);
	var zoom = function(s)
	{
		var i = 15;
		var x0 = elm.offsetLeft;
		var y0 = elm.offsetTop;
		var xStep = ((get_inner('Width') - 768) / 2 - x0) / i;
		var yStep = ((get_inner('Height') - 768) / 2 - y0) / i;
		var wStep = (768 - elm.offsetWidth) / i;
		var hStep = (768 - elm.offsetHeight) / i;
		pic.style.left = x0 + 'px';
		pic.style.top = y0 + 'px';
		pic.style.width = elm.offsetWidth + 'px';
		pic.style.height = elm.offsetHeight + 'px';
		timer = setInterval(function()
		{
			if(i == 0)
			{
				clearInterval(timer);
				timer = null;
			}
			else
			{
				pic.style.left = pic.offsetLeft + xStep + 'px';
				pic.style.top = pic.offsetTop + yStep + 'px';
				pic.style.width = pic.offsetWidth + wStep + 'px';
				pic.style.height = pic.offsetHeight + hStep + 'px';
				i--;
			}
		}, 50);
	}
	zoom();
	pic.onclick = function()
	{
		if(timer != null)
			clearInterval(timer);
		var i = 15;
		var x0 = pic.offsetLeft;
		var y0 = pic.offsetTop;
		var xStep = (elm.offsetLeft - pic.offsetLeft) / i;
		var yStep = (elm.offsetTop - pic.offsetTop) / i;
		var wStep = (elm.offsetWidth - pic.offsetWidth) / i;
		var hStep = (elm.offsetHeight - pic.offsetHeight) / i;
		pic.style.left = x0 + 'px';
		pic.style.top = y0 + 'px';
		timer = setInterval(function()
		{
			if(i == 0)
			{
				clearInterval(timer);
				timer = null;
				pic.parentNode.removeChild(pic);
			}
			else
			{
				pic.style.left = pic.offsetLeft + xStep + 'px';
				pic.style.top = pic.offsetTop + yStep + 'px';
				pic.style.width = pic.offsetWidth + wStep + 'px';
				pic.style.height = pic.offsetHeight + hStep + 'px';
				i--;
			}
		}, 50);
	}
}

function change_picture(url_big, url)
{
	if(typeof(timer) != 'undefined')
		clearInterval(timer);
	var pic = document.getElementById('picture');
	if(pic.getElementsByTagName('img')[0].src.substr(pic.getElementsByTagName('img')[0].src.indexOf('?')) != url)
	{
		var opacity = 1;
		timer = setInterval(function()
		{
			set_opacity(pic.getElementsByTagName('img')[0], opacity.toFixed(2));
			if(opacity.toFixed(2) == 0)
			{
				clearInterval(timer);
				pic.setAttribute('onclick', "new Image('" + url_big + "')");
				pic.getElementsByTagName('img')[0].src = url;
				timer = setInterval(function()
				{
					set_opacity(pic.getElementsByTagName('img')[0], opacity.toFixed(2));
					if(opacity.toFixed(2) == 1)
						clearInterval(timer);
					else
						opacity += .1;
				}, 50);
			}
			else
				opacity -= .1;
		}, 50);
	}
}

