function initKLayers(){
  isDOM=document.getElementById?true:false
  isOpera=isOpera5=window.opera && isDOM
  isOpera6=isOpera && window.print
  isOpera7=isOpera && document.readyState
  isMSIE=isIE=document.all && document.all.item && !isOpera
  isStrict=document.compatMode=='CSS1Compat'
  isNN=isNC=navigator.appName=="Netscape"
  isNN4=isNC4=isNN && !isDOM
  isMozilla=isNN6=isNN && isDOM

  if(!isDOM && !isNC && !isMSIE && !isOpera){
    KLayers=false
    return false
  }

  pageLeft=0
  pageTop=0

  KL_imgCount=0
  KL_imgArray=new Array()

  KL_imageRef="document.images[\""
  KL_imagePostfix="\"]"
  KL_styleSwitch=".style"
  KL_layerPostfix="\"]"

  if(isNN4){
    KL_layerRef="document.layers[\""
    KL_styleSwitch=""
  }

  if(isMSIE){
    KL_layerRef="document.all[\""
  }

  if(isDOM){
    KL_layerRef="document.getElementById(\""
    KL_layerPostfix="\")"
  }

  KLayers=true
  return true
}

initKLayers()

// document and window functions:

function KL_getBody(w){
  if(!w) w=window
  if(isStrict){
    return w.document.documentElement
  }else{
    return w.document.body
  }
}

function getWindowLeft(w){
  if(!w) w=window
  if(isMSIE || isOpera7) return w.screenLeft
  if(isNN || isOpera) return w.screenX
}

function getWindowTop(w){
  if(!w) w=window
  if(isMSIE || isOpera7) return w.screenTop
  if(isNN || isOpera) return w.screenY
}

function getWindowWidth(w){
  if(!w) w=window
  if(isMSIE) return KL_getBody(w).clientWidth
  if(isNN || isOpera) return w.innerWidth
}

function getWindowHeight(w){
  if(!w) w=window
  if(isMSIE) return KL_getBody(w).clientHeight
  if(isNN || isOpera) return w.innerHeight
}

function getDocumentWidth(w){
  if(!w) w=window
  if(isMSIE || isOpera7) return KL_getBody(w).scrollWidth
  if(isNN) return w.document.width
  if(isOpera) return w.document.body.style.pixelWidth
}

function getDocumentHeight(w){
  if(!w) w=window
  if(isMSIE || isOpera7) return KL_getBody(w).scrollHeight
  if(isNN) return w.document.height
  if(isOpera) return w.document.body.style.pixelHeight
}

function getScrollX(w){
  if(!w) w=window
  if(isMSIE || isOpera7) return KL_getBody(w).scrollLeft
  if(isNN || isOpera) return w.pageXOffset
}

function getScrollY(w){
  if(!w) w=window
  if(isMSIE || isOpera7) return KL_getBody(w).scrollTop
  if(isNN || isOpera) return w.pageYOffset
}

function preloadImage(imageFile){
  KL_imgArray[KL_imgCount]=new Image()
  KL_imgArray[KL_imgCount++].src=imageFile
}

var KL_LAYER=0
var KL_IMAGE=1

function KL_findObject(what,where,type){
  var i,j,l,s
  var len=eval(where+".length")
  for(j=0;j<len;j++){
    s=where+"["+j+"].document.layers"
    if(type==KL_LAYER){
      l=s+"[\""+what+"\"]"
    }
    if(type==KL_IMAGE){
      i=where+"["+j+"].document.images"
      l=i+"[\""+what+"\"]"
    }
    if(eval(l)) return l
    l=KL_findObject(what,s,type)
    if(l!="null") return l
  }
  return "null"
}

function KL_getObjectPath(name,parent,type){
  var l=((parent && isNN4)?(parent+"."):(""))+((type==KL_LAYER)?KL_layerRef:KL_imageRef)+name+((type==KL_LAYER)?KL_layerPostfix:KL_imagePostfix)
  if(eval(l))return l
  if(!isNN4){
    return l
  }else{
    return KL_findObject(name,"document.layers",type)
  }
}

function layer(name){
  return new KLayer(name,null)
}

function layerFrom(name,parent){
  if(parent.indexOf("document.")<0) parent=layer(parent).path
  return new KLayer(name,parent)
}

function image(name){
  return new KImage(name,null)
}

function imageFrom(name,parent){
  if(parent.indexOf("document.")<0) parent=layer(parent).path
  return new KImage(name,parent)
}

// class "KLayer":

function KLayer(name,parent){
  this.path=KL_getObjectPath(name,parent,KL_LAYER)
  this.object=eval(this.path)
  if(!this.object)return
  this.style=this.css=eval(this.path+KL_styleSwitch)
}

KLP=KLayer.prototype

KLP.isExist=KLP.exists=function(){
  return (this.object)?true:false
}

function KL_getPageOffset(o){ 
  var KL_left=0
  var KL_top=0
  do{
    KL_left+=o.offsetLeft
    KL_top+=o.offsetTop
  }while(o=o.offsetParent)
  return [KL_left, KL_top]
}

KLP.getLeft=function(){
  var o=this.object
  if(isMSIE || isMozilla || isOpera) return o.offsetLeft-pageLeft
  if(isNN4) return o.x-pageLeft
}

KLP.getTop=function(){
  var o=this.object
  if(isMSIE || isMozilla || isOpera) return o.offsetTop-pageTop
  if(isNN4) return o.y-pageTop
}

KLP.getAbsoluteLeft=function(){
  var o=this.object
  if(isMSIE || isMozilla || isOpera) return KL_getPageOffset(o)[0]-pageLeft
  if(isNN4) return o.pageX-pageLeft
}

KLP.getAbsoluteTop=function(){
  var o=this.object
  if(isMSIE || isMozilla || isOpera) return KL_getPageOffset(o)[1]-pageTop
  if(isNN4) return o.pageY-pageTop
}

KLP.getWidth=function(){
  var o=this.object
  if(isMSIE || isMozilla || isOpera7) return o.offsetWidth
  if(isOpera) return this.css.pixelWidth
  if(isNN4) return o.document.width
}

KLP.getHeight=function(){
  var o=this.object
  if(isMSIE || isMozilla || isOpera7) return o.offsetHeight
  if(isOpera) return this.css.pixelHeight
  if(isNN4) return o.document.height
}

KLP.getZIndex=function(){ //deprecated
  return this.css.zIndex
}

KLP.setLeft=KLP.moveX=function(x){
  x+=pageLeft
  if(isOpera){
    this.css.pixelLeft=x
  }else if(isNN4){
    this.object.x=x
  }else{
    this.css.left=x+"px"
  }
}

KLP.setTop=KLP.moveY=function(y){
  y+=pageTop
  if(isOpera){
    this.css.pixelTop=y
  }else if(isNN4){
    this.object.y=y
  }else{
    this.css.top=y+"px"
  }
}

KLP.moveTo=KLP.move=function(x,y){
  this.setLeft(x)
  this.setTop(y)
}

KLP.moveBy=function(x,y){
  this.moveTo(this.getLeft()+x,this.getTop()+y)
}

KLP.setZIndex=KLP.moveZ=function(z){ //deprecated
  this.css.zIndex=z
}

KLP.setVisibility=function(v){
  this.css.visibility=(v)?(isNN4?"show":"visible"):(isNN4?"hide":"hidden")
}

KLP.show=function(){
  this.setVisibility(true)
}

KLP.hide=function(){
  this.setVisibility(false)
}

KLP.isVisible=KLP.getVisibility=function(){
  return (this.css.visibility.toLowerCase().charAt(0)=='h')?false:true
}

KLP.setBgColor=function(c){
  if(isMSIE || isMozilla || isOpera7){
    this.css.backgroundColor=c
  }else if(isOpera){
    this.css.background=c
  }else if(isNN4){
    this.css.bgColor=c
  }
}

KLP.setBgImage=function(url){
  if(isMSIE || isMozilla || isOpera6){
    this.css.backgroundImage="url("+url+")"
  }else if(isNN4){
    this.css.background.src=url
  }
}

KLP.setClip=KLP.clip=function(top,right,bottom,left){
  if(isMSIE || isMozilla || isOpera7){
    this.css.clip="rect("+top+"px "+right+"px "+bottom+"px "+left+"px)"
  }else if(isNN4){
    var c=this.css.clip
    c.top=top
    c.right=right
    c.bottom=bottom
    c.left=left
  }
}

KLP.scrollTo=KLP.scroll=function(windowLeft,windowTop,windowWidth,windowHeight,scrollX,scrollY){
  if(scrollX>this.getWidth()-windowWidth) scrollX=this.getWidth()-windowWidth
  if(scrollY>this.getHeight()-windowHeight) scrollY=this.getHeight()-windowHeight
  if(scrollX<0)scrollX=0
  if(scrollY<0)scrollY=0
  var top=0
  var right=windowWidth
  var bottom=windowHeight
  var left=0
  left=left+scrollX
  right=right+scrollX
  top=top+scrollY
  bottom=bottom+scrollY
  this.moveTo(windowLeft-scrollX,windowTop-scrollY)
  this.setClip(top,right,bottom,left)
}

KLP.scrollBy=KLP.scrollByOffset=function(windowLeft,windowTop,windowWidth,windowHeight,scrollX,scrollY){
  var X=-parseInt(this.css.left)+windowLeft+scrollX
  var Y=-parseInt(this.css.top)+windowTop+scrollY
  this.scroll(windowLeft,windowTop,windowWidth,windowHeight,X,Y)
}

KLP.scrollByPercentage=function(windowLeft,windowTop,windowWidth,windowHeight,scrollX,scrollY){
  var X=(this.getWidth()-windowWidth)*scrollX/100
  var Y=(this.getHeight()-windowHeight)*scrollY/100
  this.scroll(windowLeft,windowTop,windowWidth,windowHeight,X,Y)
}

KLP.write=function(str){
  var o=this.object
  if(isNN4){
    var d=o.document
    d.open()
    d.write(str)
    d.close()
  }else{
    o.innerHTML=str
  }
}

KLP.add=function(str){
  var o=this.object
  if(isNN4){
    o.document.write(str)
  }else{
    o.innerHTML+=str
  }
}

// class "KImage":

KIP=KImage.prototype

function KImage(name){
  this.path=KL_getObjectPath(name,false,KL_IMAGE)
  this.object=eval(this.path)
}

KIP.isExist=KIP.exists=function(){
  return (this.object)?true:false
}

KIP.getSrc=KIP.src=function(){
  return this.object.src
}

KIP.setSrc=KIP.load=function(url){
  this.object.src=url
}

//###########################################

function ShowGift(id, center) {
	var SnowObj = document.getElementById('img_'+id);
	var SnowTextObj = document.getElementById('text_'+id);
	if (!SnowObj) return;

	SnowObj.style.display = '';
	if (center) {
		SnowObj.style.left = (getScrollX(document.body) + getWindowWidth(document.body)/2) - (SnowObj.offsetWidth/2) - 20;
		SnowObj.style.top = (getScrollY(document.body) + getWindowHeight(document.body)/2) - (SnowObj.offsetHeight/2) - 20;
	} else {
		var left = 	getrandom(getScrollX(document.body), (getScrollX(document.body) + getWindowWidth(document.body) - SnowObj.offsetWidth) - 20);
		var top = 	getrandom(getScrollY(document.body), (getScrollY(document.body) + getWindowHeight(document.body) - SnowObj.offsetHeight));
		
		
		SnowObj.style.left = left;
		SnowObj.style.top = top;
		
		SnowTextObj.style.left = left + SnowObj.offsetWidth / 2 - SnowTextObj.offsetWidth / 2;
		SnowTextObj.style.top = top + SnowObj.offsetHeight / 2 - SnowTextObj.offsetHeight / 2;
		
	}

	return false;
}

function XMLparse2(xml) {	
	// возвращает DOM-объект загруженного xml.
	var domdoc;
	if(document.implementation && document.implementation.createDocument){
		// for non-IE browsers
		var parser = new DOMParser();
		domdoc = parser.parseFromString(xml,"text/xml");
  }
  else if(window.ActiveXObject){
		// for IE-based browsers
		domdoc = new ActiveXObject('MSXML.DomDocument');
		domdoc.async = false;
		domdoc.loadXML(xml);
  }else{
  	alert ("can't DOM");
  }

  return domdoc;
}

function Empty() {
	
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			return;
		}
		
		var returned = xmlobj.xmlhandler.responseText;
	
	//alert(returned);
}

var curpid;
function GiftSend(to_user_name, gift_text, pid) {
	curpid = pid;
	var dv = document.getElementById('GiftSendWarn'+pid);
	dv.innerHTML = 'Идет отсылка';
	gift_text = gift_text.replace("'","\\'");
	var dv = document.getElementById('GiftSendWarn'+pid);
	var URL = "/index.php?automodule=gift&cmd=send&to_user_name="+to_user_name+"&text="+gift_text;

	xmlobj = new ajax_request();
	xmlobj.onreadystatechange(GiftSendProcess);
	xmlobj.process( URL );

}

function GiftSendProcess() {
	var dv = document.getElementById('GiftSendWarn'+curpid);
	var dv_coins = document.getElementById('GiftCoinsNum');
	if ( ! xmlobj.readystate_ready_and_ok() )
	{
		return;
	}

	var xml = xmlobj.xmlhandler.responseText;
	struct = XMLparse2(xml);
	var warn = struct.getElementsByTagName('warn')[0].text;
	var coins;
	if (coins = struct.getElementsByTagName('gift_coins')[0]) {
		coins = coins.text;
		dv_coins.innerHTML = coins;
	}
	dv.innerHTML = warn;
	
}

function ShowGiftText(id) {
	var dv = document.getElementById('text_'+id);
	dv.style.visibility = 'visible';
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange(Empty);
	xmlobj.process("/?automodule=gift&cmd=read&gift_id="+id);
}

function CloseGift(id) {
	var dv = document.getElementById('img_'+id);
	var dv2 = document.getElementById('text_'+id);
	document.body.removeChild(dv);
	document.body.removeChild(dv2);
}



function getrandom(min_rand, max_rand) { 
	max_rand++;
	var range = max_rand - min_rand;
	var n=Math.floor(Math.random()*range) + min_rand;
	return n;
} 

function myqu(txt) {
	txt = txt.replace("/<.*>/","");
	return txt;
}

function GiftBegin() {
	var URL = "/?automodule=gift";
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange(GiftBeginProcess);
	xmlobj.process( URL );
}

function GiftBeginProcess() {

	if ( ! xmlobj.readystate_ready_and_ok() )
	{
		return;
	}

	var xml = xmlobj.xmlhandler.responseText;

	struct = XMLparse2(xml);


var str = struct.getElementsByTagName('gifts')[0];
if (!str) return;
var step = 500;
var cnt = 1000;

for (i=0;i<str.getElementsByTagName('item').length;i++) {
	var el = str.getElementsByTagName('item')[i];
	var id = el.getElementsByTagName('id')[0].text;
	var text = el.getElementsByTagName('text')[0].text;
	var from_user_id = el.getElementsByTagName('from_user_id')[0].text;
	var from_user_name = el.getElementsByTagName('from_user_name')[0].text;
//	var type = el.getElementsByTagName('text')[0].type;
	var type = el.getElementsByTagName('type')[0].text;
	
	dv = document.createElement('div');
	dv.id = 'img_'+id;
	dv.style.position = 'absolute';
	dv.style.display = 'none';
	dv.style.border = '0px solid red';
//	dv.innerHTML = '<span onclick="ShowGiftText('+id+');this.style.cursor=\'normal\';return false;" style="cursor:hand;"><img src="/custom/img/snow_big.gif"></span>';
var pic= '';
 if (type == 'snow') {pic = type+'_big.gif';} else {pic = type+'.jpg';} 
    dv.innerHTML = '<span onclick="ShowGiftText('+id+');this.style.cursor=\'normal\';return false;" style="cursor:hand;"><img src="/custom/img/'+pic+'"></span>';
	

	dv2 = document.createElement('div');
	document.body.appendChild(dv);
	dv2.id = 'text_'+id;
	dv2.className = 'post1';
	dv2.style.border = '1px solid black';
	dv2.style.width = '200px';
	dv2.style.height = '200px';
	dv2.innerHTML = '<table width="100%" height="100%"><tr><td align=center valign=middle background="/custom/img/bgsnow.jpg"><span style="position:absolute;right: 5px;top: 4px;"><a href="#" onclick="CloseGift('+id+');return false;" title="Закрыть"><b>X</b></a></span>от <b><a href="/index.php?showuser='+from_user_id+'">'+from_user_name+'</a></b><br><br><b><font  color=red>'+myqu(text)+'</font></td></tr></table>';
	dv2.style.background = '#FFFFFF';
	dv2.style.visibility = 'hidden';
	dv2.style.position = 'absolute';
	document.body.appendChild(dv2);

	setTimeout('ShowGift('+id+', false)', cnt);
	cnt += step;
	}

}

var hint_dv;

function hint_off() {
	hint_dv.style.display = 'none';
}
	
function hint_move(e) {
    var dv = hint_dv;
    
   	var mousex; var mousey;
   if (document.layers) {
      	var mousex=e.pageX; var mousey=e.pageY;
   } else if (document.all) {
      	var mousex=event.x; var mousey=event.y+document.body.scrollTop;
   }

	var mx = mousex + getScrollX(document.body);
	var my = mousey + getScrollY(document.body);
	dv.style.left = mx + 5;
	dv.style.top = my - dv.offsetHeight - 5;
    
     if((parseFloat(dv.style.left) + dv.offsetWidth) > getWindowWidth()){
    	dv.style.left = parseFloat(dv.style.left) - parseFloat(dv.offsetWidth) - 13;
    }
  
}

function hint_on(dv) {
	hint_dv = dv;
	dv.style.display = '';
 //  document.appendChild(dv);
    
    hint_move();
    document.onmousemove = function () {
        hint_move();
    };
}

