var waypoint_locked = false;

function correctHash() {
	if(window.location.hash && /^#[^!]/.test(window.location.hash)) {
		var new_hash = window.location.hash.replace(/^#/, '#!');
		window.location.href='/' + new_hash;
	}
}

function initTypekit() {
	Typekit.load({
		active: function () {
			$(document).ready(function() {
				setTimeout(function() {
					$('.lava-lamp').lavaLamp({
						fx: "linear",
						speed: 80,
						click: function(event, menuItem) { return false; }
					});

					var path = '';
					if(path = window.location.hash) {
						var $link = $('.top-nav a[href="' + path + '"]');
						if($link.length) {
							highlight(path);
							$link.click();
						}
					}
				},200);
			});
		}
	});
}

function lockWaypoints() {
	waypoint_locked = true;
}

function unlockWaypoints() {
	waypoint_locked = false;
}

function highlight(hash) {
	if(!waypoint_locked) {
		if(/^#\w/.test(hash)) {
			hash = hash.replace(/^#/, '#!');
		}
		var $highlight = $('.top-nav li.back');
		var $menu_item = $('a[href="' + hash + '"]').closest('li');
		$highlight.stop();
		$highlight.animate({
			left: $menu_item.position().left,
			width: $menu_item.width()
		}, 500, 'easeInOutCubic');
		$highlight.show();
	}
}

// Image Preloader
var imageObj = new Image();
var images = [];
images.push("/img/headshots-b.jpg");
for (var i = 0; i < images.length; i++) {
	imageObj.src = images[i];
} 


$(function() { 	
	$('.container a').click(function() {
		var link = $(this), $menu_item = false;
		var href = link.attr('href');
		if(/^#/.test(href)) {
			href = href.replace(/^#/, '#!');
			if(($menu_item = $('.top-nav a[href="' + href + '"]')).length) {
				$menu_item.click();
				return false;
			}
		}
		
		return true;
	});
	
	//Sticky Panel Plugin
	var stickyPanelOptions = {
		afterDetachCSSClass: 'sticky',
		savePanelSpace: true 
	};
	$('.sticky-nav').stickyPanel(stickyPanelOptions); 
	
	// Smooth Scrolling w/jQuery http://tympanus.net/Tutorials/WebsiteScrolling/index_vertical.html
	$('a.stick').bind('click', function(event) {
		event.preventDefault();
		lockWaypoints();
		var selector = $(this).attr('href').replace(/!/, '');

		var scrollTopLocation;

		if (selector == '#Overview') {
			scrollTopLocation = 0;
		} else {
			scrollTopLocation = $(selector).offset().top - $('.sticky-nav').height();
		}


		$('html, body').stop().animate({
			scrollTop: scrollTopLocation 
		}, 500, 'easeInOutCubic', function() {
			unlockWaypoints();
		});
	});
	
	// SudoSlider jQuery Plugin http://webbies.dk/SudoSlider
	var sudoSlider = $("#Approach-Sections").sudoSlider({ 
		controlsShow: false,
		fade: true,
		crossFade: false,
		fadespeed:'200',
		customLink: 'a.approach-links'
	});
	
	var sudoSlider = $("#Portfolio-Sections").sudoSlider({
		continuous: true,
		fade: true,
		crossFade: false,
		fadespeed:'200',
		customLink: 'a.approach-links'
	});
	
	// Slides jQuery Plugin http://slidesjs.com
	$('.project-details').slides({
		// generateNextPrev: true,
		preload: true
	}); 
	
	// Mosaic jQuery Plugin www.buildinternet.com/project/mosaic
	$('.circle').mosaic({
		opacity: 0.8
	});
	
	$('.bar').mosaic({
		animation:	'slide'
	});
	
	// Fancybox Plugin http://fancybox.net/
	$('.fancybox').fancybox({
		'autoScale'			: true,
		'overlayColor'		: '#000',
		'overlayOpacity'	: 0.8

	});
	
	// Waypoints
	// waypoints hit while scrolling down
	$('.section-container').waypoint(function(event, direction) {
		//console.log("(down) waypoint reached: " + $(this).find('h1').text());
		if(direction == 'down') {
			highlight('#' + $(this).attr('id'));
		}
	}, {
		offset: 200
	}); 
	
	// waypoints hit while scrolling down
	$('.section-container').waypoint(function(event, direction) {
		if(direction == 'down') {
			//console.log("(down) waypoint reached: " + $(this).find('h1').text());
			highlight('#' + $(this).attr('id'));
		}
	}, {
		offset: 250
	}
	);

	// waypoints hit while scrolling up
	$('.section-container+div.spacer').waypoint(function(event, direction) {
		if(direction == 'up') {
			var $section_container = $(this).prevAll('.section-container:first');
			//console.log("(up) waypoint reached: " + $section_container.find('h1').text());
			highlight('#' + $section_container.attr('id'));
		}
	},	{
		offset: 350
	}
	);
}); // end of document-ready

$('#submit').live('click', function(e) {
	var valid = false;
	var name = $('#name').val();
	var company = $('#company').val();
	var email = $('#email').val();
	var phone = $('#phone').val();
	var url = $('#url').val();
	var budget = $('#budget').val();
	var industry = $('#industry').val();
	var situation = $('#situation').val();
	var information = $('#information').val();

	if (name == '') {
		alert('Please enter your name.');
		$('#name').focus();
		return false;
	}

	if (email == '') {
		alert('Please enter your e-mail address.');
		$('#email').focus();
		return false;
	}

	if (industry == '') {
		alert('Please describe your industry.');
		$('#industry').focus();
		return false;
	}

	if (situation == '') {
		alert('Please describe your situation.');
		$('#situation').focus();
		return false;
	}

	$.ajax({
		'type': 'POST',
		'url': '/ajax.php',
		'data': {
			'q': 'contact',
			'name': name, 
			'company': company,
			'email': email,
			'phone': phone,
			'url': url,
			'budget': budget,
			'industry': industry,
			'situation': situation,
			'information': information
		},
		'dataType': 'JSON',
		success:function(result) {
			if (result.success) {
				$('#contact_content').html('<p class="green">Thank you for your message, ' + name + '!</p><p class="green">We will respond as soon as we can.</p><p class="green">&mdash; The BrickWire Team</p>');
				var ContactScrollPosition = $('#Contact').position();
				ContactScrollPosition = ContactScrollPosition.top - 150;
				$('html, body').animate({
					scrollTop: ContactScrollPosition
				}, 300);
			} else {
				if (!result.valid) {
					alert(result.message);
					return false;
				}
			}
		},
		error:function(xhr, responseText, error) {
			alert('Unable to send your message. Please contact us at support@brickwire.com.');
		}
	});
});

function CreateBookmarkLink() {

	title = "BrickWire"; 

	url = "http://brickwire.com";

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title);
	}
	else if(window.opera && window.print) { // Opera Hotlist
		return true; 
	}
}

