/*
Áèáëèîòåêà JSL
Copyright (C) 2007 Êóáàíîâ Îëåã Âëàäèìèðîâè÷
Ñàéò áèáëèîòåêè: http://www.dojo.com.ua/JSL
*/
/*
Ð‘Ð¸Ð±Ð»Ð¸Ð¾Ñ‚ÐµÐºÐ° JSL
Copyright (C) 2007 ÐšÑƒÐ±Ð°Ð½Ð¾Ð² ÐžÐ»ÐµÐ³ Ð’Ð»Ð°Ð´Ð¸Ð¼Ð¸Ñ€Ð¾Ð²Ð¸Ñ‡
Ð¡Ð°Ð¹Ñ‚ Ð±Ð¸Ð±Ð»Ð¸Ð¾Ñ‚ÐµÐºÐ¸: http://www.dojo.com.ua/JSL
*/

JSL = {};
JSL.obj=null;

JSL.pWidget=false;
JSL.pParams=false;

var dua = navigator.userAgent;
var dav = navigator.appVersion;
JSL.opera = dua.indexOf("Opera") >= 0;
JSL.khtml = (dav.indexOf("Konqueror") >= 0)||(dav.indexOf("Safari") >= 0);
JSL.safari = dav.indexOf("Safari") >= 0;
var geckoPos = dua.indexOf("Gecko");
JSL.mozilla = JSL.moz = (geckoPos >= 0)&&(!JSL.khtml);
JSL.ie = (document.all)&&(!JSL.opera);
JSL.chrome = (dua.indexOf("Chrome") >= 0)||(dua.indexOf("chrome") >= 0);

JSL.bind=function(node,widget)
{
var name=node.getAttribute("bind");
if(widget[name]==null) widget[name]=node; else
if(!(widget[name] instanceof Array)) { var temp=widget[name]; widget[name]=new Array; widget[name].push(temp); widget[name].push(node);}
else widget[name].push(node);
}

JSL.getAttribute=function(node,name)
{
if(!node) return null;
if(node.nodeType!=1) return null;
var attr=node.getAttribute(name);
if((attr==null)||(attr=="")) return null;
return attr;
}

var evt = new Array();

function addEvent(tag,name,obj,f,params)
{
po = {t: tag, n: name, o: obj, f: f, params: params};
evt.push(po);
var s="evt["+(evt.length-1)+"].f1=function(e){ evt["+(evt.length-1)+"].f.call(evt["+(evt.length-1)+"].o,e,evt["+(evt.length-1)+"].params) }";
if(obj != null) eval(s); else evt[evt.length-1].f1=f;
//alert(name+": "+(evt.length-1));
if(tag.addEventListener) { tag.addEventListener(name, evt[evt.length-1].f1, false); }
else { tag.attachEvent('on'+name, evt[evt.length-1].f1); }
}

function removeEvent(tag,name,obj)
{
var n;
for(n=0;n<evt.length;n++)
  if((evt[n].t==tag)&&(evt[n].n==name))
    {
    //alert(name);
    if(obj) if(obj!=evt[n].o) continue;
    //alert(name);
    if(tag.removeEventListener) { tag.removeEventListener(name, evt[n].f1, false); }
      else { tag.detachEvent('on'+name, evt[n].f1); }
    evt.splice(n,1);
    }
delete n;
}

var timers = new Array();
var methods = new Array();

JSL.addMethod=function(obj,f,params)
{
po = {o: obj, f: f, params: params};
methods.push(po);
var s;
if(obj!=null) s="methods["+(methods.length-1)+"].f1=function(){ methods["+(methods.length-1)+"].f.call(methods["+(methods.length-1)+"].o,methods["+(methods.length-1)+"].params) }";
else s="methods["+(methods.length-1)+"].f1=function(){ methods["+(methods.length-1)+"].f(methods["+(methods.length-1)+"].params) }";
eval(s);
return methods[methods.length-1].f1;
}


JSL.addInterval=function(obj,f,params,delay)
{
po = {o: obj, f: f, d: delay, params: params};
timers.push(po);
var s="timers["+(timers.length-1)+"].f1=function(){ timers["+(timers.length-1)+"].f.call(timers["+(timers.length-1)+"].o,timers["+(timers.length-1)+"].params) }";
if(obj != null) eval(s); else timers[timers.length-1].f1=f;
timers[timers.length-1].timer=window.setInterval(timers[timers.length-1].f1,delay);
return timers.length-1;
}

JSL.removeInterval=function(n)
{
//for(var n in timers) 
	//if(timers[n].timer==timer)
	//alert(n);
		window.clearInterval(timers[n].timer);
//timers[n]=null;
}

$=function(id)
{
return document.getElementById(id);
}
$create=document.createElement;

function add(node,name,st)
{
var n=document.createElement(name);
for(var p in st) { n.style[p]=st[p]; }
node.appendChild(n);
return n;
}

JSL.styleWidgets={};
JSL.addStyle=function(obj)
{
//if(JSL.styleWidgets[obj.type]) return;
//JSL.styleWidgets[obj.type]=true;
obj=obj.style;
if(document.styleSheets.length==0)
  {
  alert("Ã‚Ã­Ã¨Ã¬Ã Ã­Ã¨Ã¥! Ã„Ã«Ã¿ Ã­Ã®Ã°Ã¬Ã Ã«Ã¼Ã­Ã®Ã© Ã°Ã Ã¡Ã®Ã²Ã» Ã¡Ã¨Ã¡Ã«Ã¨Ã®Ã²Ã¥ÃªÃ¨ Ã±Ã®Ã§Ã¤Ã Ã©Ã²Ã¥ Ã²Ã Ã¡Ã«Ã¨Ã¶Ã³ Ã±Ã²Ã¨Ã«Ã¥Ã©"); //Ã‡Ã Ã¬Ã¥Ã­Ã¨Ã²Ã¼
  return;
  }
	if(document.styleSheets[0].addRule)
		for(n in obj)
			document.styleSheets[0].addRule(obj[n][0], obj[n][1], 0)
	else
		for(n in obj)
			document.styleSheets[0].insertRule(obj[n][0]+" { " + obj[n][1] + " }", 0);
}

mixin=function(obj1,obj2)
{
for(var n in obj2) obj1[n]=obj2[n];
}

JSL.fParse=function(Fragment,obj)
{
JSL.parse(Fragment,obj);
/*for(var n=0; n<JSL.widgets.length; n++) 
if(JSL.widgets[n].postCreate) JSL.widgets[n].postCreate.call(JSL.widgets[n]);
*/
for(var n=0;n<JSL.widgets.length;n++) if(JSL.widgets[n].postAll) JSL.widgets[n].postAll.call(JSL.widgets[n]);
}

JSL.parse=function(Fragment,obj)
{
if(!Fragment.childNodes) return;
var node=null;
for(var n=Fragment.childNodes.length-1;n>-1;n--)
	{
	node=Fragment.childNodes[n];
        if(JSL.getAttribute(node,"bind"))JSL.bind(node,obj);
	
	if(JSL.getAttribute(node,"widget"))
	  {
	   var params={};
	   if(JSL.getAttribute(node,"params")) eval("params={ "+node.getAttribute("params")+" };");
	   //for(var k=0;k<node.attributes.length;k++) try { /*alert(node.attributes[k].nodeName+": "+node.attributes[k].nodeValue);*/ params[node.attributes[k].nodeName]=node.attributes[k].nodeValue; /*eval("params."+node.attributes[k].nodeName+"='"node.attributes[k].nodeValue+"'");*/} catch(e2){}
       var w=JSL.create(node.getAttribute("widget"),node,params);
	   if(obj) { obj.addChild(w); if(w.onAdd) w.onAdd.call(w);}
	  }
	else
	if(JSL.pWidget && JSL.pParams && JSL.getAttribute(node,JSL.pWidget)) 
		{
	   	var params={};
	   	if(JSL.getAttribute(node,JSL.pParams)) eval("params={ "+node.getAttribute(JSL.pParams)+" };");
	   	//for(var k=0;k<node.attributes.length;k++) try { /*alert(node.attributes[k].nodeName+": "+node.attributes[k].nodeValue);*/ params[node.attributes[k].nodeName]=node.attributes[k].nodeValue; /*eval("params."+node.attributes[k].nodeName+"='"node.attributes[k].nodeValue+"'");*/} catch(e2){}
       	var w=JSL.create(node.getAttribute(JSL.pWidget),node,params);
	   	if(obj) { obj.addChild(w); if(w.onAdd) w.onAdd.call(w);}		
		}
	else {JSL.parse(node,obj);}
	}
if(obj) if(obj.postAdd) obj.postAdd.call(obj);
delete node;
}

JSL.cinit=function(obj)
{
if(obj.postAdd) obj.postAdd.call(obj);
if(obj.postAll) obj.postAll.call(obj);
}


JSL.create=function(name,node,params,parent,st,inner)
  {
  if(parent)
  	{
  	var node=document.createElement(node);
  	if(inner) node.innerHTML=inner;
  	if(st) mixin(node.style,st);
  	if(parent.type) {if(parent.containerNode) parent.containerNode.appendChild(node);} else parent.appendChild(node);
  	//alert(1);
  	}
  var node1=node.cloneNode(true);
  eval("var widget=new "+name+"(node,params);");
  widget.type=name;
  if(widget.containerNode) { if(widget.containerNode!==widget.domNode) widget.containerNode.innerHTML=node1.innerHTML;
  JSL.parse(widget.containerNode,widget); }
  if(widget.postCreate) widget.postCreate.call(widget);
  //Ã„Ã®Ã¤Ã¥Ã«Ã Ã²Ã¼ Ã³Ã¤Ã Ã«Ã¥Ã­Ã¨Ã¥ Ã³Ã§Ã«Ã (Ã‚Ã®Ã§Ã¬Ã®Ã¦Ã­Â³ Ã¯Ã°Ã®Ã¡Ã«Ã¥Ã¬Ã» Ã± Ã®Ã±Ã¢Ã®Ã¡Ã®Ã¦Ã¤Ã¥Ã­Ã¨Ã¥Ã¬ Ã¯Ã Ã¬Ã¿Ã²Ã¨):
  node1.style.display="none";
  document.body.appendChild(node1);
  document.body.removeChild(node1);
  delete node1;
  
  if(parent)
  	{
  	if(parent.type) 
  		{
  		//alert(parent.addChild);
  		parent.addChild(widget);
  		if (widget.postAdd) {widget.postAdd.call(widget);}
  		}
  	}
  
  return widget;
  }

JSL.destroy=function(widget)
{
var n;
for(n=0;n<evt.length;n++) if(evt[n].o==widget) removeEvent(evt[n].t,evt[n].n);
if(widget.parent) for(n in widget.parent.children) if(widget.parent.children[n]==widget) widget.parent.children.splice(n);
for(n in widget)
	{
	try{
	if(widget[n].parentNode) widget[n].parentNode.removeChild(widget[n]);
	delete widget[n];} catch(e1) {}
	//if(widget[n]) alert(widget[n]);
	}
delete widget;
}

JSL.destroyAll=function()
  {
  document.body.innerHTML="";
  var n;
  for(n in evt) removeEvent(evt[n].t,evt[n].n);
  for(n in JSL.widgets) { try{JSL.destroy(JSL.widgets[n]); delete JSL.widgets[n];} catch(e1) {} }
  for(var n1=0;n1<1;n1++)for(n in document) {try{delete document[n];} catch(e1) {}}
  for(var n1=0;n1<1;n1++)for(n in window) {try{delete window[n];} catch(e1) {}}
  }
  
JSL.f=function(){ return false; }

JSL.setOpacity=function(node,opacity)
 {
  if(JSL.ie) {node.style.filter="Alpha(Opacity="+opacity*100+")";}
  else mixin(node.style,{opacity: opacity/*,MozOpacity:opacity,KhtmlOpacity:opacity*/});
 	//if(opacity==0.0) node.style.display="none";
 }
 
JSL.getScrollPosition=function() {
    var scrOfX = 0, scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return {"top":scrOfY, "left":scrOfX};
}

JSL.getMouse = function(e){
	var cursor = {x:0, y:0};
	if(e.pageX || e.pageY){
		cursor.x = e.pageX;
		cursor.y = e.pageY;
	}else{
		var de = document.documentElement;
		var db = document.body;
		cursor.x = e.clientX + ((de||db)["scrollLeft"]) - ((de||db)["clientLeft"]);
		cursor.y = e.clientY + ((de||db)["scrollTop"]) - ((de||db)["clientTop"]);
		//alert((document.body.scrollTop));
	}
	return cursor;
}

JSL.getScreenSize=function()
{
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
       }
return {width: myWidth, height: myHeight};
}

JSL.init=function()
{
JSL.fParse(document.body,null);
}

JSL.gStyle=function(node,property)
{
if(!node || !node.style) return false;
if((node.style[property])&&(node.style[property]!="")) return node.style[property];
var selector=property.replace(/([A-Z])/g, "-$1" ).toLowerCase();
//alert(selector);
if (document.defaultView)
if(document.defaultView.getComputedStyle)
	{
	var cs = document.defaultView.getComputedStyle(node, "");
	return cs.getPropertyValue(selector); 
	}
if(node.style.getPropertyValue){return node.style.getPropertyValue(selector);} 

if(node.currentStyle)
	{ return node.currentStyle[property]; }
if(node.style.getPropertyValue){return node.style.getPropertyValue(selector);}
return 0;
}

JSL.getStyle=function(node,property)
{
//alert(node);
if(!node) return 0;
var result=JSL.gStyle(node,property);
if(result=="auto") return 0; else return result;
}

/*JSL.getBox=function(node)
{
var sizing = JSL.getStyle(node, "MozBoxSizing");
if(!sizing){ sizing = JSL.getStyle(node, "boxSizing"); }
return sizing;
}*/

JSL.getLeft=function(node)
{
if(node==document.body) return 0;
if(!node.offsetParent) return node.offsetLeft;
return node.offsetLeft+JSL.getLeft(node.offsetParent);
}

JSL.getTop=function(node)
{
//alert(document.body.offsetTop);
//return JSL.abs(node).y;
var result=0;
if(node==document.body) return 0;
if(!node.offsetParent) return node.offsetTop;
result=node.offsetTop+JSL.getTop(node.offsetParent);
/*if(JSL.ie) 
    {
    alert(JSL.getStyle(node,"margin")+" "+JSL.getStyle(node,"marginTop"));
	result-=JSL.getStyle(node,"margin")+JSL.getStyle(node,"marginTop");
	}*/

return result;
}

JSL.abs=function(node)
{
if(node.getBoundingClientRect) 
	with(node.getBoundingClientRect()){
			return {x: left-2+JSL.getScrollPosition().left, y: top-2+JSL.getScrollPosition().top};
		}
if(document['getBoxObjectFor'])
	return document.getBoxObjectFor(node);

return {x: JSL.getLeft, y: JSL.getTop};
}

JSL.processReqChange=function(params)
{
if(this.req.readyState == 4){
        if(this.req.status == 200){ 
        //alert(params.f);
        params.f(this.req.responseText);
        }
    }
}

JSL.request=function(url,method,func) {
    if (window.XMLHttpRequest) {
        JSL.req = new XMLHttpRequest();
        JSL.req.onreadystatechange = JSL.addMethod(JSL,JSL.processReqChange,{f: func});
        JSL.req.open(method, url, true);
        JSL.req.send(null);
     } else if (window.ActiveXObject) {
        JSL.req = new ActiveXObject("Microsoft.XMLHTTP");
        if (JSL.req) {
            JSL.req.onreadystatechange = JSL.addMethod(JSL,JSL.processReqChange,{f: func});
            JSL.req.open(method, url, true);
            JSL.req.send();
         }
    }
}

JSL.getNames=[];

/*JSL.get=function(name)
{
JSL.getNames.push(name);
}*/

JSL.requireOne=function(name,obj,postFunction)
{
//alert(name);
try{eval("if("+name+")return;");} catch(e1){}
//alert(name);
JSL.load=function(text)
	{
	//alert(text);
	eval(text);
	//alert(postFunction);
	if(obj) postFunction.call(obj);
		else postFunction();
	}
JSL.request(name+".js","GET",JSL.load);
//while(1==1) {try{if(eval(name)) return;} catch(e1){}}
}

JSL._require=function()
{
if(JSL.re.names.length>0)
{
//alert(2);
//alert(JSL.re.names.length);
var name=JSL.re.names[0];
//alert(name);
JSL.re.names.splice(0,1);
//alert(name);
JSL.requireOne(name,JSL,JSL._require);
}
else 
	{
	//alert(3);
	if(JSL.re.obj) JSL.re.postFunction.call(JSL.re.obj);
		else JSL.re.postFunction();
	}
}

JSL.require=function(names,obj,postFunction)
{
//alert(names[0]);
JSL.re={names: [], obj:obj, postFunction: postFunction};
for(var n in names) JSL.re.names.push(names[n]);
JSL._require();
}

JSL.alert=function(obj,prev)
{
for(var n in obj) 
if(prev) {alert(prev+"."+n+": "+obj[n]); JSL.alert(obj[n],prev+"."+n);}
else {alert(n+": "+obj[n]); /*JSL.alert(obj[n],n);*/}
}

widget = function(node,params,templ)
{
	this.addChild=function(obj)
	{
	this.children.push(obj);
	obj.parent=this;
	};
	this.removeChild=function(obj)
	{
	if(this.containerNode) this.containerNode.removeChild(obj.domNode);
	for(var n in this.children) if(this.children[n]==obj) this.children.splice(n,1);
	}
	this.hide=function()
	{
	this.domNode.style.display="none";
	}
	this.show=function()
	{
	if(this.domNode.tagName=="SPAN")this.domNode.style.display="inline";
 	else this.domNode.style.display="block";
	}

	this.destroy=function()
	{
	for(var n1=0; n1<100; n1++)
	for(var n in this)
		{
		if(this[n].parentNode) this[n].parentNode.removeChild(this[n]);
		delete this[n];
		}
	}
	if(templ) this.template=templ;
	this.domNode=node;
	this.children=[];
	this.containerNode=null;
	if(params) mixin(this,params);
	if(!JSL.byId) JSL.byId=[];
	if(!JSL.widgets) JSL.widgets=[];
	if(JSL.getAttribute(node,"id")) JSL.byId[node.getAttribute("id")]=this;
	JSL.widgets.push(this);
	if(this.style) JSL.addStyle(this);
	if((this.template !== undefined)&&(!this.last))
		{
		this.domNode.innerHTML=this.template;
        JSL.parse(this.domNode,this);
		}
}


