﻿function Opacity(obj,opc)
{
	if (opc < 0 || opc > 100)
	{
		return;
	}
	if (obj.style.MozOpacity != null)
	{
		obj.style.MozOpacity = (opc/100) - .001;
	}
	else if (obj.style.opacity != null)
	{
		obj.style.opacity = (opc/100) - .001;
	}
	else if (obj.style.filter != null)
	{
		obj.style.filter = 'alpha(opacity=' + opc + ')';
	}
	else if (obj.KHTMLOpacity != null)
	{
		obj.KHTMLOpacity = (opc/100) - .001;
	}
}

var varEvt=0;
var varOOPCnt=0;

function GalleryScroll()
{
	var args = GalleryScroll.arguments;
	var p = document.getElementById(args[0]);
	p.gal = p.getElementsByTagName('DIV')[0];
	p.galml = p.offsetWidth - p.gal.offsetWidth;
	p.cen = p.offsetWidth / 2;
	p.top = p.gal.offsetHeight;
	p.oop = new OOPGal(p);
	p.bar = document.getElementById(args[1]);
	p.bar.p = p;
	AddEvt(p.bar, 'Move', 'mousemove');
	AddEvt(p.bar, 'MoveStop', 'mouseout');
}

function Move(e)
{
	var posx = Mse(e)[0] - Pos(this.p)[0];
	var posy = Mse(e)[1] - Pos(this.p)[1];
	if (this.p.rp)
	{
		this.p.oop.dir = (1 + Math.min(Math.abs(posx - this.p.rp)/10, 5));
	}
	if (this.p.lp)
	{
		this.p.oop.dir = -(1 + Math.min(Math.abs(posx - this.p.lp)/10, 5));
	}
	if (posx < this.p.cen + 300 && posx > this.p.cen - 300)
	{
	}
	else if ((posx) < this.p.cen && this.p.oop.pos < 0 && (this.p.oop.dir <= 0))
	{
		this.p.rp = posx;
		this.p.lp = null;
		clearTimeout(this.p.oop.to);
		this.p.oop.dir = 1;
		this.p.oop.move();
	}
	else if ((posx) > this.p.cen && this.p.oop.pos > this.p.galml && (this.p.oop.dir >= 0))
	{
		this.p.rp = null;
		this.p.lp = posx;
		clearTimeout(this.p.oop.to);
		this.p.oop.dir = -1;
		this.p.oop.move();
	}
}

function MoveStop()
{
	clearTimeout(this.p.oop.to);
	this.p.lp = null;
	this.p.rp = null;
	this.p.oop.dir = 0;
}

function OOPGal(obj)
{
	this.obj = obj;
	this.ref = 'gal' + varOOPCnt++;
	window[this.ref] = this;
	this.dir = 0;
	this.pos = obj.gal.offsetLeft;
	this.to = null;
}

OOPGal.prototype.move=function()
{
	if ((this.dir < 0 && this.pos > this.obj.galml) || (this.dir > 0 && this.pos < 0))
	{
		this.obj.gal.style.left = (this.pos += this.dir) + 'px';
		this.setTimeOut('move();', 20);
	}
	else
	{
		if (this.dir < 0)
		{
			this.obj.gal.style.left = this.obj.galml + 'px';
		}
		else
		{
			this.obj.gal.style.left = '0px';
		}
	}
}

OOPGal.prototype.setTimeOut=function(f, d)
{
	this.to = setTimeout('window.' + this.ref + '.' + f, d);
}

function Mse(event)
{
	if(!event)
		var event = window.event;
	if (document.all)
	{
		return [event.clientX + DocS()[0], event.clientY + DocS()[1]];
	}
	else
	{
		return [event.pageX, MseY = event.pageY];
	}
}

function DocS()
{
	var sx, sy;
	if (!document.body.scrollTop)
	{
		sx = document.documentElement.scrollLeft;
		sy = document.documentElement.scrollTop;
	}
	else
	{
		sx = document.body.scrollLeft;
		sy = document.body.scrollTop;
	}
	return [sx, sy];
}

function Pos(obj)
{
	var lft = obj.offsetLeft;
	var top = obj.offsetTop;
	while(obj.offsetParent != null)
	{
		par = obj.offsetParent;
		lft += par.offsetLeft;
		top += par.offsetTop;
		obj = par;
	}
	return [lft, top];
}

function EventAdd(o,t,f)
{
	if ( o.addEventListener )
	{
		o.addEventListener(t, function(e){ o[f](e);}, false);
	}
	else if ( o.attachEvent )
	{
		o.attachEvent('on' + t, function(e){ o[f](e); });
	}
	else
	{
		var Prev = o["on" + t];
		if (Prev)
		{
			o['on' + t] = function(e){ Prev(e); o[f](e); }; }
		else
		{
			o['on' + t] = o[f];
		}
	}
}

function AddEvt(temp,fun,evt)
{
	temp['addEvt' + varEvt] = window[fun];
	EventAdd(temp, evt, 'addEvt' + varEvt);
	varEvt++;
}
