window.onload = initForm;

function initForm()
{
  external_links();
  if(document.getElementById('homepage_flash')){
  	InsertFlashObject('homepage_flash', 'flash/home_banner.swf', 763, 279);
  }
  if(document.getElementById('contact_map_map')){
    init_map();
  }
  if(document.getElementById('product_id')){
    prodId = document.getElementById('product_id').value;
    updatePrice(prodId);
  }
  if(document.getElementById("filters_section")){
    if((document.getElementById("filters_section").value == 1) || (document.getElementById("filters_section").value == 2) || (document.getElementById("filters_section").value == 3)){
      getFilterOptions();
    }
  }
  if(document.getElementById("filters_section")){
    if(document.getElementById("filters_section").value == 9){
      getFilterOptions();
    }
  }
  if(document.getElementById('brand_dropdown')){
    document.getElementById('brand_dropdown').onchange = gotobrand;
  }
  load_color_box();
}

function load_color_box() {

  loadCBox = false;

  var anchors = document.getElementsByTagName("a");
  for(var i=0; i<anchors.length; i++) {
    var anchor = anchors[i];
    if(anchor.getAttribute("href") && anchor.getAttribute("rel") == "preview") {
      loadCBox = true;
    }
  }

  if(loadCBox == true) {
    $(document).ready(function(){
      $("a[rel='preview']").colorbox({
          width:"600",
          transition:"elastic",
          initialWidth:"100",
          initialHeight:"100"
          //onClosed:function(){ window.location.reload();}
      });
    });
  }
}

// Open links in new window
function external_links()
{
  if(!document.getElementsByTagName) {
    return;
  } else {
    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 switchGallery(gallery)
{
    if(isNaN(gallery)) {
        window.location = gallery + '-gallery.htm';
        return true;
    }
}
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;
}

function init_map(){
  var latlng = new google.maps.LatLng(51.384531,-2.360103);
  var myOptions = {
    zoom: 14,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("contact_map_map"), myOptions);

  var contentString = '<div>'+
      '<span class=\"b\">The Pole Company</span><br/>\r\n'+
      '1 Saracen Street,<br/>\r\n'+
      'Bath,<br/>\r\n'+
      'BA1 5BR<br/>\r\n'+
      'Phone 01225 469559<br/>\r\n'+
      '</div>';

  var infowindow = new google.maps.InfoWindow({
      content: contentString
  });

  var marker = new google.maps.Marker({
      position: latlng,
      map: map,
      title:"The Pole Company"
  });

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
  });
}

// Function to 'activate' images.
function imgOn(imgName) {
  switch(imgName)
  {
  case 'img01':
    document.getElementById('howtomeasure_step1').src='images/content/step1mo.jpg';
    document.getElementById('howtomeasure_diagram').src='images/content/measurestep1.jpg';
  break;
  case 'img02':
    document.getElementById('howtomeasure_step2').src='images/content/step2mo.jpg';
    document.getElementById('howtomeasure_diagram').src='images/content/measurestep2.jpg';
  break;
  case 'img03':
    document.getElementById('howtomeasure_step3').src='images/content/step3mo.jpg';
    document.getElementById('howtomeasure_diagram').src='images/content/measurestep3.jpg';
  break;
  case 'img04':
    document.getElementById('howtomeasure_step4').src='images/content/step4mo.jpg';
    document.getElementById('howtomeasure_diagram').src='images/content/measurestep4.jpg';
  break;
  default:
    document.getElementById('howtomeasure_diagram').src='images/content/measurestep1.jpg';
  }
}

// Function to 'deactivate' images.
function imgOff(imgName) {
  switch(imgName)
  {
  case 'img01':
    document.getElementById('howtomeasure_step1').src='images/content/step1.jpg';
    document.getElementById('howtomeasure_diagram').src='images/content/measurestep1.jpg';
  break;
  case 'img02':
    document.getElementById('howtomeasure_step2').src='images/content/step2.jpg';
    document.getElementById('howtomeasure_diagram').src='images/content/measurestep1.jpg';
  break;
  case 'img03':
    document.getElementById('howtomeasure_step3').src='images/content/step3.jpg';
    document.getElementById('howtomeasure_diagram').src='images/content/measurestep1.jpg';
  break;
  case 'img04':
    document.getElementById('howtomeasure_step4').src='images/content/step4.jpg';
    document.getElementById('howtomeasure_diagram').src='images/content/measurestep1.jpg';
  break;
  default:
    document.getElementById('howtomeasure_diagram').src='images/content/measurestep1.jpg';
  }
}

function gotobrand(){
  brandurl = document.getElementById('brand_dropdown').value;
  //alert(brandurl);
  if(brandurl.length > 1){
    window.location = brandurl;
  }
}

