$(function(){
	
	$("html").addClass("js-on");
	
	$("head").append('\
		<link rel="stylesheet" href="css/jquery.fancybox-1.3.1.css" type="text/css" media="screen">\
		<script type="text/javascript" src="js/jquery.masonry.min.js"></script>\
		<script type="text/javascript" src="js/jquery.fancybox-1.3.1.js"></script>\
		<script type="text/javascript" src="js/jquery.lazyload.min.js"></script>\
	');
	
	$('#wrapper').randomize();
	
	$('#wrapper').masonry({singleMode: true});
		
	$(".brick a").click(function() {
		pageTracker._trackPageview($(this).attr("href"));
	});
	
	$("a.mail").attr("href","mailto:hello@evolvedprimate.com");
		
	/* Desktop browsers */
	if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {

		$(".brick a").fancybox({
			"titleShow"				: true,
			"titlePosition"			: 'over',
			"showNavArrows"			: true,
			"transitionIn"			: 'fade',
			"transitionOut"			: 'fade',
			"overlayColor"			: '#000',
			"overlayOpacity"		: '0.7',
			"hideOnContentClick"	: true,
			"showCloseButton"		: false,
			'titleFormat'       	: function(title, currentArray, currentIndex, currentOpts) {
									  	if (currentArray.length>1){
									  		return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + title + '</span>';
									  	} else {
									  		return '<span id="fancybox-title-over">' + title + '</span>';
									  	}
									  }
		});
		
		$("html.js-on .brick img").fadeTo(500, 0.7);
		
		$("html.js-on .brick").hover(
			function () {
				$(this).find("img").fadeTo(500, 1);
			},
			function () {
				$(this).find("img").fadeTo(500, 0.7);
			}
		);
		
		$("img").lazyload({ 
			failurelimit : 10,
			effect : "fadeIn"
		});
	}
	
	/* Mobile Safari */
	if (/iphone|ipod|ipad/i.test(navigator.userAgent)) {
		
		function hideURLbar(){
			window.scrollTo(0,1);
		}

		addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false);

		$("head").append('\
			<meta name="viewport" content="width=device-width, user-scalable=no">\
			<meta name="apple-mobile-web-app-status-bar-style" content="black">\
		');
	}
}); 




$.fn.randomize = function() {
  function randOrd() { return(Math.round(Math.random())-0.5); }
 
  return($(this).each(function() {
    var $this = $(this);
    var $children = $this.children();
    var childCount = $children.length;
 
    if (childCount > 1) {
      $children.remove();
 
      var indices = new Array();
      for (i=0;i<childCount;i++) { indices[indices.length] = i; }
      indices = indices.sort(randOrd);
      $.each(indices,function(j,k) { $this.append($children.eq(k)); });
 
    }
  }));
}

