$(document).ready(function() {

	/* doesn't work reliably with IE8 */
/* 	var ua = $.browser;
	alert(ua.version);
	if (ua.msie && ua.version.slice(0,1) == "8") {
		$('#outerwrap').removeClass('v7').addClass('v8');
		alert('IE8');
	}
 */
	// requires jquery.pngFix.js
	if (jQuery.isFunction(jQuery.fn.pngFix)) {
		$(document).pngFix();
	}

	if (jQuery.isFunction(jQuery.fn.jCarouselLite)) {
		$("#thumbs").jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev"
		});
	}

	$('#thumbs a').handleRemoteMouseovers();

	$('#nav ul li:first-child, .services #mainwrap ul.service-list li:first-child').addClass('first-child');
	$('#home #nav ul li::nth-child(2)').addClass('second-child');

	$('.services #mainwrap ul.service-list li').not(':last-child').append('<span class="colons">::</span>');
	$('#services-listing li').not(':last-child').append('<span class="colons">: :</span>');

	$('dl.address dd').not('dl.address dd:last-child').addClass('not-last-child').append('<span class="colons">::</span>');
	$('dl.address li').not(':last-child, .postal-code, .tel, .region-postal-code').append('<span class="comma">,</span>');
	$('#footer dl.address dt').append('<span class="colon">:</span>');

	// centre footer contents
	var footer_width				= $('#footer').computeTotalWidth();
	var footer_contents_width	= $('dl.address dt, dl.address dd').computeTotalWidth();
	//alert(width);
	var margin_left	= Math.floor((footer_width - footer_contents_width) / 2);
	//alert(margin_left);
	$('#footer dl').css('margin-left', margin_left + 'px');

	$('#main h1').prepend('<span class="swoosh"></span>');

	$('#contact #mainwrap').prepend('<div class="white-burst"></div>');
	$('#home #mainwrap').prepend('<div class="white-burst-left"></div>');

	// handle world map
	//$('#services #worldmap-locations, #services #worldmap-caption').hide();
	$("#worldmap area").addClass('align-left');
	//$("#worldmap-buenos-aires, #worldmap-seattle, #worldmap-texas, #worldmap-boston, #worldmap-san-francisco, #worldmap-st-maarten-caribbean").removeClass('align-left').addClass('align-right');
	//$("map #worldmap-seattle").addClass('align-right');

	if (jQuery.isFunction(jQuery.fn.tooltip)) {
		$('#services #worldmap-caption').addClass('show');
		//$('#services #worldmap #worldmap-caption').show();
		$("#worldmap area.align-left").tooltip({ positionLeft: true, showURL: false, top: 5, left: 200});
		$("#worldmap area.align-right").tooltip({ positionLeft: true, showURL: false, top: 5, extraClass: 'align-right'});
	}

	$('#services #tooltip h3').prepend('<span class="colons">::</span>');

});

// calculate combined width of all selectors
// usage:
// var total_width = $('#selector1, #selector2').computeTotalWidth();
(function($) {
	$.fn.computeTotalWidth = function() {
		var element_width = 0;
		var width_string	= '';
		this.each(function() {
			this_width	= $(this).outerWidth(true);
			element_width += this_width;
			separator		= (element_width != 0) ? ', ' : '';
			width_string	= width_string + separator + this_width;
		});
		return element_width;
	};

})(jQuery);

// handle remote thumbnail mouseovers, for portfolio carousel
// $selector will usually be an "a[href]" tag
// usage: 	$('.nav a').handleRemoteMouseovers();
(function($) {
	$.fn.handleRemoteMouseovers = function() {

		$offsrc	= '/images/portfolio/thumbnails/';
		$onsrc	= '/images/portfolio/';

		// preload full-sized images
		this.each(function() {
			rollsrc = $(this).children("img").attr("src");
			rollON = rollsrc.replace($offsrc,$onsrc);
			newImg = new Image();
			$(newImg).attr("src", rollON);
		});

		// replace full-sized image with the thumbnail's full-sized counterpart
		this.mouseover(function(){
			imgsrc = $(this).children("img").attr("src");
			if (typeof(imgsrc) != 'undefined') {
				imgsrcON = imgsrc.replace($offsrc,$onsrc);
				$("#portfolio .photo").children("img").attr("src", imgsrcON);
			}
		});

		// prevent click on link from executing
		this.click(function(e) {
			e.preventDefault();
		});

		return this;

	};

})(jQuery);

