jQuery.preloadImages = function() {
 var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
 for(var i = a.length -1; i > 0; i--) {
  jQuery("<img>").attr("src", a[i]);
 }
}//function

function getBaseUrl() {

 strBaseUrl = '';
 
 if (strBaseUrl = $('base').attr('href')) {

  if (strBaseUrl.indexOf('http://') !== -1) {
   strBaseUrl = strBaseUrl.replace('http://', '');
   strBaseUrl = strBaseUrl.substring(strBaseUrl.indexOf('/'));
  }//if

 }//if

 return strBaseUrl;

}//function

function doRequiredLabels() {

 //Remove all existing markers
	$('span.required').remove();

 $('div.required label').each( function() {
 	if (! $(this).hasClass('inline')) {
   $(this).prepend('<span class="required">* </span>');
 	}//if

 });

}//function

function createMarker(point, number) {
 var marker = new GMarker(point);
 GEvent.addListener(marker, "click", function() {
  marker.openInfoWindowHtml(number);
 });
 return marker;
}//function

function doGoogleMap(strMapId, strLatLong, strInfo) {
 //usePointFromPostcode(strMapId, strPostcode, strInfo, placeMarkerAtPoint);
 var intPosition = strLatLong.indexOf(',');
 var strLat = strLatLong.substring(0, intPosition);
 var strLong = strLatLong.substring(intPosition+1);
 var point = new GLatLng(strLat,strLong);
 placeMarkerAtPoint(strMapId, point, strInfo); 
}//function

function initGoogleMap() {

 var WINDOW_HTML = '<div style="width: 210px; padding-left: 10px; color: #000000;"><font face="arial" size="2px"><p><strong>Address:</strong> Fast React Systems Limited, Evolution House,<br />Stephensons Way, The Wyvern, Derby. DE21 6LY. United Kingdom<br /><strong>Telephone:</strong> +44 (0)1332 668942<br /><strong>Fax:</strong> +44 (0)1332 669908<br /><strong>Email:</strong> <a href=\"mailto:info@fastreact.com\" target=\"_blank\">info@fastreact.com</a></p></font></div>';

 if (self.GBrowserIsCompatible) {
  var map = new GMap2(document.getElementById("googleMap"));
  map.removeMapType(G_HYBRID_MAP);
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  map.setMapType(G_NORMAL_MAP);
  map.enableScrollWheelZoom();

  map.setCenter(new GLatLng(52.915419,-1.429699), 13);
  var marker = new GMarker(new GLatLng(52.915419,-1.429699));

  GEvent.addListener(marker, "click", function() {
   marker.openInfoWindowHtml(WINDOW_HTML);
  });
  marker.openInfoWindowHtml(WINDOW_HTML);
  map.addOverlay(marker);

 }//if
}//function

$(document).ready(function() {
	
 var baseURL = getBaseUrl();
 
 doRequiredLabels();
 
 //Fix nav for IE7
 if ($('#nav').length == 1) {
  $('#nav > li').hover( function() {
   $(this).addClass('hover');
  },function() {
   $(this).removeClass('hover');
  });
 }//if
 
 //Quotes filter
 if ($('#formCustomerQuotes').length > 0) {
  $('#formCustomerQuotes select').change( function() {
   if ($(this).val() != 0) {
    $(this).parents('form').submit();
   }//if
  });
 }//if

 if ($('div#googleMap').length == 1) {
  initGoogleMap();
 }//if
 
 //Home page carousel
 if ($('#carousel').length > 0) {
  var carousel = $("#carousel").featureCarousel({
   trackerIndividual: false
  ,trackerSummation: false
  });
 }//if
 
 //Distributor map
 if ($('#businessTypeOverview').length > 0) {
  
  //Rollovers
  $('#businessType li').hover( function() {
   strClass = $(this).attr('class');
   $('#businessTypeInfo div.vision').hide();
   $('#businessTypeInfo div.evolve').hide();
   $('#businessTypeInfo div.align').hide();
   $('#businessTypeOverview > div').hide();
   $('#businessTypeOverview div.' + strClass).show();
   }, function() {});

  //Clicks
  $('#businessType li').click( function() {
   strClass = $(this).attr('class');
   $('#businessTypeOverview > div').hide();
   $('#businessTypeInfo div.vision').hide();
   $('#businessTypeInfo div.evolve').hide();
   $('#businessTypeInfo div.align').hide();
   $('#businessTypeInfo div.' + strClass).show();
   $('#businessTypeInfo div.' + strClass + ' div.info').show();
   
   //Fix height of businessTypeOverview div's
   var intHeight = 0;

   $('#businessTypeInfo div.' + strClass + ' div.info > div').each( function() {
    if ($(this).height() > intHeight) {
     intHeight = $(this).height();
    }//if
   });

   $('#businessTypeInfo div.' + strClass + ' div.info > div').each( function() {
    $(this).height(intHeight + 'px');
   });
      
  });
  
  //Tabs
  $('#businessTypeInfo ul.tabs li').click( function() {
   strClass = $(this).attr('class').match(/one|two|three/, '');
   $(this).parents('div.info').find('div.one').hide();
   $(this).parents('div.info').find('div.two').hide();
   $(this).parents('div.info').find('div.three').hide();
   $(this).parents('div.info').find('div.' + strClass).show();
   $(this).siblings().removeClass('selected');
   $(this).addClass('selected');
  });
    
 }//if
 
 if ($('ul#slideCustomersVision').length > 0) {
  $('ul#slideCustomersVision').bxSlider({
   auto: true
   ,autoControls: false
   ,controls: false
   ,displaySlideQty: 4
   ,moveSlideQty: 4
   ,pause: 6000
   ,autoHover: true
  });
 }//if

 if ($('ul#slideCustomersEvolve').length > 0) {
  $('ul#slideCustomersEvolve').bxSlider({
   auto: true
   ,autoControls: false
   ,controls: false
   ,displaySlideQty: 4
   ,moveSlideQty: 4
   ,pause: 6000
   ,autoHover: true
  });
 }//if

 if ($('ul#slideCustomersAlign').length > 0) {
  $('ul#slideCustomersAlign').bxSlider({
   auto: true
   ,autoControls: false
   ,controls: false
   ,displaySlideQty: 4
   ,moveSlideQty: 4
   ,pause: 6000
   ,autoHover: true
  });
 }//if

 if ($('ul#slideQuotesVision').length > 0) {
  $('ul#slideQuotesVision').bxSlider({
   auto: true
   ,autoControls: false
   ,controls: false
   ,displaySlideQty: 1
   ,moveSlideQty: 1
   ,pause: 5000
   ,autoHover: true
  });
 }//if

 if ($('ul#slideQuotesEvolve').length > 0) {
  $('ul#slideQuotesEvolve').bxSlider({
   auto: true
   ,autoControls: false
   ,controls: false
   ,displaySlideQty: 1
   ,moveSlideQty: 1
   ,pause: 5000
   ,autoHover: true
  });
 }//if

 if ($('ul#slideQuotesAlign').length > 0) {
  $('ul#slideQuotesAlign').bxSlider({
   auto: true
   ,autoControls: false
   ,controls: false
   ,displaySlideQty: 1
   ,moveSlideQty: 1
   ,pause: 5000
   ,autoHover: true
  });
 }//if

});

//Do gallery images
$(window).load( function() {

 var intTotalOffices = $('div#content div.office').length;

 if (intTotalOffices > 0) {

  var intOfficeItemsPerRow = 4;
  var intHeight = 0;
  var intCounter = 1;

  $('div.office').each( function() {

   if ($(this).height() > intHeight) {
    intHeight = $(this).height();
   }//if

   if (intCounter % intOfficeItemsPerRow == 0) {

    $(this).height(intHeight + 'px');

    for (i = 0; i < (intOfficeItemsPerRow - 1); i++) {
     $(this).prevAll().eq(i).height(intHeight + 'px');
    }//for

    intHeight = 0;

   }//if

   intCounter++;

  });

  //Pick up any strays at the end
  var intLeftOvers = intTotalOffices % intOfficeItemsPerRow; 
  if (intLeftOvers > 0) {

   intHeight = 0;

   for (i = (intTotalOffices - 1); i > (intTotalOffices - intLeftOvers - 1); i--) {
    if ($('div.office').eq(i).height() > intHeight) {
     intHeight = $('div.office').eq(i).height();
    }//if
   }//for

   for (i = (intTotalOffices - 1); i > (intTotalOffices - intLeftOvers - 1); i--) {
    $('div.office').eq(i).height(intHeight + 'px');    
   }//for   

  }//if
  
  //Do clicks on office names
  $('div.office h3 a').click( function() {
   doGoogleMap('googleMap', $(this).parents('div.office').find('input').val(), '<strong>' + $(this).text() + '</strong><br />' + $(this).parents('div.office').find('address').html());
  });

 }//if
 
 //Fix height of businessTypeOverview div's
 var intHeight = 0;

 $('div#businessTypeOverview > div').each( function() {
  if ($(this).height() > intHeight) {
   intHeight = $(this).height();
  }//if
 });

 $('div#businessTypeOverview > div').each( function() {
  $(this).height(intHeight + 'px');
 });

 //Fix height of quoteRight div's
 var intHeight = 0;

 $('div.quote div.quoteRight').each( function() {
  if ($(this).height() > intHeight) {
   intHeight = $(this).height();
  }//if
 });

 $('div.quote div.quoteRight').each( function() {
  $(this).height(intHeight + 'px');
 }); 
 
 //Fix height of quotes div's
 var intHeight = 0;

 $('div#panel div.quote').each( function() {
  if ($(this).height() > intHeight) {
   intHeight = $(this).height();
  }//if
 });

 $('div#panel div.quote').each( function() {
  $(this).height(intHeight + 'px');
 });

 var intHeight = 0;

 $('div#contentContainer div.quote').each( function() {
  if ($(this).height() > intHeight) {
   intHeight = $(this).height();
  }//if
 });

 $('div#contentContainer div.quote').each( function() {
  $(this).height(intHeight + 'px');
 }); 
 
});

