var hasRetinaDisplay = function() {
	return (window.devicePixelRatio >= 2);
}

var iOSDevice = function() {
	return (/iphone|ipod|ipad/gi).test(navigator.platform);
}
var iOSNewerThan = function(majorVersion) {
	if(isAppleDevice()) {
		var pattern = /iPhone OS (.*) like Mac/;
		var result  = navigator.userAgent.match(pattern); 
		var version = result[1].split(''); 
		var release = version[0];
		return (release >= majorVersion);
	}
	return false;
}

var supports_local_storage = function() {
  try {
    return 'localStorage' in window && window['localStorage'] !== null;
  } catch(e){
    return false;
  }
}

var fullScreenMode = function() {
	try {
		return 'standalone' in window.navigator && window.navigator.standalone;
	} catch(e) {
		return false;	
	}
}


var hideUrlBar = function () {
   !pageYOffset && !location.hash && setTimeout(function () {
      window.scrollTo(0, 1);
    }, 1000);
};


$(document).ready(function() {
	
	var externalLink = $("a[rel*=external], #tweet a");

	
	if (iOSDevice()) {
		
		hideUrlBar();
     		
		$(externalLink).bind('click', function(){
			if (confirm('This link opens in a new window.')) {
				return true;
			} else {
				return false;
			}
		});
			
   	}	
	else {
		
		$(externalLink).attr("target","_blank");	
		
	}

	$('.carousel').each(function(index, item) {
								 
		$(item).carousel({
			slider: '.carousel-slider',
			slide: '.carousel-slide',
			addNav: true,
			addPagination: true,
			speed: 500 
		});							 
	
	});
	
	
	new $.styleSwitcher({
		container: '#bottom .wrapper',
		styles: 'blue, red, green, yellow',
		label: 'Change skin'	
	});
						   					   
	$('a[href^=#]:not([href=#], .carousel .carousel-tabs a, .flex-control-nav a, .slidecontrols a)').click(function(event){
		$('html, body').animate({ 
			scrollTop: $($(this).attr("href")).offset().top 
		}, 1000);
	 	event.preventDefault();
		
	});

	$('header#top a, .go-top a, .social a').bind('touchstart', function(event){															
		$(this).addClass('hover');						   
	}).bind('touchend', function(event){
		$(this).removeClass('hover');
	});
	
	$('.flexslider').flexslider({
		animation: "slide",
		controlsContainer: '.flex-container',
		slideshowSpeed: 4500, 
		animationDuration: 1000, 
		directionNav: false   
	});
	
	$('textarea.expand').focus(function () {
    $(this).animate({ height: "90px" }, 500);
	}).blur(function () {
    $(this).animate({ height: "30px" }, 500);
	});
	
	$("form#contact-form").submit(function(e) {
									 
		var contactForm = $(this);
		var submitBtn = $('input[type=submit]', this);
			
		submitBtn.attr('disabled', 'disabled');

		$.ajax({
			type: contactForm.attr('method'),
			url: contactForm.attr('action'),
			data: contactForm.serialize(),
			success: function(response){
				$('#form-container').css('height', contactForm.height());
				
				contactForm.fadeOut(function(){
											 
					$('<div id="response">'+response+'</div>')
					.appendTo('#form-container')
					.click(function(){
						$(this).fadeOut().remove();
						submitBtn.removeAttr("disabled");
						contactForm.fadeIn();
						
						contactForm[0].reset();
						
						$('#form-container').css('height', 'auto');

					});

				}); //form fade

			}// success

		});
		
		e.preventDefault(); 
		
	});
	
	if (window.PIE) {
        $('nav a, #social a, .flex-control-nav li a, .carousel-tabs li a, .go-top a, input, textarea').each(function() {
            PIE.attach(this);
        });
    }
	
	
});

if(window.applicationCache) {
	// Check if a new cache is available on page load.
	window.addEventListener('load', function(e) {
	
	  window.applicationCache.addEventListener('updateready', function(e) {
		if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
		  // Browser downloaded a new app cache.
		  // Swap it in and reload the page to get the new hotness.
		  window.applicationCache.swapCache();
		  if (confirm('A new version of this site is available. Load it?')) {
			window.location.reload();
		  }
		} else {
		  // Manifest didn't changed. Nothing new to server.
		}
	  }, false);
	
	}, false);
}
