var map;
var point;
var marker;
var html;

window.onload = initPage;

function initPage() {
  externalLinks();
  if(document.getElementById('flash_cta')){
  	InsertFlashObject('flash_cta', 'flash/flash_cta.swf', 199, 207);
  }
  if(document.getElementById('map')) {
    load();
  }
}

function image_popup(image) {

  var image = image;
  var newLink = image.replace("main", "large");

  window.open(newLink,"","width=1024,height=768,location,menubar");
  
}

function externalLinks() { 
  if (!document.getElementsByTagName) return; 
  var anchors = document.getElementsByTagName("a"); 
  for (var i=0; i<anchors.length; i++) { 
    var anchor = anchors[i];
    if(anchor.getAttribute("href")) {
      if (anchor.getAttribute("rel") == "external" || anchor.getAttribute("rel") == "external nofollow" ) {
        anchor.target = "_blank"; 
      } else if (anchor.getAttribute("rel") == "back") {
        anchor.onclick = backLink; 
      }
    }
  } 
}

function backLink() {
  history.back();
}

function load() {
  if (GBrowserIsCompatible()) {
    var html = document.getElementById('cont').innerHTML;
    html = html.replace('linkname','linkname2');
    html = html.replace('getDirToggle','getDirToggle2');  
    
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(51.44393729295845, -1.2784051895141601), 16);
    point = new GLatLng(51.44393729295845, -1.2784051895141601);
    marker = new GMarker(point);
    map.addOverlay(marker)
    map.openInfoWindowHtml(map.getCenter(), html);
    
    GEvent.addListener(marker, "click", function() {  
      map.openInfoWindowHtml(point, html);
    });
  }
}
function prepareForm(form, cid){
  if (GBrowserIsCompatible()) {
    gApplication.prepareForm(form, cid);
  }
}

function displayDir(){    
  if (GBrowserIsCompatible()) {  
    var toggle = document.getElementById("getDirToggle").style.display;
    if(toggle == 'none'){      
      document.getElementById("getDirToggle").style.display='block';
      document.getElementById("linkname").innerHTML = "Don't get directions";      
    }else{
        document.getElementById("getDirToggle").style.display='none';
        document.getElementById("linkname").innerHTML = "Get directions";
  }
    
    var html = document.getElementById('cont').innerHTML;
    html = html.replace('linkname','linkname2');
    html = html.replace('getDirToggle','getDirToggle2');      
    map.openInfoWindowHtml(point, html);
  }
}
function InsertFlashObject(objID, flashLocation, width, height) {
  var flashObject;
  flashObject = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='" + width + "' height='" + height + "'>";
  flashObject = flashObject + "<param name='wmode' value='transparent'>";
  flashObject = flashObject + "<param name='movie' value='" + flashLocation + "' />";
  flashObject = flashObject + "<param name='quality' value='high' />";
  flashObject = flashObject + "<embed src='" + flashLocation + "' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='" + width + "' height='" + height + "' wmode='transparent'";
  flashObject = flashObject + "></embed></object>";
  document.getElementById(objID).innerHTML = flashObject;
}
