(function($){

	 //list-nav paging behaviour
	 $.fn.listnav = function(){
		$('#paging').slideDown(500);
		$('#main').find('.entry:first').siblings().hide();
		$('#paging li a').click(function(el){
			var dad = $(this).parent();
			var i = $(dad).parent().children().index(dad)+1;
			if ($('.entry:nth-child('+i+')').is(':hidden')){
				$('#main').find('.entry').slideUp(300).end().find('.entry:nth-child('+i+')').slideDown(300);
			}
			return false;
		});
	}
	
	//fade-in backgrounds on link hover/focus
	
	$.fn.hiHover = function(){
		$(this)
		.bind('mouseover focus', function(){
			//$(this).css({ 'background-color': 'd70000', color: '#ffffff' }, 400);
			$(this).stop().animate({ backgroundColor: '#d70000', color: '#ffffff' }, 200); 
		})
		.bind('mouseout blur', function(){
			//$(this).css({ 'background-color': 'transparent', color: '#d70000' }, 600);
			$(this).stop().animate({ backgroundColor: '#ffffff', color: '#d70000' }, 400); 
		});
	}
	
	//hover for the logo, faster than the others as the changing font looks dodgy.
	$.fn.fastHover = function(){
		$(this)
		.bind('mouseover focus', function(){
			$(this).stop().animate({ backgroundColor: '#d70000', color: '#ffffff'  }, 100); 
		})
		.bind('mouseout blur', function(){
			$(this).stop().animate({ backgroundColor: '#ffffff', color: '#000000' }, 100); 
		});
	}
	
	//topnav hover: background slide down
	$.fn.dropHover = function(){
		$(this)
		.css( {backgroundPosition: '0px -200px'} )
		.bind('mouseover focus', function(){
			$(this).stop().animate({ backgroundPosition: '0px 0px', color: '#ffffff' }, {queue:true, duration:500, easing: 'easeInOutQuad'})
		})
		.bind('mouseout blur', function(){
			$(this).stop().animate({ backgroundPosition: '0px -200px', color: '#d70000'  }, {queue:true, duration:500, easing: 'easeInOutQuad'})
		});
	}
	
	//bring to the fore elements that are faded out
	//fadeIn, fadeOut possible, but fadeTo = better. Guess animate does equivalent
	$.fn.fadeIn = function(){
		$(this)
		.bind('mouseover focus', function(){
			$(this).stop().animate({ opacity: '1' }, 300);
		})
		.bind('mouseout blur', function(){
			$(this).stop().animate({ opacity: '0.5' }, 1000);
		});
	}
	
	//"switch off" links with js
	$.fn.deLink = function(){
		$(this).click(function(){ return false});
	}
	
	//last.fm albums
    $.fn.lastfm = function(){
        var options = {
            url: 'http://lastfm-api-ext.appspot.com/2.0/?callback=?&outtype=js&callback=?&period=3month',
            key: '4fd5eeee451cffadc065a642aeac5ec3',
            lfm_target: '#lastfm',
            user: 'mynciboi',
            method: 'user.getTopAlbums',
            limit: 11 //NB total minus 1
        };
        
        var url = options.url + '&method=' + options.method + '&user=' + options.user + '&api_key=' + options.key;
        $('<img />').attr('class', 'loading').attr('src', '/images/layout/loading.gif').appendTo(options.lfm_target);
		
        $.getJSON(url, function (data){
			var lfm_html = '<h3><a href="http://www.last.fm/user/' + options.user + '"  title="Go to my last.fm page">lastfm</a></h3>';
			$(data.topalbums).each(function(i, item){
				lfm_html +='<a class="image-container" href="http://www.last.fm/music/' + item.artist.name/*.replace(/ /, '+')*/ + '/_/' + item.name/*.replace(/ /, '+')*/ + '" title="' + item.name + ' by ' + item.artist.name + '" style="background: #eee url(' + item.image_medium + ') no-repeat center ;"></a>';
				if (i == options.limit) {return false};
			});
			$(options.lfm_target).empty();
			$(lfm_html).appendTo(options.lfm_target);
			$('#landing h3 a').hiHover();
			$('a.image-container').fadeIn();
        });
    };
	
	//flickrpics
    $.fn.flickr = function(){
        var options = {
            url: 'http://api.flickr.com/services/rest/?&method=flickr.people.getPublicPhotos&api_key=',
            apiKey: '8789f8c903345e66a885ae2c77f9eb7f',
            flktarget: '#flickr',
            user: '10039316@N00',
            limit: 12
        };
	
		var flikr_url = options.url + options.apiKey + '&user_id=' + options.user +'&per_page='+ options.limit +'&page=1&format=json&jsoncallback=?';
        $('<img />').attr('class', 'loading').attr('src', '/images/layout/loading.gif').appendTo(options.flktarget);
		
        $.getJSON(flikr_url, function (data){
            var flk_html = '<h3><a href="http://www.flickr.com/photos/mynciboi/" title="Go to my photostream">flickr</a></h3>';  
			$(data.photos.photo).each(function (i, item){
				flk_html += '<a class="image-container" title="' + item.title + '" style="background: url(http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '_s.jpg)  no-repeat center ;" href="http://www.flickr.com/photos/' + item.owner + '/' + item.id + '"></a>';
			});
            $(options.flktarget).empty();
            $(flk_html).appendTo(options.flktarget)
			$('#landing h3 a').hiHover();
			$('a.image-container').fadeIn();
        });
    };
	
	// landingpage tips function
	// $('#hintplace')stop().delay(500).fadeTo("fast", 0).remove();
	// $('#hintplace').load('page.html #hintId')
	$.fn.handyHint = function(target){
		this.mouseover(function(){
	//alert(this.class);
			$('#intro p.hide').stop().animate({ opacity: '0' }, 300)
			$('#intro p.' + target).stop().animate({ opacity: '1' }, 300)
		})
		this.mouseout(function(){
			$('#intro p.hide').stop().animate({ opacity: '0' }, 300)
		})
	};
	
	// thumbs filter function
	$.fn.thumbshow = function(){
	//TODO:
	//add a "close button" that does the same as clicking on the category tagged with .current
	//add an "all" button
	//add functionality for "alt" (or title?) to == text for individual images.
	
	//This finds the title, the first image src and the categories from each blog post and put's them all in a thumbnail.
	$('.entry').css('display', 'none').css('position', 'absolute').each(function(i) {
			var title = $(this).find('h3').text();
			var img = $(this).find('img:first').attr('src');
			var cats = $(this).find('ul').text();
			$(this).after('<div class="thumb' + cats + '"><img src="' + img + '" alt="" /><p>' + title + '</p></div>');
		});
	$('.thumb p').css('left' , '-169px');
	$('.entry p').css('left' , '-278px');
	
	$('.entry').bind('mouseover focus', function(){
		$(this).find('p').stop().animate({ left: '0px'}, {queue:false, duration:400, easing: 'easeOutBack'});
		$(this).find('.date').stop().animate({ right: '0px'}, {queue:false, duration:400, easing: 'easeOutBack'});

		})
	$('.entry').bind('mouseout blur', function(){
		$(this).find('p').animate({ left: '-278px'}, {queue:true, duration:400, easing: 'easeInBack'});
		$(this).find('.date').animate({ right: '-77px' }, {queue:true, duration:400, easing: 'easeInBack'});
		}); 
		
	$('.thumb').bind('mouseover focus', function(){
		$(this).find('p').stop().animate({ left: '0px'}, {queue:false, duration:400, easing: 'easeOutBack'});
		})
	$('.thumb').bind('mouseout blur', function(){
		$(this).find('p').animate({ left: '-169px'}, {queue:false, duration:600, easing: 'easeInBack'});
		}); 
		
		
	$('ul#nav_categories a').click(function() {
		$('.entry').hide();
		$('ul#nav_categories .current').removeClass('current');
		$(this).parent().addClass('current');
		var filterVal = $(this).text();
		$('.thumb').each(function() {
			if(!$(this).is(':animated')) {$(this).delay(400)}
			if(!$(this).hasClass(filterVal) && $(this).is(':visible')) {$(this).toggle(400)}
			if($(this).hasClass(filterVal) && !$(this).is(':visible')) {$(this).toggle(400)}
			});
		return false;
		});	
	$('.thumb').click(function() {
		$(this).prev().slideDown(600);
		$('#work .thumb').fadeOut();
		return false;
		});	
	};
	
	// carousel initial ready function
	$.fn.slideshow = function(){
		var slideWidth = 634;
		
		$('#work .entry').each(function(i) {
			$('.slides').css('overflow', 'hidden');
			var numberOfSlides = $(this).find('.slides img').length;
			$(this).attr('rel', i).prepend('<span class="left">&lt;</span>').append('<span class="right">&gt;</span>');
			$(this).find('.slides img').css({'float' : 'left','width': slideWidth}).wrapAll('<div class="slideInner"></div>');
			$(this).find('.slideInner').css('width', slideWidth * numberOfSlides);
			$('#work .entry .left').hide()
			if(numberOfSlides <= 1){
				$(this).find('.right').hide()
			}
			});

	  $('#work .entry .left, #work .entry .right').live('click', function() {
			var relNo = $(this).parents('.entry').attr('rel');
			if (!$('#work .entry[rel="' + relNo + '"] .slideInner').is(':animated')) {$(this).slider(relNo,slideWidth)};
		});	
	};
	
	// carousel move function
	$.fn.slider = function(relNo,windowWidth){
		var imageCount = $('#work .entry[rel="' + relNo + '"]').find('.slides img').size();
		var slideOffset = parseInt($('.entry[rel="' + relNo + '"] .slideInner').css('marginLeft'), 10);
		if($(this).is('.left')) {
		$(this).stop().animate({ left: '-60px'}, {queue:true, duration:200, easing: 'easeInOutQuad'}).animate({ left: '-50px' }, {queue:true, duration:200, easing: 'easeInOutQuad'});
			if(slideOffset >= (windowWidth * -1)) {$(this).hide()}
			var delta = slideOffset + windowWidth;
			$(this).siblings('.right').show()
			$('#work .entry[rel="' + relNo + '"] .slideInner').animate({'marginLeft': delta}, {queue:true, duration:500, easing: 'easeInOutQuad'})
		} else {
				$(this).stop().animate({ right: '-60px'}, {queue:true, duration:200, easing: 'easeInOutQuad'}).animate({ right: '-50px' }, {queue:true, duration:200, easing: 'easeInOutQuad'});
			if(slideOffset <= (imageCount - 2) * (windowWidth * -1)) {$(this).hide()}
			var delta = slideOffset - windowWidth;
			$(this).siblings('.left').show()
			$('#work .entry[rel="' + relNo + '"] .slideInner').animate({'marginLeft': delta}, {queue:true, duration:500, easing: 'easeInOutQuad'})
		};
	};
	

	
})(jQuery);

	//var test = $(this).attr('class');
	//alert(test);

 $(document).ready(function(){ 
	if ($('#paging').length){$('body').listnav()};
	if ($('#work').length){$('body').slideshow()};
	if ($('#work').length){$('body').thumbshow()};
	if ($('#flickr').length){$('#flickr').flickr()};
	if ($('#lastfm').length){$('#lastfm').lastfm()};
	
	$('p a, h3 a, #footer a, #subnav a, #twitter a, #landing h3 a').hiHover();
	$('.logo a').fastHover();
	$('#nav a').dropHover();
	$('#landing .tolanding, #blogpage .toblog, #info .toinfo, #work .towork, #cv .tocv').deLink();
	
	$('#landing #nav .toblog').handyHint('w-blog');
	$('#landing #nav .toinfo').handyHint('w-info');
	$('#landing #nav .towork').handyHint('w-work');
	$('#landing #nav .tocv').handyHint('w-cv');
	$('#lastfm').handyHint('w-lastfm');
	$('#flickr').handyHint('w-flickr');
	$('#twitter').handyHint('w-twitter');
});
