(function() {

	$(document).ready(function() {
	
		var $site = $('#siteWrapper');
		var $header = $site.find('header .contentWrapper');
		var $page = $site.find('#page');
		var $footer = $site.find('footer .contentWrapper');
		var $body = $('body');
	
		var $notFeature = $page.find('.contentWrapper:not(:has(#feature))');
		var duration = 0;
		var fadeTo = 0.4;
		var wait = 0;

		var timeout = null;

/*
		$('#feature').hover(function() {

				$header
					.add( $notFeature )
					.add( $footer )
					.append('<div class="overlay"></div>')
						.find('.overlay')
							.add( $body.prepend('<div class="body-overlay"></div>').find('.body-overlay') )
								.stop(true,true)
								.fadeTo( 0, 0 )
								.fadeTo( duration, fadeTo );

		}, function() {
		
			$header
				.add( $notFeature )
				.add( $footer )
				.find('.overlay')
					.add( $('body .body-overlay') )
						.stop(true,true)
						.fadeTo( 0, 0, function() {
							 $(this).remove();
						});
		
		});  
*/
	
	});
	
})();

(function() {
	
	$(document).ready(function() {
		
		$('#homepageShowcase li a').fancybox({
			centerOnScroll : true,
      overlayColor: '#000',
      padding: 0,
			titleShow : false
		});

	});
	
})();

(function() {

	$(document).ready(function() {
		
		$('#homepageSlideshow').cycle({
			'fx': 'fade',
			'next': '#homepageSlideshow li a',
			'speed': 300
		});
		
		if( $('#homepageSlideshow img').length <= 1 ) {
			$('#homepageSlideshow li a').bind('click', function(e) {
				e.preventDefault();
			});
		}
		
	});
	
	
})();

(function() {
	
	$(document).ready(function() {
		
		var $selectors = $('#homepageFeed ul li a, #exhibitListing ul li, #pressListing ul li');
		var duration = 100;
		var fadeTo = 0.5;

		$selectors.hover(function() {
			$(this).addClass('hover');

			$selectors
				.filter(':not(.hover)')
				// .find('img')
					.stop(true,true)
					.fadeTo(0,1)
					.fadeTo(duration, fadeTo);

		}, function(e) {

			if(
				$selectors.index(e.toElement) >= 0 ||
				$selectors.index( $(e.toElement).parents('li') ) >= 0 ||
				$selectors.index( $(e.toElement).parents('a') ) >= 0
			) {
				duration = 0;
			} else {
				duration = 100;
			}

			$selectors
				.filter(':not(.hover)')
				// .find('img')
					.stop(true,true)
					.fadeTo(duration, 1);

			$(this).removeClass('hover');

		});
		
	});
	
	
})();


(function() {

	var addMarker = function( item, map ) {

		if( !item.location ) {
			item.location = new google.maps.LatLng( item.latitude, item.longitude );
		}

		var markerOpts = {
			map: map,
			position: item.location
		};

		var marker = new google.maps.Marker(markerOpts);

		// Center the Map
		if( item.marker == 'center' ) {
			marker.setMap( map );
			map.setCenter( item.location );
			map.setZoom(14);
		}

	}

	var addMarkerGeocode = function( item, map ) {
		var geocoder = new google.maps.Geocoder();

		 geocoder.geocode({'address': item.address}, function(results, status) {
			if( status == google.maps.GeocoderStatus.OK ) {
				item.location = results[0].geometry.location;
				addMarker( item, map );
			} // else { console.log( item.address, results, 'FAILED' ); }
		});

	}

	var makeMapWithSingleAddress = function( elementID, address ) {
		
		// just in case you fuck up.
		if( document.getElementById(elementID) == null ) {
			return false;
		}
		
		var $element = $('#' + elementID);
		
		if( $element.attr('data-address') ) {
			address = $element.attr('data-address');
		}
		
		// straight up, this is here incase you seriously fuck up.
		// Love, Naz
		if( !( $element.attr('data-address') || ( $element.attr('data-address-lat') && $element.attr('data-address-lng') ) ) ) {
		  return false;
		}
		
		var map;
		var geocoder;    
		var markers;

	  var styles = [{
			featureType: "all",
			elementType: "all",
			stylers: [
				{ hue: "#000000" },
				{ lightness: -13 },
				{ saturation: -100 }
			]
		}];

		map = new google.maps.Map($element.get(0), {
			zoom: 16,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControlOptions: {
				mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'grayscale']
			},

			disableDefaultUI: true,

			scrollwheel: false,

			maxZoom: 17,

			zoomControl: true,
			zoomControlOptions: {
				style: google.maps.ZoomControlStyle.SMALL,
				position: google.maps.ControlPosition.TOP_RIGHT
			},

			panControl: ( $element.width() > 200 ),
			panControlOptions: {
	        position: google.maps.ControlPosition.TOP_RIGHT
	    }
		});

		var rsMapType = new google.maps.StyledMapType(styles, {name:"Gray Scale"});
		map.mapTypes.set('grayscale', rsMapType);
		map.setMapTypeId('grayscale');

		if( $element.attr('data-address') ) {
		
			addMarkerGeocode({
				'marker': 'center',
				'address': address
			}, map);
			
		} else {
			
			addMarker({
				'marker': 'center',
				'latitude': parseInt( $element.attr('data-address-lat'), 10 ),
				'longitude': parseInt( $element.attr('data-address-lng'), 10 )
			}, map);
			
		}
		
	};
	
	$(document).ready(function() {
		makeMapWithSingleAddress( 'featureMap' );
		makeMapWithSingleAddress( 'contactMap' );
		makeMapWithSingleAddress( 'exhibitMap' );
	});
	
})();

(function() {
	
	$(document).ready(function() {
		
		var $nav = $('header nav');
		var $newsletter = $('header #newsletter');

		var space = 34;
		var duration = 200;
		
		$nav.find('.newsletter').bind('click', function() {
			
			var _close = $(this).hasClass('close');

			_nav = {
				active: 0,
				inactive: $newsletter.height() - space
			};
			
			_newsletter = {
				active: 0,
				inactive: -1 * $nav.height()
			};
			
			_nav_ul = {
				active: 1,
				inactive: 0
			};
			
			$nav
				.stop(true,true)
				.css({
					'top': ( _close ? _nav.inactive : _nav.active )
				})
				.animate({
					'top': ( _close ? _nav.active : _nav.inactive )
				}, duration);
			
			$nav
				.find('ul')
					.animate({
						'opacity': ( _close ? _nav_ul.active : _nav_ul.inactive )
					}, duration);
			
			$newsletter
				.stop(true,true)
				.css({
					'top': ( _close ? _newsletter.active : _newsletter.inactive )
				})
				.animate({
					'top': ( _close ? _newsletter.inactive : _newsletter.active )
				}, duration);
			
			if( _close ) {
				
				$('#menuWrapper').unbind('click');
				$(this).add($nav).removeClass('close');
				
			} else {
				
				$(this).addClass('close');
				
				setTimeout(function() {
					$('#menuWrapper').bind('click', function() {
						$nav.find('.newsletter').trigger('click');
					});
				}, 400);
				
				$nav.add($nav).addClass('close');

			}
			
		});
		
	});
	
})();

(function() {
	
	$(document).ready(function() {
		
		$('#exhibitFlyer a').fancybox({
			centerOnScroll : true,
      overlayColor: '#000',
      padding: 0,
			titleShow : false
		});
		
	});
	
	
})();


(function() {
	
	$(document).ready(function() {
		
		var $slideshow = $('#exhibitSlideshow');
		var $thumbs = $('#exhibitThumbs');
		
		var duration = 300;
		
		$slideshow.cycle({
			'fx': 'fade',
			'speed': duration,
			'next': $slideshow,
			'pager': $thumbs,
			'pagerAnchorBuilder': function(i) {
				return $thumbs.find('li a').eq( i );
			},
			'timeout': 0
		});
		
	});
	
})();

(function() {
	
	$(document).ready(function() {
		
		var $studioShowcase = $('#studioShowcase');

		var duration = 300;
		
		if( $studioShowcase.find('img').length > 1 ) {
			$studioShowcase.cycle({
				'fx': 'fade',
				'speed': duration,
				'next': $studioShowcase
			})
		}
		
	});
	
})();

(function() {
	
	$(document).ready(function() {
		
		var $studioShowcase = $('#pressSlideshow');

		var duration = 300;
		
		if( $studioShowcase.find('img').length > 1 ) {
			$studioShowcase.cycle({
				'fx': 'fade',
				'speed': duration,
				'next': $studioShowcase
			})
		}
		
	});
	
})();

(function() {
	
	var selectors = [
  	'#siteHeader a',
		'#showcase li a',
		'.featureLink .featureImg',
		'#homepageShowcase li a img',
		'#homepageSlideshow li a img',
		'#homepageFeed ul li a img'
	];
	
	$(document).ready(function() {
		
		$( selectors.join(',') ).css('opacity', 0).bind('load', function() {
			$(this).animate({'opacity':1});
		});
		
	});
	
	$(window).load(function() {
		
		$( selectors.join(',') ).stop(true,true).animate({
			'opacity': 1
		});
		
	});
	
})();

(function() {
	
	$(document).ready(function() {
		$('a[rel="external"]').attr('target', '_blank');
	});
	
})();

(function() {
	
	$(document).ready(function() {
		
		$('.newsletter form').bind('submit', function(e) {
			
			e.preventDefault();
			
			var url = $(this).attr('action');
			var $this = $(this);
			
			$(this).find('.n_submit').addClass('loading');
			
			$.ajax({
				'url': url,
				'type': 'post',
				'data': {
					'e_mail_address': $this.find('.e_mail_address').val()
				},
				'success': function() {
					
					$this.html('<p class="thanks">Thanks for subscribing</p>');
					
					if( $this.parents('header').length > 0 ) {
  					setTimeout(function() {
  						$('nav .newsletter').trigger('click');
  					}, 500);
					}
					
				}
			})
			
		});
		
	});
	
	
})();

(function() {
	
	$(document).ready(function() {
		
		$('.contactForm form').validate();
		
	});
	
	
})();
