
/* Cufon Styling
-----------------------------------------------------------------------------*/

// Apply Cufon styling to h3 headings
Cufon.replace('h3, dt');

/* Home Page Slideshow Callback Functions
-----------------------------------------------------------------------------*/

// Change current slideshow image via dots underneath
function jc_initCallback(carousel) {
	$('.jc_ctrl a').bind('click', function() {
		carousel.scroll($.jcarousel.intval($(this).text()));
		return false;
	});
	
	// Pause slideshow when mouse hovers over image
	carousel.clip.hover(function() {carousel.stopAuto();}, function() {carousel.startAuto();});

	// Pause slideshow when mouse hovers over dots
	$('.jc_ctrl a').hover(function(){carousel.stopAuto();}, function(){carousel.startAuto();});
};

// Highlight dot underneath slideshow when image changes
function jc_itemVisibleInCallback(carousel, item, idx, state) {
	$('a:contains(' + idx + ')').addClass('jc_ctrl_active');
};

// Return dot to normal when image changes to another
function jc_itemVisibleOutCallback(carousel, item, idx, state) {
	$('a:contains(' + idx + ')').removeClass();
};

/* Start processing when document is ready
-----------------------------------------------------------------------------*/

$(document).ready(function(){
	
	/* Home Page Slideshow
	-----------------------------------------------------------------------------*/
	
	// Enable carousel on home page
	$('#feature').jcarousel({
		auto: 5,
		scroll: 1,
		wrap: 'last',
		initCallback: jc_initCallback,
		itemVisibleInCallback:  jc_itemVisibleInCallback,
        itemVisibleOutCallback: jc_itemVisibleOutCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null
	});
	
	/* Previous Work Portfolio Links
	-----------------------------------------------------------------------------*/

	// Portfolio item hover function
	// When either a link within h3 or the frame is hovered over
	$('#work h3 a, #work .frame').hover(
		function(){
			// Expand back to the parent column then drill down to both the h3 link and the frame.
			// Apply the hover class to these two elements
			$(this).parents('.col1, .col2, .col3').find('h3 a, .frame').addClass('hover');
			// Refresh cufon so it picks up the hover class
			Cufon.refresh();
		},
		function(){
			// When mouse leaves the element remove all instances of the hover class on these elements
			$('.frame, h3 a').removeClass('hover');
			// Refresh cufon again
			Cufon.refresh();
		}
	);
		
	/* Contact Form
	-----------------------------------------------------------------------------*/
	
	// Hide the PHP error row when javascript is enabled
	$('.errorrow').hide();
	
	// Set ajax flag to true
	var use_ajax = true;
	
	// Initialise validation settings
	//$.validationEngine.settings={};

	// Enable or disable ajax submission depending on state of form
	$('#frmContact').validationEngine({
		success: function() { use_ajax = true },
		failure: function() { use_ajax = false }
	});
	
	
	$('#frmContact').submit(function(e) {
		if (use_ajax)
		{
			$('#loading').css('visibility', 'visible');
			
			$.post('php/submit.php', $(this).serialize()+'&ajax=1',
				function(){
					$('#frmContact').hide('fast');
					$('.success').css('display', 'block');
			});
		}
		e.preventDefault();
	});

	
	/* External Links
	-----------------------------------------------------------------------------*/
	
	// Find all links starting with http:// 
	// but that don't contain swwd.local (swwd.com.au for live site)
	// then add target _blank and an appropriate title
	$('a[href^="http://"]').not("[href*=swwd.com.au]").attr({
		target: "_blank", 
		title: "Opens in a new window"
	});

});
