(function($){var $container;$.fn.boxify=function(options){var opts=$.extend({},$.fn.boxify.defaults,options);return this.each(function(){$this=$(this);var o=$.meta?$.extend({},opts,$this.data()):opts;$container=$this;create_hidden_boxes($this);initial_layout($this);$this.find(".box.resizable.closed .opener").live("click",function(){$parent=$(this).closest(".box");$.fn.boxify.open_box($parent);}).live("mouseover",function(){$.fn.boxify.hover_box($(this));}).live("mouseout",function(){$.fn.boxify.unhover_box($(this));});$this.find(".box.resizable.opened .closer").live("click",function(){$parent=$(this).closest(".box");$.fn.boxify.close_box($parent);}).live("mouseover",function(){$.fn.boxify.hover_box($(this));}).live("mouseout",function(){$.fn.boxify.unhover_box($(this));});$this.find(".box.resizable.closed.opener").live("click",function(){$.fn.boxify.open_box($(this));}).live("mouseover",function(){$parent=$(this).closest(".box");$.fn.boxify.hover_box($parent);}).live("mouseout",function(){$parent=$(this).closest(".box");$.fn.boxify.unhover_box($parent);});$this.find(".box.resizable.opened.closer").live("click",function(){$.fn.boxify.close_box($(this));}).live("mouseout",function(){$parent=$(this).closest(".box");$.fn.boxify.unhover_box($parent);}).live("mouseover",function(){$parent=$(this).closest(".box");$.fn.boxify.hover_box($parent);});$(".closer").mouseover(function(){$parent=$(this).closest(".box");$.fn.boxify.hover_box($parent);});$(".closer").mouseout(function(){$parent=$(this).closest(".box");$.fn.boxify.unhover_box($parent);});$(window).resize(function(){calculate_layout($container,false);move_boxes_into_place(false);});});};$.fn.boxify.hover_box=function($this){$this.addClass("hover");};$.fn.boxify.unhover_box=function($this){$this.removeClass("hover");};$.fn.boxify.open_box=function($this,no_scroll){if(!$this.hasClass("keep-closed"))
{$this.css("zIndex",10000);calculate_target_size($this,"open");calculate_layout($container,false);move_boxes_into_place(true);resize_box($this,true);$this.css("zIndex",100);}
$this.removeClass("hover");if(!no_scroll)
document.location.hash=$this.attr('id')+"?";};$.fn.boxify.close_box=function($this){$this.css("zIndex",10000);calculate_target_size($this,"close");calculate_layout($container,false);resize_box($this,false);move_boxes_into_place(true);document.location.hash="home";$this.css("zIndex",100);$this.removeClass("hover");};var scroll_to_div=function($this){var scrolltop=$container.offset().top+parseInt($this.attr("targettop"),10)-grid.y.margin-grid.y.inner/2-80;if($.scrollTo)$.scrollTo(Math.max(parseInt(scrolltop),0),1200,{'offset':{'top':-158}});};var calculate_target_size=function($this,type){if(type=="open"){var ogw=$this.attr("ogw");var ogh=$this.attr("ogh");if(!ogh){ogh=grid.measure_height($this.find(".opened_content"),ogw);}
var newsize=grid.dim_to_inner(ogw,ogh,$this);$this.attr("gw",ogw);$this.attr("gh",ogh);}else{var cgw=$this.attr("cgw");var cgh=$this.attr("cgh");var newsize=grid.dim_to_inner(cgw,cgh,$this);$this.attr("gw",cgw);$this.attr("gh",cgh);}
$this.attr("targetwidth",newsize.x);$this.attr("targetheight",newsize.y);};var resize_box=function($this,change_content_first){if(change_content_first){$this.find(".closed_content").fadeOut(400,function(){$this.find(".opened_content").fadeIn(400);});}
var tw=parseInt($this.attr("targetwidth"),10);var th=parseInt($this.attr("targetheight"),10);$this.animate({"width":tw,"height":th},600,"linear",function(){if(change_content_first){$this.addClass("opened");$this.removeClass("closed");}else{$this.addClass("closed");$this.removeClass("opened");$this.find(".closed_content").fadeIn(400);$this.find(".opened_content").fadeOut(400);}});};$.fn.infinitescrolling=function(callback){$container.infinitescroll({navSelector:"div.pagination",nextSelector:"div.pagination a.next",itemSelector:"#boxcontainer div.box",bufferPx:300,loadingText:"",donetext:"This is the end of the interwebs!",loadingImg:"/media/images/chrome/ajax-loader-grey.gif",localmode:false},function(){calculate_layout($container,false);move_boxes_into_place(false);if(callback){callback();}});};var grid=new Grid2D;var bin_heights;var startbin;function px_to_integer(px_string){return parseInt(px_string.substr(0,px_string.length-2),10);}
function Vector2D(x,y){this.x=x;this.y=y;}
function GridDim(){this.inner=0;this.margin=0;this.outer=function(){return this.inner+this.margin;}
this.dim_to_inner=function(dim,$div){if($div){offset=this.measure_border($div);}else{offset=0;}
return-offset+dim*this.inner+(dim-1)*this.margin;}
this.dim_to_outer=function(dim){return dim*this.outer();}
this.from_inner_px=function(px){var r=(px+this.margin)/(this.inner+this.margin);return Math.ceil(r);}}
function Grid2D(){this.x=new GridDim();this.y=new GridDim();this.x.measure_border=function($div){return parseInt($div.css("borderLeftWidth"),10)+parseInt($div.css("borderRightWidth"),10);}
this.y.measure_border=function($div){return parseInt($div.css("borderBottomWidth"),10)+parseInt($div.css("borderTopWidth"),10);}
this.dim_to_inner=function(dimx,dimy,$div){return new Vector2D(this.x.dim_to_inner(dimx,$div),this.x.dim_to_inner(dimy,$div));}
this.dim_to_outer=function(dimx,dimy){return new Vector2D(this.x.dim_to_outer(dimx),this.x.dim_to_outer(dimy));}
this.measure_height=function($content,gridx){var $test=$(".box.test_measure");$test.css("height","auto");$test.empty();$c2=$content.clone();$c2.css("display","block");$test.append($c2);$test.width(this.x.dim_to_inner(gridx,$test)+"px");var h=$test.outerHeight();var r=this.y.from_inner_px(h);$test.empty();return r;}}
var css_units_to_px=function(x,y){$test=$(".box.test_measure");$test.width(x);$test.height(y);return{'x':$test.outerWidth(),'y':$test.outerHeight()};};var css_units_to_grid=function(x,y){var px=css_units_to_px(x+grid.rmargin,y+grid.bmargin);return px_to_grid(px);};var px_to_grid=function(px){return{'x':Math.ceil(px['x']/grid.x.outer()),'y':Math.ceil(px['y']/grid.y.outer())};};function create_hidden_boxes($div){$div.append('<div class="box reference"/><div class="box test_measure"/>');}
var open_openme_boxes=function($container){if(typeof $container==='object'){$container.find(".box.closed.openme").each(function(){$this=$(this);if($this.hasClass("showcase")){slideshow("#"+$this.attr("id"));}
if($(this).hasClass('hash-opened')){$.fn.boxify.open_box($this,false);}else{$.fn.boxify.open_box($this,true);}
$this.removeClass("openme");if($this.attr("id")==$(hash).attr("id"))
{scroll_to_div($this);}});}};var initial_layout=function($container){var $ref=$container.find(".box.reference")
var rmargin=$ref.css("margin-right");grid.x.margin=px_to_integer(rmargin);var bmargin=$ref.css("margin-bottom");grid.y.margin=px_to_integer(bmargin);if(!grid.x.inner){grid.x.inner=$ref.outerWidth();grid.y.inner=$ref.outerHeight();}
calculate_layout($container,true);move_boxes_into_place(true);};var calculate_layout=function($container,force){if(!force)force=false;var docwidth=$container.width();var numbins=Math.floor(docwidth/grid.x.outer());bin_heights=new Array(numbins);for(var i=0;i<bin_heights.length;i++){bin_heights[i]=0;}
startbin=0;$container.find(".box").not(".reference").not(".test_measure").each(function(){measure_div($(this),force);place_in_next_bin($(this));});$container.height(grid.y.outer()*bin_heights[0])};var move_boxes_into_place=function(animate){if(!animate)animate=false;$container.find(".box").not(".reference").not(".test_measure").each(function(){var $this=$(this);spec={'top':$this.attr("targettop"),'left':$this.attr("targetleft")}
if(animate){$this.animate(spec);}else{$this.animate(spec,0);}});$invisibles=$container.find(".box.invisible");$invisibles.hide();$invisibles.fadeIn(400,function(){open_openme_boxes($container);});$invisibles.removeClass("invisible");};var find_bins_to_fit=function(width){var numbin_heights=bin_heights.length;var numtests=numbin_heights-width+1;if(startbin>numtests)startbin=0;var min_bin_height=999999999;var min_bin;for(var i=0;i<numtests;i++){var thebin=i+startbin%numtests;var workingheight=bin_heights[thebin];for(var j=1;j<width;j++){workingheight=Math.max(workingheight,bin_heights[thebin+j]);}
if(workingheight<min_bin_height){min_bin_height=workingheight;min_bin=thebin;}}
return{"bin":min_bin,"height":min_bin_height};};var place_div_in_bin=function($div,bin_spec){var gridwidth=$div.attr("gw");var new_height=bin_spec['height']+parseInt($div.attr("gh"),10);for(i=0;i<gridwidth;i++){bin_heights[bin_spec['bin']+i]=new_height;}
for(i=bin_spec['bin']+gridwidth;i<bin_heights.length;i++){if(bin_heights[i]<bin_spec['height'])bin_heights[i]=bin_spec['height'];}
for(i=0;i<bin_spec['bin'];i++){if(bin_heights[i]<=bin_spec['height']){bin_heights[i]=bin_spec['height']+1;}}
var pos=grid.dim_to_outer(bin_spec['bin'],bin_spec['height']);$div.attr("targettop",pos['y']);$div.attr("targetleft",pos['x']);};var place_in_next_bin=function($div){var bin_spec=find_bins_to_fit($div.attr("gw"));place_div_in_bin($div,bin_spec);};var measure_div=function($div,force){if(!force)force=false;if($div.css('display')=="block"){if(!$div.attr("gw")||force){var w=$div.outerWidth();var h=$div.outerHeight();var current_grid_dims=px_to_grid({'x':w,'y':h},true);if($div.hasClass("opened")){if($div.attr("closedwidth"))$div.addClass("resizable");if(!$div.attr("openedheight"))$div.attr("openedheight",h+"px");if($div.hasClass("resizable")){if(!$div.attr("openedwidth"))$div.attr("openedwidth",w+"px");if(!$div.attr("openedheight"))$div.attr("openedheight",h+"px");}}else{if($div.attr("openedwidth"))$div.addClass("resizable");if($div.hasClass("resizable")){if(!$div.attr("closedwidth"))$div.attr("closedwidth",w+"px");if(!$div.attr("closedheight"))$div.attr("closedheight",h+"px");}}
if($div.hasClass("resizable")){var ow=$div.attr("openedwidth");var oh=$div.attr("openedheight");var ogrid_px=css_units_to_px(ow,oh);var ogrid_dims=px_to_grid(ogrid_px);var cw=$div.attr("closedwidth");var ch=$div.attr("closedheight");var cgrid_px=css_units_to_px(cw,ch);var cgrid_dims=px_to_grid(cgrid_px);$div.attr("ogw",ogrid_dims['x']);$div.attr("cgw",cgrid_dims['x']);$div.attr("cgh",cgrid_dims['y']);if(oh){$div.attr("ogh",ogrid_dims['y']);}}
$div.attr("gw",current_grid_dims['x']);$div.attr("gh",current_grid_dims['y']);}}else{$div.attr("gw",0);$div.attr("gh",0);}};$.fn.boxify.defaults={};})(jQuery);var $document;var $window;var $body;var $html;var hash="";var found_hash=false;var highlighted_section;var keepScrolling=false;var scroll_duration=600;var top_section_height=158;$.fn.ixc_fade=function(){return this.fadeTo(400,0.15).addClass("faded");};$.fn.ixc_unfade=function(){return this.fadeTo(400,1.0).removeClass("faded");};var check_hash=function(){hash=document.location.hash?document.location.hash:"";if(hash){hash=hash.replace("?","");if($(hash).length!=0){found_hash=true;$(hash).addClass('openme').addClass('hash-opened');}
else{$(document).trigger('retrieve.infscr');}}};var update_fade=function(){var $boxes_to_stay;if(highlighted_section){$(".box:not(."+highlighted_section+"):not(.sprinkle, .reference, .test_measure)").ixc_fade();}
$boxes_to_stay=$('.box.'+highlighted_section+":not(.sprinkle, .reference, .test_measure)").ixc_unfade();if(keepScrolling){scroll_to_next_item($boxes_to_stay);}
if($document.height()<=$window.height()){$document.trigger('retrieve.infscr');if(!found_hash&&hash!==""){check_hash();}}
return $boxes_to_stay;};var scroll_to_next_item=function($box_set,do_nothing_if_already_scrolled){var $this;var found=false;var anyloaded=false;var maxscroll=$.scrollTo.max($html[0],'y');var cancel=false;if(do_nothing_if_already_scrolled){$box_set.each(function(i){if(($(this).offset().top-$html.scrollTop())-8===0){cancel=true;return false;}});}
if(cancel){return false;}
$box_set.each(function(i){var $this=$(this);var block_y_pos=$this.offset().top-top_section_height;var scrolled=Math.max($body.scrollTop(),$html.scrollTop());if((block_y_pos>scrolled)){if(scrolled<maxscroll){$.scrollTo($this,scroll_duration,{'offset':{'top':-top_section_height}});found=true;return false;}}});if(!found){if(!all_loaded){keepScrolling=true;$document.trigger('retrieve.infscr');}
else{$.scrollTo($box_set,scroll_duration,{'offset':{'top':-158}});}}};$(document).ready(function(){$document=$(document);$window=$(window);$body=$("body");$html=$("html");check_hash();$window.mousewheel(function(){$body.stop()});$(".box .nojs-permalink").live("click",function(e){e.preventDefault();});var $links=$("#nav_menu").find(".nav,.us");var $show_all=$('<span title="Show all" class="all"></span>');var mute_nav=function(){return $links.removeClass('solo').addClass('mute');};var unmute_nav=function(){return $links.removeClass('solo').removeClass('mute');};var unmute_item=function(item){return item.removeClass('mute').addClass('solo');};var get_boxes=function(extra_selector){if(extra_selector===undefined){extra_selector="";}
return $(".box"+extra_selector+":not(.sprinkle, .reference, .test_measure)");};$("#nav_menu").find("a.us").click(function(e){var $this=$(this);var $name=$this.attr("name");var $box;get_boxes(":not(#"+$name+")").ixc_fade();mute_nav();unmute_item($this);$box=$("#"+$name).ixc_unfade();if($box.attr('id')!=="flatpage-2"){$.fn.boxify.open_box($box);}
$.scrollTo($box,scroll_duration,{'offset':{'top':-158}});$('.nav .all').animate({"opacity":"0.0"}).remove();return false;});$(".go-home").click(function(e){get_boxes().ixc_unfade();unmute_nav();$('.nav .all').animate({"opacity":"0.0"}).remove();return false;});$("#nav_menu .nav:not(.solo):not(.all)").live("click",function(e){var $this=$(this);var $boxes_to_stay;$('.all').remove();$show_all.css("opacity","1").appendTo($this);mute_nav();unmute_item($this);highlighted_section=$this.attr("name");$boxes_to_stay=update_fade();scroll_to_next_item($boxes_to_stay,true);return false;});$("#nav_menu .nav.solo").live("click",function(e){scroll_to_next_item(get_boxes("."+$(this).attr("name")));return false;});$(".nav .all").live("click",function(e){get_boxes().ixc_unfade();$links.removeClass('solo').removeClass('mute');$(this).animate({"opacity":"0.0"}).remove();return false;});$(".box.faded:not(.sprinkle)").live("click",function(e){$(this).ixc_unfade();all_loaded=false;e.preventDefault();});});
