var timeHandle, currentSerp = 0, lastKeyword = '';
if(document.forms['searchform']) {
    document.onkeyup=escHandler;
    with(document.getElementById('searchtrack')) {
        onkeydown=serpHandler;
        name = (''+Math.random()).substr(2);
    }
}

var hash = ''+window.location;
if(hash.lastIndexOf('#') >= 0) {
    hash = hash.substr(hash.lastIndexOf('#')+1);
    window.onload = function(){if(document.getElementById(hash))document.getElementById(hash).lastChild.style.display = 'block';}
}

if (window.XMLHttpRequest) {
  http = new XMLHttpRequest();
} else if (window.ActiveXObject) { // Internet Explorer
  try {
	http = new ActiveXObject('Msxml2.XMLHTTP');
  } catch (e) {
    try {
	  http = new ActiveXObject('Microsoft.XMLHTTP');
    } catch (e) {}
  }
}

function beginSearch(){
    if (timeHandle) clearTimeout(timeHandle);
    timeHandle = window.setTimeout('sendSearchRequest()', 300);
}

function sendSearchRequest() {
    var keyword = document.getElementById('searchtrack').value;
    if(keyword == lastKeyword) return;
    lastKeyword = keyword;
    if(keyword.length < 3 || keyword == defaultsearch) {showSearchBox(false); return;}

    with(document.getElementById('resultbox').style) {
        left = leftoffset(document.getElementById('searchtrack')) + 'px';
        top = (topoffset(document.getElementById('searchtrack')) + document.getElementById('searchtrack').offsetHeight) + 'px';
    }

    http.open('POST', basepath + 'ajax.php', true);
    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.onreadystatechange = getSearchResults;
    http.send('search=&trackname=' + escape(keyword).replace(/\+/g, '%2B'));
}

function getSearchResults() {
    if(http.readyState != 4) return;
    var content = http.responseText;
    if(content.length) content += '<a href="javascript:showSearchBox(0)" style="height:18px;">'+searchclose+'</a>';
    document.getElementById('resultbox').innerHTML = content;
    showSearchBox(content.length > 0);
    currentSerp = 0;
}

function gotoResult(id) {
    window.location = '#t-' + id;
    showSearchBox(false);
}

function submitSearch() {
    if(currentSerp > 0) document.location = document.getElementById('serp' + currentSerp).href;
    if (timeHandle) clearTimeout(timeHandle);
    with(document.getElementById('searchtrack')) {
        var retval = (currentSerp == 0 && value.length > 0 && value != defaultsearch);
        name=(retval)?'trackname':(''+Math.random()).substr(2);
        return retval;
    }
}

function showSearchBox(active) {
    if(!active) highlightSerp(0);
    document.getElementById('resultbox').style.display = (active) ? 'block' : 'none';
}

function searchFocus(gotfocus) {
    with(keyword = document.getElementById('searchtrack')) {
        if(value == defaultsearch && gotfocus) value = '';
        if(value == '' && !gotfocus) value = defaultsearch;
    }
}
        
function escHandler(ev) {
    var key = getKeyEvent(ev);
    if(key == 27) {
        if (timeHandle) clearTimeout(timeHandle);
        showSearchBox(false);
    }
}

function serpHandler(ev) {
    var key = getKeyEvent(ev);
    if(key == 38) highlightSerp(currentSerp-1);
    if(key == 40) highlightSerp(currentSerp+1);
}

function getKeyEvent(ev) {
    if (!ev) ev = window.event;
    if (ev.which) return ev.which;
    else if (ev.keyCode) return ev.keyCode;
}

function highlightSerp(newSerp) {
    if(document.getElementById('resultbox').childNodes.length < 2) return;
    showSearchBox(true);
    if(document.getElementById('serp' + currentSerp)) document.getElementById('serp' + currentSerp).className='';
    if(newSerp < 1) newSerp = document.getElementById('resultbox').lastChild.previousSibling.id.substr(4);
    if(!document.getElementById('serp' + newSerp)) newSerp = 1;
    currentSerp=newSerp;
    if(document.getElementById('serp' + currentSerp))
        document.getElementById('serp' + currentSerp).className='resulthover';
}

function leftoffset(obj){
    var lPos = 0;
    if (obj.offsetParent){
        while (obj.offsetParent){
            lPos += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        lPos = obj.x;
    return lPos;
}
function topoffset(obj){
    var oPos = 0;
    if (obj.offsetParent){
        while (obj.offsetParent){
            oPos += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        oPos += obj.y;
    return oPos;
}

function readcomments(tune) {
 if(document.getElementById('comments_' + tune).style.display == 'block') document.getElementById('comments_' + tune).style.display = 'none'; else 
 document.getElementById('comments_' + tune).style.display = 'block';
 return false;
}

//////////////////// code for preview player

var currentTune = '', currentId = 0, audioElement = null;
function previewtune(tune, id) {
    if(currentId != 0) document.getElementById('preview' + currentId).style.fontWeight = 'normal';
    if(currentTune == tune) {
        document.getElementById('main').removeChild(audioElement);
        audioElement = null;
        currentTune = 0;
        return;
    }
    if(audioElement != null) document.getElementById('main').removeChild(audioElement);
    audioElement = document.createElement('audio');
    audioElement.innerText = 'Sorry, but your browser does not support the HTML 5 <audio> tag.';
    audioElement.setAttribute('src', basepath + 'music_files/ogg/saga_musix_-_' + tune + '.ogg');
    audioElement.setAttribute('autoplay', true);
    audioElement.setAttribute('controls', true);
    audioElement.id = 'audioplayer';
    document.getElementById('main').appendChild(audioElement);

    document.getElementById('preview' + id).style.fontWeight = 'bold';
    currentTune = tune;
    currentId = id;
}

