var TA;////emehdy@gmail 20100125 20100603
if(!TA) TA={};
if(!TA.U)
TA.U={};
var table_row= /MSIE 6.0/.test(navigator.userAgent)  ?'block':'table-row';
TA.U.setValue=function(sel,values)
{	
	Spry.$$(sel).forEach(function(input){ 
			for(var name in values) if(input.name==name){if(input.tagName.toLowerCase()=='select')TA.U.selectSetValue(input,values[name]);else input.value=values[name];}
		});
};
TA.U.resetValue=function(sel)
{	
	Spry.$$(sel).forEach(function(input){ 
			if(input.tagName.toLowerCase()=='select')input.selectedIndex=0;else input.value='';
		});
};

TA.U.selectSetValue=function(sel,value)
{	for(var i=0;i<sel.options.length;i++)if(sel.options[i].value==	value) sel.selectedIndex=i;}
TA.U.setOptions=function(ob,opt){for(var op in opt)ob[op]=opt[op];};

TA.U.JSON2OBJ=function(jstr)
{
	var temp={};
	if (jstr=='') return temp;
	jstr=jstr.replace(/[\n\r]/g, ' ')
	if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(jstr))
		eval (" temp="+jstr);
	return temp;
}
TA.U.drawJson=function(json,template)
{
	var s=template['head'];
	if(typeof json =='object' )
	{
		for(var key in json)
			for(var t_ in template['repeat'])
				if(t_=='key')
					s+=TA.U.drawJson(key ,template['repeat'][t_]);
				else
					{s+=TA.U.drawJson(json[key] ,template['repeat'][t_]);alert(s);}
	}
	else
		s+=json;
	s+=template['foot'];
	return s;
}
TA.U.drawJsonRows=function(tbl){
	var s="";
	for(var r in tbl)
	{
		s+="<tr>";
		for(var d in tbl[r])
			s+="<td>"+tbl[r][d]+"</td>";
		s+="</tr>";
	}return s;
	
}
TA.U.getJsonHTML=function(json,template)
{
	var ele=document.createElement(template[0]);
	if(typeof json =='object')
	{	
		for(var key in json)
		{
			var _ele=document.createElement(template[1]);
			if(template[2])
			_ele.setAttribute(template[2],key);
			_ele.appendChild(document.createTextNode(json[key]));ele.appendChild(_ele);
		}
		
	}
	return ele;
}
TA.U.nextSibling=function(e)
{
	while (e=e.nextSibling)
	{
		if (e.nodeType == 1  )	return e;
	}
};
//															GET
TA.U.GET=function(url,opt)
{
	this.console='';
	this.keep=false;
	this.action=function(xml){if(console!=undefined)console.log(xml);};
	TA.U.setOptions(this,opt);
	this.url=url;
	this.createIframeLayer();
	this.bind();
};

TA.U.GET.prototype.createIframeLayer = function()
{
	if (typeof this.iframe == 'undefined')
	{
		var layer = document.createElement('iframe');
		layer.tabIndex = '-1';
		layer.src = 'javascript:"";';
		layer.scrolling = 'no';
		layer.frameBorder = '0';
		layer.style.display = 'none';
		document.documentElement.appendChild(layer);
		this.iframe = layer;
	}
};

TA.U.GET.prototype.bind=function()
{
	var self=this;
	Spry.Utils.addEventListener(this.iframe,'load',function (e){return self.onreturn(self,e)},false);
	this.iframe.src=this.url;
}
TA.U.GET.prototype.onreturn=function(e)
{
	var res=this.iframe.contentWindow.document.body.innerHTML;
	if(this.console)Spry.$(this.console).innerHTML=res;
	this.action(res);
	if(!this.keep) this.unbind();
}
TA.U.GET.prototype.reload=function()
{
	return this.iframe.contentWindow.location.reload();
}
TA.U.GET.prototype.unbind=function()
{	
	this.iframe.contentWindow.close();
	Spry.Utils.removeEventListener(this.iframe,'load',function (e){return self.onreturn(self,e)},false);
	this.iframe.contentWindow.document.body.innerHTML='';
}

TA.U.DataTemplate=function (ele,json,opt)
{
	this.ele=ele;
	this.ele_='#'+ele;
	this.element=Spry.$(ele);
	this.json=json;
	this.auto=true;
	this.reload=false;
	this.TA={};
	TA.U.setOptions(this,opt);
	if( this.element !=null) 
	{
		this.init();
		if(this.auto)
			this.fill(this.json['rows']);
	}
}
TA.U.DataTemplate.prototype.init=function (){
	this.element.style.display='none';
	var self=this;
	Spry.$$(this.ele_+' [TA]').forEach(function(t){ if (t.getAttribute('TA'))self.TA[t.getAttribute('TA')]=t;});
	if(!this.TA['repeate']|| !this.json['rows'])return;
	var H={};
	this.TA['holder']=this.TA['repeate'].parentNode;
	if(this.TA['repeate'].parentNode!= null)
	{
		this.TA['repeate'].parentNode.removeChild(this.TA['repeate']);
	}
}
TA.U.DataTemplate.prototype.fill=function (rows){
	if(	this.element==null)
	{
		this.element=Spry.$(this.ele);
		this.init();
	} 
	if(	this.element==null || !this.TA['holder']) return;
	this.element.style.display='none';
	if(rows !=undefined)
		this.json['rows']=rows;

	for(var i=this.TA['holder'].childNodes.length-1;i>=0;i--)
			if(this.TA['holder'].childNodes[i].nodeType==1)
				this.TA['holder'].removeChild(this.TA['holder'].childNodes[i]);
	var H={};
	for(var key in this.json['rows'])
	{	try{
		var r=this.TA['repeate'].cloneNode(true);
		this.TA['holder'].appendChild(r);
		TA.U.changeInnerHTML(r,new RegExp("\\{key\\}","g"),key);
		
		if(typeof this.json['rows'][key]=='object')
			for(var cell in this.json['rows'][key])
			TA.U.changeInnerHTML(r,new RegExp("\\{val\\["+cell+"\\]\\}","g"),this.json['rows'][key][cell]);
		else
			TA.U.changeInnerHTML(r,new RegExp("\\{val\\}","g"),this.json['rows'][key]);
		r.setAttribute('TA','repeated');
		}catch(ex){alert(ex);}
	}
	H['match']=this.element.innerHTML.match(/\{[a-zA-Z_0-9]+\}/g );
	if(H['match'])
	for(var i=0;i<H['match'].length;i++)
	{
		var v=this.json[H['match'][i].replace("}","").replace("{","")]||'';
		if(this.json[H['match'][i].replace("}","").replace("{","")]==0) v='0';
		try{
			TA.U.changeInnerHTML(this.element,new RegExp(H['match'][i],"g"),v);
		}catch(ex){}
	}
	
	this.element.style.display='';
}
//				-					-					-					-			
TA.U.rowView=function (id,opt)
{
	this.pre='tr';
	this.images=['images/plus.gif','images/minus.gif'];
	this.view=['none',table_row];//'block''table-row'
	this.attr='defaultview';
	TA.U.setOptions(this,opt);
	var self=this;
	if(id) this.pre='#'+id+' '+this.pre;
	Spry.$$(this.pre+' img['+this.attr+']').forEach(function(e){return self.bind(e);});
}
TA.U.rowView.prototype.onhit=function (e)
{
	e.setAttribute(this.attr, (e.getAttribute(this.attr)==this.view[0]) ?this.view[1] :this.view[0] );
	try{
	TA.U.nextSibling(e.parentNode.parentNode).style.display=e.getAttribute(this.attr);
	e.src= (e.getAttribute(this.attr)==this.view[0])?this.images[0]:this.images[1];
	} catch(ex){
		TA.U.nextSibling(e.parentNode.parentNode).style.display='';	
		e.src=this.images[1];
	}
	
}
TA.U.rowView.prototype.bind=function (e)
{
	e.style.cursor='pointer';
	var self=this;
	Spry.Utils.addEventListener(e,'click',function(v){return self.onhit(e);});
	TA.U.nextSibling(e.parentNode.parentNode).style.display=e.getAttribute(this.attr);
	e.src= (e.getAttribute(this.attr)==this.view[0])?this.images[0]:this.images[1];
}
//			-				-				-				-				-
TA.U.bgiframe=function (ele){
	if(! /MSIE 6.0/.test(navigator.userAgent)) return;
	this.element=document.getElementById(ele);
	this.top='auto';this.width='auto';this.height='auto';this.opacity=true;this.src='javascript:false;';this.left='auto';
	this.init();
}
TA.U.bgiframe.prototype.prop = function(n){return n&&n.constructor==Number?n+'px':n;};
TA.U.bgiframe.prototype.init=function (){
	var s=this;
	this.html = '<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+
		               'style="display:block;position:absolute;z-index:-1;'+
			               (s.opacity !== false?'filter:Alpha(Opacity=\'0\');':'')+
					       'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+
					       'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+
					       'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+
					       'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+
					'"/>';
	this.element.insertBefore( document.createElement(this.html), this.element.firstChild );
}
//						/						/							/					
TA.U.childNodes = function(element)
{
	var children = [];var child = element.firstChild;
	while (child){
		if (child.nodeType == 1  )	children.push(child);
		child = child.nextSibling;	}return children;
};
TA.U.changeInnerHTML=function(element,reg,val)
{
	try{element.innerHTML=element.innerHTML.replace(reg,val);}catch(e)
	{for(var i=0;i<element.cells.length;i++)element.cells[i].innerHTML=element.cells[i].innerHTML.replace(reg,val);	}
		
}
TA.U.topLeft=function(obj)
{
	
	var father=[0,0];
	if(obj.offsetParent!=null)
	father=TA.U.topLeft(obj.offsetParent);
	return [obj.offsetTop+father[0],	obj.offsetLeft+father[1],obj.offsetHeight,obj.offsetWidth];
}
TA.U.stopEvent=function(e)
{try{if (e){
			if (e.stopPropagation)e.stopPropagation();
			else e.cancelBubble = true;
			if (e.preventDefault) e.preventDefault();
			else e.returnValue = false;
}}catch(e){}};
TA.U.join=function(json,sep,glue)
{	var temp=[];for(var i in json) temp.push(i+glue+json[i]); return temp.join(sep);}
if(! window.console) {
 window.console=document.createElement('textarea');console.rows=10;console.style.display='none';console.style.width='100%';console.log=console.info=function(x){console.value+= "\n\r"+x;}
window.onload=function(){ if(document.documentElement.childNodes[1]) document.documentElement.childNodes[1].appendChild(console);}
document.ondblclick=function(){if(0)if(console.style.display=='none' )console.style.display='';else console.style.display='none';};
}


//															POST
TA.U.POST=function(url,data,opt)
{
	this.console='';
	this.action=function(xml){if(console!=undefined)console.log(xml);};
	TA.U.setOptions(this,opt);
	this.url=url;
	this.data=data;
	this.createIframeLayer();
	this.makeHTML()
	this.send();
};
TA.U.POST.last_id=0;
TA.U.POST.prototype.makeHTML=function()
{
	this.form=document.createElement('form');
	document.body.appendChild(this.form);
	this.form.style.display='none';
	for(var name in this.data)
	{
		var txt=document.createElement('textarea');
		this.form.appendChild(txt)
		txt.name=name;
		txt.value=this.data[name];
	}
	this.form.method='POST';
	this.form.action=this.url;
	this.form.target=this.target;
	return this.form;
}
TA.U.POST.prototype.createIframeLayer = function()
{
	if (typeof this.iframe == 'undefined')
	{
		var layer = document.createElement('iframe');
		layer.tabIndex = '-1';
		layer.src = 'javascript:"";';
		layer.scrolling = 'no';
		layer.frameBorder = '0';
		layer.style.display = 'none';
		this.target='POST_'+( ++TA.U.POST.last_id);
		layer.name=this.target;
		document.documentElement.appendChild(layer);
		this.iframe = layer;
	}
	return this.iframe;
};

TA.U.POST.prototype.send=function()
{
	var self=this;
	Spry.Utils.addEventListener(this.iframe,'load',function (e){return self.onreturn(self,e)},false);
	this.form.submit();
}
TA.U.POST.prototype.onreturn=function(e)
{
	var res=this.iframe.contentWindow.document.body.innerHTML;
	if(this.console!='')Spry.$(this.console).innerHTML=res;
	this.action(res);
	this.unbind();
}
TA.U.POST.prototype.unbind=function()
{	
	this.iframe.contentWindow.close();
}
TA.U.fireEvent =function (element,event)
{
	if(document.createEvent){
	var evt = document.createEvent("HTMLEvents");
	evt.initEvent(event, true, true ); 
	return !element.dispatchEvent(evt);
	}
	else{
	var evt = document.createEventObject();
	return element.fireEvent("on"+event,evt)
	}
}