/* Common Lib (from http://ir2.ru/common.js) -utf */
document.write('<style type="text/css">#unjs {display:none;}</style>')

var hiddclass = 'disnone'

function scriptRequest(src) {
 var el=document.createElement("SCRIPT");
 el.type="text/javascript";
 document.body.appendChild(el);
 el.src=src;
}

function prevent(e) {
 e = e || window.event
 e.cancelBubble = true
 if (e.stopPropagation) {
  e.stopPropagation()
  e.preventDefault()
 }
}

function hidTemp() { /* Спрятать всплывшее <>*/
 tempobj=window.tempobj || window.parent.tempobj //вызов из iframe
 if (!tempobj) return
 var el, t = (tempobj.length) ? tempobj : [tempobj] //несколько элементов
 for (var i=0; i<t.length; i++) {
  if (t[i]) cc(t[i], hiddclass)
 }
 if (d.active_input) {
  d.active_input.cpl = true
  cc(d.active_input, null, hiddclass)
 }
}

function initvar(c) {
 if (!window.d || "object" != typeof window.d) window.d={}
 c = c || window.c
 if (!c) return
 for (var prop in c) {
  var b=c[prop]
  if (0<b.length)
   d[b]=document.getElementById(b)
 }
}

function addLoadEvent(func) {
	var old = window.onload
	if (typeof old != 'function') window.onload = func
	else window.onload = function() { old(); func(); }
}

function findParent(el, tag, cN){ 
	
	if (!el || !el.tagName) return null
	tag = tag || ''; cN = cN || '.*'
	var body = document.body
	
	if (tag && tag.tagName) { /* м.б. объект элемент */
		while (el && body != el.parentNode) {
			el = el.parentNode
			if (el == tag) return el
		}
		return null
	}
	
	tag = new RegExp('^(' + tag + ')$', 'i')
	cN = new RegExp('(^|\\s)(' + cN + ')(\\s|$)', 'i')
	
	while (el.parentNode) {
		if (body == el) break;
		el = el.parentNode //не сам объект!!
		if (el.tagName && el.tagName.match(tag) && el.className.match(cN)) return el
	}
	return null
}

function findChild(obj, tag, classn, rec) {
	if (!obj) return null
	if (!obj.hasChildNodes()) return null
	var oTag 
	var el, el_class, list = (rec) ? obj.getElementsByTagName(tag) : obj.childNodes
	var l = list.length
	for (var i=0; i<l; i++) {
		el = list[i]
		oTag = el.tagName
		el_class = el.className
		if (oTag && tag.toLowerCase()==oTag.toLowerCase() 
			&& (!classn || hc(el_class, classn))) {
			return el
			break
		}
	}
	return null
}

function hc(cN /*string only*/, c) { /*hasClass*/
	return (!c || !cN) ? false : ((" " + cN + " ").indexOf(" " + c + " ") !== -1)
}

function cc(o, add, del) { /*cnangeClass*/
	var n = "className", cN = (undefined != o[n]) ? o[n] : o, ok = 0
	if ("string" !== typeof cN) return false
	var re = new RegExp('(\\s+|^)' + del + '(\\s+|$)', 'g')
	if (add) /*addClass*/
		if (!hc(cN, add)) {cN += " " + add; ok++}
	if (del) /*delClass*/
		if (hc(cN, del)) {cN = cN.replace(re, " "); ok++}
	if (!ok) return false
	if ("object" == typeof o) o[n] = cN 
	else return cN
}

function getTopLeft(el) {
	var top=0, left=0
	while(el) {
		top = top + parseInt(el.offsetTop)
		left = left + parseInt(el.offsetLeft)
		el = el.offsetParent
	}
	return {top:top, left:left}
}


function gi(i, d) {d = d || document; return d.getElementById(i)}
function ge(i, d) {d = d || document; return d.getElementById(i)}
function ce(t, d) {d = d || document; return d.createElement(t)}
function ct(tx, d) {d = d || document; return d.createTextNode(tx)}
function gt(t, e) {e = e || document; return e.getElementsByTagName(t)}
function ac (n, e) {e = e || document.body; return e.appendChild(n);}


function fixTime() {
	var d1, d0 = new Date(), ret, i, 
		obj = arguments[0], f = arguments[1],
		args = Array.prototype.slice.call(arguments, 2)
	obj = obj || window
	if (!(f in obj) || typeof obj[f] !== 'function') return
	ret = obj[f].apply(this, args)
	d1 = new Date()
	Log(d1 - d0, f)
	return ret
}

function getCookie(name) {
	if (name && new RegExp('(?:^|;\\s*)' + name + '=([^;]*)').test(document.cookie)) {
		return unescape(RegExp.$1);
	}
}

function setCookie(name, value, path, time) {
 path = (path) ? "; path=" + path : ""
 var expires = new Date()
 if (isNaN(time)) expires = ""
 else {
  if (time < 0) time = -(12*60*60*1000)
  else if (time === 0) time = 36*24*60*60*1000
  expires.setTime(expires.getTime()+time)
  expires = expires.toGMTString()
  expires = "; expires=" + expires
 }
 name=("string"==typeof(name) && name) || this.name || this.id
 value = (undefined == value) ? this.value : value
 value=(this.type && "checkbox"==this.type && "on"==value)?1:value
 value=(this.type && "select-one"==this.type)?this.selectedIndex:value
 
 document.cookie = escape(name) + "=" + escape(value) + expires + path
}

function cancel(e) {
 e = e || window.event
 if (27==e.keyCode) hidTemp()
}

function hex2dec(str){
 return (parseInt(str,16))
}

function dec2hex(num){
 return num.toString(16)
}

function dec2bin(num){
 return num.toString(2)
}

function trim(s) {
	s = s.replace(/\&nbsp\;/g, "\xa0");
	return s.replace(/^\s+|\s+$/, "");
}

function stripTags(s) {
	return s.replace(/\<[^\<\>]+\>/ig,"");
}

function showChildSpan(obj, timerId) {
 var that = (this.tagName) ? this : obj
 if (that.timer) clearTimeout(that.timer)
 cc(that, null, "hide_ul")
}

function hideChildSpan(obj, timerId) {
 var that = (this.tagName) ? this : obj
 that.timer = setTimeout(function(){cc(that, "hide_ul"); }, 900)
}

function setchbox(el) {
  if (!(el && el.type && "checkbox"==el.type)) return
  gc=parseInt(getCookie(el.id))
  if (gc) { /* else значения из PHP */
   ch=(1==gc)?true:false
   el.checked=ch
  }
}

function hidd(obj, yes) {
 if (yes) cc(obj, "disnone")
 else cc(obj, null, "disnone")
}

function getStyleVal(el, name) {
 var style = (window.getComputedStyle) ? window.getComputedStyle(el, null) : el.currentStyle
 return style && style[name]
}

function sqw_1() {
	return dec2hex(parseInt(d.artbox[0]) + parseInt(d.artbox[1]))
}

function tre_1(d1, d2) {
	return dec2hex(parseInt(d.artbox[0]) * parseInt(d.artbox[1]))
}

function amt_1(d1, d2) {
	return dec2bin(parseInt(d.artbox[0]) + parseInt(d.artbox[1]))
}

function calendar_data() {
	var arr = {}
	arr.month = []
	arr.day = ['вс', 'пн', 'вт', 'ср', 'чт', 'пт', 'сб']
	return arr
}

function pravda(){
	return confirm('Вы уверены?')
}

function build_x(doc) {
	/** Это "модальный" крест. Он может быть одновременно только у одного объекта.
	 **/
	 doc = doc || document
	if (!window.d || "object" != typeof window.d) window.d = {}
// 	совместимость, бл
	if (!window.dict || "object" != typeof window.dict) window.dict = {}
	if (d.krest) {return}
	d.krest = dict.krest = ac(buildEl2('button', {className: 'krest disnone'}, 'x', doc), doc.body);
}

function addKrest(box) {
	var krest = dict.krest, coo = getTopLeft(box), w = box.offsetWidth;
	krest.onclick = function(){cc(box, 'disnone'); cc(krest, 'disnone'); }
	cc(krest, null, 'disnone')
	krest.style.top = coo.top - 10 + 'px'
	krest.style.left = coo.left + w - 20 + 'px'
	if (window.tempobj) tempobj.push(box, krest)
}

function buildField (el, params, idxs) {
	if (params.type == 'radio') return buildRadio (el, params, idxs)
	var el = ce(el), name = params.name, value = params.value;
	for (var id in params) {
		//try{el.setAttribute(id, params[id])}
		//catch(e){}
		el[id] = params[id]
	}
	if (idxs) {
		
		var params;
		for (id in idxs) {
			params = {value:id}
			if (id == value) params.selected = true
			ac(buildEl2('option', params, idxs[id]), el)
		}
	}
	return el
}

function buildRadio (el, params, idxs) {
	var field, span, p = ce('p'), name = params.name, value = params.value;
	for (var id in params) el[id] = params[id]
	if (idxs) {
		var params;
		for (id in idxs) {
			field = ce(el)
			field.type = 'radio'
			field.name = name
			field.value = id
			span = buildEl2('span', {}, idxs[id])
			span.insertBefore(field, span.firstChild)
			ac(span, p)
		}
	}
	p.insertBefore(buildEl2('span', {className:'radio_label'}, params.title + ':'), p.firstChild)
	return p
}
/*
<input type="radio" name="labeled" value="1" id="labeled_1" /><label for="labeled_1">Раз</label>
*/
function buildEl2 (el, params, txt, d) {
	d = d || document
	txt = txt || undefined
	var el = ce(el, d);
	for (var id in params) {
		el[id] = params[id]
	}
	if (txt != undefined) el.appendChild(ct(txt, d))
	return el
}

var Entity = {
	/* by Rumata, http://code.google.com/p/jsxt/source/browse/trunk/js/web/Entity.js*/
	encode: function(s){
	        var div = ce('div'), text = ct(s);
	        ac(text, div);
	        return div.innerHTML.replace(/"/g, '&quot;').replace(/'/g, '&#39;');
	}, 

	decode: function(s){
	        var textarea = ce('textarea');
	        textarea.innerHTML = s;
		
	        return textarea.value;
	}

};

function fetch(obj) {
 var msg = '', val, own
 for (var prop in obj) {
  if (prop) {
   try {val = obj[prop]}
   catch (e) {val = e && e.message + "!!"}
  }
  try {own = obj.hasOwnProperty(prop)}
  catch (e) {own = false}
  prop = own ? "<em>" + prop + "</em>" : prop
  msg += "<strong>" + prop + "</strong>" + ": " + val + "<br>\n"
 }
 return msg
}

function findObj(arr, key, div) {
	div = div || ':'
	key = key.split(div)
	for (var i = 0, l = key.length; i < l; i ++) {
		arr = arr && arr[key[i]]
	}
	return arr
}

