function attach(obj,evt,fnc) {
	if (obj.addEventListener) return obj.addEventListener(evt,fnc,false);
	if (obj.attachEvent) return obj.attachEvent('on'+evt,fnc);
	obj['on'+evt]=fnc;
}
function autoCE(type) {
	var i,t,ret,a=arguments,u='undefined',d=(typeof (t=a[6])!=u?t:document);
	ret=d.createElement(type);
	try {
		if (typeof (t=a[1])!=u && t!=null) for (i in t) ret[i]=t[i];
		if (typeof (t=a[2])!=u && t!=null) for (i in t) ret.style[i]=t[i];
		if (typeof (t=a[3])!=u && t!=null) for (i in t) 
			if (t[i].length&&typeof t[i] !='function') try {
				for (j=0; j<t[i].length; j++)
					attach(ret,i,t[i][j]);
			} catch (e) {  } else{
				attach(ret,i,t[i]);
			}
		if (typeof (t=a[4])!=u && t!=null) for (i=0; i<t.length; i++) ret.appendChild(t[i]);
		if (typeof (t=a[5])!=u && t!=null) t.appendChild(ret);
	} catch (e) {  }
	return ret;
	/* type, attribues, styles, events, children, parent */
}
function norm (E) {
	var u='undefined';
	if (document.all) {
		E.target=E.srcElement;
		E.relatedTarget=E.fromElement || E.toElement;
	}
	var elem=E.target;
	if (elem && elem.parentNode && elem.parentNode.window && elem.parentNode.window.control) elem=elem.parentNode.window;
	else try {
		while (!elem.control && elem!=null) elem=elem.parentNode;
	} catch (e) { }
	if (typeof E.pageX==u && typeof E.pageY==u && (E.clientX && E.clientY)) {
		E.pageX=E.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		E.pageY=E.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	E.control=elem!=null?elem.control:null;		
	E.cancel = function () {
		if (document.all) {
			this.returnValue=false;
			this.cancelBubble=true;
		}else{
			this.preventDefault();
			this.stopPropagation();
		}
		return false;
	}
	return E;
}
function $(a) { return document.getElementById(a); }
function cache(img) {
	var ret=new Image();
	ret.src=img;
	return ret;
}
Trading = function (elem) {
	this.main=elem;
	this.index=Trading.instances.length;
	this.cur=0;
	this.cacheImages();
	this.getText();
	this.setupShop();
}
Trading.prototype = {
	cacheImages:function () {
		this.cache=[];
		var i,x=this.main.getElementsByTagName('img');
		for (i=0; i<x.length; i++) this.cache.push(cache(x[i].src));
		for (i=x.length-1; i>=0; i--) x[i].parentNode.removeChild(x[i]);
	},
	getText:function () {
		this.stats=[];
		var i,x=this.main.getElementsByTagName('div');
		for (i=0; i<x.length; i++) this.stats.push(x[i].innerHTML);
		for (i=x.length-1; i>=0; i--) x[i].parentNode.removeChild(x[i]);		
	},
	setupShop:function () {
		var rnd=Math.floor(Math.random()*8999+1000);
		this.imageID='gallery_'+rnd;
		this.statID='stats_'+rnd;
		autoCE('div',{className:'gallery_controls'},{},{},[
			autoCE('img',{src:this.main.getAttribute('prevsrc'),control:this},{},{click:Trading.events.previousClick}),
			autoCE('img',{src:this.main.getAttribute('nextsrc'),control:this},{},{click:Trading.events.nextClick})
		],this.main);
		autoCE('table',{className:'gallery_image'},{},{},[
			autoCE('tbody',{},{},{},[
				autoCE('tr',{},{},{},[
					autoCE('td',{align:'center'},{},{},[
						autoCE('img',{src:this.cache[0].src,id:this.imageID}),
						autoCE('div',{innerHTML:this.stats[0], id:this.statID})
					])
				])
			])
		],this.main);
	},
	display:function () {
		$(this.imageID).src=this.cache[this.cur].src;
		$(this.statID).innerHTML=this.stats[this.cur];
	},
	previous:function () {
		this.cur-=1;
		if (this.cur<0) this.cur+=this.cache.length;
		this.display();
	},
	next:function () {
		this.cur+=1;
		if (this.cur>=this.cache.length) this.cur-=this.cache.length;
		this.display();
	}
}
Trading.newInstance = function (elem) {
	
	Trading.instances.push(new Trading(elem));
}
Trading.instances=[];
Trading.events = {
	winLoad:function () {
		var i,x = document.getElementsByTagName('div');
		for (i=0; i<x.length; i++) 
			if (/(^| )trading( |$)/i.test(x[i].className))
				Trading.newInstance(x[i]);
	},
	previousClick:function (e) {
		var ctl=(e=norm(e)).target.control;
		ctl.previous();
	},
	nextClick:function (e) {
		var ctl=(e=norm(e)).target.control;
		ctl.next();
	}
}
attach(window,'load',Trading.events.winLoad);
