function Caller(action, onerror){
	this.netHandler = function(){
		if (this.readyState == 4) {
			if (this.status == 200 && this.responseXML != null){
				if (this.action != null)  this.action(this.responseXML)
			}
			else
			if (this.on_error != null) this.on_error()

		}
	}

	this.execGET = function (url){
		this.c.open("GET", url)
		this.c.send('')
	}

	this.execPOST = function(url, params){
		this.c.open("POST", url)
		this.c.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		data = ""
		for(var i in params)
		    data += i+'='+encodeURI(params[i])+'&'

		this.c.send(data);
	}

	this.c = new XMLHttpRequest()

    if(onerror){
        this.c.on_error = onerror
    }

    if(action){
        this.c.action = action
        this.c.onreadystatechange = this.netHandler
    }
}

function Parser(xml){
    this.xml = xml
	this.t = function(name){
      this.l = this.xml.getElementsByTagName(name)[0]
      if( this.l)
        return this.l.textContent
      else
        return ''
    }

    this.a = function(name, attr){
      if(attr){
        this.t(name)
        name = attr
      }
      return this.l.getAttribute(name)
    }
}

function addClass(e, value){
  if (!e.className) {
    e.className = value;
  } else {
      var ca = e.className.split(" ")
      var i = ca.length;
      while(i-- > 0){
          if(ca[i] == value) break
      }
      if( i == -1 )
        e.className = e.className+" "+value;
      else 
        return false
    }
    return true
}

function delClass(e, value){
  if (e.className) {
      var ca = e.className.split(" ")
      for(var i=0;i<ca.length;i++){
          if(ca[i] == value) break;
      }
      e.className = ca.slice(0, i).concat(ca.slice(i+1)).join(" ")
    }
}

function mkNode(n, c, t){
  var d = document.createElement(n)
  if(c) d.className = c
  if(t) d.textContent = t
  return d
}

function mkDiv(c, t){
  return mkNode('div', c, t)
}

function mkSpan(c, t){
  return mkNode('span', c, t)
}

function i$(id){
  return document.getElementById(id)
}

function ghostField(ids){
  var i = ids.length;
  while(i--){
	var f = i$('a_'+ids[i])
	var t = i$('t_'+ids[i])
	if(t){
		var title = t.innerHTML
		f.value = f.ghostText = title.substr(0, title.length-1)
		t.parentNode.removeChild(t)
	}

	f.style.color='#8C8C9E'

	f.onfocus = function(){ if(this.value==this.ghostText){ this.value=''; this.style.color='#000' }}
	f.onblur = function(){ if(this.value==''){this.value=this.ghostText; this.style.color='#8C8C9E'}}
  }
} 

//-------------------------------
//Drag and Drop
//-------------------------------

var dragObject  = null;
var mouseOffset = null;

function dndGetMouseOffset(target, ev){
	ev = ev || window.event;

	var docPos    = getPosition(target);
	var mousePos  = dndMouseCoords(ev);
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}

function getPosition(e){
	var left = 0;
	var top  = 0;

	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;

	return {x:left, y:top};
}

function dndMouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

function dndMouseMove(ev){
	ev           = ev || window.event;
	var mousePos = dndMouseCoords(ev);
	
	if(dragObject){
		dragObject.globalY = mousePos.y
		dragObject.globalX = mousePos.x

		dragObject.style.top      = mousePos.y - mouseOffset.y;
		dragObject.style.left     = mousePos.x - mouseOffset.x;

		return false;
	}
}

function dndMouseUp(){
	dragObject = null;
	
	document.onmousemove = function(){};
	document.onmouseup = function(){};
}

function dndMouseDown(ev){
  return;
}

function makeDraggable(item, mUp, mMove, mDown){
	mUp =   (mUp)?(mUp):(dndMouseUp)
	mMove = (mMove)?(mMove):(dndMouseMove)
	mDown = (mDown)?(mDown):(dndMouseDown)

	if(!item) return;
	  item.mDown = mDown
	  item.mMove = mMove
	  item.mUp = mUp
	  
	  item.onmousedown = function(ev){
		  dragObject = this
		  mouseOffset = dndGetMouseOffset(this, ev);
		  
		  document.onmousemove = this.mMove
		  document.onmouseup   = this.mUp 
		  
		  this.style.position = 'absolute'
		  dragObject.mDown(ev, this)
		  return false;
	  }
}

//-------------------------------------------
// gAlert
//-------------------------------------------

function gAlertHide(){
  alertbox= i$('galert')
  if(alertbox.style.opacity>0){
	alertbox.style.opacity-=0.1
	setTimeout('gAlertHide()', 100)
  }else
	alertbox.style.display='none'
}


function gAlertShow(){
  alertbox= i$('galert')
  if(alertbox.opacity<0.9){
	alertbox.opacity+=0.1
	alertbox.style.opacity=alertbox.opacity
	setTimeout('gAlertShow()', 100)
  }else
  setTimeout('gAlertHide()', 3000)
}
  
function gAlert(text, class_name){
  class_name = (class_name)?(class_name):('galert_notify')
  alertbox= i$('galert')
  alertbox.opacity = 0
  alertbox.innerHTML=text
  alertbox.className = class_name
  alertbox.style.display='block'
  gAlertShow()
}


//-------------------------------------------
// Image grow
//-------------------------------------------

IMAGE_GROW_STEP = 20
DIV_TO_GROW = document.createElement('div')
DIV_TO_GROW.className = 'imgframe'
IMAGE_TO_GROW = document.createElement('img')
DIV_TO_GROW.style.display = 'none'
DIV_TO_GROW.style.position = 'absolute'
DIV_TO_GROW.style.zIndex = 10
A_TO_GROW = document.createElement('a')
A_TO_GROW.appendChild(IMAGE_TO_GROW)
DIV_TO_GROW.appendChild(A_TO_GROW)
IMG_CACHE = new Array()

INFO_TO_GROW = document.createElement('div')
DIV_TO_GROW.appendChild(INFO_TO_GROW)
GROW_TIMEOUT = 0
I_GROW = 0

TIME_TO_GROW = 700
TIMER_TO_GROW = 0

function imageGrow()
{
	DIV_TO_GROW.style.left = (IMAGE_TO_GROW.pos.x-=IMAGE_TO_GROW.wstep/2.0)

	DIV_TO_GROW.style.top = (IMAGE_TO_GROW.pos.y-=IMAGE_TO_GROW.hstep/2.0)

	IMAGE_TO_GROW.style.width = (IMAGE_TO_GROW.w+=IMAGE_TO_GROW.wstep)
	IMAGE_TO_GROW.style.height = (IMAGE_TO_GROW.h+=IMAGE_TO_GROW.hstep)

	if(I_GROW < IMAGE_GROW_STEP){
		GROW_TIMEOUT = setTimeout('imageGrow()', 10)
		I_GROW++;
	}
	else{
		if(IMAGE_TO_GROW.wstep < 0){ 
		  DIV_TO_GROW.style.display="none"
		  if(special_img) special_img.style.zIndex=1
		}
		else{
		  DIV_TO_GROW.style.width = IMAGE_TO_GROW.style.width;
		  INFO_TO_GROW.innerHTML = INFO_TO_GROW.info
		}
	}
}

function imageShrinkStart()
{
  I_GROW = 0
  IMAGE_TO_GROW.wstep = -IMAGE_TO_GROW.wstep
  IMAGE_TO_GROW.hstep = -IMAGE_TO_GROW.hstep
  INFO_TO_GROW.innerHTML = ""
  DIV_TO_GROW.style.width = 'auto';
  imageGrow()
}

//IMAGE_TO_GROW.onmouseout = imageSrinkStart

function imageShrinkStart2(ev)
{
	ev = ev || window.event;
	var pos = dndMouseCoords(ev);
	if(pos.x < growImageRect.x1 || pos.x > growImageRect.x2 || 
	   pos.y < growImageRect.y1 || pos.y > growImageRect.y2 ){
		 imageShrinkStart()
		 document.onmousemove = function(){}
	}
}

function getSizeFromClass(img)
{
  var ca = img.className.split(" ")
  for(var i=0; i < ca.length; i++){
	if(ca[i][0] == 's'){
		var wh = ca[i].split('x')
		if(wh.length == 2 && !isNaN(wh[0]) && !isNaN(wh[1]) ) return {w:wh[0], h:wh[1]}
	}
  }
  return {w:520, h:360}
}

function imageGrowStart(img_id, info_id, link)
{
	info_id = (info_id)?(info_id):('')
	link = (link)?(link):('')
	
	I_GROW = 0
	if(GROW_TIMEOUT) clearTimeout(GROW_TIMEOUT)
	if(info_id) INFO_TO_GROW.info = i$(info_id).innerHTML

	if(link) A_TO_GROW.href = link
	else delete(A_TO_GROW.href)

	document.body.appendChild(DIV_TO_GROW)

	var img = i$(img_id)
	IMAGE_TO_GROW.src = img.src
	IMAGE_TO_GROW.src = img.src.substr(0, img.src.length-1)

	var wh = getSizeFromClass(img)

	IMAGE_TO_GROW.wstep = (wh.w-img.width)/IMAGE_GROW_STEP
	IMAGE_TO_GROW.hstep = (wh.h-img.height)/IMAGE_GROW_STEP

	var pos = getPosition(img)
	IMAGE_TO_GROW.style.left = pos.x+'px'
	IMAGE_TO_GROW.style.top =  pos.y+'px'

	IMAGE_TO_GROW.style.width = img.offsetWidth
	IMAGE_TO_GROW.style.height =  img.offsetHeight

	IMAGE_TO_GROW.w = img.offsetWidth
	IMAGE_TO_GROW.h =  img.offsetHeight

	IMAGE_TO_GROW.pos = pos

	DIV_TO_GROW.style.display = 'block'

	growImageRect = {x1:pos.x, x2:pos.x+img.offsetWidth, y1:pos.y, y2:pos.y+img.offsetHeight }
	document.onmousemove = imageShrinkStart2

	if(special_img=i$(img_id+'_special')) special_img.style.zIndex=0

	imageGrow()
}

function cacheBigImg(img_id)
{
  var i = new Image()
  var img = i$(img_id)
  i.src = img.src.substr(0, img.src.length-1)
  IMG_CACHE.push(i)
}

function imageGrowStop()
{
  if(TIMER_TO_GROW) clearTimeout(TIMER_TO_GROW);
}

function imageGrowShedule(img_id, info_id, link)
{
  TIMER_TO_GROW = setTimeout('imageGrowStart("'+img_id+'", "'+info_id+'", "'+link+'")', TIME_TO_GROW)
}

