var gdir;
var geocoder = null;
var marker;
var map;
var fromAddress;
var toAddress;

function initialize(mapID) {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById(mapID));
        map.setCenter(center, 9);
        map.setUIToDefault();
        gdir = new GDirections(map, document.getElementById("directions_response"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
        marker = new GMarker(center);
        //GEvent.addListener(marker, "click", function() {
            //marker.openInfoWindowHtml(popUp);
        //});
        map.addOverlay(marker);
        //marker.openInfoWindowHtml("test");
    }
    else {
        //document.getElementById("map_container").style.display = "none";
    }
}

function resetMap() {
    map.clearOverlays();
    map.setZoom(7);
    map.panTo(center);
    map.addOverlay(marker);
    //marker.openInfoWindowHtml(popUp);
}

function setDirections() {
    /*if (typeof (Page_ClientValidate) == 'function') {
        isValid = Page_ClientValidate();
    }*/
    fromAddress = document.getElementById("ctl00_MainContentePlaceHolder_DirectionsInput").value;
    if (fromAddress.toLowerCase() !== "type your address.") {
        if (fromAddress.toLowerCase() !== "") {
            processing();

            //if(fromAddress=="")
            //{alert("Please enter either a postcode, or a street and town and try again.");resetMap();return;}
            //toAddress = "HBP Systems Woodhouse Road, Scunthorpe @ 53.5744, -0.6231";
            var locale = "en_UK";

            map.clearOverlays();
            gdir.load("from: " + fromAddress + " to: " + toAddress);
        }
    }
    //else {
    //    return false;
    //}
}

function handleErrors() {
    processing();
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
        alert("We couldn't find your exact address \"" + fromAddress + "\". This may be because the address you specified is relatively new, incorrect, or you may have to be more specific.\n\nPlease try again.\n\nIf this problem persists, please contact HBP on 01724 400 300");
        resetMap();
        return;
    }
    if (gdir.getStatus().code == G_GEO_SERVER_ERROR) {
        alert("Your directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code + "\n\nPlease try again, putting either postcode or street and town in to the directions box");
        resetMap();
        return;
    }
    if (gdir.getStatus().code == G_GEO_MISSING_QUERY) {
        alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
        resetMap();
        return;
    }
    //   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
    //     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);

    if (gdir.getStatus().code == G_GEO_BAD_KEY) {
        alert("We could not process your query at this present time.\n\n Please contact HBP on 01724 400 300 with the following error:\n\n Error code: " + gdir.getStatus().code + " - Map Key Invalid");
        resetMap();
        return;
    }
    if (gdir.getStatus().code == G_GEO_BAD_REQUEST) {
        alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
        resetMap();
        return;
    }
    else {
        alert("Please check your address and try again.");
        resetMap();
        return;
    }

}

function onGDirectionsLoad() {
    //processing();
    // Use this function to access information about the latest load()
    // results.

    // e.g.
    // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
    // and yada yada yada...
    document.getElementById("directions_response_container").style.display = "block";
}
function processing() {
    //if(document.getElementById("processing").style.display=="none")
    //	{document.getElementById("processing").style.display="block"}
    //else{document.getElementById("processing").style.display="none"}
}

function view(request, width, height, title, description, type) {
    width = (width == "") ? "300" : width;
    //height = (height == "") ? "300" : height;
    type = (type == undefined) ? null : type;
    description = (description == "" || description == undefined) ? "<img src='../images/layout/Saul-Home-Building-For-A-Better-Community-Small.jpg' />" : description;
    var frameSrc = request.href;
    title = (title == "undefined" ? request.title : title)
    if (type == null) {
        frameSrc = frameSrc + "&iframe=true&width=" + width + "&height=" + height;
    }
    jQuery.prettyPhoto.open(frameSrc, title, description);
}

function printerFriendlyDirections(request) {
    description = "<img src='../images/layout/Saul-Home-Building-For-A-Better-Community-Small.jpg' />"
    var frameSrc = request.href;
    var title = request.title;
    var prettyPhotoRequest = frameSrc;
    prettyPhotoRequest = frameSrc + "?&mapdirections=http://maps.google.com/maps?f=d&source=s_d&saddr=" + fromAddress.replace(/ /g, "+") + "&daddr=" + toAddress.replace(/ /g, "+").replace(/@/g, "%40") + "&hl=en&geocode=FXjUEQMd5BL-_yl13iGvC6DYRzGZKtXdWjqWUg%3BFQB7MQMdBH72_w&mra=ls&sll=37.0625,-95.677068&sspn=33.435463,134.912109&ie=UTF8&z=7&layer=c&pw=2&?iframe=true&width=800&height=600"
    jQuery.prettyPhoto.open(prettyPhotoRequest, title, description);
}

function popUp() {
    return "<div class='pop_up'><img src='../images/layout/map_logo.jpg' alt='" + CustomerName + "' align='middle' /><br /><br /><span class='business_name'>" + CustomerName + "</span><br/>" + Address[0] + ", " + Address[1] + "<br />" + Address[2] + ", " + Address[3] + "<br /><br /><span><a href='mailto:" + EmailAddress + "' title='Send Email to " + CustomerName + "'>" + EmailAddress + "</a><br /><a href='" + CorporateWebAddress + "' title='" + CustomerName + " Home' class='map_text_link'>" + CorporateSiteName + "</a></span></div>"
}

var Selectors = Array();
var HeaderPanes = Array();
var SubPanes = Array();

function SetUpSelectors() {
    var AllLiTags = document.getElementsByTagName("li");
    var AllUlTags = document.getElementsByTagName("ul")
    var i;
    for (i = 0; i < AllLiTags.length; i++) {
        if (AllLiTags[i].className.match("header_pane")) {
            HeaderPanes.push(AllLiTags[i]);
        }
        if (AllLiTags[i].className.match("sub_pane")) {
            SubPanes.push(AllLiTags[i]);
        }
    }
    var j;
    for(j= 0; j<AllUlTags.length; j++){
        if (AllUlTags[j].className.match("_selector")) {
            Selectors.push(AllUlTags[j]);
        }   
    }
}

function SwitchPane(PaneType, PaneNumber) {
    var Selector, Pane;
    var i;
    for (i = 0; i < Selectors.length; i++) {
        if (Selectors[i].className.match(PaneType)) {
            Selector = Selectors[i];
        }
    }
    var SelectorLinks = Selector.getElementsByTagName("a");
    var j;
    for (j = 0; j < SelectorLinks.length; j++) {
        SelectorLinks[j].className = "off";
        if (PaneType.match("header")) {
            HeaderPanes[j].className = HeaderPanes[j].className.replace("show", "hide");
        }
        else {
            SubPanes[j].className = SubPanes[j].className.replace("show", "hide");
        }
        if (j == PaneNumber - 1) {

            if (PaneType.match("header")) {
                SelectorLinks[j].className = "on";
                SelectorLinks[j].blur();
                HeaderPanes[j].className = HeaderPanes[j].className.replace("hide", "show");
            }
            /*else {
                SubPanes[j].className = SubPanes[j].className.replace("hide", "show");
            }*/
        }
        else {
            if (PaneType.match("header")){
                if (HeaderPanes[j].className.match(PaneNumber)) {
                    HeaderPanes[j].className = HeaderPanes[j].className.replace("hide", "show");
                    SelectorLinks[j].className = "on";
                    SelectorLinks[j].blur();
                }
            }
            else{
                if (SubPanes[j].className.match(PaneNumber)) {
                    SubPanes[j].className = SubPanes[j].className.replace("hide", "show");
                    SelectorLinks[j].className = "on";
                    SelectorLinks[j].blur();
                }
            }   
        }     
    }
}

function SetupPopUps() {
    jQuery(document).pngFix(); 
//    jQuery(".site_plan_image .bubble .bubble_btn").hover(function() {
//        jQuery(this).stop(true, true).prev("ul").animate({ opacity: "show", top: "-100" }, 300);
//    }, function() {
//        jQuery(this).stop(true, true).prev("ul").animate({ opacity: "hide", top: "-95" }, 300); 
//    });
//    jQuery(".site_plan_image .bubble .info_window").hover(function() {
//        jQuery(this).stop(true, true).animate({ opacity: "show", top: "-100" }, 300);
//    }, function() {
//        jQuery(this).stop(true, true).animate({ opacity: "hide", top: "-95" }, 300);
//    });
    jQuery(".site_plan_image .bubble .bubble_btn").hover(function() {
        jQuery(this).prev("ul").css({ display: "block" });
    }, function() {
        jQuery(this).prev("ul").css({ display: "none" });
    });
    jQuery(".site_plan_image .bubble .info_window").hover(function() {
        jQuery(this).css({ display: "block" });
    }, function() {
        jQuery(this).css({ display: "none" });
    });
}

