var ADAPT_CONFIG = {
	path: '/public/css/',
	dynamic: true,
	range: [
		'0px to 1110px = 860.css',
		'1110px = 1110.css'
	]
};

// Home, Features
var features_page = 0;
var features_interval = 0;
var features_length = 5000;
var features_width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0;

function loadNextFeature() {
	var feature_next = features_page + 1;
	if (feature_next >= features.length) {
		feature_next = 0;
	}
	loadFeature(feature_next);
}
function loadPrevFeature() {
	var feature_prev = features_page - 1;
	if (feature_prev < 0) {
		feature_prev = features.length - 1;
	}
	loadFeature(feature_prev);
}
function loadFeature(index) {	
	if (index != features_page) {
		clearInterval(features_interval);		
		var img = new Image();
		var src = features[index][3];
		img.onload = function() {			
			$('#feature a').attr("src", features[index][1]);
			$('#feature h1').text(features[index][0]);
			$('#feature h2').text(features[index][4]);
			$("#feature #divstretch").remove();
			$.divstretch(src);
			$('#feature').mousedown(function() {
				window.location = features[index][1];
			});
		}
		img.src = src;
		features_page = index;
		updateFeatureNav();
	}
}
function updateFeatureNav() {
	features_interval = setInterval(loadNextFeature, features_length);
	$("#features .wrapper .project").each(function() {
		if ($(this).index() == features_page) {
			$(this).addClass('active');
		} else {
			$(this).removeClass('active');
		}
	});
}

// Studio
var people_page = null;
var people_next = 0;
var people_prev = 0;

// Work
var work_category = "";

$(document).ready(function() {
	// Home page features	
	if ($('#feature').length) {
		if (features.length > 0) {
			// Set main feature
			$('#feature a').attr("src", features[0][1]);
			$('#feature h1').text(features[0][0]);
			$('#feature h2').text(features[0][4]);
			$.divstretch(features[0][3], '', function() {
				$('#feature').removeClass('feature-loading');
			});
			$('#feature').mousedown(function() {
				window.location = features[0][1];
			});
			$('#feature .wrapper').show();
			
			// Build feature nav
			$.each(features, function(index, value) {				
				var project_html = "";
				var project_class = (index == 0) ? "project active" : "project";
				project_html += '<div class="' + project_class + '">';
				project_html += '<a class="thumb" href="' + this[1] + '">';
				project_html += '<img src="' + this[2] + '" alt="" />';
				project_html += '</a>';
				project_html += '<h4><a href="' + this[1] + '">' + this[0] + '</a></h4>';
				project_html += '</div>';
				$('#features .wrapper').append(project_html);
			});
			
			// Start rotation
			features_interval = setInterval(loadNextFeature, features_length);
		}
	}
	// Studio nav
	if ($('#people').length) {
		var people = $('#people ul li');
		// Find active profile
		$.each(people, function(index, value) {
			if ($(this).hasClass("active")) {
				people_page = index;
			}
		});
		// Set next, prev links
		if (people_page == null) {
			people_next = 0;
			people_prev = people.length - 1;				
		} else {
			people_next = people_page + 1;
			people_prev = people_page - 1;
			if (people_next >= people.length) {
				people_next = 0;
			}
			if (people_prev < 0) {
				people_prev = people.length - 1;
			}				
		}
		$('#people .next').attr("href", $(people[people_next]).find("a").attr("href"));
		$('#people .prev').attr("href", $(people[people_prev]).find("a").attr("href"));		
	}
	// Work layout
	$('.work .projects .wrapper').masonry({
		singleMode: true,
		columnWidth: 220,
		animate: false,
		itemSelector: '.project',
		saveOptions: true
	});
	$('.work #categories ul a').click(function() {
		var category = $(this).attr("class");		
		if (category != work_category) {
			work_category = category;
			$('#category_projects').stop(true, true);
			$.get('/ee/index.php/work/category/' + category, function(data) {
				$('#category_projects .wrapper').remove();
				$('#category_projects').show().html('<div class="wrapper"></div>');
				$('#category_projects .wrapper').html(data).masonry();
				$('#category_projects').hide().slideDown("normal");
			});			
		} else {
			if ($('#category_projects').css("display") == "none") {
				$('#category_projects').slideDown("normal");
			} else {
				$('#category_projects').slideUp("normal");			
			}
		}
		// Update category nav
		$(this).parent().addClass("active");
		var category_active = $(this).attr("class");
		$('.work #categories ul a').each(function() {
			if ($(this).attr("class") != category_active) {
				$(this).parent().removeClass("active");
			}
		});
		
		return false;
	});
	// News comment form
	$('#comments_form input[name=name]').focus(function() {
		if ($(this).val() == "NAME") {
			$(this).val("");
		}
	});
	$('#comments_form input[name=name]').blur(function() {
		if ($(this).val() == "") {
			$(this).val("NAME");
		}		
	});
	$('#comments_form input[name=email]').focus(function() {
		if ($(this).val() == "EMAIL ADDRESS") {
			$(this).val("");
		}
	});
	$('#comments_form input[name=email]').blur(function() {
		if ($(this).val() == "") {
			$(this).val("EMAIL ADDRESS");
		}		
	});
	$('#comments_form textarea[name=comment]').focus(function() {
		if ($(this).val() == "COMMENT") {
			$(this).val("");
		}
	});
	$('#comments_form textarea[name=comment]').blur(function() {
		if ($(this).val() == "") {
			$(this).val("COMMENT");
		}		
	});
	// Contact form
	$('#contact_form input[name=name]').focus(function() {
		if ($(this).val() == "NAME") {
			$(this).val("");
		}
	});
	$('#contact_form input[name=name]').blur(function() {
		if ($(this).val() == "") {
			$(this).val("NAME");
		}		
	});
	$('#contact_form input[name=email]').focus(function() {
		if ($(this).val() == "EMAIL ADDRESS") {
			$(this).val("");
		}
	});
	$('#contact_form input[name=email]').blur(function() {
		if ($(this).val() == "") {
			$(this).val("EMAIL ADDRESS");
		}		
	});
	$('#contact_form textarea[name=comment]').focus(function() {
		if ($(this).val() == "COMMENT") {
			$(this).val("");
		}
	});
	$('#contact_form textarea[name=comment]').blur(function() {
		if ($(this).val() == "") {
			$(this).val("COMMENT");
		}		
	});
	$("#contact_form input[type=submit]").click(function() {
		var contact_name = $("#contact_name").val();
		var contact_email = $("#contact_email").val();
		var contact_comment = $("#contact_comment").val();
					
		// Create query string
		var q = "action=contact&name=" + contact_name + "&email=" + contact_email + "&comment=" + contact_comment;
		
		// Validate, submit
		$.ajax({type: "POST", url: "/public/helpers/contact.php", data: q, dataType: "json", 
			success: function (data, textStatus) {
				if (data.errors) {
					for (i = 0; i < data.errors.length; i++) {
						if (data.errors[i].display.length > 0) {
							$("#"+data.errors[i].input).addClass("error");
						} else {
							$("#"+data.errors[i].input).removeClass("error");
						}
					}
				}
				if (data.complete == "1") {
					$("#contact_form form").html("<p>Thanks for your question or comment, we&#8217;ll be in touch!<\/p>");
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				//alert(XMLHttpRequest.responseText);
				alert(errorThrown);
			}
		});
		
		return false;
	});
	// Mailing list form
	$('#footer input[name=q]').focus(function() {
		if ($(this).val() == "SEARCH") {
			$(this).val("");
		}
	});
	$('#footer input[name=q]').blur(function() {
		if ($(this).val() == "") {
			$(this).val("SEARCH");
		}		
	});	
});

// Google Maps
//http://code.google.com/apis/maps/documentation/javascript/maptypes.html#StyledMaps

function initialize() {
	var myLatlng = new google.maps.LatLng(37.568734, -77.474238);
	var myOptions = {
		zoom: 15,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: false,
		streetViewControl: false,
		zoomControl: true,
		mapTypeControlOptions: {mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'bam']}		
	}
	var desaturated = [{
		featureType: "all",
		elementType: "all",
		stylers: [{saturation: -100},{lightness: 10}]
	}];
	
	if (document.getElementById("google_map")) {
		var map = new google.maps.Map(document.getElementById("google_map"), myOptions);
		
		// Apply map styles
		var styledMapOptions = {name: "BAM Architects"}		
		var bamMapType = new google.maps.StyledMapType(desaturated, styledMapOptions);		
		map.mapTypes.set('bam', bamMapType);
		map.setMapTypeId('bam');
		
		// Create marker
		var marker = new google.maps.Marker({
			position: myLatlng, 
			map: map
		});		
	}
}

function loadScript() {
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
	document.body.appendChild(script);
}
  
window.onload = loadScript;
