// Max character length
var maxLength = 500;

$(function(){

	// Portfolio

	$('.project').each(function(){
		var id = $(this).attr('id');
		// Hide Gallery
		$('#' + id + ' .thumbList').hide();
		// Toggle button action
		$('#' + id + ' .switch').click(function(){
			if( $(this).hasClass('on') ){
				// Show thumbs
				$(this).removeClass('on');
				$('#' + id + ' .switch .toggle').text('Show');
				$('#' + id + ' .thumbList').slideUp(200);
			} else {
				// Hide thumbs
				$(this).addClass('on');
				$('#' + id + ' .switch .toggle').text('Hide');
				$('#' + id + ' .thumbList').slideDown(200);
			}
			return false;
		});
		// switch video swf to href
		var thumbAlt = $('#' + id + ' .mainimage').attr('alt');
		if( thumbAlt != '' ){
			$('#' + id + ' .mainimage').attr('href',thumbAlt);
		}
	});	
	
	// Character Count
	
	$('#message').keyup(function(event) {
		var spaceLeft = maxLength - $(this).val().length;
		if ( spaceLeft < 0 ){
			spaceLeft = 0;
		}
		if ( $('#charCount').length ) {
			$('#charCount').html(spaceLeft);
		}
		if ( $(this).val().length > (maxLength -1) ) {
			event.preventDefault();
			$(this).val( $(this).val().substr(0,maxLength) );
		}
	});
	
	// reset count if value is not blank
	if( $('#message').length && $('#message').val().length > 0 ){
		$('#charCount').html( maxLength - $('#message').val().length );
	}
	
	// Gallery Thumbs
	
	if( $('.thumb').length ){
		Shadowbox.init({
			//handleOversize: "drag",
			modal: true,
			overlayOpacity: 0.7,
			animateFade: false,
			resizeDuration: 0.6,
			displayNav: false,
			displayCounter: false,
			flashParams: { wmode: 'transparent' },
			onOpen: function(){
				// add close button
				$('#sb-body').prepend('<div id="sb-close">Close</div>');
				$('#sb-close').click(function(){
					Shadowbox.close()
				});    		
			},
			onChange: function(){
				// remove buttons
				$('#sb-hoverleft, #sb-hoverright').remove();
			},
			onClose: function(){
				// remove buttons
				$('#sb-close, #sb-hoverleft, #sb-hoverright').remove();
			},
			onFinish : function(element){
				// place next buttons
				// add left and right buttons
				if(Shadowbox.current!=0){
					$('#sb-body').prepend('<div id="sb-hoverleft"><div id="sb-prev"></div></div>');
					$('#sb-hoverleft').click(function(){
						Shadowbox.previous();
					});
				}
				if(Shadowbox.hasNext()){
					$('#sb-body').prepend('<div id="sb-hoverright"><div id="sb-next"></div></div>');	
					$('#sb-hoverright').click(function(){
						Shadowbox.next();
					});
				}
			}
		});	
	}
    
});

// Contact form

function checkContact() {
	if( !$('#name').val().length || !$('#email').val().length || !$('#message').val().length ){
		$('.infoBox div').html('Please complete the form.').animate({opacity: 0},0);
		$('.infoBox').slideDown(300,function(){
			$('.infoBox div').animate({opacity: 1},300);
		});
		return false;
	}
}
