$(document).ready(function () {
	
	/* Background */
	if ($('body').hasClass('slideshow')) {
		$.supersized({
		
			// Functionality
			slide_interval          :   4000,		// Length between transitions
			transition              :   1, 			// 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
			transition_speed		:	2000,		// Speed of transition

			horizontal_center		:	1,
			min_width				:	$(window).width(),
													   
			// Slideshow Images							
			slides 					:  	[
				{image : 'placeholders/1024x768/3.jpg', thumb: ''},
				{image : 'placeholders/1024x768/5.jpg', thumb: ''},
				{image : 'placeholders/1024x768/6.jpg', thumb: ''},
				{image : 'placeholders/1024x768/11.jpg', thumb: ''}
			]
			
		});
	}

	/* Contact form */
	$('#contact_form').submit(function () {
		$.ajax({
			type: 'POST',
			url: 'contact.php',
			data: {
				name: $('#contact_form input[type=text]').val(),
				email: $("#contact_form input[type=email]").val(),
				text: $("#contact_form textarea").val()
			},
			success: function(data) {
				if ( data == 'sent' ) {
					$('#contact_form .status').html('E-mail has been sent.');
				} else if ( data == 'invalid' ) {
					$('#contact_form .status').html('Your name, email or message is invalid.');
				} else {
					$('#contact_form .status').html('E-mail could not be sent.');					
				}
			},
			error: function () {
				$('#contact_form .status').html('E-mail could not be sent.');
			}
		});
		return false;
	});

	/* Contact map */
	if (typeof(showMap) == 'function') showMap();

	/* Accordion */
	$('.accordion li').each(function () {
		toggle = $(this).children('.toggle');
		toggle.text($(this).hasClass('opened') ? '-' : '+');
	});
	$('.accordion .toggle').click(function () {
		$(this).parent('li').children('p').slideToggle();
		$(this).text(($(this).text() == '-') ? '+' : '-');
		return false;
	});

	/* Tabs */
	$('.tabs .selectors a').click(function () {
		/* Select menu item */
		$('.tabs .selectors li').removeClass('selected');
		$(this).parent('li').addClass('selected');
		/* Display tab */
		$('.tabs .tab').removeClass('selected');
		$('.tabs .tab[data-tab="' + $(this).attr('data-tab') + '"]').addClass('selected');
		return false;
	});

	/* Category filter */
	$('.filter a').click(function () {
		/* Select menu item */
		$('.filter a').removeClass('selected');
		$(this).addClass('selected');
		/* Fade in category */
		$('.filterable li, .filterable li img').fadeTo('slow', 0.3);
		$('.filterable li.' + $(this).attr('data-category') + ', .filterable li.' + $(this).attr('data-category') +' img').stop().fadeTo('slow', 1);
		return false;
	});

	/* Fancybox */
	$('a.fancybox_photo').fancybox({
		'transitionIn'		: 'elastic',
		'padding'			: 0,
		'overlayColor'		: '#000'
	});
	$('a.fancybox_video').fancybox({
		'transitionIn'		: 'elastic',
		'padding'			: 0,
		'overlayColor'		: '#000',
		'type'				: 'iframe'
	});

	/* Nivo Slider */
	$('#nivo_slider').nivoSlider();

	/* Regular slider */
	$('#simple_slider').each(function () {

		/* Functions */
		function resetInterval () {
			clearInterval(interval);
			interval = setInterval(nextSlide, 8000);
		}
		function nextSlide () {
			next = $('#simple_slider nav a.selected').removeClass('selected').next();
			if (next.length == 0) next = $('#simple_slider nav a:first-child');
			slide = next.addClass('selected').attr('data-slide');
			moveTo(slide);
		}
		function moveTo (slide) {
			index = $('li[data-slide="' + slide + '"]').index();
			$('#simple_slider ul').animate({left: '-' + 930*index + 'px'}, 1000);
		}

		/* Initialize */
		var interval; resetInterval();

		/* Controls */
		$('#simple_slider nav a').click(function () {
			$('#simple_slider nav a').removeClass('selected');
			$(this).addClass('selected');
			moveTo($(this).attr('data-slide'));
			resetInterval();
			return false;
		});

	});

	/* Settings */
	$('.settings_toggle').click(function () {
		$('#settings').animate({'left': ($('#settings').css('left') == '-200px') ? '0px' : '-200px'});
		return false;
	});
	$('#settings input[type=radio]').change(function () {
		$('body').css('background', '');
		$('body').attr('class', '');
		if ($(this).attr('value') == 'color') { $('#colorpicker').slideDown(); } else { $('#colorpicker').slideUp(); }
		if ($(this).attr('value') == 'pattern') { $('#settings .patterns').slideDown(); } else { $('#settings .patterns').slideUp(); }
	});
	$('#settings input').change(function () {
		$('#supersized').css('display', 'none');
		$('body').addClass($(this).attr('value'));
		if ($(this).attr('value') == 'slideshow') $('#supersized').css('display', 'block');
		if ($('#settings input[type=checkbox]').is(':checked')) {
			$('body').addClass('gradient');
		} else {
			$('body').removeClass('gradient');
		}
	});
	$('#colorpicker').ColorPicker({
		flat: true,
		color: '#eeeeee',
		onChange: function(hsb, hex, rgb) {
			$('body').css('background-color', '#' + hex);
		}
	});
	$('body').append('<link rel="stylesheet" href="colorpicker/css/colorpicker.css">');
	$('#settings .patterns a').each(function () {
		$(this).css('background-image', 'url("patterns/' + $(this).attr('data-pattern') + '")');
	});
	$('#settings .patterns a').click(function () {
		$('body').css('background-image', 'url("patterns/' + $(this).attr('data-pattern') + '")');
	});
	
});
