$(function() {

	//handles both single images and galleries (via rel attribute)
	$('.fancybox-default').fancybox({
		cyclic: true,
		type: 'image'
	});
});

// Spotlight scroller plugin
// v 1.0 - James Edmonds, james@threeamdesign.com.au 20/01/2010
// Usage $('.spotlight').spotlight(options);
// Returns a jQuery object

jQuery.fn.spotlight = function(options) {
	var settings = jQuery.extend({
		wrapper: '.spotlight-items',
		items: '.item',
		duration: 100,
		cycle: true,
		constant: false,
		jump: false,
		force: false,
		margin: true
	}, options);
	
	return this.each(function() {
		var target = $(this);
		var parent = target.parent();
		var wrapper = target.find(settings.wrapper).addClass('spotlight-items-wrapper');
		var items = target.find(settings.items);
		var total = items.length;
		
		settings.prev = settings.prev || parent.find('a.previous');
		settings.next = settings.next || parent.find('a.next');
		
		var first = $(items[0]).addClass('first');
		var last = $(items[total - 1]).addClass('last');
		
		target.trigger('onInit', [items]);
		
		var width = 0;
		items.each(function() { width += $(this).outerWidth(); });
		
		target.find(settings.wrapper).width(width);
		target.serialScroll(settings).addClass('spotlight-processed');
	});
};

// Texta text replacement plugin
// v 1.0 James Edmonds, james@threeamdesign.com.au 16/02/2010
// Usage $('.texta').texta(options)
// Returns a jQuery object

jQuery.fn.texta = function(options) {
	var settings = jQuery.extend({
		size: 0,
		lineHeight: 0,
		font: '',
		colour: '',
		background: '',
		inline: false,
		textaURL: '/texta.php?t='
	}, options);
	return this.each(function() {
		var target = $(this);
		var width = target.css('display') == 'inline' ? 0 : target.width();//inline stuff should not wrap because of font width differences
		var height = target.height();
		var text = target.text() || target.val();
		switch (target.css('text-transform')) {
			case 'lowercase':
				text = text.toLowerCase();
				break;
			case 'uppercase':
				text = text.toUpperCase();
				break;
			case 'capitalize':
				text = text.replace(/\b\w/, function(match) {
					return match.toUpperCase();
				});
				break;
		}
		var texta = settings.textaURL + encodeURIComponent(text) + '&w=' + width;
		if (settings.size)
			texta += '&s=' + settings.size;
		if (settings.lineHeight)
			texta += '&lh=' + settings.lineHeight;
		if (settings.font)
			texta += '&f=' + settings.font;//url friendly switch cases in texta.php
		if (settings.colour)
			texta += '&c=' + settings.colour;
		else if (settings.color)
			texta += '&c=' + settings.color;
		if (settings.background)
			texta += '&bg=' + settings.background;
		
		target.fadeTo(0, 0);
		
		var preload = new Image();
		preload.onload = function() {
			if (settings.inline)
				//IE needs vertical align on parent as-well
				target.css('vertical-align', 'text-top').html('<img src="' + $.htmlspecialchars(texta) + '" />').children('img').css('vertical-align', 'text-top');
			else
				target.css({
					textIndent:'-9999em',
					width: preload.width,
					height: preload.height,
					//display: 'block',
					backgroundImage: "url('" + texta + "')",
					backgroundPosition: '0 0',
					backgroundRepeat: 'no-repeat'
				});
			target.attr('title', text).addClass('texta-processed').fadeTo('normal', 1);
		}
		preload.src = texta;
	});
};

jQuery.fontExists = function(fontFamily, fontFamilyIsFixedWidth) {
	var defaultFont = fontFamilyIsFixedWidth ? 'serif' : 'monospace';
	
	if (fontFamily.toLowerCase() == defaultFont)
		return true;
	
	var $body = this('body'),
		$div = this('<div><span>mmmmmmmmmmxxxxxxxxxxiiiiiiiiii</span></div>'),
		$span = $div.children('span'),
		generic = {
			serif: true,
			'sans-serif': true,
			cursive: true,
			fantasy: true,
			monospace: true
		};
	
	$div.css({
		visibility: 'hidden',
		'font-size': '72px',
		'font-family': defaultFont
	});
	$span.css({
		'font-family': ''
	});
	$body.append($div);
	var defaultWidth = $span.width(), defaultHeight = $span.height(), exists = false;
	$span.css('font-family', generic[fontFamily] ? fontFamily : "'" + fontFamily + "', " + defaultFont);
	exists = $span.width() != defaultWidth || $span.height() != defaultHeight;
	$div.remove();
	return exists;
};

// Fading elements plugin
// v 1.0 James Edmonds, james@threeamdesign.com.au 16/02/2010
// Usage $('.fading').fading(options)
// Returns a jQuery object

jQuery.fn.fading = function(options) {

	var settings = jQuery.extend({
		startOpacity: 1,
		endOpacity: 0.5,
		speed: 'normal'
	}, options);
	
	return this.each(function() {
		var target = $(this).fadeTo(0, settings.startOpacity);
		target.hover(
			function() { $(this).fadeTo(settings.speed, settings.endOpacity); },
			function() { $(this).fadeTo(settings.speed, settings.startOpacity); }
		);
	});
};

jQuery.fn.tableDrag = function(options) {

	var settings = jQuery.extend({}, options);
	var need_select_workaround = typeof $(document).attr('onselectstart') != 'undefined';
	
	return this.each(function() {
		
		var table = $(this);
		var reordered = false;
		
		table.find('tbody tr td:first-child').mousedown(function(event) {
		
			var lastY = event.pageY;
			var tr = $(this).parent().fadeTo('fast', 0.2);
			
			$('tr', tr.parent()).not(this).mouseenter(function(event) {
				
				if(event.pageY > lastY) {
					$(this).after(tr);
				} else {
					$(this).before(tr);
				}
				lastY = page.pageY;
			})
			
			$('body').mouseup(function() {
				tr.fadeTo('fast', 1);
				$('tr', tr.parent()).unbind('mouseenter');
				$('body').unbind('mouseup');
				
				if(need_select_workaround) {
					$(document).unbind('selectstart');
				}
				
				if(reordered === false) {
					table.after('<div class="messages warning">The order of these items has been changed.</div>');
					reordered = true;
				}
			});
		}).css('cursor', 'move');
		
	});
};

