window.addEvent('load', function() {
	initAccords();
	initTabs();
	initListings();
	initLang();
});


var Timer = new Class({
	initialize: function (ms, name) {
		this.start_ms	=	0;
		this.ms			=	ms;
		this.name		=	name;
		this.is_started	=	false;
	},

	start: function () {
		d=new Date();
		this.start_ms	=	d.getTime();
		this.is_started	=	true;
		setTimeout(this.name+'.run()', 100);
	},

	run: function () {
		d=new Date();
		t=d.getTime();
		
		if((t - this.start_ms) >= this.ms) {
			this.end();
		} else {
			setTimeout(this.name+'.run()', 100);
		}
	},

	reset: function () {
		d=new Date();
		this.start_ms = d.getTime();
	},
	
	stop: function () {
		this.initialize(this.ms, this.name);
	}
});

var inOp	=	false;
function getContent(path, update, evaluate, func, getAll) {
	if(inOp) return false;
	
	inOp	=	true;
	oldHTML	=	update.innerHTML;
	height	=	update.offsetHeight;
	update.innerHTML	=	'<div style="clear: both; height: '+ height +'px; padding: 0; margin: 0; text-align: center;"><br /><img class="loader" src="/UI/Images/Icons/loading2_16x16.gif" align="center" alt="Loading" /></div>';
	if(getAll == null) {
		getAll = false;
	}
	new Ajax(path, {
		method: 'post',
		data: 'ajaxReq=1',
		evalScripts: evaluate,
		onComplete: function (response) {
			inOp	=	false;
			div	=	$('hdContent');
			if(!div) {
				div	=	document.createElement('div');
				div.setAttribute('id', 'hdContent');
				document.body.appendChild(div);
				div	=	$('hdContent');
			}
			div.style.display	=	'none';
			
			if(getAll == false && response.match("<!DOCTYPE")) {
				update.innerHTML	=	oldHTML;
				return;
			}
			div.setHTML(response);
			content	=	$$('div#hdContent div#mainContent')[0];
			if(!content) {
				content	=	$$('div#hdContent div#itemContent')[0];
			}
			if(content) {
				content	=	content.innerHTML;
				div.innerHTML	=	'';
				update.setHTML(content);
				initAccords();
				initListings();
				if(func) {
					eval(func);
				}
			} else {
				div.innerHTML	=	'';
			}
		}
	}).request();
}

function initTabs() {
	$$('ul.tabs').each(function (ul) {
		var tabs	=	ul.getChildren();
		var i		=	0;
		var c		=	ul.getParent().getChildren();
		
		for(i = 0; i < c.length; i++)
		{
			if(ul == c[i])
			{
				break;
			}
		}
		var cont	=	c[i + 1];
		
		tabs.each(function(el) {
			el.style.cursor	=	'pointer';
			el.addEvent('click', function(e) {
				e	=	new Event(e).stop();
				if(inOp) return false;
				str	=	'';
				getAll = false;
				if(el.className.match('sel')) return;
				if(el.className.match('getAll')) getAll = true;
				if(el.className.match('fn-')) {
					func	=	el.className.replace(/.*?fn-([\w-]+).*/, '$1');
					func	=	func.split('-');
					str		=	func[0] + '(';
					for(i = 1; i < func.length; i++) {
						str	+=	'\''+func[i]+'\',';
					}
					str	=	str.replace(/,$/, '');
					str		+=	')';
					//eval(str);
				}
				tabs.each(function (el2) {el2.className = el2.className.replace(/sel/ig, '')});
				el.className	+=	' sel';
				a	=	el.getChildren()[0];
				getContent(a.href, cont, true, str, getAll);
			});
			if(i == 0 && 1 == 0) {
				a	=	el.getChildren()[0];
				getContent(a.href, cont);
				el.className	+=	' sel';
			}
			i++;
		});
	});
	
	$$('ul.formtabs').each(function(ul) {
		var tabs	=	ul.getChildren();
		var i		=	0;
		var c		=	ul.getParent().getChildren();
		
		for(i = 0; i < c.length; i++)
		{
			if(ul == c[i])
			{
				break;
			}
		}
		var cont	=	c[i + 1];

		tabs.each(function(el) {
			el.style.cursor	=	'pointer';
			el.addEvent('click', function(e) {
				e	=	new Event(e).stop();
				if(el.className.match('sel')) return;
				if(el.className.match('fn-')) {
					func	=	el.className.replace(/.*?fn-([\w-]+).*/, '$1');
					func	=	func.split('-');
					str		=	func[0] + '(';
					for(i = 1; i < func.length; i++) {
						str	+=	'\''+func[i]+'\',';
					}
					str	=	str.replace(/,$/, '');
					str		+=	')';
					eval(str);
				}
				tabs.each(function (el2) {el2.className = el2.className.replace(/sel/ig, '')});
				el.className	+=	' sel';
				a	=	el.getChildren()[0];
				getContent(a.href, cont, true);
			});
			i++;
		});
	});
}

function initAccords() {
	$$('.accordion').each(function (el) {
		links	=	el.getElements('h2');
		if(links.length == 0)
			links = el.getElements('h1');
		if(links.length == 0)
			links = el.getElements('h3');
		conts	=	el.getElements('div');

		if(conts.length > links.length) {
			var tmp = el.id;
			el.id	=	'accd1';
			conts	=	$$('#accd1 div.info');
			el.id	=	tmp;
		} else if(links.length > conts.length) {
			conts	=	el.getElements('p');
		}
		
		if(links.length == conts.length) {
			if(el.className.match(/nodisp/))
			{
				acdn	=	new Accordion(links, conts, {
					alwaysHide: true,
					show: -1,
					opacity: true
				});
			}
			else
			{
				acdn	=	new Accordion(links, conts, {
					display: 0,
					opacity: true
				});
			}
			links.each(function (el) {
				el.style.cursor	=	'pointer';
			});
		}
	});
}

function initListings() {
	$$('table.listing').each(function(table) {
		var id = table.id;
		table.id = "tmplistingtable1";
		tr = $$('#tmplistingtable1 tr');
		table.id = id;
		
		tr.each(function(el) {
			
			$ES('a', el).each(function(ela) {
				ela.style.zIndex = 0;
			});
			el.style.zIndex = 10;
			
			if(el.className.match(/nil/)) {
				return;
			}
			el.addEvent('mouseover', function(e) {
				el.className	+=	' over';
			});
			el.addEvent('mouseout', function(e) {
				el.className	=	el.className.replace(/over/ig, '');
			});
			el.addEvent('click', function (e) {
				new Event(e).stop();
				if(inOp) return false;
				
				if(el.className.match(/clicked/i) && !table.className.match(/or/i)) {
					el.className	=	el.className.replace(/clicked/ig, '');
				} else {
					if(el.className.match(/clicked/i)) {
						return;
					}
					tr.each(function(el) {
						el.className	=	el.className.replace(/clicked/ig, '');
					});
					el.className	+=	' clicked';
					if(el.className.match('fn-')) {
						func	=	el.className.replace(/.*?fn-([\w-]+).*/, '$1');
						func	=	func.split('-');
						str		=	func[0] + '(';
						for(i = 1; i < func.length; i++) {
							str	+=	'\''+func[i]+'\',';
						}
						str	=	str.replace(/,$/, '');
						str		+=	')';
						eval(str);
					}
				}
			});
		});
	});
}

function initLang() {
	$$('.openLangDialog').each(function(el) {
		el.addEvent('click', function(e) {
			e	=	new Event(e).stop();
			x	=	e.client.x;
			y	=	e.client.y;

			locale	=	el.className.replace(/.*locale_([0-9+]).*/, '$1');
			lbl		=	el.innerHTML.replace(/[\[\]]/ig, '');
			id		=	'lbl_'+lbl;
			if($(id)) {
				$(id).remove();
				return;
			}
			newel	=	document.createElement('div');
			newel.className			=	'langEdit';
			newel.id				=	id;
			newel.style.position	=	'absolute';
			newel.style.top			=	(y + 15) + 'px';
			newel.style.left		=	(x + 0) + 'px';
			newel.style.zIndex		=	1000;
			inner	=	'<form action="'+el.href+'" method="post">';
			inner	+=	'<input type="hidden" name="ajaxReq" value="1" />';
			inner	+=	'<input type="hidden" name="locale_InfoID" value="'+locale+'" />';
			inner	+=	'<input type="hidden" name="data[label]" value="'+lbl+'" />';
			inner	+=	'<input type="text" name="data[value]" value="" />';
			inner	+=	'</form>';
			newel.innerHTML	=	inner;
			if(el.getParent().getChildren().length > 1) {
				newel.injectAfter(el);
			} else {
				newel.injectAfter(el.getParent());
			}

			form = $$('#'+id+' form')[0];
			form.addEvent('submit', function(e) {
				e	=	new Event(e).stop();
				value	=	this.getChildren()[3].value;
				this.send({
					onComplete: function() {
						newel	=	document.createElement('span');
						newel.innerHTML	=	value;
						newel.injectAfter(el);
						el.remove();
						$(id).remove();
					}
				});
			});
		});
	});
}
