//This is the function that is called after the new page data is loaded
function handle_response(my_data) {
        $("#text").html("");
        my_data = $("#text",my_data).html();
        $("#text").html(my_data);
        $("#text").scrollTo(0);
}

//This function loads the page
function load_page(href, type) {
        href = href.replace(/type=[A-z]+/gi, "");
        if (type == undefined) {
                href = href+"&type="+$("#type").attr("value");
        }
        else {
                href = href+"&type="+type;
        }
        $.ajax({ 
                type: "GET", 
                url: href, 
                success: function(data){ 
                        handle_response(data);
                } 
        });
}

//Function used to switch the international and domestic nav on and off
function region_nav(state) {
        if (state == "international") {
                $("#btn_domestic img").attr({"src":"/yngwie/english/img/btn_domestic_off.jpg"});
                $("#btn_international img").attr({"src":"/yngwie/english/img/btn_international.jpg"});
        }
        else {
                $("#btn_domestic img").attr({"src":"/yngwie/english/img/btn_domestic.jpg"});
                $("#btn_international img").attr({"src":"/yngwie/english/img/btn_international_off.jpg"});
        }
}

//Begin processing for the dealer finder
$(document).ready(function() {
        //Change the form so that it submits through ajax
        $("#dealer_panel form").submit(function() {
                //type = $("#type").attr("value");
                zip = $("#zipfield").attr("value");
                href = "dealer.php?&ZIP="+zip;
                load_page(href);
                return false;
        });
        //Change the links so they submit through ajax
        $("#btn_domestic a").mouseover(function(event) {
                region_nav("domestic");
        });
        $("#btn_international a").mouseover(function(event) {
                region_nav("international");
        });
        $("#btn_domestic a").mouseout(function(event) {
                type = $("#type").attr("value");
                region_nav(type);
        });
        $("#btn_international a").mouseout(function(event) {
                type = $("#type").attr("value");
                region_nav(type);
        });
        $("#btn_domestic a").click(function(event) {
                event.preventDefault();
                $("#type").attr({"value":"domestic"});
                href = "dealer.php?type=domestic"
                load_page(href);
                region_nav("domestic");
        });
        $("#btn_international a").click(function(event) {
                event.preventDefault();
                $("#type").attr({"value":"international"});
                href = "dealer.php?type=international"
                load_page(href);
                region_nav("international");
        });
        $("#sortstate a").click(function(event) {
                event.preventDefault();
                href = "dealer.php?t=international"
                load_page(href);
        })
        $("#sortname a").click(function(event) {
                event.preventDefault();
                href = $(this).attr("href");
                load_page(href);
        })
        $("#loading").ajaxStart(function(){ 
                $(this).show(); 
        });
        $("#loading").ajaxStop(function(){ 
                $(this).hide(); 
        });
})
