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() {

 var WINDOW_HTML = '<div style="width: 210px; padding-left: 10px; color: #000000;"><br /><font face="arial" size="2px"><strong>Pro-Shift</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.531652, -1.380308), 13);
  var marker = new GMarker(new GLatLng(52.531652, -1.380308));

  GEvent.addListener(marker, "click", function() {
   marker.openInfoWindowHtml(WINDOW_HTML);
  });
  marker.openInfoWindowHtml(WINDOW_HTML);
  map.addOverlay(marker);

 }//if
}//function

$(function() {
 imgLoadingImage = document.createElement("img");
 $(imgLoadingImage).attr("src", "images/loader.gif");
 $(imgLoadingImage).attr("alt", "Loading...");
 $(imgLoadingImage).attr("width", "18");
 $(imgLoadingImage).attr("height", "18");
 $(imgLoadingImage).attr("id", "loadingimage");
 $("body").append(imgLoadingImage);
 hideLoadingImage();
});

function loadImage (url) {
 showOverlayDiv();
 showLoadingImage();

 imgPreload = new Image();
 $(imgPreload).load(function () {
  arrPageSize = getPageSize();
  arrPageScroll = getPageScroll();

  imageContainer = document.createElement("div");
  $(imageContainer).attr("id", "largeimagecontainer");

  imgImage = document.createElement("img");
  $(imgImage).attr("src", url);
  $(imgImage).attr("alt", "");
  $(imgImage).attr("width", imgImage.width);
  $(imgImage).attr("height", imgImage.height);
  $(imageContainer).append(imgImage);

  imgImageButton = document.createElement("img");
  $(imgImageButton).attr("src", "images/close.png");
  $(imgImageButton).attr("alt", "Close");
  $(imgImageButton).attr("id", "closeButton");
  $(imgImageButton).attr("width", "30");
  $(imgImageButton).attr("height", "30");
  $(imgImageButton).click(hideImage); 
  $(imageContainer).append(imgImageButton);
  
  $("#largeimagecontainer").hide();
  $("body").append(imageContainer);

  $("#largeimagecontainer").css("position", "absolute");
  $("#largeimagecontainer").css("z-index", "110");

  if ($.browser.msie) {
   if ((arrPageSize[2] < arrPageSize[0])) {
    imagecontainertop = (((arrPageSize[3] - imgImage.height - 22) / 2) + 9);
   } else {
    imagecontainertop = (((arrPageSize[3] - imgImage.height - 22) / 2));
   }//if

   if ((arrPageSize[3] < arrPageSize[1])) {
    imagecontainerleft = (((arrPageSize[2] - imgImage.width - 22) / 2) + 9);
   } else {
    imagecontainerleft = (((arrPageSize[2] - imgImage.width - 22) / 2));
   }//if
  } else {
   imagecontainertop = (((arrPageSize[3] - imgImage.height - 22) / 2) - 9);
   imagecontainerleft = (((arrPageSize[2] - imgImage.width - 22) / 2) - 9);
  }//if

  if (imagecontainertop < 0) { imagecontainertop = 0; }//if
  if (imagecontainerleft < 0) { imagecontainerleft = 0; }//if

  $("#largeimagecontainer").css("top", arrPageScroll[1] + imagecontainertop + "px");
  $("#largeimagecontainer").css("left", arrPageScroll[0] + imagecontainerleft + "px");

  $("#largeimagecontainer").show();
  hideLoadingImage();

 });

 imgPreload.src = url;
}//function

function hideImage () {
 $("div#largeimagecontainer").remove();
 hideOverlayDiv();
}//function

function showLoadingImage () {
 arrPageSize = getPageSize();
 arrPageScroll = getPageScroll();

 $("#loadingimage").css("position", "absolute");
 $("#loadingimage").css("z-index", "109");
 $("#loadingimage").css("top", (arrPageScroll[1] + ((arrPageSize[3] - 16) / 2)) + "px");
 $("#loadingimage").css("left", (arrPageScroll[0] + ((arrPageSize[2] - 16) / 2)) + "px");
 $("#loadingimage").show();
}//function

function hideLoadingImage () {
 $("#loadingimage").hide();
}//function

function showOverlayDiv () {
 arrPageSize = getPageSize();

 overlayDiv = document.createElement("div");
 $(overlayDiv).attr("id", "overlay");
 $(overlayDiv).css("height", arrPageSize[1] + "px");
 $(overlayDiv).css("position", "absolute");
 $(overlayDiv).css("top", "0");
 $(overlayDiv).css("left", "0");
 $(overlayDiv).css("width", "100%");
 $(overlayDiv).css("z-index", "108");

 $("body").append(overlayDiv);
 $("select").hide();
}//function

function hideOverlayDiv () {
 $("div#overlay").remove();
 $("select").show();
}//function

// Core code from - quirksmode.org
function getPageScroll(){
 var yScroll;

 if (self.pageYOffset) {
  yScroll = self.pageYOffset;
 } else if (document.documentElement && document.documentElement.scrollTop) {
  yScroll = document.documentElement.scrollTop;
 } else if (document.body) {
  yScroll = document.body.scrollTop;
 }//if

 arrayPageScroll = new Array('', yScroll)
 return arrayPageScroll;
}

// Core code from - quirksmode.org
// Edit for Firefox by pHaez
function getPageSize(){
 var xScroll, yScroll;

 if (window.innerHeight && window.scrollMaxY) {
  xScroll = document.body.scrollWidth;
  yScroll = window.innerHeight + window.scrollMaxY;
 } else if (document.body.scrollHeight > document.body.offsetHeight) {
  xScroll = document.body.scrollWidth;
  yScroll = document.body.scrollHeight;
 } else {
  xScroll = document.body.offsetWidth;
  yScroll = document.body.offsetHeight;
 }//if

 var windowWidth, windowHeight;
 if (self.innerHeight) {
  windowWidth = self.innerWidth;
  windowHeight = self.innerHeight;
 } else if (document.documentElement && document.documentElement.clientHeight) {
  windowWidth = document.documentElement.clientWidth;
  windowHeight = document.documentElement.clientHeight;
 } else if (document.body) {
  windowWidth = document.body.clientWidth;
  windowHeight = document.body.clientHeight;
 }//if

 if(yScroll < windowHeight){
  pageHeight = windowHeight;
 } else {
  pageHeight = yScroll;
 }//if

 if(xScroll < windowWidth){
  pageWidth = windowWidth;
 } else {
  pageWidth = xScroll;
 }//if

 arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
 return arrayPageSize;
}

$(document).ready(function() {
	
 var baseURL = getBaseUrl();

 //Fade stuff on home page
 if ($('#banner img').length > 1) {
  $('#banner').innerfade({
    speed: 1000
   ,timeout: 4500
   ,type: 'sequence'
   ,containerheight: $('#banner').height() + 'px'
  });
 }//if 

 //Fade testimonials on home page
 if ($('#testimonialsInner .testimonial').length > 1) {
  $('#testimonialsInner').innerfade({
    speed: 1000
   ,timeout: 7500
   ,type: 'sequence'
   ,containerheight: $('#testimonials').height() + 'px'
  });
 }//if 
 
 doRequiredLabels();
 
 //Do gallery images
 $(window).load( function() {

  var intTotalGalleryItems = $('div#content div.gallery').length;

  if (intTotalGalleryItems > 0) {

   $('div.gallery:not(".video") a').click( function() {
    loadImage($(this).attr('href'));
    return false;
   });

   var intGalleryItemsPerRow = 4;
   var intHeight = 0;
   var intCounter = 1;

   $('div.gallery').each( function() {

    if ($(this).height() > intHeight) {
     intHeight = $(this).height();
    }//if

    if (intCounter % intGalleryItemsPerRow == 0) {

     $(this).height(intHeight + 'px');

     for (i = 0; i < (intGalleryItemsPerRow - 1); i++) {
      $(this).prevAll().eq(i).height(intHeight + 'px');
     }//for

     intHeight = 0;

    }//if

    intCounter++;

   });

   //Pick up any strays at the end
   var intLeftOvers = intTotalGalleryItems % intGalleryItemsPerRow; 
   if (intLeftOvers > 0) {

    intHeight = 0;

    for (i = (intTotalGalleryItems - 1); i > (intTotalGalleryItems - intLeftOvers - 1); i--) {
     if ($('div.gallery').eq(i).height() > intHeight) {
      intHeight = $('div.gallery').eq(i).height();
     }//if
    }//for

    for (i = (intTotalGalleryItems - 1); i > (intTotalGalleryItems - intLeftOvers - 1); i--) {
     $('div.gallery').eq(i).height(intHeight + 'px');    
    }//for   

   }//if

  }//if

  //Do partner images
  /*
  var intTotalPartnerItems = $('div#content div.partner').length;

  if (intTotalPartnerItems > 0) {

   var intPartnerItemsPerRow = 2;
   var intHeight = 0;
   var intCounter = 1;

   $('div.partner').each( function() {

    if ($(this).height() > intHeight) {
     intHeight = $(this).height();
    }//if

    if (intCounter % intPartnerItemsPerRow == 0) {

     $(this).height(intHeight + 'px');

     for (i = 0; i < (intPartnerItemsPerRow - 1); i++) {
      $(this).prevAll().eq(i).height(intHeight + 'px');
     }//for

     intHeight = 0;

    }//if

    intCounter++;

   });

   //Pick up any strays at the end
   var intLeftOvers = intTotalPartnerItems % intPartnerItemsPerRow; 
   if (intLeftOvers > 0) {

    intHeight = 0;

    for (i = (intTotalPartnerItems - 1); i > (intTotalPartnerItems - intLeftOvers - 1); i--) {
     if ($('div.partner').eq(i).height() > intHeight) {
      intHeight = $('div.partner').eq(i).height();
     }//if
    }//for

    for (i = (intTotalPartnerItems - 1); i > (intTotalPartnerItems - intLeftOvers - 1); i--) {
     $('div.partner').eq(i).height(intHeight + 'px');    
    }//for   

   }//if

  }//if 
  */
  
 });
 
 if ($('div.answer').length > 0) {
  $('div.answer').hide();
  
  $('div.faq h4').toggle( function() {
   $(this).parents('div.faq').find('div.answer').show(200);
   $(this).addClass('selected');
  }, function() {
   $(this).parents('div.faq').find('div.answer').hide(200);
   $(this).removeClass('selected');
  });

 }//if
 
 if ($('div#googlemap').length == 1) {
  doGoogleMap();
 }//if
 
 if ($('div.newsCarousel').length > 0) {
  $('div.newsCarousel').lofJSidernews( {interval: 8000
                                       ,direction:'opacity'
                                       ,duration:1000
                                       ,wapperSelector:'.newsCarouselContent'
                                       ,mainItemSelector:'.newsCarouselContentItem'
                                       ,navInnerSelector:'ul'
                                       ,navSelector:'li'
                                       ,navigatorHeight:29
                                       ,navigatorWidth:270
                                       ,navItemsSelector:'.newsCarouselNav ul li'
                                       ,navOuterSelector:'.newsCarouselNav'
                                       ,easing:'easeInOutSine'} );
 }//if
 
});
