/**
 * BEGINS: carousel
 */

$.fn.carousel = function () {
	return this.each(function () {
		var $wrapper = $('> div', this).css('overflow', 'hidden'),
			$slider = $wrapper.find('> ul'),
			$items = $slider.find('> li'),
			$single = $items.filter(':first'),
			
			singleWidth = $single.outerWidth(),
			singleHeight = $single.height(),
			currentItem = 1,
			numItems = $items.length;
		
		$slider.css('width', numItems * singleWidth);
		$slider.css('height', singleHeight);
		$wrapper.css('height', singleHeight);
		
		
		if (numItems <= 1) { return; }

		// reset: to first item 
		$wrapper.animate({
			scrollLeft : '-=' + numItems * singleWidth
		}, 1);
		

		// add the previous and next arrows
		$wrapper.before('<a href="#" class="prev">&lt; Previous</a>');
		$wrapper.after('<a href="#" class="next">Next &gt;</a>');
		
		var $next = $('> .next', this),
			$prev = $('> .prev', this),
			distance = Math.ceil(singleHeight/2 - $next.height()/2);
		
		$next.css('marginTop', distance);
		$prev.css('marginTop', distance);
		

		function move(direction)
		{
			if( $wrapper.is(':animated') ) {
				return;
			}
			
			var scrollAmount = singleWidth * direction
				currentItem += direction;

			if (currentItem == 0) {
				// jump to end
				scrollAmount = numItems * singleWidth;
				currentItem = numItems;
			} else if (currentItem > numItems) {
				// jump to beginning
				scrollAmount = -numItems * singleWidth;
				currentItem = 1;
			}

			$wrapper.animate({
				scrollLeft : '+=' + scrollAmount
			}, 600, 'easeOutBack');

		}
		
		$('a.next', this).click(function () {
			move(1);
			return false;
		});

		$('a.prev', this).click(function () {
			move(-1);
			return false;
		});
		
	});
};

// fire after ALL content is loaded! (wait for images)
$(window).load(function () {
	$('#main.work.single .images').carousel();
});

/**
 * END OF carousel
 */


/*		Init		*/
$(function () {

	/* Function definitions ------------------------------------------------- */
    
	$.fn.helpText = function(text) {
		$(this).data('help', text);

		$(this).focus(function() {
			if (this.value == $(this).data('help')) {
				this.value = '';
				$(this).removeClass('help');
			}
		}).blur(function() {
			if (this.value == '') {
				this.value = $(this).data('help');
				$(this).addClass('help');
			}
		});
		
		if (this.value == text) {
			$(this).addClass('help');
		}
		
		return this;
	}
	
	function didSendToPHP(data) {
		var error = false;
		
		if (data['name'] != null) {
			$('input[name="cname"]').after(data['name']);
			error = true;
		}

		if (data['email'] != null) {
			$('input[name="cemail"]').after(data['email']);
			error = true;
		}

		if (data['message'] != null) {
			$('textarea[name="cmessage"]').after(data['message']);
			error = true;
		}
		
		$('.warning').hide();
		$('.warning').fadeIn();

		$('.send').remove();
	
		if (data['status'] == 'success') {
			$('.go').after(data['html_sending']);
			$('#main.contact form').before(data['html_msg']);

			$result = $('div.result');
			$result.css('opacity', '0');


			$result.animate({
				opacity: 1.0
			}, 1000);
			
			
		}
		
	}
	
    
	/* Misc ----------------------------------------------------------------- */
	
	$('#main.contact form').submit(function () {
		$('.warning').fadeOut().remove();
		$('.go').attr('disabled', true);
		$('.send').remove();
		$('.go').after('<span class="send">Sending...</span>')
		
        $.ajax({
            type: "POST",
            url: "http://ajk.im/wordpress/contacted.php",
            data: "ajax=1&" + $(this).serialize(),
			dataType: 'json',
            success: didSendToPHP
        });

		$('.go').attr('disabled', false);
		return false;
	});
    
});

