var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;

isMSIE=document.all&&document.all.item;
isMozilla=navigator.appName=="Netscape"||navigator.appName=="Opera";

heavyImage = new Image();
img1 = new Image();
img1 = "http://www.wowarea.ru/i/tooltip_loading.gif";
img2 = new Image();
img2 = "http://www.wowarea.ru/i/tooltip_notfound.gif";

if(isMSIE)
heavyImage.src = "http://img.wowarea.net/i/tooltip.gif";
else
heavyImage.src = "http://img.wowarea.net/i/tooltip.png";

var mousex = 0;
var mousey = 0;

var dW=0, dH=0;

if (isMSIE){
  document.onmousemove = function(){
    mousex = event.clientX + document.body.scrollLeft;
    mousey = event.clientY + document.body.scrollTop;
    wowarea_link_mousemove();
    return true;
  }
}
else if (isMozilla){
  document.onmousemove=function(e){
    mousex = e.pageX;
    mousey = e.pageY;
    wowarea_link_mousemove();
    return true;
  }
}

tabb='<table cellspacing=0 cellpadding=0 border=0><tr><td id="bg_TL"></td><td id="bg_TC"></td><td id="bg_TR"></td></tr><tr><td id="bg_CL"></td><td class=it id="bg_CC">';
tabe='</td><td id="bg_CR"></td></tr><tr><td id="bg_BL"></td><td id="bg_BC"></td><td id="bg_BR"></td></tr></table>';

function link2id(s) {    var str = s.match(/\?item=([0-9]+)/g);
    str=str.toString();
    return str.match(/([0-9]+)/g);
}

var id=null;
var op=0;
function wowarea_link_hover() {
	RereadField();
	id = link2id(this.href);

    sp=appendC("span",{id:"preview"})

    if(itemDataStatus[id]==null) {
      sp.style.width='auto';
      itemData[id]='<div class="wowarea_tooltip">'+tabb+'<img src="http://www.wowarea.ru/i/tooltip_loading.gif" />'+tabe+'</div>';
  	  itemDataStatus[id]=0;
  	  loader[id]=appendC("script",{type:"text/javascript",src:this.href+"&export&new"});
	  setTimeout("wowarea_showdata("+id+")",333);
	  setTimeout("wowarea_checkdata("+id+")",5000);
  	  N(document.body,loader[id]);
    }

    if(itemDataStatus[id]==1) {
      sp.style.width='350px';
    }

    sp.innerHTML=itemData[id];
    wowarea_changeop(0);
    op=0;
    setTimeout("wowarea_fader()",40);
    wowarea_link_mousemove();
    N(document.body,sp);
}

function wowarea_fader() {  if(id!=null)
    if(op<1) {
      op+=0.2;
      wowarea_changeop(op);
  	  setTimeout("wowarea_fader()",40);
    }
}

function wowarea_link_mousemove() {
  if(id!=null) {
    r1=(sp.offsetLeft>=mousex-20);


    if(sp.offsetWidth+mousex > dW+document.body.scrollLeft) {
      sp.style.left = (mousex-sp.offsetWidth<document.body.scrollLeft)?document.body.scrollLeft:mousex-sp.offsetWidth+'px';
    }
    else
      sp.style.left = (mousex)+'px';

    r2=(sp.offsetLeft>=mousex);

    if(r1!=r2) {      wowarea_changeop(0);
      op=0;
      setTimeout("wowarea_fader()",40);
    }

    sp.style.top  = mousey+20;
  }
}

function wowarea_changeop(o) {  sp.style.opacity=o;
  sp.style.MozOpacity = o;
  sp.style.KhtmlOpacity = o;
  sp.style.filter = "alpha(opacity=" + (o*100) + ")";
}

function wowarea_showdata(fid) {
	if(itemDataStatus[fid]==0) {
      if(id!=null&&fid==id) sp.style.width='auto';
	  setTimeout("wowarea_showdata("+fid+")",333);
    }
    else {
      if(id!=null&&fid==id) {
        if(itemDataStatus[id]==1) {
          sp.style.width='350px';
        }
        sp.innerHTML=itemData[fid];
      }
  	  document.body.removeChild(loader[fid]);
  	}
}

function wowarea_checkdata(fid) {
	if(itemDataStatus[fid]==0) {
  	  itemData[fid]='<div class="wowarea_tooltip">'+tabb+'<img src="http://www.wowarea.ru/i/tooltip_notfound.gif" />'+tabe+'</div>';
  	  itemDataStatus[fid]=null;
  	}
}


function wowarea_link_out() {
   id=null;
   document.body.removeChild(sp);
}

function wowarea_load() {

  var deb=appendC("div",{id:"deb"});
  N(document.body,deb);

  loader	     = new Array();
  itemData 		 = new Array();
  itemDataStatus = new Array();
  hd=document.getElementsByTagName('head')[0];
  N(hd,appendC("link",{type:"text/css",href:"http://www.wowarea.ru/wowarea.css",rel:"stylesheet"}));

  var ref=document.getElementsByTagName('A');
  var elems = new Array();
  var re=/wowarea\swa([0-9])/;
  for(i=0;i<ref.length;i++)
    if(ref[i].className.substring(0,7)=="wowarea") {
      ref[i].onmouseover = wowarea_link_hover;
      ref[i].onmousemove = wowarea_link_mousemove;
      ref[i].onmouseout  = wowarea_link_out;
      if(ref[i].className=="wowarea")
        elems.push(link2id(ref[i].href));
    }
  if(elems.length>0) {  	wowarea_itemcolors = new Array();
  	linkloader=appendC("script",{type:"text/javascript",src:"http://www.wowarea.ru/?item&export&new&color="+elems.join (',')+""});
  	N(document.body,linkloader);
  }
}

function wowearea_colorlinks() {
  document.body.removeChild(linkloader);
  var ref=document.getElementsByTagName('A');
  var elems = new Array();
  var re=/wowarea\swa([0-9])/;
  for(i=0;i<ref.length;i++)
    if(ref[i].className=="wowarea") {
      	lid=link2id(ref[i].href);
      	al=wowarea_itemcolors.length;
      	for(var x=0;x<al;x++) {          d=wowarea_itemcolors[x].match(/([0-9]+)=([0-9])/);
          if(d[1]==lid)            ref[i].className='wowarea wa'+d[2];
        }
    }
}

function RereadField() {if(typeof window.innerWidth=="number") {
	dW=window.innerWidth;
	dH=window.innerHeight
}
else {
	if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)) {
		dW=document.documentElement.clientWidth;
		dH=document.documentElement.clientHeight
	}
	else {
		if(document.body&&(document.body.clientWidth||document.body.clientHeight)) {
			dW=document.body.clientWidth;
			dH=document.body.clientHeight
		}
	}
}
}

function N(AM,AN) {
	return AM.appendChild(AN)
}

function appendC(AO,AN) {
	var AM=document.createElement(AO);
	if(AN) {
		AG(AM,AN)
	}
	return AM
}

function AG(AO,AM) {
	for(var AN in AM) {
		if(typeof AM[AN]=="object") {
			if(!AO[AN]) {
				AO[AN]={}
			}
			AG(AO[AN],AM[AN])
		}
		else {
			AO[AN]=AM[AN]
		}
	}
}

function wowarea_addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}

wowarea_addLoadEvent(wowarea_load);

