var last_url;
var busy_timer;
var scroll_timer;
var scroll_cnt;

function doreqw(allreq, lnk, params, f_scroll)
{
    if ( !ajax_enable ) return;

    last_url = lnk;

    reqs = allreq.split(',');

    for(i=0;i<reqs.length;i++)
    {
        v = reqs[i].split(':');

	var req_section = v[0] ? v[0] : "main_frame";
        var query = "alt_url="+lnk.slice(1)+"&ajax="+v[1]+":"+v[2]; // +(params ? "&"+params : "");

	$.ajax({
	   type: "GET",
	   async: false,
	   cache: false,
	   url: "/index.php",
	   data: query,

	   success: function(data){
 		$("#"+req_section).html(data);

		initLytebox();				// reinit lytebox for new content
	   }
	 });

    }

    if ( f_scroll ) doscroll();				// scrolling to content

    unFocus.History.addHistory(lnk.slice(1));

    return false;
}

function historyHandler()
{
   if ( last_url )
   {
      var t = window.location.href.split("#");

      if ( t[1] != last_url.slice(1) )
	 doreqw('tm_frame:tm,content_frame:content', "/" + (t[1] ? t[1] : '') );
   }
}

function doscroll()
{
//   window.scroll(0, 580); return;

   scroll_cnt = 0;
   scroll_timer = window.setInterval("makescroll();", 10);
}

function makescroll()
{
   pos = (window.scrollY) ? window.scrollY : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;

   if ( scroll_cnt < 10 && pos < 580 )
      window.scrollBy(0, 58)
   else
   {
      clearInterval(scroll_timer);
      window.scroll(0, 580)
   }

   scroll_cnt++;
}


function openpop(url,x,y)
{
    if (!x) x = 600; if (!y) y = 600;
    var left = (screen.width-x)/2;
    var top  = 60;

    window.open(url, "", "width="+x+", height="+y+", left="+left+", top="+top+", menubar=no, status=no, scrollbars=yes");
    return(false);
}

function showBusyLayer() {
        var busyLayer = document.getElementById("busy_layer");

    if (window.innerHeight && window.scrollMaxY) {    
        yScroll = window.innerHeight + window.scrollMaxY;
        var deff = document.documentElement;
        var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
        yScroll -= (window.innerHeight - hff);
    } else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){
        yScroll = document.body.scrollHeight;
    } else { 
        yScroll = document.body.offsetHeight;
      }

    if (busyLayer != null) {
        busyLayer.style.visibility = "visible";
        busyLayer.style.height = yScroll;
    }
};

function hideBusyLayer() {
        var busyLayer = document.getElementById("busy_layer");
        if (busyLayer != null) {
            busyLayer.style.visibility = "hidden";
            busyLayer.style.height = "0px";
        }
};

function startbusytimer()
{
   busy_timer = window.setTimeout("dobusy(true);", 500);
}

function dobusy(busy)
{
  busy_frame = document.getElementById('busy_frame');

  if (busy)
  {
     busy_frame.style.display='';

     windowWidth  = window.navigator.userAgent.indexOf("MSIE") >= 0 ? document.body.clientWidth : document.documentElement.clientWidth;
     windowHeight = window.navigator.userAgent.indexOf("MSIE") >= 0 ? document.body.clientHeight : document.documentElement.clientHeight;

     busy_frame.style.top  = 300 + document.body.scrollTop;
     busy_frame.style.left = (windowWidth - 20) / 2;
  }
  else
  {
     busy_frame.style.display='none';
     clearInterval(busy_timer);
  }
}

function hlight(o, nm)
{
    document.getElementById(o).src=nm;
    return(true);
}

function fnShowProps(obj, objName)
{
    var result = "";
    for (var i in obj) // обращение к свойствам объекта по индексу
        result += objName + "." + i + " = " + obj[i] + "<br />\n";
    document.write(result);
}

function getOffsetRect(elem) {
    // (1)
    var box = elem.getBoundingClientRect();

    // (2)
    var body = document.body;
    var docElem = document.documentElement;

    // (3)
    var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop;
    var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft;

    // (4)
    var clientTop = docElem.clientTop || body.clientTop || 0;
    var clientLeft = docElem.clientLeft || body.clientLeft || 0;

    // (5)
    var top  = box.top +  scrollTop - clientTop;
    var left = box.left + scrollLeft - clientLeft;

    return { top: Math.round(top), left: Math.round(left) };
}


var ua = navigator.userAgent.toLowerCase();

// Определим Internet Explorer
isIE = (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1); 

// Opera
isOpera = (ua.indexOf("opera") != -1); 

// Gecko = Mozilla + Firefox + Netscape
isGecko = (ua.indexOf("gecko") != -1);

// Safari, используется в MAC OS
isSafari = (ua.indexOf("safari") != -1);

// FireFox
isMozilla = (ua.indexOf("mozilla") != -1 && ua.indexOf("msie") == -1);

