window.onload = initPage;

function initPage() {
  externalLinks();
  if(document.getElementById('toggle_btn')) {
    document.getElementById('toggle_btn').onclick = toggle;
  }
}
     
function toggle() {
  if(document.getElementById('product_options').style.display == 'block') {
    document.getElementById('product_options').style.display = 'none';
    document.getElementById('product_header_left').className= 'container_wide_header_product_left_closed';      
  } else {
    document.getElementById('product_options').style.display = 'block';
    document.getElementById('product_header_left').className='container_wide_header_product_left';      
  }
}

// make global variables.
var map;
var point;
var marker;
var html;

function mapload() {
  if(GBrowserIsCompatible()) {
    // get the HTML which we need to use to create the new info box
    var html = document.getElementById('shop').innerHTML;
    // replace the linkname tag for a different one, so our toggle function doesnt get confused.
    html = html.replace('linkname','linkname2');
    // replace the getDirToggle tag for a different one, so our toggle function doesnt get confused.
    html = html.replace('getDirToggle','getDirToggle2');  
    
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(51.45819524344993, -0.9435474872589111), 15);
    point = new GLatLng(51.45819524344993, -0.9435474872589111);
    marker = new GMarker(point);
    map.addOverlay(marker)
    // open the info window with the modified HTML. no duplicate ID's for us!
    map.openInfoWindowHtml(map.getCenter(), html);
    
    // add an onclict event, too!
    GEvent.addListener(marker, "click", function() {  
      // open the info window with the modified HTML. no duplicate IDs for us!
      map.openInfoWindowHtml(point, html);
    });
  }
}

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";
    }
    
    // get the HTML which we need to use to create the new info box
    var html = document.getElementById('shop').innerHTML;
    // replace the linkname tag for a different one, so our toggle function doesnt get confused.
    html = html.replace('linkname','linkname2');
    // replace the getDirToggle tag for a different one, so our toggle function doesnt get confused.
    html = html.replace('getDirToggle','getDirToggle2');      
    // open the info window with the modified HTML. no duplicate ID's for us!
    map.openInfoWindowHtml(point, html);
  }
}

function display_overlay(url) {
  var objLink = document.createElement('a');
  objLink.setAttribute('href',url);
  objLink.setAttribute('rel','overlay');
  jQuery.prototype.start(objLink);
}

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

function getClient(clientID) {
  var htmlObj = document.getElementById('client_select');
  var title = new Array();
  var value = new Array();
  xmlhttp.open("GET", 'logicajax/clients.php?client=' + clientID);
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      htmlObj.innerHTML = xmlhttp.responseText;
    }
  }  
  xmlhttp.send(null);  
}

function InsertFlashObject(objID, flashLocation, width, height, flashString) {
  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' />";
  if(typeof(flashString) == "undefined") {
    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'";
  } else {
    flashObject = flashObject + "<param name='FlashVars' value='" + flashString + "'";
    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 + "FlashVars='" + flashString + "'";
  }
  flashObject = flashObject + "></embed></object>";
  if(document.getElementById(objID)) {
    document.getElementById(objID).innerHTML = flashObject;
  }
}