function GetMap(map_width, map_height) {
    map = new VEMap('map');
    map.SetCredentials(bingAPIKey);
    map.SetDashboardSize(VEDashboardSize.Tiny);
    map.HideScalebar();
    map.LoadMap();
    map.Resize(map_width, map_height);

    map.Find(null, currentOffice, null, null, null, null, true, true, true, true, Recenter);

    //map.DeleteAllPushpins();

    //try {
    //    var loc = map.GetCenter();
    //    var address = $('.street-address').text();
    //    var city = $('.locality').text();
    //    var state = $('.region').text();
    //    var zip = $('.postal-code').text();
    //    var phone = $('.tel .value').text();
    //var pin = new VEPushpin(1, loc, 'http://secure.jacksonhewitt.com/App_Themes/Default/images/JacksonHewittLogoIcon.gif', 'Jackson Hewitt Tax Service <br/>' + address + '<br />' + city + ' ' + state + ', ' + zip + '<br/>' + phone, '');
    //var pin = new VEPushpin(1, loc,null, 'Jackson Hewitt Tax Service <br/>' + address + '<br />' + city + ' ' + state + ', ' + zip + '<br/>' + phone, '');            
    //            map.AddPushpin(pin);
    //       }
    //catch (err) {
    //           map.Find("Jackson Hewitt", currentOffice, VEFindType.Businesses, null, 0, 1, true, true, true, true, Recenter);
    //}


}

function Recenter(layer, resultsArray, places, hasMore, veErrorMessages) {
    //this will ensure that the map is centered on the pin even when the pin is not the address of the the current office.
    if (resultsArray != null && resultsArray.length > 0) {
        map.DeleteAllPushpins();

        try {
            var address = $('.street-address').text();
            var city = $('.locality').text();
            var state = $('.region').text();
            var zip = $('.postal-code').text();
            var phone = $('.tel .value').text();
            map.SetCenter(resultsArray[0].LatLong);
            var pin = new VEPushpin(1, resultsArray[0].LatLong, 'http://secure.jacksonhewitt.com/App_Themes/Default/images/JacksonHewittLogoIcon.gif', 'Jackson Hewitt Tax Service <br/>' + address + '<br />' + city + ' ' + state + ', ' + zip + '<br/>' + phone, '');
        }
        catch (err) { }

    }
    else if (places != null && places.length > 0) {

        map.DeleteAllPushpins();

        try {

            var address = $('.street-address').text();
            var city = $('.locality').text();
            var state = $('.region').text();
            var zip = $('.postal-code').text();
            var phone = $('.tel .value').text();
            var pin = new VEPushpin(1, places[0].LatLong, 'http://secure.jacksonhewitt.com/App_Themes/Default/images/JacksonHewittLogoIcon.gif', 'Jackson Hewitt Tax Service <br/>' + address + '<br />' + city + ' ' + state + ', ' + zip + '<br/>' + phone, '');
            //var pin = new VEPushpin(1, places[0].LatLong,null, 'Jackson Hewitt Tax Service <br/>' + address + '<br />' + city + ' ' + state + ', ' + zip + '<br/>' + phone, '');            
            map.AddPushpin(pin);
        }
        catch (err) { }
    }
}

function GetTraffic() {
    if (already == true) { return; }
    traffic = new VEMap('traffic_map');
    traffic.SetCredentials(bingAPIKey);
    traffic.HideDashboard();
    traffic.HideScalebar();
    traffic.LoadMap();
    traffic.Resize('482', '220');
    traffic.Find("Jackson Hewitt", currentOffice, VEFindType.Businesses, null, 0, 1, false, false, false, true);
    traffic.LoadTraffic(true);
    setTimeout('traffic.SetZoomLevel(10)', 1200);
    already = true;
}

function PrepList() {

    //    $('.tabbed_container ul.clearfix li ul li:first-child').addClass("title");

    //	$('.tabbed_container ul.clearfix li ul li:last-child').hide().css({ 'width': '482px', 'position': 'relative' });
    //	//fix the internal lists
    //	$('.tabbed_container ul.clearfix li ol li:first-child').removeClass("title");
    //	$('.tabbed_container ul.clearfix li ol li:last-child').show().css({ 'width': '', 'position': '' });
    //	
    //	
    //	if(jQuery.browser.msie){
    //	    $('.tabbed_container ul.clearfix > li').css({ 'margin-top': '-15px' });
    //	}

    //	$('.tabbed_container ul.clearfix li ul li.title').toggle(function() {
    //		$(this).addClass("selected");
    //		}, function () {
    //		$(this).removeClass("selected");
    //	});
    //	$('.tabbed_container ul.clearfix li ul li.title').click(function() {
    //		$(this).next("li").slideToggle("slow");
    //	});
    $('ul#exp_points li ul li:first-child').addClass("title");
    $('ul#exp_points li ul li:last-child').hide().css({ 'width': '482px', 'position': 'relative' });

    $('ul#exp_points li ol li:first-child').removeClass("title");
    $('ul#exp_points li ol li:last-child').show().css({ 'width': '', 'position': '' });

    if (jQuery.browser.msie) {
        $('ul#exp_points > li').css({ 'margin-top': '-15px' });
    }

    $('ul#exp_points li ul li:first-child').toggle(function() {
        $(this).addClass("selected");
    }, function() {
        $(this).removeClass("selected");
    });
    $('ul#exp_points li ul li:first-child').click(function() {
        $(this).next("li").slideToggle("slow");
    });


}

function PrepShadowboxes() {

    Shadowbox.init({
        skipSetup: true,
        players: ["iframe"],
        viewportPadding: 40
    });

    $('#tp_views a#gmapsLink, #tp_views a#yahooLink, #map_options a').each(function() {
        Shadowbox.setup($(this), {
            content: $(this).attr("href"),
            player: "iframe",
            width: "1024",
            height: "640",
            title: 'This Jackson Hewitt location: ' + $(this).text()
        });
    });

}

$(document).ready(function() {

    // Setting up tabs
    var tabContainers = $('div#office_info > div.tabbed_container');
    $('div#office_info ul.tabs a').click(function() {
        tabContainers.hide().filter(this.hash).show();
        $('div#office_info ul.tabs a').removeClass('selected');
        $(this).addClass('selected');
        return false;
    }).filter(':first').click();

    var tabContainersAlt = $('div#office_details > div.tabbed_container');
    $('div#office_details ul.tabs a').click(function() {
        tabContainersAlt.hide().filter(this.hash).show();
        $('div#office_details ul.tabs a').removeClass('selected');
        $(this).addClass('selected');
        return false;
    }).filter(':first').click();




    about_jh = $('#aboutjhtx > div:first-child');
    about_jh.attr('id', 'about_jh');
    about_jh.children().each(function(i) { $(this).replaceWith("<p>" + $(this).text() + "<\/p>"); });

    exp_points = about_jh.next();
    exp_points.attr('id', 'exp_points');
    exp_points.children('li:empty').remove();
    exp_points.children('ul').wrap('<li />');



    // Prepping list toggles and shadowboxes
    PrepList();
    PrepShadowboxes();

    // Some IE6 fixes
    $("#col_right input[type='text']").addClass('ie_text_inputs text');

    // Form validation
    $("#col_right form").RSV({
        onCompleteHandler: null,
        errorFieldClass: "error",
        rules: [
			"required,fname,Please enter your first name.",
			"required,lname,Please enter your last name.",
			"required,email,Please enter your email address.",
			"valid_email,email,Please enter a valid email address.",
			"required,zip,Please enter your zip code.",
			"digits_only,zip,Please enter a valid postal code."
		]
    });

    // Prepping maps
    $("a[href='#traffic']").hover(GetTraffic);
    GetMap($('#map').width(), $('#map').height());

    var about_jh = $('#aboutjhtx > div:first-child');
    about_jh.attr('id', 'about_jh');
    about_jh.children().each(function(i) { $(this).replaceWith("<p>" + $(this).text() + "<\/p>"); });

    $('#pnlDefaultDesc li').wrapInner('<span />');

    $('a.terms').click(function() {
        window.open($(this).attr('href'), 'terms', 'width=400,height=400,resizable=yes,menubar=no,toolbar=no,titlebar=no,scrollbars=yes', false);
        return false;
    });

    $('p.submit_image input').mouseover(function() {
        $(this).attr('src', '/img/button-download_over.gif');

    });

    $('p.submit_image input').mouseout(function() {
        $(this).attr('src', '/img/button-download.gif');
    });
});

function isTermsChecked(sender, args) {
    if ($('.terms_checkbox input').attr('checked')) {

        args.IsValid = true;

    }
    else {
        args.IsValid = false;

    }
}

function pause(milliseconds) {
    var dt = new Date();
    while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
}

function js_gaaw_track() {
    var current_path = document.location.pathname;
    pageTracker._trackEvent('download_form', 'submit', current_path);
    pause(1000);
}

function ValidateAndTrack() {

    Page_ClientValidate();
    if (Page_IsValid) {
        js_gaaw_track();
        return true;
    }
    else {
        return false;
    }
}
