﻿
// Custom sorting plugin
(function($) {
  $.fn.sorted = function(customOptions) {
    var options = {
      reversed: false,
      by: function(a) { return a.text(); }
    };
    $.extend(options, customOptions);
    $data = $(this);
    arr = $data.get();
    arr.sort(function(a, b) {
      var valA = options.by($(a));
      var valB = options.by($(b));
      if (options.reversed) {
        return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;				
      } else {		
        return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;	
      }
    });
    return $(arr);
	
  };
})(jQuery);

// DOMContentLoaded
$(function() {

  // bind radiobuttons in the form
  var $filterType = $('#filter input[name="type"]');
  var $filterSort = $('#filter input[name="sort"]');

  // get the first collection
  var $applications = $('#applications');

  // clone applications to get a second collection
  var $data = $applications.clone();

  // attempt to call Quicksand on every form change
  $filterType.add($filterSort).change(function(e) {
											  
	if ($($filterType+':checked').val() == 'all') {
      var $filteredData = $data.find('li');
    } else {
      var $filteredData = $data.find('li[data-type=' + $($filterType+":checked").val() + ']');
    }

    // if sorted by size
    if ($('#filter input[name="sort"]:checked').val() == "size") {
      var $sortedData = $filteredData.sorted({
        by: function(v) {
          return parseFloat($(v).find('span[data-type=size]').text());
        }
      });
    } else {
      // if sorted by name
      var $sortedData = $filteredData.sorted({
        by: function(v) {
          return $(v).find('strong').text().toLowerCase();
        }
      });
    }   

    // finally, call quicksand
    $applications.quicksand($sortedData, {
      duration: 800,
      easing: 'easeInOutQuad'

	});
	
  });
	
});
$(document).ready(function() {
	
				
	$(document).pngFix(); 
	$('#menu1').localScroll();
	$('#menu2').localScroll();
	$('#menu3').localScroll();
	$('#introText').localScroll();
	$('#nothingToSee').localScroll();
	$('.icon').tipsy({fade: 'true', gravity: 's'});
	
					
	// Intro quotes
	var intros = new Array();
	intros[0] = "Marvin 'does' front-end";
	intros[1] = "Marvin knows 'user experience'";
	intros[2] = "Marvin keeping it cleaner than Ajax";
	intros[3] = "Marvin, 'Zen like' in his approach";
	var randomIntro = Math.floor(Math.random()*intros.length);	
	$("#intro").html(intros[randomIntro]);
	
	// twitter feed
	$("#twitter").getTwitter({
		userName: "marvin_speakman",
		numTweets: 2,
		loaderText: "Loading tweets...",
		slideIn: true,
		showHeading: false,
		headingText: "Latest Tweets",
		showProfileLink: true
	});
	
	$(function() {
		$("#filter").buttonset();
	});
	
	
	// a copy of this call is in quicksand.js to activate after sorting has taken place //
	$('#applications li').hover(function(){  
		$(".cover", this).stop().animate({top:'178px'},{queue:false,duration:700});  
	}, function() {  
		$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:700});  
	});
	
});

