//include GA
if(GOOGLE_ANALYTICS) document.write('<script type="text/javascript" src="http://www.google-analytics.com/ga.js"></script>');


document.onload=function(){
	init_content(document);
	if(GOOGLE_ANALYTICS) ga();
}


function init_content(root){
	var tags=root.getElementsByTagName("*");
	for(var i=0, l=tags.length; i<l ;i++){
		var tag=tags[i];
		if(tag && tag.tagName=="A" && _ATTR(tag, "aref")) tag.onclick=function(){return GET(this)};
		if(tag && tag.tagName=="A" && _ATTR(tag, "autoload")!=undefined) tag.onclick();
		if(tag && tag.tagName=="A" && _ATTR(tag, "type")=="checkbox") Forms.checkbox(tag);
		if(tag && tag.tagName=="A" && _ATTR(tag, "type")=="radio") Forms.radio(tag);
		if(tag && (tag.tagName=="INPUT" || tag.tagName=="TEXTAREA") && _ATTR(tag, "default")!=undefined) Forms.input(tag);
		if(tag && tag.tagName=="SELECT") new Forms.select(tag);
	}
}

var pageTracker;
function ga(){
	try {
		pageTracker = _gat._getTracker(GOOGLE_ANALYTICS);
		pageTracker._trackPageview();
	} catch(err) {}
}


function login(id){
	CSS(id).set({display:CSS(id).check({display:"none"})?"block":"none"}); return false;
}


// activate item on click (set className="act")
function _act(obj){
	if(!obj || !obj.parentNode) return false;
	if(_ATTR(obj, "noact")!=undefined) return false;
	var par=(obj.parentNode.tagName=="LI" || obj.parentNode.tagName=="VAR" ? obj.parentNode.parentNode : obj.parentNode);
	if(!par.set) par.set=function(i){return _(par, '~@a')[i].onclick();}; //init funtion [set]
	if(!par.act) par.act=function(i){return _act(_(par, '~@a')[i]);}; //init funtion [set]
	if(!par.unset) par.unset=function(){Style(par.cur).remove("act"); par.cur=null; return false;}; //init funtion [set]
	if(par.cur==obj) return false;
	if(!par.cur) par.cur=_(par, "a.act"); // if no [cur] try to find it
	if(par.cur) Style(par.cur).remove("act");
	Style(obj).add("act");
	par.cur=obj;
	(_ATTR(obj, "onact") || function(){})(); //call [onact] event
	if(obj.tagName=="A") obj.blur();
	return false;
};


//simple tabs control
var Tab=function(obj){
	return {
		act:function(tab_id){
			_act(obj);
			if(!obj.parentNode.cur_tab) obj.parentNode.cur_tab=_(tab_id, "/^");
			this.close(obj.parentNode.cur_tab);
			obj.parentNode.cur_tab=_(tab_id);
			this.open(obj.parentNode.cur_tab);
			obj.parentNode.className="tabs "+obj.parentNode.cur_tab.id;
		},
		open:function(block){
			Style(block).add("act");
		},
		close:function(block){
			Style(block).remove("act");
		}
	}
};


document.alert=function(content, type){
	if(!_("dialog_alert")) 
		makeHTML({tag:"div",id:"dialog_alert",append:[
			{tag:"a",className:"close",onclick:function(){this.parentNode.style.display="none";}},
			{tag:"div",className:"content"}
		]},document.body);
	var root=_("dialog_alert");
	root.className=type?"skin_"+type:"";
	_(root, "~div.content").innerHTML=content;
	init_content(_(root, "~div.content"));
	root.style.display="block";
}


document.layer=function(b, html){
	if(!_("layer")){
		makeHTML({tag:"div",id:"modal",append:[{tag:"span"}]},document.body);
		makeHTML({tag:"div",id:"layer"},document.body);
		
	}
	if(html) _("layer").innerHTML=html;
	_("modal").style.display=_("layer").style.display=b?"block":"none";
	return false;
}


function vote(obj, e){
	if(obj.voted) return false;
	var s=_(obj, '~@s');
	if(!obj.onmouseout){
		obj.onmouseout=function(){vote(obj)};
		for(var i=0; i<s.length; i++){
			if(s[i].className) break; 
			obj.cur=s[i];
		}
	}
	obj.set=function(num){
		for(i=0; i<5; i++) s[i].className=num>i?'':'off';
	}
	var target=e?(window.event?event.srcElement:e.target):obj.cur;
	var state=target?'':'off';
	for(var i=0; i<s.length; i++){
		if(obj.minValue && !target) state=obj.minValue>i?'':'off';
		s[i].className=state;
		if(s[i]==target || !target) state='off';
		if(!s[i].onclick){ //init
			s[i].num=i+1;
			s[i].style.cursor="pointer";
			s[i].onclick=function(){
				if(obj.getAttribute("href")==undefined){
					if(_(obj, "~input")) _(obj, "~input").value=this.num;
					obj.set(this.num);
					obj.minValue=this.num;
					return false;	
				}
				if(obj.voted) return false;
				obj.voted=true;
				GET({href:obj.getAttribute("href")+this.num, oncomplete:function(response){obj.set(response.obj.raiting)}})
			}
		}
	}
}


function show_form(obj){
	if(obj.tagName=="A"){
		_(obj, "/").style.display='none';
		_(obj, "/>form").style.display='block';
	}
	else{
		_(obj, "/form").style.display='none';
		_(obj, "//<.but").style.display='block';
	}
}

function init_mail(id){
	_(id).innerHTML=_(id).lang+"@"+_(id).innerHTML;
	_(id).href="mailto:"+_(id).innerHTML;
}


function pipes_autoheight(id, mess_id){
	var pipes_root=_("pipes");
	var pipes_tm=setInterval(function(){
		var block=pipes_root.getElementsByTagName("div")[3];
		if(block && block.className=="ybr" && block.innerHTML){
			clearInterval(pipes_tm);
			//if(!block.getElementsByTagName("div")[0]) pipes_root.getElementsByTagName("div")[0].style.height="auto";
			if(!block.getElementsByTagName("div")[0]){
				pipes_root.style.display="none"; //hide pipes
				_(mess_id).style.display="block"; //hide pipes	
			}
		}
	},100);
}
				 


// show/hide single block (e.g. password forgot block in menu_login)
function showhide(obj,args){
	obj=_(obj);
	var show=(args!=undefined && typeof args=="object")?args.show:args;
	if(show!=undefined) obj.style.display=(show?"block":"none");
	else obj.style.display=(CSS(obj).check({display:'block'})?"none":"block");
	show=CSS(obj).check({display:'block'});
	if(args!=undefined && typeof args=="object" && args.sender) Checkbox(args.sender, show);
	return show;
};


function toogle(obj1, obj2){
	_(obj1).style.display="block";
	_(obj2).style.display="none";
}






//Forms functions

var Forms={};



Forms.checkbox=function(obj){
	obj.chb=obj.getElementsByTagName("input")[0];
	if(obj.chb.checked) obj.className="act";
	//obj.chb.onchange=function(){this.parentNode.onclick(); };
	obj.onclick=function(e){
		var target=window.event?window.event.srcElement:e.target;
		this.className=this.className=="act"?"":"act";
		if(target.tagName==this.tagName){ 
			this.chb.checked=this.className?1:0;  
			if(this.chb.onchange) this.chb.onchange();
			return false; 
		}
		return true;
	}
}

Forms.radio=function(obj){
	obj.chb=obj.getElementsByTagName("input")[0];
	var form=_(obj, "/form");
	if(obj.chb.checked){
		obj.className="act";
		form["rad"+obj.chb]=obj;
	}
	
	//obj.chb.onchange=function(){this.parentNode.onclick(); };
	obj.onclick=function(e){
		var target=window.event?window.event.srcElement:e.target;
		
		if(form["rad"+this.chb]) form["rad"+obj.chb].className="";
		form["rad"+this.chb]=this;
		
		this.className="act";
		if(target.tagName==this.tagName){ 
			this.chb.checked=1;  
			if(this.chb.onclick) this.chb.onclick();
			return false; 
		}
		return true;
	}
}



Forms.dropAjaxFilter=function(input){
	//return false;
	var list=document.createElement("div");
	list.className="drop_filter";
	input.parentNode.insertBefore(list,input.nextSibling);
	list.style.display="none";
	input.inFocus=true;
	input.isOpen=false;
	input.onkeydown=function(e){if(Key(e).is(13)) return false; } //lock form autosend
	input.onkeyup=function(e){
		if(this.isOpen && (Key(e).is(40) || Key(e).is(38))) return list.act(Key(e).code());
		if(Key(e).is(13)) return list.exec();
		this._value=this.value; 
		if(this.value) GET(this);
		else list.close();
	}; 
	input.onfocus=function(){ this.onkeyup(); this.inFocus=true};
	input.onblur=function(){ list.close(); this.inFocus=false;};
	input.oncomplete="if(this.inFocus){ this.list.showhide(1); this.list.init(); }";
	input.onenter=_EV(input, "onenter");
	input.setAttribute("indicator","0");
	input.target=input.list=list;
	list.input=input;
	list.init=function(){
		var items=_(list, "~@a");
		if(!items) return list.showhide(0);
		for(var i=0; i<items.length; i++){
			items[i].onmousedown=function(){list.lock=true};
			items[i].onclick=function(){input.value=this.innerHTML; return list.go(); };
			items[i].onmouseup=function(){list.lock=false};
		}
	};
	list.close=function(){
		if(!list.lock) setTimeout(function(){list.showhide(0);},100);
	};
	list.showhide=function(b){
		input.isOpen=showhide(list,b);
	};
	list.exec=function(){
		 if(list.cur) input._value=input.value;
		 return list.go();
	};
	list.act=function(code){
		if(!list.firstChild) return false;
		var next;
		list.cur=_(list, "~a.act");
		if(list.cur) list.cur.className="";
		if(!list.cur) next=list[code==40?"firstChild":"lastChild"];
		else next=list.cur[code==40?"nextSibling":"previousSibling"];
		if(next){
			next.className="act";
			input.value=next.innerHTML;
			list.cur=next;
		}
		else input.value=input._value;
		return false;
	};
	list.go=function(code){
		 list.showhide(0); 
		 input.onenter();
		 return false;
	};
};




//Custom select control
//--------------------------------
//> INITIALIZE
//  new Select(select_obj)

Forms.select=function(obj){
	var _this=this;
	var _block, _element, _field, _content, _dropdown, _arrow,  _inInit;
	
	this.isOpen=false;
	
	this.init=function(){
		_inInit=true;
		obj.style.display="none";
		
		this.isOver=0;
		
		_block=document.createElement("s");
		_block.className="custom_select "+obj.className;
		_block.onmouseover=function(){_this.isOver=1};
		_block.onmouseout=function(){_this.isOver=0};
				
		//create hidden input element for submit
		_element=document.createElement("input");
		_element.type="hidden";
		_element.value=obj.value;
		_element.name=obj.name;
		_element.id=obj.id;
		_element.disabled=obj.disabled;
		_element.onchange=obj.onchange;
		_element.autoload=(_ATTR(obj,"autoload")!=undefined);
		_element.className="";
		_element.update=function(){_this.update(_element)};
		_element.reset=function(){_this.set(_element.defaultElement)};
		_block.appendChild(_element);
		
		
		_field=document.createElement("span");
		_field.className="field";
		_element.field=_field;
		_arrow=document.createElement("a");
		_arrow.className="arrow";
		_field.appendChild(_arrow);
		_content=document.createElement("span");
		_field.appendChild(_content);
		_block.appendChild(_field);
		_content.onclick=_arrow.onclick=function(){return _this.dropdown()};
									
		_dropdown=document.createElement("div");
		_dropdown.className="dropdown";
		
		_element.options=[];
		if(obj.options.length)
			for(var i=0; i<obj.options.length; i++) {
				var a=document.createElement("a");
				a.innerHTML=obj.options[i].innerHTML;
				a.value=obj.options[i].value;
				a.onclick=function(){return _this.set(this);};
				CSS(a).set({display:"block", whiteSpace:"nowrap", textDecoration:"none" });
				//CSS(a).copy("color", obj.options[i]);
				_element.options.push(a);
				if(obj.options[i].selected || obj.value==a.value) _element.selected=_element.defaultElement=a;
				if(_ATTR(obj.options[i],"image")){
					var img=document.createElement("img");
					img.src=_ATTR(obj.options[i],"image");
					a.insertBefore(img, a.firstChild);
				}
				_dropdown.appendChild(a);
			}
		_block.appendChild(_dropdown);
		setTimeout(function(){_this.create()},12); //replace select to custom select after initializing
		
		return this;
	};
	
	this.create=function(){
		if(!obj || !obj.parentNode) return false;
		
		obj.parentNode.replaceChild(_block, obj);
		
		//overwrite styles
		CSS(_block).set({position:"relative", textDecoration:"none"});
		if(ie){ if(document.selects_count==undefined) document.selects_count=0; CSS(_block).set({zIndex:100-(document.selects_count++)});} //fix z-index for IE
		CSS(_field).set({whiteSpace:"nowrap"});
		CSS(_content).set({fontStyle:"normal"});
		CSS(_arrow).set({position:"absolute", display:"block", top:0, right:0, borderLeftColor:CSS(_block).get("borderLeftColor")});
		CSS(_dropdown).set({position:"absolute", visibility:"hidden"});

		if(obj.options.length){
			_element.selected ? this.set(_element.selected) : this.set(_element.options[0]); //set default or act
			_addEvent(document,"mouseup", function(){if(_this.isOpen && !_this.isOver) setTimeout(function(){_this.dropdown(0)},40)}); //define exernal click event
		}
		_inInit=false;
	};
	
	this.update=function(_element){
		//Style(_element.field)[_element.disabled?"add":"remove"]("disabled");
		for(var i=0; i<_element.options.length; i++) {
			if(_element.options[i].value==_element.value){
				this.set(_element.options[i]);
				break;	
			}				
		}
	};
	
	this.dropdown=function(d){
		if(_element.disabled) return false;
		if(parseInt(CSS(_dropdown).get("width"))==_dropdown.clientWidth) CSS(_dropdown).set({height:"auto"}); //if scroll bar = auto height
		if(d==undefined) d=_dropdown.style.visibility!="visible";
		_dropdown.style.visibility=d?"visible":"hidden";
		
		this.isOpen=d;
		return false;
	};
	
	this.set=function(obj){
		_element.prevValue=_element.value;
		_element.value=obj.value;
		_element.selected=obj;
		_content.innerHTML=obj.innerHTML;
		
		//this.autoInlineWidth();
		this.dropdown(0);
		
		if(((!_inInit && _element.prevValue!=_element.value) || _element.autoload) && _element.onchange) _element.onchange(); //call onchange event
		
		//act element in dropdown
		if(this.cur) Style(this.cur).remove("act");
		Style(obj).add("act");
		this.cur=obj;
		
		return false;
	};
	
	this.autoInlineWidth=function(){
		if(CSS(_field).check({display:"block"})) return false; //control is block
		
		if(!_dropdown.inited){ 
			CSS(_dropdown).set({width:(_dropdown.offsetWidth+_arrow.offsetWidth)+"px"});
			 _dropdown.inited=1;
		}

		_field.style["paddingRight"]=0;
		var offset=_dropdown.offsetWidth-_field.offsetWidth-parseInt(CSS(_field).get("marginRight"))-parseInt(CSS(_field).get("marginLeft"))-parseInt(CSS(_block).get("borderRightWidth"))-parseInt(CSS(_block).get("borderLeftWidth"));
		if(offset>0) _field.style["paddingRight"]=offset+"px";
	};
	
		
	return this.init();
};


//input functions
Forms.input=function(obj){
	
	var _this=this;
	
	this.init=function(){
		obj.onfocus=null;
		if(obj.getAttribute("mask")){
			_addEvent(obj, "keyup", function(event){_this.mask(this)});
			_addEvent(obj, "blur", function(event){_this.mask(this)});
		}
		if(obj.getAttribute("onenterkey")){
			_addEvent(obj, "keyup", function(event){if(_this.isEnter(event)) _EV(this,'onenterkey')()});
		}
		if(obj.getAttribute("default")!=undefined){
			this.setDefault(obj);
		}
		
		return this;
	};
	
	//apply RegExp mask onkey up
	this.mask=function(obj){
		var re=new RegExp("[^"+obj.getAttribute("mask")+"]","gi");
		obj.value=obj.value.replace(re,"");
	};
	
	//check enter key
	this.isEnter=function(event){
		return (window.event?window.event.keyCode:event.which)==13;
	};
	
	//init default input value & input styles
	this.setDefault=function(obj){
		function setOpt(obj){
			if(obj.def_opt) CSS(obj).set(obj.def_value!=obj.value?obj.def_style:obj.def_opt);
		};
		//init
		if(!obj.def_value){
			var opt=_ATTR(obj,"default");
			if(opt && /^{[^}]*}$/.test(opt)){ 
				obj.def_opt=(new Function("return "+opt))();
				obj.def_value=obj.value=obj.getAttribute("value") || obj.value;
				obj.def_style=CSS(obj).get(obj.def_opt);
			}
			else if(opt) obj.def_value=obj.def_text=opt;
			_addEvent(obj, "focus", function(){_this.setDefault(this)});
			_addEvent(obj, "blur", function(){if(!this.value) this.value=this.def_value; setOpt(this)});
		}
		else if(obj.def_value==obj.value) obj.value=obj.def_text?obj.def_text:"";
		setOpt(obj);
		
	};
	return this.init();
};



function checkbox_group(obj){
	var chb=_(obj, "/form~@input");
	var checked_all=true;
	var set_all;
	if(chb[0]==obj) set_all=obj.checked;
	for(var i=1; i<chb.length; i++){
		if(chb[i].type!="checkbox") continue;
		if(!chb[i].checked) checked_all=false;
		if(set_all!=undefined) chb[i].checked=set_all;
	}
	if(set_all!=undefined) return true;
	chb[0].checked=checked_all;
}

function toTop(num){
	document.documentElement.scrollTop=num;
}