$(function() {

	nav_current();

	$('.blink').on('focus', function() {
        if(this.title==this.value) {
            this.value = '';
        }
        $(this).parent().addClass('focus');
    }).on('blur', function(){
        if(this.value=='') {
            this.value = this.title;
        }
        $(this).parent().removeClass('focus');
    });

    $('#nav li').on('mouseenter', function() {
    	$(this).addClass('hover');
    	var current = $('#nav li.hover');
		var current_width = current.outerWidth();
		var current_left = current.position().left;
		$('#nav div.current').animate({
			width: current_width,
			left: current_left
		}, {
			queue: false
		});
    }).on('mouseleave', function() {
    	$(this).removeClass('hover');
		if($('#nav li.current').length) {
			var current = $('#nav li.current');
			var current_width = current.outerWidth();
			var current_left = current.position().left;
		} else {
			var current_width = 0;
			var current_left = 0;
		}

		$('#nav div.current').animate({
			width: current_width,
			left: current_left
		}, {
			queue: false
		});
	});

	if($('#mountain').length) {
		$(window).load(function() {
			$('#mountain .bg').animate({
					opacity: 1
				}, {
					duration: 500,
					queue: false
				});
			start_mountain();
		});

		if($.browser.msie && $.browser.version < 9) {
			$('html').css('overflow-x', 'hidden');
		}

		$('#mountain .zoom-link').on('mouseenter', function() {
			if($(this).next().hasClass('animated')) {
				$(this).next().animate({
					opacity: 1
				}, {
					duration: 400,
					queue: false
				});
			}
		}).on('mouseleave', function() {
			if($(this).next().hasClass('animated')) {
				$(this).next().animate({
					opacity: 0.4
				}, {
					duration: 400,
					queue: false
				});
			}
		}).on('click', function() {
			$('.zoom-link.current').removeClass('current');
			$(this).addClass('current');
			var loc = $(this).attr('href').toString().replace('#', '');
			zoom_mountain(loc);
			return false;
		});

		$('#mountain .zoom-entry .close').on('click', function() {
			unzoom_mountain();
		});

		$('#mountain .zoom-entry .prev').on('click', function() {
			switch_pos('prev');
		});

		$('#mountain .zoom-entry .next').on('click', function() {
			switch_pos('next');
		});
	}

	$('.plans .circles li').on('click', function() {
		if($(this).hasClass('current') == false) {
			var idx = $(this).index();
			var item_class = $(this).attr('class');
			$('.plans .entries .arrow').attr('class', 'arrow ' + item_class);
			$(this).addClass('current').siblings('.current').removeClass('current');
			$('.plans .entries > div').eq(idx).addClass('current').siblings('.current').removeClass('current');
		}
	});

	$('.plans .prev').on('click', function() {
		var current = $('.plans .circles li.current');
		var cprev = '';
		current.prev().length ? cprev = current.prev() : cprev = $('.plans .circles li:last');
		cprev.trigger('click');
	});

	$('.plans .next').on('click', function() {
		var current = $('.plans .circles li.current');
		var cnext = '';
		current.next().length ?  cnext = current.next() : cnext = $('.plans .circles li:first');
		cnext.trigger('click');
	});

	if($('.services-page').length) {
		$('.services-page .items li').each(function() {
			var hei = $(this).innerHeight() - 46;
			$(this).find('.category').css('width', hei);
		});

		if($.browser.msie && $.browser.version < 9) {
			$('.services-page .items').addClass('ie');
		}
	}
	
	$(window).load(function() {
		getTumblrFeed();
	});
});

// ----------------------------------------------------
function zoom_mountain(loc) {
	hide_flags();

	$('#mountain .clouds-static').animate({
		marginTop: 120
	}, {
		duration: 800,
		queue: false
	});

	$('#mountain .clouds-move').animate({
		marginTop: 150
	}, {
		duration: 800,
		queue: false
	});

	$('#mountain .backgrounds').animate({
		height: 600
	}, {
		duration: 700,
		queue: false,
		easing: 'easeInOutQuart'
	});
	$('#mountain .bg').animate({
		height: 220
	}, {
		duration: 800,
		queue: false
	});
	
	if(loc == 'item1') {
		var zoom_left = '-600px';
	} else if(loc == 'item2') {
		var zoom_left = '-900px';
	} else if(loc == 'item3') {
		var zoom_left = '-1200px';
	} else if(loc == 'item4') {
		var zoom_left = '-1450px';
	}
	
	var item_class = 'div.' + loc;

	$('#mountain .zoomed').animate({
		width: 1935,
		height: 344,
		bottom: 0,
		marginLeft: zoom_left,
		opacity: 1
	}, {
		duration: 800,
		queue: false,
		easing: 'easeInOutQuart',
		complete: function() {
			if($.browser.msie && $.browser.version < 9) {
				$(item_class).show();
			} else {
				$(item_class).fadeIn(500);
			}
		}
	});
}

// ----------------------------------------------------
function unzoom_mountain() {
	$('.zoom-entry:visible').fadeOut(400, function() {
		$('.zoom-link.current').removeClass('current');
		$('#mountain .zoomed').animate({
			width: 1120,
			height: 199,
			bottom: '-200px',
			marginLeft: '-560px',
			opacity: 0
		}, {
			duration: 600,
			queue: false
		});

	$('#mountain .clouds-static').animate({
		marginTop: 0
	}, {
		duration: 600,
		queue: false
	});

	$('#mountain .clouds-move').animate({
		marginTop: 0
	}, {
		duration: 600,
		queue: false
	});

		$('#mountain .bg').animate({
			height: 370
		}, {
			duration: 700,
			queue: false
		});

		$('#mountain .backgrounds').animate({
			height: 370
		}, {
			duration: 600,
			queue: false,
			complete: function() {
				show_flags();
			}
		});
	});
}

// ----------------------------------------------------
function switch_pos(direction) {
	if(direction == 'prev') {
		var curr = parseInt($('.zoom-link.current').attr('href').replace('#item', ''));
		var new_idx = curr-1;
		curr == 1 ? new_idx = $('#mountain .zoom-link').length : new_idx = new_idx;
	} else if(direction == 'next') {
		var curr = parseInt($('.zoom-link.current').attr('href').replace('#item', ''));
		var new_idx = curr+1;
		curr == $('#mountain .zoom-link').length ? new_idx = 1 : new_idx = new_idx;
	}

	$('#mountain .zoom-link .current').removeClass('current');
	if($.browser.msie && $.browser.version < 9) {
		$('#mountain .zoom-entry:visible').hide(0, function() {
			$('#mountain .zoom-link').eq(new_idx-1).trigger('click');
		});
	} else {
		$('#mountain .zoom-entry:visible').fadeOut(500, function() {
			$('#mountain .zoom-link').eq(new_idx-1).trigger('click');
		});
	}
}

// ----------------------------------------------------
function nav_current() {
	if($('#nav li.current').length) {
		var current = $('#nav li.current');
		var current_width = current.outerWidth();
		var current_left = current.position().left;
		$('#nav div.current').animate({
			width: current_width,
			left: current_left
		});
	}
}

// ----------------------------------------------------
function start_mountain() {
	if($.browser.msie && $.browser.version < 9) {
		$('#mountain .glow').addClass('ie');
	} else {
		$('#mountain .glow').addClass('animated');
	}

	setTimeout("drop_flags();",500);
	
	
}

// ----------------------------------------------------
function drop_flags() {

	$('#mountain a.item1').animate({
		top: 183
	}, {
		duration: 500,
		queue: false
	});
	$('#mountain a.item1').next().animate({
		top: 85
	}, {
		duration: 500,
		queue: false
	});
	

	setTimeout(function() {
		$('#mountain a.item2').animate({
			top: 132
		}, {
			duration: 500,
			queue: false
		});
		$('#mountain a.item2').next().animate({
			top: 118
		}, {
			duration: 500,
			queue: false
		});
	},200);

	setTimeout(function() {
		$('#mountain a.item3').animate({
			top: 33
		}, {
			duration: 500,
			queue: false
		});
		$('#mountain a.item3').next().animate({
			top: '-8px'
		}, {
			duration: 500,
			queue: false
		});
	}, 200);
			
	setTimeout(function() {
		$('#mountain a.item4').animate({
			top: 105
		}, {
			duration: 500,
			queue: false
		});
		$('#mountain a.item4').next().animate({
			top: 65
		}, {
			duration: 500,
			queue: false,
			complete: function() {
				if($.browser.msie && $.browser.version < 9) {
					$('#mountain .clouds-move').show(0, function() {
						move_cloud();
					});
				} else {
					$('#mountain .clouds-move').fadeIn(500, function() {
						move_cloud();
					});
				}
			}
		});
	}, 400);
	
	setTimeout(function() {
		load_mountain();
	},600);
}

// ----------------------------------------------------
function load_mountain() {
	$('<img>', {
		src: 'css/images/zoomed.png',
		alt:'',
		'class':'zoomed'
	}).appendTo('#mountain .backgrounds');
}

// ----------------------------------------------------
function hide_flags() {
	if($.browser.msie && $.browser.version < 9) {
		$('#mountain .zoom-link, #mountain .glow, #mountain .headline').hide();
	} else {
		$('#mountain .zoom-link, #mountain .glow, #mountain .headline').fadeOut();
	}
}

// ----------------------------------------------------
function show_flags() {
	if($.browser.msie && $.browser.version < 9) {
		$('#mountain .zoom-link, #mountain .glow, #mountain .headline').show();
	} else {
		$('#mountain .zoom-link, #mountain .glow, #mountain .headline').fadeIn();
	}
}

// ----------------------------------------------------
function move_cloud() {
	$('#mountain .clouds-move').css({
		marginLeft: '-1500px'
	}).animate({
		marginLeft: '-500px'
	}, {
		duration: 90000,
		easing: 'linear',
		complete: function() {
			move_cloud();
		}
	});
}

// ----------------------------------------------------
function init_map() {
	var latlng = new google.maps.LatLng(41.920108,-87.663224);
	var myOptions = {
		zoom: 15,
		center: latlng,
		disableDefaultUI: true,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById("map-container"),

	myOptions);

	var address_marker = new google.maps.Marker({
        position: latlng,
        map: map,
        icon: 'css/images/map-marker.png'
    });
}

// ----------------------------------------------------
function validateThis(){
	var theirName = $('#Full_Name').val();
	var theirEmail = $('#Email_Address').val();
	var theirMessage = $('#Your_Message').val();
	var failed = false;
	
	if(theirName.length < 2 || theirName == "Name"){
		$('#Full_Name').attr("class","blink failed");
		failed = true;
	} else {
		$('#Full_Name').attr("class","blink");
	}
	
	if ( theirEmail.length < 2 || theirEmail == "Email Address" || !isValidEmailAddress(theirEmail)){
		$('#Email_Address').attr("class","blink failed");
		failed = true;
	} else {
		$('#Email_Address').attr("class","blink");
	}
	
	if (theirMessage.length < 2 || theirMessage == "What would you like to talk about?") {
		$('#Your_Message').attr("class","blink failed");
		failed = true;
	} else {
		$('#Your_Message').attr("class","blink");
	}
	
	if (failed) {
		return false;
	}
	else {
		return true;
	}
}

// ----------------------------------------------------
function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][\d]\.|1[\d]{2}\.|[\d]{1,2}\.))((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\.){2}(25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
};

// ----------------------------------------------------
function getTumblrFeed(){
	$.getJSON("http://obrientest.tumblr.com/api/read/json?callback=?", function(data) {
		$.each(data.posts, function(i,posts){
			// Only want one blog post
			if(i==0){
				var postURL = this.url; 
				var postTitle = this['regular-title'];
				
				// Trim post title if it's too long, but make sure not to cut a word in half
				if(postTitle.length > 80){
					var p = postTitle.substring(70, postTitle.length);
					var c = p.indexOf(' ');
					if (c != undefined){
						postTitle = postTitle.substring(0,70+c) + " ...";
					}
				}
				
				// Index Page ------------------
				$('<p>', {
					html: postTitle
				}).appendTo('.blog-feed .entry');
				
				// Follow that with the link to the blog post
				$('<a>',{
					'href': postURL,
					'class': "button",
					'html': "Read More >"
				}).appendTo('.blog-feed .entry');
				
				// Work Page ------------------
				$('<p>', {
					html: postTitle
				}).appendTo('.blog-feed .items .last');
				
				// Follow that with the link to the blog post
				$('<a>',{
					'href': postURL,
					'class': "more",
					'html': "Read More >"
				}).appendTo('.blog-feed .items .last');
				
			}
		});
	});
}

