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

function isAppleDevice() {
	return (/iphone|ipod|ipad/gi).test(navigator.platform);
}

function iOSNewerThan(majorVersion) {
	if(isAppleDevice()) {
		// Check the version
		var pattern = /iPhone OS (.*) like Mac/;
		var result  = navigator.userAgent.match(pattern); // Returns "iPhone OS X_Y like Mac, X_Y"
		var version = result[1].split(''); // Returns X, Y
		var release = version[0];
		return (release >= majorVersion);
	}
	return false;
}

function showProjects () {
	if ( current_page === pages[0] || current_page === pages[1] ) {
		if ( current_page === pages[1] ) {
			$('article').each(function() {
				$(this).fadeOut(0).fadeTo(Math.random()*1200 + 200, 1);
			});

			if ( document.URL == "http://degafolio.info/" || document.URL == "http://degafolio.info/#" ) {
				$('.project_news_tag').parent().parent().parent().each(function(){
					$(this).stop(true, false);
					$(this).fadeOut(0); 
				});
				$('.project_inspiration_tag').parent().parent().parent().each(function(){
					$(this).stop(true, false);
					$(this).fadeOut(0); 
				});
			}
		}
	} else {
		$('#ajx').load("http://degafolio.info/ #projects", function() {
	       startAjaxProjectsLinks ()
			window.scroll(0,0);
		   	$('article').each(function() {
				$(this).fadeOut(0).fadeTo(Math.random()*1200 + 200, 1);
			});
			$('.project_news_tag').parent().parent().parent().each(function(){
				$(this).stop(true, false);
				$(this).fadeOut(0); 
			});
			$('.project_inspiration_tag').parent().parent().parent().each(function(){
				$(this).stop(true, false);
				$(this).fadeOut(0); 
			});
	    });
	}
	
	current_page =pages[0];
	selectedButton( current_page );
}


var pages = [ "projects", "thoughts", "info", "single" ];
var current_page = pages[1];

function showNews () {
	if ( current_page === pages[0] || current_page === pages[1] ) {
		if ( current_page !== pages[1] ) {
			$('article').each(function() {
				$(this).fadeOut(0).fadeTo(Math.random()*1200 + 200, 1);
			});
		}
	} else {
		$('#ajx').load("http://degafolio.info/ #projects", function() {
	        startAjaxProjectsLinks ()
			window.scroll(0,0);
		   	$('article').each(function() {
				$(this).fadeOut(0).fadeTo(Math.random()*1200 + 200, 1);
			});
	    });
	}
	
	current_page = pages[1];
	selectedButton( current_page );
}

function showInfo () {
	if ( current_page !== pages[2] ) {
		$('#ajx').load("http://degafolio.info/?page_id=9 #page_wrapper", function() {
			$('#page_wrapper').fadeOut(0).fadeIn();
	       	startAjaxProjectsLinks ()
			window.scroll(0,0);
	    });
	} 	
	current_page = pages[2];
	selectedButton( current_page );
}

function selectedButton ( current_page_selected ) {
	for ( var i = 0; i < pages.length ; i++ ) {
		$('.btn_' + pages[i]).css("color", "gray");	
		$('.btn_' + pages[i]).css("text-decoration", "none");
	}
	$('.btn_' + current_page_selected).css("color", "#222");
	$('.btn_' + current_page_selected).css("text-decoration", "line-through");
}

function startNavLinks () {
	
	$('header nav ul li a').each(function(){
		
		var currentColor = '#ff0000';
		
		$(this).mouseover(function(){
			currentColor = $(this).css('color');
			$(this).css('color','black');
		})
		
		$(this).mouseout(function(){
			$(this).css('color', currentColor);
		})
		
		console.log($(this).attr('title'));
		if($(this).attr('title') == 'Projects') {
			$(this).click(function(){
				showProjects();
				return false;
			});
	    } else if ($(this).attr('title') == 'Thoughts') {
				$(this).click(function(){
				  showNews();
				  return false;
				});
			} else if ($(this).attr('title') == '& Information about me') {
				console.log("entro");
				$(this).click(function(){
				  showInfo();
				  return false;
				});	
			}
	})
}

function startAjaxProjectsLinks () {
	
	$('#projects article a').each(function(){
		var link = $(this).attr('href');
	    link += ' #single_container'
	   
	    $(this).click( function() {	
		  $('#projects article').fadeTo(500,0);
		  current_page = pages[3];
		
	      $('#ajx').load(link, function() {
			 resizeForMobile ();
			 startAjaxProjectsLinks();
			 startAsideAjaxLinks();
			 showContentTransition();
			 window.scroll(0,0);
	      })   
	      return false;
	   })   
	})
}

function showContentTransition() {
	$('#ajx').fadeTo(0,0.01).delay(500).fadeTo(500,1);
}

function startAsideAjaxLinks () {
	$('aside a').each(function(){
		var link = $(this).attr('href');
	    link += ' #single_container'

	    $(this).click( function() {	
		  current_page = pages[3];
	      $('#ajx').load(link, function() {
			 resizeForMobile ();
			 startAjaxProjectsLinks();
			 startAsideAjaxLinks();
			 showContentTransition();
			 window.scroll(0,0);
	      });   
	      return false;
	   })   
	})
}
function resizeForMobile () {
	$("#wpstats").css("display","none");
	$('iframe').wrap('<div class="video" />');

	var last_scroll_position = 0;
	var container_width = $(".single_post").width();
    var orig_width = 0;
	var orig_height = 0;
    var ratio = 0;
	
	$(".video iframe").each(function(){
      orig_width = $(this).attr("width");
      orig_height = $(this).attr("height");
      ratio = (orig_height/orig_width).toFixed(2);
      $(this).width(container_width);
      $(this).height(ratio*container_width);
    });
	
	$('#projects .single_post img').wrap('<div class="single_img" />');
	
	$("#projects .single_post .single_img img").each(function(){
      orig_width = $(this).attr("width");
      orig_height = $(this).attr("height");
      ratio = (orig_height/orig_width).toFixed(2);
      $(this).width(Math.floor(container_width));
      $(this).height(Math.floor(ratio*container_width));
    });
}

$("document").ready( function() {
	
	$("#shadow").fadeOut(0);
	current_page = pages[0];
	selectedButton( current_page );
	startNavLinks ();
	startAjaxProjectsLinks ();

	//supporting startup image on the iphone 5 retina display
	//not working with ios4 and retina display
	if(hasRetinaDisplay() && iOSNewerThan(5)) { 
		var highResSplash = '<link rel="apple-touch-startup-image" href="/wp-content/themes/degafolio/images/startup@2x.png" />'; 
		$('head').append(highResSplash); 
	}
	
	

	resizeForMobile ();
	
	$('article').each(function() {
		$(this).fadeOut(0).fadeTo(Math.random()*1200 + 200, 1);
	});

	if ( document.URL == "http://degafolio.info/" || document.URL == "http://degafolio.info/#" ) {
		$('.project_news_tag').parent().parent().parent().each(function(){
			$(this).stop(true, false);
			$(this).fadeOut(0); 
		});
		$('.project_inspiration_tag').parent().parent().parent().each(function(){
			$(this).stop(true, false);
			$(this).fadeOut(0); 
		});
	}

	
	$('header h1 a').mouseout(function() {	
	   $('header h1 a').html('Degafolio');
	   $('header h1 a').css('width','60px');
	});
	
	$('header h1 a').mouseover(function() {
	   $('header h1 a').html('← Home');
	   $('header h1 a').css('width','70px');
	});

	$('header').mouseover(function() {
	    $("header").css("top","0px");
	});

	$('header').mouseout(function() {
		if ( last_scroll_position > 80 ){
		    $("header").css("top", -170 + "px");
	    } else {
		   	$("header").css("top", "0px");
		}
	});

	$(window).scroll(function() {
		var yscroll = window.pageYOffset;
		
		if (yscroll>80) {
		    $("header").css("top", -170 + "px");
            $("aside").css("top", 35 + "px");
		    $("#categories_wrapper").css("position","fixed");
			$("#shadow").fadeIn(200);
		} else {
			$("#shadow").fadeOut(200);
            $("aside").css("top", 200 + "px");
		    $("header").css("top","0px");
		}
		
		last_scroll_position = yscroll;
	});
	
	// $('article').css('position', 'absolute');

	// $(window).resize(function () { 
	// 	var initPositionY = 230;
	// 	var posx = 0;
	// 	var posy = initPositionY;
	// 	var x_increment = 330;
	// 	var y_increment = 350;
	// 	var maxWidth = window.innerWidth - x_increment - 20;
	// 	$('article').each( function () {
	// 	  if ( posx > maxWidth ) {
	// 	     posx = 0;
	// 	     posy += y_increment;
	// 	  } 
	// 	  $(this).stop(true, false);
	// 	  $(this).animate({left:posx, top:posy}, { duration: 500, queue: false });
	// 	  posx += x_increment;
	// 	});
	// 
	// 	});
	
});







