function debug(o) {
	if(window.console && window.console.log) {
		window.console.log(o);
	}
}

// hover state
var sfHover = function() {
	var sfEls, length,
		mouseOver, mouseOut;
		
	sfEls		= document.getElementsByTagName('li');
	length		= sfEls.length;
	
	mouseOver	= function() {
		this.className += ' hover';
	};
	mouseOut	= function() {
		this.className = this.className.replace('hover', '');
	};
	
	for(var i=0; i<length; i++) {
		sfEls[i].onmouseover	= mouseOver;
		sfEls[i].onmouseout		= mouseOut;
	}
};
if(document.getElementById && document.getElementsByTagName) {
	window.onload = sfHover;
}


if(typeof jQuery != "undefined") {
	jQuery.noConflict();
	jQuery(document).ready(function($) {
		var $body, $content, $primary, $secondary, $nav,
			$projects, $projectsList,
			$gallery, $thumbnails, $galleryImage, $galleryImages, $carousel,
			cycleOptions,
			customScrollbar = true,
			$mapContainer, maps;
		
		/**
		* General
		*/
		$body		= $('body').addClass('jquery');
		$nav		= $('#nav');
		$content	= $('#content');
		$primary	= $('#content-primary');
		$secondary	= $('#content-secondary');
		
		
		/**
		* @section Gallery
		* --------------------------------------------------------------------------------------------------
		*/
		$projects		= $('ul.projects');
		$projectsList	= $('#projects-list');
		$gallery		= $('div.gallery');
		$thumbnails		= $('#thumbnails');
		$galleryImage	= $('div.gallery > img');
		$galleryImages	= $('div.gallery li img');
		$thumbnails.bind('click.thumbnails', {active: 'active'}, function(event){
			var $$, $li, href, large;
			
			$$	= $(event.target).closest('a');
			$li	= $$.closest('li');
			
			if($$.is('a') && $$.length === 1) {
				href	= $$.attr('href');
				large	= href.replace('/thumbs', '');
				
				if($li.is(':not(.' + event.data.active + ')')) {
					$li.addClass(event.data.active).siblings().removeClass('active');
					$galleryImage.attr('src', large);
				}
				
				$$.blur();
				event.preventDefault();
			}
		}).children('li').filter(':first').addClass('f').end().filter(':last').addClass('l');
		
		$projects.add($projectsList).bind('mouseover.projects mouseout.projects', {active: 'hovered'}, function(event){
			var $$, href, $hovered;
			
			$$		= $(event.target).closest('a');
			href	= $$.attr('href');
			
			if($$.length === 1 && typeof href !== 'undefined') {
				$hovered = $('a[href=' + href + ']').closest('li');
				switch(event.type) {
					case 'mouseover':
					case 'mouseenter':
						$hovered.addClass(event.data.active).children('a').addClass(event.data.active);
						$hovered.siblings().removeClass(event.data.active).children('a').removeClass(event.data.active);
						break;
						
					case 'mouseout':
					case 'mouseleave':
						$hovered.removeClass(event.data.active).children('a').removeClass(event.data.active);
						break;
				}
			}
		});
		
		
		/**
		* @section Carousel
		* @uses jCarouselLite()
		* @see http://www.gmarwaha.com/jquery/jcarousellite/
		* --------------------------------------------------------------------------------------------------
		*/
		if($.fn.jCarouselLite && $galleryImages.length > 10) {
			$carousel	= $('<div />').attr('id', 'carousel');
			
			$prevNext	= []; li=0;
			prevNext	= ['Previous', 'Next'];
			$.each(prevNext, function(i){
				var className = this.toLowerCase();
				if(i === 0) {
					className += ' disabled';
				}
				$prevNext[li++] = '<li class="' + className + '">';
				$prevNext[li++] = '<a href="#' + this.toLowerCase() + '">';
				$prevNext[li++] = this;
				$prevNext[li++] = '</a>';
				$prevNext[li++] = '</li>';
			});
			$prevNext = $('<ul />').addClass('prev-next').append($prevNext.join(''));
			
			$thumbnails.wrap($carousel);
			$gallery.append($prevNext);
			$thumbnails.parent().jCarouselLite({
				visible:		10,
				scroll:			10,
				start:			0,
				speed:			300,
				auto:			false,
				circular:		false,
				btnNext:		'.prev-next .next a',
				btnPrev:		'.prev-next .previous a',
				afterEnd:		function(a) {
					var $$, $lis, index;
					$$		= $(a);
					$first	= $$.filter('.f');
					$last	= $$.filter('.l');
					
					if($first.length === 1) {
						// first image
						$prevNext.find('li').removeClass('disabled').filter('.previous').addClass('disabled');
					}
					else if($last.length === 1) {
						// last image
						$prevNext.find('li').removeClass('disabled').filter('.next').addClass('disabled');
					}
					else {
						$prevNext.find('li').removeClass('disabled');
					}
					$prevNext.find('a').blur();
				}
			});
		}
		
		/**
		* @section Rotating Quotes
		* @uses cycle()
		* @see http://www.malsup.com/jquery/cycle/
		* --------------------------------------------------------------------------------------------------
		*/
		if($.fn.cycle) {
			cycleOptions = {
				fx:				'fade',			// name of transition effect
				delay:			-6000,			// additional delay (in ms) for first transition (hint: can be negative)
				timeout:		8000,			// milliseconds between slide transitions (0 to disable auto advance)
				speed:			7000,			// speed of the transition
				speedIn:		7000,			// speed of the 'in' transition 
				speedOut:		7000,				// speed of the 'out' transition
				startingSlide:	0,				// zero-based index of the first slide to be displayed,
				pause:			true,			// true to enable "pause on hover"
				next:			null,			// selector for element to use as click trigger for next slide 
				prev:			null,			// selector for element to use as click trigger for previous slide
				pager:			null,			// selector for element to use as pager container 
				prevNextClick:	function(isNext, zeroBasedSlideIndex, slideElement) {
					$(this.next).add(this.prev).children('a').blur();
				},
				pagerClick:		function(zeroBasedSlideIndex, slideElement){
					$(this.pager).children('a').blur();
				}
			};
			
			if($body.is('.index-index')) {
				cycleOptions.speedIn	= null;
				cycleOptions.speedOut	= null;
			}
			
			$('img.cycle').each(function(i){
				var $$ = $(this),
					data;
				
				// get the images for this page
				data = {
					pathname:		window.location.pathname,
					origination:	$$.attr('src').slice(0, $$.attr('src').lastIndexOf('/'))
				};
				if(data.pathname === '/') {
					data.pathname = 'homepage';
				}
				$.post('/ajax/images', data, function(data, status){
					var $div, $images, $arrows,
						selector, options;
						
					$div		= $('<div />').addClass('images').attr('id', 'gallery-' + (i + 1));
					selector	= '#' + $div.attr('id') + ' ';
					options		= {};
					
					// build the images
					// && data.images.length > 0
					if(data.path.length > 0 && typeof data.images === 'object' && data.images.length > 0) {
						var list = [], li = 0, className,
							total = data.images.length, count = 0;
							
						$.each(data.images, function(){
							list[li++] = '<li>';
							list[li++] = '<img src="' + data.path + this + '" alt="" title="">';
							list[li++] = '</li>';
							
							$('<img />').bind('load', function(event){
								count++;
								
								if(total === count) {
									// completed loading images, start the gallery
									startGallery();
								}
							}).attr('src', data.path + this);
						});
						$images = $('<ul />').addClass('cycle-images');
						$images.append(list.join('')).appendTo($div);
						
						$div.bind('mouseover.hover mouseout.hover', function(event){
							switch(event.type.toLowerCase()) {
								case 'mouseover':
									$(this).addClass('hover');
									break;
									
								case 'mouseout':
									$(this).removeClass('hover');
									break;
							}
						});
	
						// different effect
						if($$.is('.scroll')) {
							options.fx = 'scrollHorz';
						}
	
						// don't move by default
						if($$.is('.static')) {
							options.timeout = 0;
						}
						
						// based on the class name on the image
						if($$.is('[class*=start-]')) {
							options.startingSlide = parseInt($$.attr('class').replace(/.*start-([0-9]+).*/g, "$1"), 10) - 1;
						}
						
						
						// create the arrows
						if($$.is('.arrows') && list.length > 1) {
							options.next = selector + '.next';
							options.prev = selector + '.previous';
							
							$arrows = $('<ul />').addClass('cycle-arrows');
							
							var parts = [], ii = 0,
								arrows = ['Previous', 'Next'];
							
							$.each(arrows, function(i, val){
								parts[ii++] = '<li class="' + val.toLowerCase() + '">';
								parts[ii++] = '<a href="#' + val.toLowerCase() + '">';
								parts[ii++] = '<span>';
								parts[ii++] = val;
								parts[ii++] = '</span>';
								parts[ii++] = '</a>';
								parts[ii++] = '</li>';
							});
							
							$arrows.append(parts.join('')).appendTo($div);
							$div.addClass('has-cycle-arrows');
						}
						
						startGallery = function(){
							$$.replaceWith($div);
							$images.cycle($.extend({}, cycleOptions, options));
							sfHover();
						}
					}
				}, 'json');
			});
		}		
		
		
		/**
		* @section Scrollbar
		* @uses jScrollPane()
		* @uses mousewheel() (optional)
		* @see http://www.kelvinluck.com/assets/jquery/jScrollPane/jScrollPane.html
		* --------------------------------------------------------------------------------------------------
		*/
		if(typeof customScrollbar !== 'undefined' && customScrollbar === true && $.fn.jScrollPane) {
			$primary.add($secondary).children('div.scroll').jScrollPane({
				scrollbarWidth:	20,
				showArrows:		true
			});
		}
		
		/**
		* @section Google Maps
		* --------------------------------------------------------------------------------------------------
		*/
		$mapContainer = $('div.map'); maps = {};
		if($mapContainer.length > 0 && GBrowserIsCompatible()) {
			$mapContainer.each(function(i, element){
				var $$, $locations, $map, id,
					$findUsMap, $findUsH4, $findUsA,
					$location, locationLon, locationLat,
					map, mapUI, zoom, center;
		
				$$			= $(element);
				id			= 'map-' + (i + 1);
				$map		= $('<div />').addClass('map').attr('id', id).hide().css({'height': '442px', 'width': '532px'});
				$locations	= $('div.vcard', $$);
				zoom		= 2;
				center		= new GLatLng(25, 0);
				
				$findUsMap	= $('<div />').addClass('view-map');
				$findUsH4	= $('<h4 />').text('How to find us:');
				$findUsA	= $('<a />').attr('href', '#' + id).text('View Map').data('text', 'Hide Map');
				
				$findUsMap.append($findUsH4).append($findUsA);
				$$.addClass('has-map').append($findUsMap);
				$secondary.append($map);
				
				if($locations.length === 1) {
					// only one place, set zoom level and center based values
					$location	= $locations.eq(0);
					locationLon	= $('span.longitude', $location).text();
					locationLat	= $('span.latitude', $location).text();
					zoom		= parseInt($('span.zoom', $location).text(), 10);
					center		= new GLatLng(locationLat, locationLon);
				}
				
				map		= new GMap2($map.get(0));
				mapUI	= map.getDefaultUI();
				mapUI.controls.scalecontrol			= false;
				mapUI.controls.maptypecontrol		= false;
				mapUI.controls.menumaptypecontrol	= false;
				mapUI.controls.largemapcontrol3d	= true;
				//map.addControl(new GOverviewMapControl(null)); 
				map.setCenter(center, zoom);
				map.setUI(mapUI);
				
				maps[i + 1] = {
					'map':		map,
					'center':	center,
					'zoom':		zoom
				};
		
				$locations.each(function(a, adr){
					var $adr, lon, lat, height,
						point, marker, markerHtml;
		
					$adr	= $(adr);
					lon		= $('span.longitude', adr).text();
					lat		= $('span.latitude', adr).text();
					height	= $adr.height();
		
					point		= new GLatLng(lat, lon);
					marker		= new GMarker(point);
					markerHtml	= $adr.html();
		
					GEvent.addListener(marker, 'click', function() {
						marker.openInfoWindowHtml('<div class="adr" style="height: ' + height + 'px">' + markerHtml + '</div>', {maxWidth: 200});
					});
		
					map.addOverlay(marker);
				});
			});
		
			$body.bind('unload.googleMaps', function(){
				if(GBrowserIsCompatible()) {
					GUnload();
				}
			});
			
			$('div.view-map').live('click.showMap', function(event){
				var $$, href, $div,
					text, data, int;
				
				$$ = $(event.target).closest('a');
				
				if($$.is('a') && $$.length === 1) {
					href	= $$.attr('href');
					int		= parseInt(href.replace('#map-', ''), 10);
					$div	= $(href);
					
					if($div.length === 1 && typeof maps === 'object' && typeof maps[int] !== 'undefined') {
						$div.toggle(0, function(){
							maps[int].map.checkResize();
							maps[int].map.setCenter(maps[int].center, maps[int].zoom);
							text = $$.text();
							data = $$.data('text');
							
							$$.text(data).data('text', text);
						});
						$$.blur();
						event.preventDefault();
					}
				}
			});
		}

	});
}