var currentHash = '';
var firstContent = '';
var browser = TypeBrowser();
var historyArray = new Array();
var historyKey = 0;
var frameLocation = '';
var lastHistoryKey = '';
var moveOnHistory = 0;
var isCached = false; // Включение кеширования страничек
// var isCached = true; // Выключение кеширования страничек

function contentLoad(uri, div_in) {
    if (! uri) {
        return true;
    }
    if (! div_in) {
      var div_id = 'content';
    } else {
      var div_id = div_in;
    }
    isCached = false;
    if (uri.indexOf('itsinterest') !=-1) {
       // Если обработка - Это интересно, тогда отключаем кеширование
       isCached = true;
       div_id = 'id_interest';
    }

    var coords = getCoords(document.getElementById(div_id));
    var progressbar = document.getElementById('progressbar');
    progressbar.style.left = coords['left'] + 'px';
    progressbar.style.top = coords['top'] + 'px';
    progressbar.style.display = '';
    JsHttpRequest.query(
        '/ajax' + uri,
        { },
        function(result, errors) {
            if (result['content']) {
                document.getElementById(div_id).innerHTML = result['content'];
            }
            progressbar.style.display = 'none';
            return false;
        },
        isCached
//         false
    );

      var result = uri.replace("?", "..");
      result = result.replace("=", "...");
      window.location.hash = '#ajax' + result;

//       window.location.hash = '#ajax' + uri;
      if (browser == 'IE') {
        if (moveOnHistory == 0) {
//            var result = uri.replace("?", "..");
//            result = result.replace("=", "...");
           historyKey = Math.random();
           historyArray[historyKey] = window.location.hash;
//            historyArray[historyKey] = '#ajax' + result;
           document.getElementById('iframe_ie').src = '/files/ajax.html?rnd=' + historyKey;
           lastHistoryKey = historyKey;
        }
        moveOnHistory = 0;
      }
    currentHash = window.location.hash;
    return false;
}
function bookmarkLoad() {
    if ('#ajax/' == window.location.hash.substring(0, 6)) {
//         if (browser == 'IE') {
           var result = replaceUrl(window.location.hash);
           contentLoad(result.substring(5));
//         } else {
//            contentLoad(window.location.hash.substring(5));
//         }
    }
}
function historyLoad() {
    var hash = window.location.hash;
    frameLocation = window.iframe_ie_name.document.location.href;
    var urlKey = getURLParam("rnd");
    if ('#ajax/' == hash.substring(0, 6) && currentHash != hash) {
//         if (browser == 'IE') {
           var result = replaceUrl(hash);
           contentLoad(result.substring(5));
//         } else {
//            contentLoad(hash.substring(5));
//         }
    } else if ('#ajax/' != hash.substring(0, 6) && currentHash) {
        currentHash = '';
        document.getElementById('content').innerHTML = firstContent;
    } else if (frameLocation != 'about:blank' && lastHistoryKey != urlKey) {
        lastHistoryKey = urlKey;
        moveOnHistory = 1;
        var result = replaceUrl(historyArray[urlKey]);
        contentLoad(result.substring(5));
    }
    setTimeout('historyLoad()', 100);
}
function getCoords(element) {
    var left = element.offsetLeft;
    var top = element.offsetTop;
    for (var parent = element.offsetParent; parent; parent = parent.offsetParent) {
        left += parent.offsetLeft - parent.scrollLeft;
        top += parent.offsetTop - parent.scrollTop
    }
    return {
    	left: left,
    	top: top,
    	width: element.offsetWidth,
    	height: element.offsetHeight
    };
}
function TypeBrowser() {
    browser = navigator.appName;
    if (browser == "Netscape") browser = "Ns";
    else if (browser == "Opera") browser = "Op";
    else if (browser == "Microsoft Internet Explorer") browser = "IE";
    else browser = "xz";
    return browser;
}
function getURLParam(strParamName){
    var strReturn = "";
//     var strHref = window.location.href;
    var strHref = frameLocation;
    if ( strHref.indexOf("?") > -1 ){
        var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
        var aQueryString = strQueryString.split("&");
        for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
            if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
                var aParam = aQueryString[iParam].split("=");
                strReturn = aParam[1];
                break;
            }
        }
    }
    return unescape(strReturn);
}
function replaceUrl(url) {
   var result_replace = url.replace("..", "?");
   result_replace = result_replace.replace("...", "=");
   return result_replace;
}
