$(document).ready(function() {

	//set and resize the #frame to be the same dimensions as the browser window
	
//	$('#frame').width(window.innerWidth).height(window.innerHeight);
		
//	$(window).resize(function() {
//		$('#frame').width(window.innerWidth).height(window.innerHeight);
//		});
	
	$('a').not('.anythingslider').addClass('clickable');
	
	//sets current navigation on page load/refresh

	var	hash = window.location.hash;
	
	function setCurrent(){
	if (hash == '#blog'){$('#nav li.blog').addClass('current')}
	else if(hash == '#contact'){$('#nav li.contact').addClass('current')}
	else if(hash == '#projects'){$('#nav li.projects').addClass('current')}
	else if(hash == '#about'){$('#nav li.about').addClass('current')}
	else{$('#nav li.home').addClass('current')}
	};

	setCurrent();
	
	//general function to scroll to proper station, update current class & hash
	
	function goTrain(station){
	if (station == 'blog'){
		$('.current').removeClass('current');
		$('#frame').scrollTo(
			{top:'0px', left:'12000px'},
			1500,
			{easing:'easeInOutExpo',
			onAfter:function(){
				window.location.hash = 'blog';
				$('#nav li.blog').addClass('current');
			}});
		return false;
	}
	else if (station == 'contact'){
		$('.current').removeClass('current');
		$('#frame').scrollTo(
			{top:'0px', left:'9000px'},
			1500,
			{easing:'easeInOutExpo',
			onAfter:function(){
				window.location.hash = 'contact';
				$('#nav li.contact').addClass('current');
			}});
		return false;
	}
	else if (station == 'projects'){
		$('.current').removeClass('current');
		$('#frame').scrollTo(
			{top:'0px', left:'6000px'},
			1500,
			{easing:'easeInOutExpo',
			onAfter:function(){
				window.location.hash = 'projects';
				$('#nav li.projects').addClass('current');
			}});
		return false;
	}
	else if (station == 'about'){
		$('.current').removeClass('current');
		$('#frame').scrollTo(
			{top:'0px', left:'3000px'},
			1500,
			{easing:'easeInOutExpo',
			onAfter:function(){
				window.location.hash = 'about';
				$('#nav li.about').addClass('current');
			}});
		return false;
	}
	else if (station == 'home'){
		$('.current').removeClass('current'); 			//remove current class
		$('#frame').scrollTo(
			{top:'0px', left:'0px'},					//scroll to absolute position
			1500,										//duration
			{easing:'easeInOutExpo',					//easing
			onAfter:function(){
				window.location.hash = 'home';			//set hash
				$('#nav li.home').addClass('current');	//set new current class for navigation
			}});
	}
	};
	
	//get links that link to the different stations

	var $homeLink = $("a[href^='#home']"),
		$aboutLink = $("a[href^='#about']"),
		$projectsLink = $("a[href^='#projects']"),
		$contactLink = $("a[href^='#contact']"),
		$blogLink = $("a[href^='#blog']");
	
	//apply goTrain function to station links (above)
		
	$homeLink.click(function(){goTrain('home');return false;});
	$aboutLink.click(function(){goTrain('about');return false;});
	$projectsLink.click(function(){goTrain('projects');return false;});
	$contactLink.click(function(){goTrain('contact');return false;});
	$blogLink.click(function(){goTrain('blog');return false;});

	//keyboard navigation
	
	$(document).keyup(function(e) {
		switch(e.keyCode)
		{ 
         	case 39:							// right arrow
            navigate('right'); return false;
         	break;
         	case 37:							// left arrow
            navigate('left'); return false;
         	break;
		}
		switch(e.which)
		{
         	case 49:							// [1] key
            goTrain('home'); return false;
        	break;
         	case 50:							// [2] key
            goTrain('about'); return false;
        	break;
         	case 51:							// [3] key
            goTrain('projects'); return false;
        	break;
         	case 52:							// [4] key
            goTrain('contact'); return false;
        	break;
         	case 53:							// [5] key
            goTrain('blog'); return false;
        	break;
         	case 32:							// [space] key
            navigate('loop'); return false;
         	break;
		}
	});
	
	//keyboard scroll function, activates hover on navigation arrows for visual feedback
	
	function navigate(direction){
	if (direction == 'right'){
		$('ul.arrow-nav').find('li.next').addClass('hover').animate({opacity:1},300, function(){$('li.hover').removeClass('hover')});
		if (window.location.hash == '#home'){goTrain('about')}
		else if(window.location.hash == '#about'){goTrain('projects')}
		else if(window.location.hash == '#projects'){goTrain('contact')}
		else if(window.location.hash == '#contact'){goTrain('blog')}
		else if(window.location.hash == '#blog'){}
		else {goTrain('about')}
	}
	else if (direction == 'left'){
		$('ul.arrow-nav').find('li.prev').addClass('hover').animate({opacity:1},300, function(){$('li.hover').removeClass('hover')});
		if (window.location.hash == '#blog'){goTrain('contact')}
		else if(window.location.hash == '#contact'){goTrain('projects')}
		else if(window.location.hash == '#projects'){goTrain('about')}
		else if(window.location.hash == '#about'){goTrain('home')}
		else if(window.location.hash == '#home'){}
	}
	else if (direction == 'loop'){
		$('ul.arrow-nav').find('li.next').addClass('hover').animate({opacity:1},300, function(){$('li.hover').removeClass('hover')});
		if (window.location.hash == '#home'){goTrain('about')}
		else if(window.location.hash == '#about'){goTrain('projects')}
		else if(window.location.hash == '#projects'){goTrain('contact')}
		else if(window.location.hash == '#contact'){goTrain('blog')}
		else if(window.location.hash == '#blog'){goTrain('home')}
		else{goTrain('about')}
	}
	};

	//mousewheel
	
	//$('#frame').mousewheel(function(event, delta) {
    //console.log(delta);
	//});
	
	//$('#frame').mousewheel(function(event, delta) {
   	//	if (delta > 0){navigate('right');unmousewheel(event, delta)}
   	//	else if (delta < 0){navigate('left')}
	//});
	
	//Twitter feeds for contact page, added to contact list
	
	(function($){
	$.fn.lastTwitterMessage = function(username){
	var $base = this;
	if(!username || username == "") return this; // username required
	var url = "http://twitter.com/statuses/user_timeline.json?callback=?";
	$.getJSON( url, { count: 10, screen_name: username },
	function(data){
	if(data && data.length >= 1){
	try{
	var item = null;
	for(var i = 0; i < data.length; i++){
	if(/^@/i.test(data[i].text)) continue;
	item = data[i]; break;
	}
	if(!item) return;
	var $tweet = $("<p></p> ").text(item.text);
	$tweet.html(
	$tweet.html()
	.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1" class="clickable" target="_blank">$1</a>')
	.replace(/(^|\s)#(\w+)/g,'$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>')
	.replace(/(^|\s)@(\w+)/g,'$1<a href="http://twitter.com/$2">@$2</a>')
	)
	$tweet.append(" &nbsp;<a href='http://twitter.com/" + username + "' class='clickable'  target='_blank'>[&#x21AA;]</a>&nbsp; ").wrapInner("<span>");
	$base.empty().append($tweet).show();
	} catch (e) { };
	};
	});
	return this; // Don't break the chain
	};
	})(jQuery);
	$(function() {
	$("<li class='tweet'></li>").hide().appendTo("ul#david")
	.lastTwitterMessage('Davidthought');
	$("<li class='tweet'></li>").hide().appendTo("ul#tighe")
	.lastTwitterMessage('tighef');
	});
	
	//Projects Slider
	
	function formatText(index, panel) {
		  return index + "";
	    }
    
        $(function () {
        
            $('.anythingSlider').anythingSlider({
                easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
                autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
                delay: 6000,                    // How long between slide transitions in AutoPlay mode
                startStopped: false,            // If autoPlay is on, this can force it to start stopped
                animationTime: 750,             // How long the slide transition takes
                hashTags: false,                 // Should links change the hashtag in the URL?
                buildNavigation: true,          // If true, builds and list of anchor links to link to each slide
        		pauseOnHover: false,             // If true, and autoPlay is enabled, the show will pause on hover
        		startText: "Go",             // Start text
		        stopText: "Stop",               // Stop text
		        navigationFormatter: formatText       // Details at the top of the file on this use (advanced use)
            });
            
            $("#slide-jump").click(function(){
                $('.anythingSlider').anythingSlider(6);
            });
            
        });

});
