$(document).ready(
	function() {
		initPrimaryNav();
		initZoom();
		buildBreadCrumb();
		searchFocusBlur();
		
		// force ie to load bgs from cache
		try { 
			document.execCommand("BackgroundImageCache", false, true); 
		} catch(e) {} 

	}
);

/*$(document).load (
	function() {
		restoreSearchBG();
	}
)*/



/**
 * initZoom 
 * --------
 * insert the enlarged photo into the document and attach the 
 * click events that control its display on the "show" and "hide" buttons 
 */
function searchFocusBlur() {
	$('#search-term').focus(
		function() {
			if( this.value == this.defaultValue ) {
				this.value = '';
			}
		}
	).blur (
		function() {
			if( this.value == '' ) {
				this.value = this.defaultValue;
			}
		}
	)
}




/**
 * initZoom 
 * --------
 * insert the enlarged photo into the document and attach the 
 * click events that control its display on the "show" and "hide" buttons 
 */
function initZoom() {
	var jqPhoto = $('#photo img');
	if( jqPhoto.size() < 1 ) {
		return false;
	}

	//var sLgImageSrc = jqPhoto.get(0).src.replace('sm_', 'lg_');
	// get the large version from the zoom href
	var jqZoom = $('#zoom');
	var sLgImageSrc = jqZoom.attr('href');
	
	// insert the zoom-target element with the enlarged version of the photo
	$('#right-column').prepend('<div id="zoom-target"><img src="' + sLgImageSrc + '" /><a href="#" id="close" class="zoom-ctrl"><img src="/images/icon_close.gif" width="48" height="16" alt="" /></a></div>');

	// show the enlarged version
	jqZoom.click(
		function() {
			//var photo = document.getElementById('photo');

			// target for enlarged version
			var zoomTarget = $('#zoom-target');
			// thumbnail container
			var photo = $('#photo').get(0);
			// get thumbnail container offset - container width, and then position the target accordingly
			var coords = getCumulativeOffset(photo);
			var nLeft = (coords.x - 337) + "px";
			var nTop = coords.y;
			zoomTarget.css( {top: nTop, left: nLeft, display: "block" } );
			return false;
		}
	);
	
	// close the enlarged version
	$('#close').click(
		function() {
			$('#zoom-target').hide();
		}
	);
	
}


/**
 * initPrimaryNav
 * --------------
 * add/remove the "hover" class from elements in primary nav, as they receive and lose focus
 */
function initPrimaryNav() {
	$('#primary-nav li').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	);
}


/**
 * buildBreadCrumb
 * ---------------
 * copy the UL that this page is a member of, and insert it into the primary content region as 
 * breadcrumb navigation.  highlight this item as active with the "active" class name
 */
function buildBreadCrumb() {
	
	// get just the file name and remove #, if it exists
	var sURL = window.location.href.match(/[\w\d.#\-\_]+$/);
	
	if( sURL == null ) {
		return false;
	} else {
		sURL[0].replace('#', '');
	}

	var bAppended = false;
	// locate this item in primary navigation
	$('#primary-nav a').each(
		function() {
			var _this = $(this);
			// if this is it:
			if( _this.attr('href').indexOf(sURL) != -1 && bAppended == false && _this.attr('skipBreadCrumb') != 'true') {

				// first, add an 'active' class, which only has any meaning in the breadcrumb element
				_this.addClass('active');
				
				// then clone the whole list, add the necesssary class names and id to it and then insert it into the content area
				var jqBreadCrumb = $(_this.parents("ul").get(0)).clone().addClass('adjacent clearfix').attr('id', 'breadcrumb');
				// get all the list items
				var jqBCItems = jqBreadCrumb.find('li');
				// store a count
				var nBCItems = jqBCItems.size() - 1;
				jqBCItems.each(
					function(index) {
						// if this isn't the last item in the list, append a separator after it
						if( index < nBCItems ) {
							$('<li class="sep">|</li>').insertAfter( $(this) );
						}
					}
				);				
				jqBreadCrumb.prependTo('#content-wrapper');
				
				bAppended = true;
			}
		}
	);
}

/**
 * restoreSearchBG
 * ---------------
 * google search will update this element - restore the bg we originally applied to the element
 */
function restoreSearchBG() {
	//$('query').css( {background: "#fff url(../images/bg_search.gif) 0 0 no-repeat"} );
}



/**
 *  @author:    Danny Ng (http://www.dannytalk.com)
 *  @date:      21/09/08
 *  @notes:     Free to use and distribute for non-commercial use without altering this notice. Would appreciate a link back.
 */
function getCumulativeOffset(el) {
	var x = 0;
	var y = 0;
	var cur = (el) ? el : this;
	do {
		if (cur.nodeName.toLowerCase != 'td') {
			x += cur.offsetLeft;
			y += cur.offsetTop;
		}
	}
	while ((cur = cur.offsetParent) && cur.nodeName.toLowerCase() != 'body');	

	return { x: x, y: y };
}

function autoQuoteTop() {
	var query = document.getElementById('search-term').value;
	if(query.substring(0,1) != '"') {
		document.getElementById('search-term').value = '"'+query+'"';
	}
}