var map;
var destinationGuide;
function getLabel(){
    labelextraction = location.href;
    urlpattern = /http.*\/([a-z\-]+)/;
    if(urlpattern.test(labelextraction))
        {
        return RegExp.$1;   
        }
}

function mapLoad(){
var lat;
var lng;
var elevation;
var display;
    GDownloadUrl("/guides/includes/smallmapinfo.cfm?label="+getLabel(), function(data, responseCode) {
    var xml = GXml.parse(data);
    var info = xml.documentElement.getElementsByTagName("map");
    lat = info[0].getAttribute("centerLat");
    lng = info[0].getAttribute("centerLng");
    elevation = parseInt(info[0].getAttribute("elevation"));
    display = parseInt(info[0].getAttribute("display"));
   
    map = new GMap2(document.getElementById("small-map")); 
    map.setCenter(new GLatLng(lat, lng), elevation);
    map.disableDragging();
    
    if(display){
        GEvent.addListener(map, "click", function() 
            {
                location.replace("/map/"+getLabel());
            }
        );
    }
  });
}

try { if(GDownloadUrl)addLoadEvent(mapLoad); } catch(err) { /* Google Maps not loaded */ }


function emailer(){
$("emailer-trigger").onclick = Email;
}

addLoadEvent(emailer);

var state = 1;
function Email(){
    state = state * -1;
    if(state < 0)
        Effect.BlindDown('emailer', {duration:1});
    if(state > 0)
        Effect.BlindUp('emailer', {duration:1});
    
    validEmail = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/gi;
    document.getElementById("emailpage").onsubmit = function() {
        
        if(validEmail.test(this.toEmail.value)){
            sendit(Form.serialize(this));         
        } else {
            Effect.Shake('emailer');
            if(!validEmail.test(this.toEmail.value))
                $("to").style.display = "inline";
            
            if(!validEmail.test(this.fromEmail.value))
                $("from").style.display = "inline";
        }
    return false;
    }

    return false;
}

function sendit(datasubmit){
   new Ajax.Request("includes/emailpage.cfm",{
    parameters: datasubmit,
    onSuccess: formemailsuccess,
    method: 'post'
   });
  //  $javaConsole(to);
  //for(prop in this.Field){$javaConsole(prop + " " + this.Field[prop]);}
}

function formemailsuccess(e) {
    Element.update($("emailer"), "<p>Your email with this article has been sent, Thank you.</p>")
    setTimeout("Effect.BlindUp('emailer', {duration:0.3});", 5000);
}
    
window.onunload = function(){
    try
    {
        if(GUnload)GUnload();
    } catch(err) { /* Google Maps not loaded */ }
    map = false;
    destinationGuide = false;
    return false;
    }