window.scrollTo(0, 1);

window.addEventListener("load", setupSearchCloseButton, false);

var IS_IPHONE = false;
if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) 
     IS_IPHONE = true;



function getAjaxObject() {
    var xmlHttp;
    try { xmlHttp = new XMLHttpRequest(); }
    catch(e) { 
        try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
        catch(e) {
            try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
            catch(e) { alert("Your browser does not support AJAX!"); return false; }
        }
    }
    return xmlHttp;
}

function getActionUrl(action_name, params) {
    var url = '/index.php?action=' + action_name + '&';
    for (var word in params) {
        url += word + '=' + params[word] + '&';
    }
    return url;
}

function isAjaxSuccess(ajax) { if (ajax.readyState == 4) return true; return false; }

function loadMore(start_num, action, query) {
    var ajax = getAjaxObject();
    ajax.onreadystatechange = function() {
        if (isAjaxSuccess(ajax)) {
            var id = 'loadMore-' + start_num;
            document.getElementById(id).innerHTML = ajax.responseText;
        }
    }
    var params = new Array();
    params['start_num'] = start_num;
    params['query'] = query;
    ajax.open("GET", getActionUrl(action, params));
    ajax.send(null);
}

function logQuery(query) {
    var ajax = getAjaxObject();
    
    var params = new Array();
    params['query'] = query;
    ajax.open("GET", getActionUrl('log_query', params));
    ajax.send(null);
}

function showHideSearch() {
    var id = "searchBox";
    if (document.getElementById(id).style.display == "none") {
        showLayer(id);
    }
    else {
        hideLayer(id);
    }
}

function hideLayer(layerId) {
        document.getElementById(layerId).style.display = "none";
}

function showLayer(layerId) {
        document.getElementById(layerId).style.display = "";
}

function goToSearchResults() {
    var query = document.getElementById('query').value;
    window.location = '/search/' + query.replace(/ /gi, '+');
}

function setupSearchCloseButton() {
    var close = document.querySelector(".textBoxClose");
    var input = document.querySelector(".searchInput");
    var img = document.querySelector(".textBoxClose img");
    var emptyMsg = 'Search iPhone sites...';
    var firstFocus = true;

    if (IS_IPHONE) {
        checkInput();
        close.onmousedown = function(e) { if (e) e.preventDefault(); };
        close.onclick = function(e) {
            input.value = '';
            input.focus();
            img.style.display = "none";
            if (e) e.preventDefault();
        };
    }
    if (input.value == '' && firstFocus)
        input.value = emptyMsg;

    input.onfocus = function() {
        if (firstFocus && input.value == emptyMsg) {
            input.value = '';
            firstFocus = false;
        }
    }
    input.onkeyup = function() { checkInput(); };

    function checkInput() {
        if (IS_IPHONE) {
            if (input.value == '') {
                if (img.style.display != 'none')
                    img.style.display = "none";
            }
            else {
                if (img.style.display == 'none')
                    img.style.display = "block";
            }
        }
    }
}

function getCookie(name) {
        var pos = document.cookie.indexOf(name+"=");
        if (pos<0) return null;
        var endpos = document.cookie.indexOf(";",pos);
        if (endpos<0) endpos=document.cookie.length;
        return unescape(document.cookie.substring(pos+name.length+1,endpos));
}

