jQuery(document).ready(function($) {

	$.flare = function(){

		$('.flare').wrapInner("<span></span>");
		$('.flare span').css({ "opacity":0 });	
		
		$('.flare').hover(function() {
			$(this).children("span").animate({ "opacity":1 }, { duration: 450, easing: 'swing', queue: false });
			}, function() {
			$(this).children("span").animate({ "opacity":0 }, { duration: 600, easing: 'swing', queue: false });
		});
	};

	$.flare();

	$(function() {
        $('.main-nav a, .link, #do-work, .button, .return').bind('click',function(event){
            var $anchor = $(this);
            
            $('html, body').stop().animate({
                scrollTop: $($anchor.attr('href')).offset().top
            }, 600,'easeInOutExpo');
            event.preventDefault();
        });
    });

	$('.portfolio-items li a').css('opacity', 0);

	$('.portfolio-items li').hover(function() {
		$(this).children(".overlay").animate({ "opacity":0 }, { duration: 200, easing: 'swing', queue: false });
		$(this).children("a").animate( {"opacity":1}, { duration: 200, easing: 'swing', queue: false });
		}, function() {
		$(this).children(".overlay").animate({ "opacity":1 }, { duration: 200, easing: 'swing', queue: false });
		$(this).children("a").animate( {"opacity":0}, { duration: 200, easing: 'swing', queue: false });
	});
	
	$('.portfolio-items a.view-image').fancybox({
		centerOnScroll: true,
		padding: 10,
		cyclic: true,
		autoScale: true,
		overlayOpacity: 0.85,
		overlayColor: '#000'
	});

	$('.nebraska').click(function(){
		alert('Alas, it was not the best year we could have asked for, but it was a good one indeed. Nebraska finished 5-3 in conference play in their first year in the Big 10, and 9-3 overall. Give it up for those Huskers! On to the Capital One Bowl!');
		return false;
	});

	$('.leave').click(function(){
		alert('Whoa, not so fast. I cannot believe you actually thought you would get away with that.');
		return false;
	});

	var access_token = '4321538.8462bff.cf78b7e7572f4af3b588855df64b3643';

	loadFeed();

	function loadFeed() {
		var param = {access_token:access_token};
		cmd(param, onPhotoLoaded);
	}

	function cmd(param, callback) {
		//popular
		var cmdURL = 'https://api.instagram.com/v1/users/4321538/media/recent?callback=?';
		$.getJSON(cmdURL, param, callback);
	}

	function onPhotoLoaded(data) {
		if (data.meta.code == 200) {
			var photos = data.data;
			
			if (photos.length > 0) {
				for (var key in photos ){
					var photo = photos[key];
					$('<div id=p' + photo.id + '></div>').addClass('photoWrapper').appendTo('.instagram-container');
					
					var str = '<a href="' + photo.link + '" target="_blank"><img id="' + photo.id + '" src="' + photo.images.standard_resolution.url + '" width="100%"></a><p class="caption">' + photo.caption.text + '</p><div class="clearfloat"></div>';
					$('<div></div>').addClass('photo').html(str).appendTo('#p' + photo.id);
		
					$('#' + photo.id).load(function() {
						$('#p' + $(this).attr('id')).fadeTo('slow', 1.0);
					});

				}
			} else {
				alert('empty');
			}
			
		} else {
			alert(data.meta.error_message);
		}
	}

	$('#insta-div').hide();
	$('.return').hide();

    $('.instagram').live('click', function(event){
	$('#insta-div').show();
	$('.return').fadeIn('slow');
	$('#contact').css('marginBottom', 80);
	$('.instagram').addClass('activated');
	$('#insta-div').slideDown(500);
		var $anchor = $(this);
		$('html, body').stop().animate({
                scrollTop: $($anchor.attr('href')).offset().top
            }, 600,'easeInOutExpo');
            event.preventDefault();
		return false;
    });

    $('a.activated').live('click', function(event) {
		$('.instagram.activated').removeClass('activated');
		$('#insta-div').slideUp(500);
		$('#contact').css('marginBottom', 420);
			var $anchor = $('#contact');
		$('html, body').stop().animate({
                scrollTop: $anchor.offset().top
            }, 600,'easeInOutExpo');
            event.preventDefault();
		$('.return').fadeOut('slow');
		return false;
    });

    $('.return').click(function(){
		$('#insta-div').slideUp(500);
		$('.instagram.activated').removeClass('activated');		
		$('#contact').css('marginBottom', 420);
			var $anchor = $(this);
		$('html, body').stop().animate({
                scrollTop: $($anchor.attr('href')).offset().top
            }, 600,'easeInOutExpo');
		$('.return').hide();
		return false;
    });

});
