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 doTabs() {
 
 $('#panel ul.tabs li').each( function() {

  if ($(this).hasClass('selected')) {
   
   strId = $(this).attr('id');

   //Show relevant tab content
   $('#panel div.tab').each( function() {
    if ($(this).hasClass(strId)) {
     $(this).show();
    } else {
     $(this).hide();
    }//if
   });
   
  }//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="font-family: sans-serif; size: 1em; width: 210px; line-height: 1.6em; color: #000000;"><img align="right" width="50" height="50" style="margin: 0 5px 0 5px" src="images/ph_map_logo.gif" border="0" /></a><span style="font-weight: bold; font-size: 1.2em">Pepperhed Creative</span><br />St. Michael\'s Studios<br />Queen Street<br />Derby&nbsp;&nbsp;DE1 3SU</div>';

 if (self.GBrowserIsCompatible) {
  var map = new GMap2(document.getElementById("contactMap"));
  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.925808,-1.47813), 13);
  var marker = new GMarker(new GLatLng(52.925808,-1.47813));

  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();
	doTabs();

 //Do tab stuff in panel
 $('#panel ul.tabs li').click( function() {
  $(this).siblings().removeClass('selected');
  $(this).addClass('selected');
  doTabs();
  return false;
 });
 	
 //Fade banner images
 if ($('#bannerContainer img').length > 1) {
  $('#bannerContainer').innerfade({
   speed: 1000,
   timeout: 6000,
   type: 'sequence',
   containerheight: '443px'
  });
 }//if
 
 if ($('#contactMap').length == 1) {
  doGoogleMap();
 }//if
 
 //Home page columns
 if ($('div.col3').length > 0) {
  intHeight = 0;
  $('div.col3').each( function() {
   if ($(this).height() > intHeight) {
    intHeight = $(this).height();
   }//if
  });
  $('div.col3').each( function() {
   $(this).height(intHeight);
  });
 }//if
 
 //Contact form
 if ($('#formContact').length > 0) {
  $('#formContact').submit( function() {
   $('#button-Submit').hide();
   $('#imgContactWaiting').show();
   $.post(getBaseUrl() + 'ajax/enquiry.php', $('#formContact').serialize(), function(data) {
    
    if (data.success) {
     $('#formContact input[type=text]').each( function() {
      $(this).val('');
     });
     $('#formContact textarea').each( function() {
      $(this).val('');
     });
     $('#button-Submit').hide();
     $('#enquiryFeedback').show();
     $('#formContact').append(data.response);
    } else {
     alert(data.response);
     $('#button-Submit').show();
    }//if
    
    $('#imgContactWaiting').hide();
   });
   return false;   
  });
 }//if
 
 //Newsletter form
 if ($('#formNewsletter').length > 0) {
  $('#formNewsletter').submit( function() {
   $('#buttonNewsletter').hide();
   $('#imgNewsletterWaiting').show();
   $.post(getBaseUrl() + 'ajax/newsletter.php', $('#formNewsletter').serialize(), function(data) {
    
    if (data.success) {
     $('#formNewsletter input[type=text]').each( function() {
      $(this).val('');
     });
     $('#buttonNewsletter').hide();
     $('#newsletterFeedback').show();
     $('#formNewsletter').append(data.response);
    } else {
     alert(data.response);
     $('#buttonNewsletter').show();
    }//if
    
    $('#imgNewsletterWaiting').hide();
   });
   return false;   
  });
 }//if
 
});
