// Site specific JavaScript

$(document).ready(function () {

	// Log pre-loaded libraries
	$.refractiv.data.archive.Modernizr = {
		check: true,
		status: "loaded"
	}

	// Load required libraries
	$.refractiv.core.load([
		$.refractiv.libraries.placeholder,
		$.refractiv.libraries.validate,
		$.refractiv.libraries.cycle,
		$.refractiv.libraries.elastic,
		$.refractiv.libraries.tabsConvert
	]);

	// Form Validation and HTML5 Placeholder support for unsupported browsers
	$('input[placeholder]').refractiv_placeholder();
	$('form').refractiv_validate();
	$('textarea').elastic();
	
	// Load the slideshow if available
	$('section#header-image').cycle({
		fx: 'fade',
		speed: 4000,
		timeout: 12000,
		after: function (currSlideElement, nextSlideElement) {
			$(currSlideElement).css({
				'z-index': 50
			});
			$(nextSlideElement).css({
				'z-index': 100
			});
		}
	});

	// Remove image overlay if no text is available
	($("#header-image h1").contents().length == 0) ? $("#overlay").remove() : null;

	// Tag Cloud config
	var cloud = {
		maxPercent: 400,
		minPercent: 100,
		max: 10,
		min: 1,
		total: parseInt($("#cloud").attr("data-total"))
	}

	// Build Tag Cloud
	$("#cloud a").each(function(i, el){
		var count = $(el).attr("data-count");
		$(el).css({'font-size': 150 * (1.0 + (1.5 * count - cloud.max / 2) / cloud.max) + "%"})
	});

	// Search fieldset to tabs conversion
	$('body.page-requested-search').refractiv_tabsConvert({
		children: 'fieldset', 
		title: 'legend',
		min: 1
	});

	$.ajax({
		async: false,
		dataType: 'script',
		cache: true,
		type: "GET",
		url: "http://widgets.twimg.com/j/2/widget.js",
		data: null,
		success: function() {
			if ($('#twtr')[0]) {
				new TWTR.Widget({
					version: 2,
					type: 'profile',
					rpp: 4,
					interval: 30000,
					width: 250,
					height: 300,
					id: 'twtr',
					theme: {
						shell: {
							background: '#333333',
							color: '#ffffff'
						},
						tweets: {
							background: '#000000',
							color: '#ffffff',
							links: '#AFBD20'
						}
					},
					features: {
						scrollbar: false,
						loop: false,
						live: false,
						hashtags: true,
						timestamp: true,
						avatars: false,
						behavior: 'all'
					}
				}).render().setUser('evocutis').start();
			}
		}
	});

});
