jQuery(function() {
	var themeDirectory = '/wp-content/themes/iccgw/';
	
	
	// lightbox handling (not tracked)
	
	if(jQuery('body').width() >= 660) {
		var jqLightBoxed = jQuery('#content ul.PhotoThumbnailListing > li > a');
		
		jQuery('#content div.wp-caption a')
			.each(function() {
				if(this.href.match(/\.(gif|jpe?g|png)$/)) {
					var jqThis = jQuery(this);
					
					if(!jqThis.attr('title')) {
						var jqSibling = jqThis.siblings('p.wp-caption-text');
						if(jqSibling.length)
							jqThis.attr('title', jqSibling.text());
					}
					
					jqLightBoxed = jqLightBoxed.add(jqThis);
				}
			});
		
		jqLightBoxed.lightBox({
			imageLoading:  themeDirectory + 'images/loader-003399-on-ffffff.gif',
			imageBtnPrev:  themeDirectory + 'jquery-lightbox/images/lightbox-btn-prev.gif',
			imageBtnNext:  themeDirectory + 'jquery-lightbox/images/lightbox-btn-next.gif',
			imageBtnClose: themeDirectory + 'jquery-lightbox/images/lightbox-btn-close.gif',
			imageBlank:    themeDirectory + 'jquery-lightbox/images/lightbox-blank.gif'
		});
	}
	
	
	// PDF/e-mail/offsite link handling (tracked)
	
	if(window && window.location && window.location.href) {
		var locationHrefNormalized = jQuery.trim(window.location.href).toLowerCase();
		var locationHrefTokens = locationHrefNormalized.split('/');
		
		jQuery('a').each(function() {
			if(!this || !this.href)
				return;
			
			var linkHrefNormalized = jQuery.trim(this.href).toLowerCase();
			var linkHrefTokens = linkHrefNormalized.split('/');
			
			if(linkHrefNormalized.match(/\.pdf$/))
				this.myLinkCharacteristics = {
					iconSrc: '/wp-content/themes/iccgw/images/pdf-icon.gif', iconAlt: '[PDF]', iconWidth: 16, iconHeight: 16,
					eventName: 'PDF Download'
				};
			
			else if(linkHrefNormalized.match(/^mailto\:/))
				this.myLinkCharacteristics = {
					iconSrc: '/wp-content/themes/iccgw/images/email-icon.gif', iconAlt: '[e-mail]', iconWidth: 16, iconHeight: 16,
					eventName: 'E-mail Link'
				};
			
			else if(
				(linkHrefNormalized.indexOf('http://') == 0 || linkHrefNormalized.indexOf('https://') == 0 || linkHrefNormalized.indexOf('//') == 0) &&
				linkHrefTokens.length > 2 && locationHrefTokens.length > 2 && linkHrefTokens[2] != locationHrefTokens[2]
			)
				this.myLinkCharacteristics = {
					iconSrc: '/wp-content/themes/iccgw/images/offsite-icon.gif', iconAlt: '[offsite]', iconWidth: 12, iconHeight: 10,
					eventName: 'Outbound Link'
				};
			
			if(this.myLinkCharacteristics) {
				var jqThis = jQuery(this);
				
				if(this.myLinkCharacteristics.iconSrc && !jqThis.find('img').length)
					jqThis
						.wrapInner('<span />')
						.append(' ')
						.append(
							jQuery('<img />').attr({
								src: this.myLinkCharacteristics.iconSrc, alt: this.myLinkCharacteristics.iconAlt,
								width: this.myLinkCharacteristics.iconWidth, height: this.myLinkCharacteristics.iconHeight
							})
						)
						.addClass('IconLink')
					;
				
				if(this.myLinkCharacteristics.eventName)
					jqThis
						.click(function() {
							try {
								if(_gaq)
									_gaq.push([ '_trackEvent', this.myLinkCharacteristics.eventName, this.href ]);
								
								setTimeout('window.location.href = "' + this.href + '"', 100);
							}
							catch(err) { window.location.href = this.href; }
							
							return false;
						})
					;
			}
		});
	}
	
	
	// image preload
	
	var imagePreloads = [ ];
	var imageSources = [
		'images/email-icon.gif',
		'images/go-dashboard-icon.gif',
		'images/go-home-icon.gif',
		'images/go-key-icon.gif',
		'images/loader-003399-on-ffffff.gif',
		'images/offsite-icon.gif',
		'images/pdf-icon.gif',
		'jquery-lightbox/images/lightbox-btn-prev.gif',
		'jquery-lightbox/images/lightbox-btn-next.gif',
		'jquery-lightbox/images/lightbox-btn-close.gif',
		'jquery-lightbox/images/lightbox-blank.gif'
	];
	
	for(var i = 0; i < imageSources.length; ++i) {
		var newImage = new Image();
		newImage.src = themeDirectory + imageSources[i];
		imagePreloads.push(newImage);
	}
});

