TOM79 = {
		bgChangeFrequency:12000,
		bgFadeDuration:900,
		bgImageIndex:1,
		bgImageCount:13,
		bgCacheImg:null,
		
		keywordIndex:null,
		wordChangeFrequency:2000
};

$( document).ready( function(){

	//$("#outerContent").hide();
	$("#q").focus();
	//$("#outerContent").dragable();

/*	
	$("#content-toggle").removeAttr('href', '#');
	$("#content-toggle").click( function() {
		var button = $( this);
		if( button.hasClass('minimized') ) {
			$("#card-tabs").slideDown();
			$("#card-tab-content").slideDown( function() {
				button.removeClass('minimized');
				button.html('-');
			});
		} else {
			$("#card-tab-content").slideUp();
			$("#card-tabs").slideUp( function() {
				button.addClass('minimized');
				button.html('+');
			});
		}
		return false;
	});
*/
	
	
	$("#tabs li a").click( function() {
	
		//DISABLED:
		return true;
		
		var menuItem = $( this);
		
		if( menuItem.hasClass("selected") ) {

		} else {
			$("#tabs li a").removeClass("selected");
			menuItem.addClass("selected");

			var contentURL = $( this).attr("href") + 'index_content.php';
			var wireUpFunctionName = 'wireUp_' + $( this).attr("href").replace(/\//g, '');	//remove '/'

			$("#card-tab-content").slideUp( function() {
				$.get( contentURL, function( data) {
					$("#card-tab-content").html( data);
					$("#card-tab-content").slideDown( function() {
						if( $.isFunction( TOM79[ wireUpFunctionName]) ) {
							TOM79[ wireUpFunctionName]();
						}
					});
				});
				
			});
		}
		return false;
	});		
	
	//remove border-right of last tab:
	$("#tabs li a:last").css('border-right', 'none');
	
	
	TOM79.wireUp_contact_me();

	//random background change:
	TOM79.loadNextOffscreen();
	var backgroundTimer = setTimeout( 'TOM79.backgroundChanger();', TOM79.bgChangeFrequency);
	
	
	//var wordTimer = setTimeout( 'TOM79.showNextWord();', TOM79.wordChangeFrequency);
});

TOM79.msg = function( text) {
	alert( text);
};

TOM79.wireUp_contact_me = function() {
	
	$("#email-field").focus();
	
	$("#contact-form").ajaxForm({
		beforeSubmit:function( formData, jqForm, options) {
			var emailField = $("#email-field").val();
			var questionField = $("#question-field").val();
			if( "" === emailField || "" === questionField) {
				TOM79.msg("Hi there, please complete both the Email and Question fields, thanks.");
				return false;
			}
			
			return true;
		},
		success:function( responsePage) {
			
			var responseContents = $( responsePage).find('#contactme-response_content');
			responseContents.hide();
			$("#contact-form").fadeOut( function() {
				$( this).replaceWith( responseContents);
				responseContents.fadeIn();
			});
			
			//TOM79.msg('Thank you - I will get back to you soon.');
			//$("#contact-form").get(0).reset();
		},
		error:function(){
		}
	});	
};

TOM79.backgroundChanger = function() {

	if( TOM79.bgImageIndex >= TOM79.bgImageCount) {
		TOM79.bgImageIndex = 1;
	} else {
		TOM79.bgImageIndex++;
	}
	var nextImage = '/css/images/bg_' + TOM79.bgImageIndex + '.jpg';
	/*
	$('#bg-image').fadeOut( TOM79.bgFadeDuration, function() {
		$('#bg-image img').remove();
		$('#bg-image').append('<img src="' + nextImage + '">');
		$('#bg-image').fadeIn( TOM79.bgFadeDuration);
	});
	*/
	
	//var finalOpacity = 0.90;
	var finalOpacity = 1;
	
	$('#bg-image').animate( { opacity:0}, TOM79.bgFadeDuration, function() {
		$('#bg-image img').remove();
		$('#bg-image').append('<img src="' + nextImage + '">');
		$('#bg-image').animate( { opacity:0 /*do nothing*/ }, 800, function() {
			$('#bg-image').animate( { opacity:1 }, TOM79.bgFadeDuration);
			$('#outerContent').animate( {opacity:finalOpacity}, TOM79.bgFadeDuration);
		});
	});
	$('#outerContent').animate( {opacity:1}, TOM79.bgFadeDuration);
	
	TOM79.loadNextOffscreen();
	
	var backgroundTimer = setTimeout( 'TOM79.backgroundChanger();', TOM79.bgChangeFrequency);
};

TOM79.loadNextOffscreen = function() {
	var nextIndex = 1;
	if( TOM79.bgImageIndex + 1 > TOM79.bgImageCount) {
		nextIndex = 1;
	} else {
		nextIndex = TOM79.bgImageIndex + 1;
	}
	var nextImage = '/css/images/bg_' + nextIndex + '.jpg';
	TOM79.bgCacheImg = new Image();
	TOM79.bgCacheImg.src = nextImage;	//this will fetch the image, so the browser will have it cached...
};


/*
TOM79.showNextWord = function() {
	
	var fadeoutCallback = function() {
		
		//select next word to show:
		var wordCount = $('#words li').size();
		if( null === TOM79.keywordIndex || TOM79.keywordIndex + 1 >= wordCount) {
			TOM79.keywordIndex = 0;
		} else {
			TOM79.keywordIndex++;
		}
		var wordToShow = $('#words li:eq(' + TOM79.keywordIndex + ')');
		
		//show word:
		wordToShow.css('left', Math.random() * 450);	//0 - 10
		wordToShow.css('top', Math.random() * 70); //0 - 10
//		wordToShow.css('left', $('#words').position().left + Math.random() * 450);	//0 - 10
//		wordToShow.css('top', $('#words').position().top + Math.random() * 160); //0 - 10
		wordToShow.fadeIn( 'slow', function() {
			//repeat:
			var wordTimer = setTimeout( 'TOM79.showNextWord();', TOM79.wordChangeFrequency);
		});
	}
	
	//hide all:
	if( $('#words li:visible').size() > 0) {
		$('#words li:visible').fadeOut( 'slow', function() { fadeoutCallback(); });
	} else {
		fadeoutCallback();
	}
	
};
*/
