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

window.onload = initPage;

function initPage() {

  var iterator = 0;
  
  if(document.getElementById('diploma')) {
    document.getElementById('diploma').onmouseover = mouseover_border;
    document.getElementById('diploma').onmouseout = mouseout_border;
    document.getElementById('diploma').onclick = click_border;
  }
  if(document.getElementById('cad')) {
    document.getElementById('cad').onmouseover = mouseover_border;
    document.getElementById('cad').onmouseout = mouseout_border;
    document.getElementById('cad').onclick = click_border;
  }
  if(document.getElementById('cpd')) {
    document.getElementById('cpd').onmouseover = mouseover_border;
    document.getElementById('cpd').onmouseout = mouseout_border;
    document.getElementById('cpd').onclick = click_border;
  }
  if(document.getElementById('quickcontact')) {
    document.getElementById('q_contact_name').onfocus = clearfield;
    document.getElementById('q_contact_email').onfocus = clearfield;
    document.getElementById('q_contact_number').onfocus = clearfield;
    document.getElementById('q_contact_name').onblur = resetfield;
    document.getElementById('q_contact_email').onblur = resetfield;
    document.getElementById('q_contact_number').onblur = resetfield;
  }
    
  document.getElementById('header_right').onclick = viewHome;
  externalLinks();
  if(document.getElementById('mapcontainer') || document.getElementById('mapcontainerbristol')) {
    load();
  }
  
  // Loop through divs/uls
  loop_divs();  
}

function viewHome() {
  window.location = 'index.php?view=home';
  return true;
}

function loop_divs() {

  if(!document.getElementsByTagName) {
    return;
  } else {
    var anchors = document.getElementsByTagName('a');
    for(var i=0; i<anchors.length; i++) {
      var anchor = anchors[i];
      if(anchor.id.substring(0, 2) == 'ul') {
        anchor.onclick = toggle_div;
      }
    }    
  }

}

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") && anchor.getAttribute("rel") == "external") 
      anchor.target = "_blank"; 
  } 
}

function mouseover_border() {
  if(this.id != 'diploma') {
    document.getElementById('diploma_main').className = "home";
    document.getElementById('diploma_head').className = "home_head";
    document.getElementById('diploma_base').className = "home_base";
  }
  document.getElementById(this.id + '_main').className = "home_over";
  document.getElementById(this.id + '_head').className = "home_head_over";
  document.getElementById(this.id + '_base').className = "home_base_over";
}

function mouseout_border() {
  document.getElementById(this.id + '_main').className = "home";
  document.getElementById(this.id + '_head').className = "home_head";
  document.getElementById(this.id + '_base').className = "home_base";
}

function click_border() {
  if(this.id == 'diploma') {
    window.location = 'index.php?view=diplomacourse';
    return true;
  }
  if(this.id == 'cad') {
    window.location = 'index.php?view=cadcourses';
    return true;
  }
  if(this.id == 'cpd') {
    window.location = 'index.php?view=cpdcourse';
    return true;
  }
}

function clearfield() {
  if(this.id == 'q_contact_name') {
    if(this.value == 'Name') {
      this.value = '';
    }
  }
  if(this.id == 'q_contact_email') {
    if(this.value == 'E-mail') {
      this.value = '';
    }
  }
  if(this.id == 'q_contact_number') {
    if(this.value == 'Contact Number') {
      this.value = '';
    }
  }
}

function resetfield() {
  if(this.id == 'q_contact_name') {
    if(this.value == '') {
      this.value = 'Name';
    }
  }
  if(this.id == 'q_contact_email') {
    if(this.value == '') {
      this.value = 'E-mail';
    }
  }
  if(this.id == 'q_contact_number') {
    if(this.value == '') {
      this.value = 'Contact Number';
    }
  }
}

// Toggle div element
function toggle_div() {
  
  var div = this.id;
  div = div.replace('ul', '');
  element = document.getElementById('ul' + div + 'hide');
  
  if(element.style.display == 'none') {
    element.style.display = 'block';
  } else {
    element.style.display = 'none';
  }
  
}

function load() {
  if (GBrowserIsCompatible()) {
    var html = document.getElementById('shop').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());
    if(document.getElementById('mapcontainerbristol')) {
      map.setCenter(new GLatLng(51.44697, -2.62144), 14);
      point = new GLatLng(51.44697, -2.62144);
    } else if (document.getElementById('mapcontainer')) {
      map.setCenter(new GLatLng(51.49757, -0.11918), 14);
      point = new GLatLng(51.49757, -0.11918);
    }
    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('shop').innerHTML;
    html = html.replace('linkname','linkname2');
    html = html.replace('getDirToggle','getDirToggle2');      
    map.openInfoWindowHtml(point, html);
  }
}