isTextBox = false;

$(document).ready(function () {
    focusFields();

    Cufon.replace("#header h2, #leftCol h2, #leftCol .submit, body#home h3");
    Cufon.replace("#button-join", { textShadow: '#fff 1px 2px' });

    $(".location-search").width($("#locationSearchText").width());
    $(".change_city").css("left", $("#locationSearchText").width() + "px");

    // web method call
    $(".change_city").click(function () {
        if (isTextBox == false) {
            toggleLocationSearch();
            isTextBox = true;
            $(".location-search").focus();
        } else {
            if (isTextBox == true) {

                var params = $(".location-search").val();

                var lastThree = params.slice(-3);
                if (lastThree.charAt(0) == " " && params.charAt(params.length - 4) != ",") {
                    params = params.replace(lastThree, "," + lastThree);
                    $(".location-search").val(params);
                }

                $.getJSON(locationServiceUrl,
                        {
                            loc: params
                        },
                        function (obj) {
                            if (obj != "dne") {
                                toggleLocationSearch();
                                $(".userLocation").html(obj.city.toLowerCase() + ", " + obj.stateAbbr + " (" + obj.ZipCode + ")");
                                $(".location-search").width($("#locationSearchText").width());
                                $(".change_city").css("left", $("#locationSearchText").width() + "px");
                            } else {
                                $(".location-search").val("Sorry, we could not find that location.");
                                $(".location-search").css("color", "#777");
                                isTextBox = true;
                            }
                        }
                    );
                isTextBox = false;
            }

        }
    });

    
});

function focusFields(){
    $(".search-box").focus(function() {
	    if( this.value == this.defaultValue ) {
		    this.value = "";
		
	    }
    }).blur(function() {
	    if( !this.value.length ) {
		    this.value = this.defaultValue;
	    }
    });
}

function toggleLocationSearch() {
    $("#locationSearchText").fadeToggle(200);
    $("#location-search-wrap").fadeToggle(200);
    $(".location-search").fadeToggle(200);
    $("#change_city_text").fadeToggle(200);
    $("#continue_text").fadeToggle(200);
    $("#change_city_button").fadeToggle(200);
}

$(document).ready(function () {

});
