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

	// Add Typekit class
	$('h1,h2,h3,h4,h5,h6,nav.nav ul li a strong span,.login-button,#footer ul li a,#footer .holder, .form-block label,.pager').addClass('tk-m-1c');
	$('p,aside li').addClass('tk-myriad-pro');
	
	// Login button
	$('#header div.login-button .drop').hide();
	$('#header div.login-button a').attr('href','javascript:void(0)');
	$('#header div.login-button a').toggle(function() {
		$(this).addClass('active');
		$('#header div.login-button .drop').show();
	}, function() {
		$(this).removeClass('active');
		$('#header div.login-button .drop').hide();
	});
	
	// general Do hover
	$('.dohover').hover(
		function () {
			$(this).addClass('hover');
		},
		function () {
			$(this).removeClass('hover');
		}
	);

	// Catalog page
	$('div.slide-section').each(function(){
	    contents = $('.title a',this).html();
	    $('.title a',this).remove();
	    $('.title h2',this).html(contents);
	});
	$('.slide-block').css('cursor','pointer');
	$('.slide-block').hover(
		function () {
			$(this).addClass('hover');
		}, 
		function () {
			$(this).removeClass('hover');
		}
	);
	$('.slide-block').click(function() {
		$(this).parents('.slide-section').children('.other-block').toggle(100);
		if ( $(this).parents('.slide-section').hasClass('active') ) {
			$(this).parents('.slide-section').removeClass('active');
		}
		else {
			$(this).parents('.slide-section').addClass('active');
		}
	});
	$('.pager-block .pager a:contains(Next)').addClass('next');
	$('.pager-block .pager a:contains(Previous)').addClass('prev');
	$('body.archive').each(function(){
	    $('.slide-section').sortElements(function(a, b){
	        return $(a).text() > $(b).text() ? 1 : -1;
	    });
	});
	$('.block-list ul').css('padding-left','5px');
	$('.block-list ul').each(function(){
		$('li:first', this).css('background','none');
	});
	$('.block-list').hide();
	$('.categories a').each(function(){
	    oldlink = $(this).attr('href');
	    $(this).attr('href',oldlink+'?sortoption=categories');
	});
	// sorting controls
	$('.sort-options button').click(function(){
		$('.block-list').hide();
		if ( $(this).hasClass('active') ) {
			$(this).removeClass('active');
		}
		else {
			$(this).addClass('active');
		}
		$('.sort-options button').each(function(){
			if ( $(this).hasClass('active') ) {
				classy = $(this).attr('name');
			    $('.block-list').children('.'+classy).each(function(){
			        $(this).parent().show();
			    });
			}
		});
	});
	alpha = $.getUrlVar('alpha');
	sortoption = $.getUrlVar('sortoption');
	if ( alpha != null ) {
		$('.sort-options button.alphabetical').click();
	}
	if ( sortoption != null ) {
		$('.sort-options button.categorical').click();
	}


});

// get $_GET
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});























/**
 * jQuery.fn.sortElements
 * --------------
 * @param Function comparator:
 *   Exactly the same behaviour as [1,2,3].sort(comparator)
 *   
 * @param Function getSortable
 *   A function that should return the element that is
 *   to be sorted. The comparator will run on the
 *   current collection, but you may want the actual
 *   resulting sort to occur on a parent or another
 *   associated element.
 *   
 *   E.g. $('td').sortElements(comparator, function(){
 *      return this.parentNode; 
 *   })
 *   
 *   The <td>'s parent (<tr>) will be sorted instead
 *   of the <td> itself.
 */
jQuery.fn.sortElements = (function(){
 
    var sort = [].sort;
 
    return function(comparator, getSortable) {
 
        getSortable = getSortable || function(){return this;};
 
        var placements = this.map(function(){
 
            var sortElement = getSortable.call(this),
                parentNode = sortElement.parentNode,
 
                // Since the element itself will change position, we have
                // to have some way of storing its original position in
                // the DOM. The easiest way is to have a 'flag' node:
                nextSibling = parentNode.insertBefore(
                    document.createTextNode(''),
                    sortElement.nextSibling
                );
 
            return function() {
 
                if (parentNode === this) {
                    throw new Error(
                        "You can't sort elements if any one is a descendant of another."
                    );
                }
 
                // Insert before flag:
                parentNode.insertBefore(this, nextSibling);
                // Remove flag:
                parentNode.removeChild(nextSibling);
 
            };
 
        });
 
        return sort.call(this, comparator).each(function(i){
            placements[i].call(getSortable.call(this));
        });
 
    };
 
})();
