﻿Font.replace('.font', { fontFamily: 'Variable' });

var filter_timeout = undefined;

$(document).ready(function() {

	$('#viewport').grid({ callback: prepareAjax });
	
	$('#filter input').hide();
	$('#filter label').click(function() {
		if(filter_timeout != undefined) {
			clearTimeout(filter_timeout);
        }
        
		$(this).hasClass('selected') ? $(this).removeClass('selected') : $(this).addClass('selected');
		
		filter_timeout = setTimeout(function() {
			filter_timeout = undefined;
			categories = new Array();
			$("#filter input:checked").each(function() {
				categories.push($(this).attr('id'));
			});

			$.ajax({type: "GET",
				url: '/filter/'+categories.toString()+'/',
				dataType: "html",
				success: function(data) {
					$('#viewport').fadeOut(300, function() {
						$(this).attr('id', 'old_viewport');
						viewport = $('<div></div>').insertAfter($('#apex')).html(data).attr('id', 'viewport');
						$('#old_viewport').remove();
						Font.refresh();
						$('#viewport').grid({ callback: prepareAjax });
					});
				}
			});
        }, 500);
	});


	$(document).bar({
		position: 'bottom',
		height: '30',
		style: 'bar'
	});
	
	$('a.tip').qtip({
		content: false,
		position: {
			corner: {
				tooltip: 'bottomMiddle',
				target: 'topMiddle'
			}
		},
		style: {
			border: {
				width: 1,
				radius: 5,
				color: '#ffffff'
			},
			padding: 5, 
			textAlign: 'center',
			tip: true,
			background: '#ffffff',
			color: '#000000',

		},
	});
	
	$(function(){
		$('a[rel="external"]').click(function() {
			window.open($(this).attr('href'));
			return false;
		});
	});
	
	$(document).ajaxComplete(function() {
		Font.refresh();
		
		// Let's attach a clearing event to each link to prevent the 
		// main event from firing if you click a link inside the box.				
		$('#viewport').find('.post a').click(function(e) {
			if ($(this).attr('rel') == 'bookmark') {
				e.preventDefault();
			} else {
				window.location = $(this).attr('href');
				return false;
			}
		});
	});
	
	$.jGrowl("This is an experimental site, looks a mess and will frequently break, otherwise enjoy! - Jay", { sticky: true });
	
});

function prepareAjax(obj, focus) {
	
	var o = obj;
	var focus = $(document).find('.focus');
	
	focus.find('.epitome').fadeOut(300, function() {
		focus.find('.synopsis').fadeIn(300, function() {
			focus.find('.epitome').remove();
		});
	});
 	
	$.ajax({type: "GET",
			url: o.find('.synopsis h2 a').attr('href'),
			dataType: "html",
			beforeSend: function() {
				o.find('.synopsis').fadeOut(300, function() {
					$('<div></div>').hide().appendTo(o).addClass('load').fadeIn(300);
				});
			},
			success: function(data) {
				$('<div></div>').appendTo(o).addClass('epitome').html(data);
				o.find('.load').fadeOut(300, function() {
					o.find('.epitome').fadeIn(300, function() {
						o.find('.load').remove();
					});
				});
			}
	});

}