/*	
	
	jQuery/JavaScript functions for timknapton.com
	
	Author: 				Tim Knapton
	Initial Release Date: 	25.April 2010
	Latest Revision:		25.April 2010	

*/

var subpage 		= false; //signals if the user is in a subpage to disable menu controls
var portfolioLinks 	= new Array('home','about','noise','contact');
var aboutLinks 		= new Array('home','work','noise','contact');
var links 			= new Array('home','about','work','noise','contact');
var pageLoc 		= 0; //Initial page location, used for tracking the glow-effect in the header's navigation

jQuery(document).ready(function($) {
		
		//Swipe to the desired page using the header's navigation links
		$('.navButton').click(function(){
			if(subpage == false){
				var goToView = $(this).attr("id").charAt(3);
				pageLoc = goToView-1;
				stepcarousel.stepTo('mygallery', goToView);
				for(i = 0; i <= 4; i++){
					$('#nav-'+links[i]).attr("src",'./images/nav-'+links[i]+'.png');
				}
				$('#nav-'+links[goToView-1]).attr("src",'./images/nav-'+links[goToView-1]+'-on.png');
			}
		});
		
		$('#logo').click(function(){
				stepcarousel.stepTo('mygallery', 1);
				for(i = 0; i <= 4; i++){
					$('#nav-'+links[i]).attr("src",'./images/nav-'+links[i]+'.png');
				}
				$('#nav-'+links[0]).attr("src",'./images/nav-'+links[0]+'-on.png');

		});
		
		$('#talkToMe').click(function(){
				pageLoc = 5;
				stepcarousel.stepTo('mygallery', 5);
				for(i = 0; i <= 4; i++){
					$('#nav-'+links[i]).attr("src",'./images/nav-'+links[i]+'.png');
				}
				$('#nav-'+links[4]).attr("src",'./images/nav-'+links[4]+'-on.png');
		});
		
		//Swipe left when #leftArrow is clicked
		$('#leftArrow').click(function(){
			if(subpage == false && $(this).attr("class") == 'enabledArrow'){
				stepcarousel.stepBy('mygallery', -1);
				if(pageLoc == 0){
					$('#leftArrow').attr("class","waitingArrow");
					setTimeout(function(){$('#leftArrow').attr("class","enabledArrow")},1500);
					pageLoc = 4;
				}
				else pageLoc--;
				for(i = 0; i <= 4; i++){
					$('#nav-'+links[i]).attr("src",'./images/nav-'+links[i]+'.png');
				}
				$('#nav-'+links[pageLoc]).attr("src",'./images/nav-'+links[pageLoc]+'-on.png');
			}
		})
		
		
		//Swipe right when #rightArrow is clicked
		$('#rightArrow').click(function(){
			if(subpage == false && $(this).attr("class") == 'enabledArrow'){
				if(pageLoc == 4){
					$('#rightArrow').attr("class","waitingArrow");
					setTimeout(function(){$('#rightArrow').attr("class","enabledArrow")},1500);
					pageLoc = 0;
				}
				else pageLoc++;
				for(i = 0; i <= 4; i++){
					$('#nav-'+links[i]).attr("src",'./images/nav-'+links[i]+'.png');
				}
				$('#nav-'+links[pageLoc]).attr("src",'./images/nav-'+links[pageLoc]+'-on.png');
				stepcarousel.stepBy('mygallery', 1);
			}
		})
		
		
		//Builds Portfolio Rollover Effect
		$('.portfolioPreview').append('<span class="hover"></span>').each(function () {
		  var $span = $('> span.hover', this).css('opacity', 0);
		  $(this).hover(function () {
			$span.stop().fadeTo(500, 1);
		  }, function () {
			$span.stop().fadeTo(500, 0);
		  });
		})
		
		//Fades out portfolio overview, fades in individual entry
		$('.portfolioPreview').click(function(){
			portfolioproject = '#'+$(this).attr("id")+'-full';
			if(portfolioproject != '#more-full'){
				subpage = true;
				$('.navButton').attr('class','navButton-disabled');
				for(i = 0; i <= 3; i++){
					$('#nav-'+portfolioLinks[i]).attr("src",'./images/nav-'+portfolioLinks[i]+'-off.png');
				}
				$('#rightArrow,#leftArrow').fadeOut('fast', function() {
					$('#rightArrow,#leftArrow').attr('class','disabledArrow');
					$('#rightArrow,#leftArrow').fadeIn('fast', function() {
						//alert stuff
					});
				});
				$('#portfolio').fadeOut('slow', function() {
					$(portfolioproject).fadeIn('slow', function() {
						//alert stuff
					});
				});
			}
		})
		
		//Fades out of individual entry and replaces it with portfolio overview
		$('.portfolioBackButton').click(function(){
			subpage = false;
			$('.navButton-disabled').attr('class','navButton');
			for(i = 0; i <= 3; i++){
				$('#nav-'+portfolioLinks[i]).attr("src",'./images/nav-'+portfolioLinks[i]+'.png');
			}
			$('#rightArrow,#leftArrow').fadeOut('fast', function() {									 
				$('#rightArrow,#leftArrow').attr('class','enabledArrow');
				$('#rightArrow,#leftArrow').fadeIn('fast', function() {
							//alert stuff
				});
			});
			$(portfolioproject).fadeOut('slow', function() {
    			$('#portfolio').fadeIn('slow', function() {
				//stuff in here
  				});
  			});
		})
		
		//Fades out the about-me overview and fades in selected page.
		$('.aboutButton').click(function(){
			aboutpage = '#'+$(this).attr("id")+'-full';
			$('.navButton').attr('class','navButton-disabled');
			subpage = true;
			for(i = 0; i <= 3; i++){
				$('#nav-'+aboutLinks[i]).attr("src",'./images/nav-'+aboutLinks[i]+'-off.png');
			}
			$('#rightArrow,#leftArrow').fadeOut('fast', function() {
				$('#rightArrow,#leftArrow').attr('class','disabledArrow');
				$('#rightArrow,#leftArrow').fadeIn('fast', function() {
					//alert stuff
				});
			});
			$('#aboutMe').fadeOut('slow', function() {
    			$(aboutpage).fadeIn('slow', function() {
    				//alert stuff
  				});
  			});
		})
		
		//Fades out either skills or education view and fades in about-me overview
		$('.aboutBackButton').click(function(){
			subpage = false;
			$('.navButton-disabled').attr('class','navButton');
			for(i = 0; i <= 3; i++){
				$('#nav-'+aboutLinks[i]).attr("src",'./images/nav-'+aboutLinks[i]+'.png');
			}
				$('#rightArrow,#leftArrow').fadeOut('fast', function() {									 
				$('#rightArrow,#leftArrow').attr('class','enabledArrow');
				$('#rightArrow,#leftArrow').fadeIn('fast', function() {
							//alert stuff
				});
			});
			$(aboutpage).fadeOut('slow', function() {
    			$('#aboutMe').fadeIn('slow', function() {
				//stuff in here
  				});
  			});
		})
		
		//Finds out which portfolio dot you've clicked and fades in/out the respective image associated with it
		$('.portfolioDot,.selectedDot').click(function(){
			dotsPrefix = $(this).attr("id").substring(0,($(this).attr("id").indexOf('-')));
			toBeVisible = '#'+ dotsPrefix + $(this).attr("id").substr(($(this).attr("id").indexOf('-')+1),1);
      		currentVisible = '#' + $('#'+dotsPrefix+' > *:visible').attr("id");
			if(currentVisible != toBeVisible){
				$("#"+dotsPrefix+"Dots>div").attr("class","portfolioDot");
				$('#'+$(this).attr("id")).attr("class", "selectedDot");
				$(currentVisible).fadeOut('slow', function() {
					$(toBeVisible).fadeIn('slow', function() {
						//alert stuff
					});
				});
			}
		})
		
		//Hover functionality for non-selected portfolio dots
		$('.portfolioDot').hover(function(){
			document.getElementById("#"+$(this).attr("id")).style.backgroundImage='./images/opencircle.png';
		})
		
		//First function: mouse over. Second: mouse out. Changes container div's class to show arrows pointing at the hovered option.
		$('#education').hover(function(){
			document.getElementById("aboutContainer").className='eduhighlight';
		},
		function(){
			document.getElementById("aboutContainer").className='blank';
		})
		$('#skills').hover(function(){
			document.getElementById("aboutContainer").className='skillshighlight';
		},
		function(){
			document.getElementById("aboutContainer").className='blank';
		})
		$('#downloadResume').hover(function(){
			document.getElementById("aboutContainer").className='resumehighlight';
		},
		function(){
			document.getElementById("aboutContainer").className='blank';
		})	
		
		//Sets up "Latest Tweet" on the Noise view.
		$("#tweets").tweet({
			join_text: "auto",
			username: "timknapton",
			avatar_size: 64,
			count: 1,
			auto_join_text_default: 'I said,', 
			auto_join_text_ed: 'I said,',
			auto_join_text_ing: 'I said,',
			auto_join_text_reply: 'I said,',
			auto_join_text_url: 'I said,',
			loading_text: "loading tweets..."
		})
		
		//Handles the contact form mailing/effects
		$('#sendMail').click(function(){
			var error = 0;
			var errorString = '';
			var name = $('#contactname').val();
			var email = $('#contactemail').val();
			var message = $('#contactmessage').val();
			if(name == ''){
				$("#nameLabel").css("color","#ff0000");
				error++;
			} 
			else $("#nameLabel").css("color","#424242");
			if(email == ''){
				$("#emailLabel").css("color","#ff0000");
				error++;
			}
			else $("#emailLabel").css("color","#424242");
			if(message == ''){
				$("#messageLabel").css("color","#ff0000");
				error++;
			}
			else $("#messageLabel").css("color","#424242");
			
			if(error == 0){
				$('#sendMail').fadeOut(500);
				$('#mailLoading').fadeIn(500);
				$('#contactname,#contactemail,#contactmessage').attr("disabled",true);
				var msgData = 'name='+ name + '&email=' + email + '&message=' + message;
				$.ajax({
					type: 'POST',
					url: 'mail.php',
					dataType: 'text',
					data: msgData,
					success: function(){
						$('#mailLoading').fadeOut(500);
						$('#formContainer').fadeOut(500);
						$('#thanks').delay(500).fadeIn(500);
					}
				});
			}
		});
		$('#contactname,#contactemail,#contactmessage').attr("disabled",false);
		
		
});
