var sc = "music"; //значение по умолчанию

function createXMLHttp() {
    if(typeof XMLHttpRequest != "undefined") {
        return new XMLHttpRequest();
    } else if(window.ActiveXObject) {
        var aVersions = ["MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0",
                        "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp",
                        "Microsoft.XMLHttp"
        ];
        for (var i = 0; i < aVersions.length; i++) {
            try {
                var oXmlHttp = new ActiveXObject(aVersions[i]);
                //alert(aVersions[i]);
                return oXmlHttp;
            } catch (oError) {
            }
        }
        throw new Error("Невозможно создать объект XMLHttp.");
    }
}

function displayCustomerInfo(sText) {
sElem = document.getElementById("srchCacheContentBody");
sElem.innerHTML = sText;
}

function getRequest(inputValue)
{
    var ccb = document.getElementById('srchCacheContentBody');
//    console.log("ccb: ", ccb);
    
//    if (ccb)
//        console.log("ccb.innerhtml len: ", ccb.innerHTML.length);


    inputValue = inputValue || '';
    inputValue = inputValue.toLowerCase();


    //var sId = document.getElementById("inputSearchRequest").value == '' ? '___' : document.getElementById("inputSearchRequest").value ;
    var sId = document.getElementById("inputSearchRequest").value;
    sId = sId.toLowerCase();

    //
    // если перед вызовом функции значение успело измениться, то отбой
    //
    if (sId != inputValue) return;
    
    //скрываем, если пустой инпут
    if (sId == '')
    {
        hide_srchCacheContent();
        return;
    }
    
    var sc = document.getElementById("inputSearchCat").options[document.getElementById("inputSearchCat").options.selectedIndex].value;
    
    var oXmlHttp = createXMLHttp();
    oXmlHttp.open("GET","/srch-cache/a/srch-cache/" + sc + "/z/str/"+encodeURIComponent(sId),true);
    oXmlHttp.onreadystatechange = function() {
        if(oXmlHttp.readyState == 4) {
        if(oXmlHttp.status == 200) {
        
        //
        // выдёргиваем из пришедшего ответа строку запроса
        //
        //var scstr = oXmlHttp.responseText.match(/scstr="([^"]*)"/);
        
        var start = oXmlHttp.responseText.indexOf('scstr="');
        var scstr = '';
        if (start!=-1) {
            scstr = oXmlHttp.responseText.substr(start+7);
            scstr = scstr.substr(0, scstr.indexOf('"'));
        }

        //
        // получаем текущее значение в инпуте
        //
        sId = document.getElementById("inputSearchRequest").value == '' ? '___' : document.getElementById("inputSearchRequest").value ;
        sId = sId.toLowerCase();
        //alert(sId);
        //
        // если не совпадают то вываливаемся
        //
        if (scstr && (scstr != sId)) return;

    
        displayCustomerInfo(oXmlHttp.responseText);
        
        if (ccb.innerHTML=='')
            hide_srchCacheContent();
        else
            document.getElementById("srchCacheContent").style.display = 'block';

        if (document.getElementById("srchCacheContentBody"))
            {
                if ( document.getElementById("srchCacheContentBody").offsetHeight >= 320 )
                {
                    document.getElementById("srchCacheContentBody").style.height = '320px';
                    document.getElementById("srchCacheContentBody").style.overflow = 'auto';
                        
                    //2 раза из-за глюка IE (на 2й раз определяет правильно!)
                    bodyWidthWithScroll = document.getElementById("srchCacheContentBody").clientWidth;
                    bodyWidthWithScroll = document.getElementById("srchCacheContentBody").clientWidth;
                    bodyWidthWithoutScroll = document.getElementById("srchCacheContentBody").offsetWidth;
                    bodyWidthWithoutScroll = document.getElementById("srchCacheContentBody").offsetWidth;
                    scrlWidth = document.getElementById("srchCacheContentBody").offsetWidth - document.getElementById("srchCacheContentBody").clientWidth;
                    scrlWidth = document.getElementById("srchCacheContentBody").offsetWidth - document.getElementById("srchCacheContentBody").clientWidth;
                
                
                    bodyWidth = 240 + scrlWidth;
                    document.getElementById("csTitle").style.width = bodyWidth;
                    document.getElementById("csTitle").style.width = bodyWidth;
                    
                    document.getElementById("srchCacheContentBody").style.width = bodyWidth;
                    document.getElementById("srchCacheContentBody").style.width = bodyWidth;
                    //document.getElementById("srchCacheContentBody").style.borderBottom = '1px solid black';
                }
                else {
                    document.getElementById("csTitle").style.width = 240;
                    document.getElementById("csTitle").style.width = 240;
                }
            }
            } else {
            //displayCustomerInfo("Ошибка: " + oXmlHttp.statusText);
            }
        }
    };
    oXmlHttp.send(null);      
}

function hide_srchCacheContent()
{
    document.getElementById("srchCacheContent").style.display = 'none';
}

function scOver(obj)
{
    //alert(obj);
    obj.className = 'bg_green';
    imgBox = obj.getElementsByTagName('td')[1];
    imgSelf = imgBox.firstChild;
    imgBox.style.width = '60px';
    imgSelf.style.width = '60px';
}
function scOut(obj)
{
    obj.className = 'bg_beige';
    imgBox = obj.getElementsByTagName('td')[1];
    imgSelf = imgBox.firstChild;
    imgBox.style.width = '30px';
    imgSelf.style.width = '30px';
}

function scGo(cat, idn, city, part)
{
    pageTracker._trackEvent('Search Cache', 'Show More', document.getElementById('inputSearchRequest').value);
    document.location = '/-/a/more/z/cat/' + cat + '/id/' + idn + '/city/' + city + '/part/' + part + '/ftype/srch';
}

/*
function scGetCat()
{
    if (sc=='games' || sc=='films' || sc=='music')
    {
        document.getElementById("srchCacheContent").style.display = 'block';
        getRequest();
    }
    else
    {
        document.getElementById("srchCacheContent").style.display = 'none';
    }
}
*/