jQuery(document).ready(function($){
// EVERY PAGE : main menu...
  //centre the main menu horizontally within the page...
  var sf = $('ul.sf-menu'), mn = $('#navigation'), sfdiff = Math.floor((mn.width() - sf.width())/2);
  mn.css({paddingLeft:sfdiff});
  //apply superfish & sub-menu dropdowns...
  sf.supersubs({
            minWidth:    12,   // minimum width of sub-menus in em units
            maxWidth:    27,   // maximum width of sub-menus in em units
            extraWidth:  1     // extra width can ensure lines don't sometimes turn over
                               // due to slight rounding differences and font-family
        }).superfish({dropShadows:false, autoArrows:false, speed:'fast'}).find('ul').bgIframe({opacity:true});
//CONTACT BOX BUTTON...
	$('.contactBoxButton').hover( function(){ this.src = this.src.replace(/4\.gif$/, '5.gif'); }
	                         		, function(){ this.src = this.src.replace(/5\.gif$/, '4.gif'); } );
//SERVICES JUMP
	$('#servicesOptions').change(function(){
			var v = $(this).val();
			if(v){ window.location.href = 'index.php?id=' + v; }
			return false;
		});
//CAROUSEL...
  var carouselCt = 4;
  $('#carousel_pics')
      .find('>ul>li>a').bind('click', function(e){
      	  $('#carousel_text>div').html($('.carouselText', this).html()).show();
      	  return false;
      	})
        .hover( function(){ $('>img', this).css({opacity:0.4}).siblings('.carouselHover').css({display:'block'}); }
              , function(){ $('>img', this).css({opacity:1}).siblings('.carouselHover').css({display:'none'}); }
              )
        .end()
      //intialise the carousel...
      .carousel({ dispItems:carouselCt
                , btnsPosition: 'outside'
                , nextBtn:'<div id="carousel_next"></div>'
                , prevBtn:'<div id="carousel_prev"></div>'
                });
  $('#carousel_shield').fadeOut(1000);
  $('#carousel_text>div').bind('click', function(){ $(this).hide(); });
//CONTACT FORM...
  $('#contactSubmit').each(function(){ this.disabled = false; });
  //Submit button handler...
  $('#contactForm').ajaxForm({
      //pre-submission validation for required fields...
      beforeSubmit:function(data, fm, opt){
          var valid = true;
          //for each customer-accessible field, if the class indicates that it's required then check that it has a value...
          fm.find(':input[name^=cust_]', this).each(function(){
              var el = $(this);
              if(el.hasClass('reqd_field')){ //IS required...
                if(!el.val()){ //does NOT have a value...
                  el.addClass('reqd_field_empty'); //give it a class to hilite the field...
                  if(valid){ //if this is the first error...
                    el.get(0).focus(); //focus the offending field
                  }
                  valid = false;
                }else{ //HAS a value, so clear any previous 'missing' class...
                  el.removeClass('reqd_field_empty');
                }
              }
            });
          if(valid){ //all required fields entered :clear any previous error and disable Submit...
            $('#contactError').empty().hide();
            $('#contactSubmit').get(0).disabled = true;
          }else{ //missing 1 or more required fields : show an error...
            $('#contactError').html('One, or more, required fields are missing...').show();
          }
          return valid;
        }
      //on-success handler...
    , success:function(resp){
        if(resp){ //problem! show returned error and re-enable Submit...
          $('#contactError').html(resp).show();
          $('#contactSubmit').get(0).disabled = false;
        }else{ //OK! clear any previous error, hide Submit, show success message...
          $('#contactError').empty().hide();
          $('#contactSubmit').hide();
          $('#contactSuccess').show();
        }
      }
    });
//SITE MAP...
	var bottomOfContent = $('#content').height() + $('#content').offset()['top'];
	$('#content.sitemap .colspan_3 .column').each(function(){
		  var col = $(this)
		    , ncol = $(this).next('.column')
		  	, ul = ncol.length ? $(this).children('ul') : []
		  	, ulen = !!ul.length
		    ;
		  if(ulen){
				ul.each(function(i){
						if(($(this).height() + $(this).offset()['top']) > bottomOfContent){
							ul.slice(i).appendTo(ncol);
							return false;
						}
						return true;
					});
			}
			return ulen;
		});
	$('#content.sitemap ul.rootLevel').removeClass('rootLevel');
});

