var newTweets = 0;
var time = 0;
var documentTitle;
var searchString = "";
var interval = 45000;

$(document).ready(function() {
    if (typeof geoip_city == "function") {
        $("#locationSearch").val(geoip_city() + ", " + geoip_region() + ", " + geoip_country_code());
    }
    
    $("#locationSearch").autocomplete("/weather/findLocation", 
        { minChars:2, cacheLength: 0 }
    );
    
    $("#locationSearch").click(function() { 
        searchString = $(this).val();
        $(this).css({backgroundColor: "#d6ecff", color: "#000000"}).val(""); 
    }).blur(function() { 
        if ($(this).val() == "") {
            $(this).val(searchString);
        }
        $(this).css({backgroundColor: "#ffffff", color: "#000000"}); 
    }).keypress(function(e) {
        if (e.which == 13) {
            newSearch();
            $("#searchButton").css({opacity: ".4", filter: "alpha(opacity=40)"}); 
            $("#searchButton").blur(); 
            setLoader(true);
        }    
    });
    
    $("#searchButton").hover(function() {
            $(this).attr({src: "/sites/tweetweather/static/images/searchhover.png"});
        }, function() {
            $(this).attr({src: "/sites/tweetweather/static/images/searchbutton.png"});     
        }
    ).click(function() { 
        $(this).css({opacity: ".4", filter: "alpha(opacity=40)"}); 
        $(this).blur(); 
        setLoader(true);
    });
    
    $(".twitterReply").hover(function() {
       $(this).animate({width: "50px"}, 200); 
    }, function() {
       $(this).animate({width: "15px"}, 200);         
    });

    if ($("#newTweets").length > 0) {
        setIntervalSearch();
    }
});

function newSearch() {
    document.location.href = "/weather/location/" + encodeURI($("#locationSearch").val());
    return false;
}

function setLoader(bool) {
    if (bool) {
        $("#loader").html("<img src=\"/sites/tweetweather/static/images/loader.gif\" />");
    } else {
        $("#loader").html("");
    }
}

function setIntervalSearch() {
    if (interval == 45000) {
        documentTitle = document.title;
    }
    setTimeout(function() {
        setLoader(true);
        if (typeof q1 == "undefined") {
            q1 = "";
        }
        var location = document.location.href.replace("/location/", "/newTweets/") + "?time=" + time + "&interval=" + interval + "&q1=" + q1 + "&q2=" + q2;
        $.getJSON(location, function(json){
            setLoader(false);
            newTweets += json.newTweets; 
            time = json.time;
            if (newTweets > 0) {
                $("#newTweets").html(
                    "<span>There " + (newTweets != 1 ? "are" : "is") + " " + newTweets + " new Tweet" + (newTweets != 1 ? "s" : "") +
                    ". Please <a href=\"javascript:document.location.href=document.location.href\">refresh the page</a> to view " + (newTweets != 1 ? "them" : "it") + ".</span><b>Hey You!</b>"
                );
                $("#newTweets").css({display: "block"});
                document.title = "(" + newTweets + ") " + documentTitle;
            }
            setIntervalSearch();
        });
    }, parseInt(interval));
    interval = parseInt(interval) + 15000;
}

function showAlert(element) {
    var action = "block";
    if ($(element).next("div.alert").css("display") == "block") {
        action = "none";
    }
    $(element).next("div.alert").css({display: action});
    $(element).blur();
}
