/* Author: Coordinate

*/

(function($){
	
	$.getJSON('http://twitter.com/status/user_timeline/Picmebooths.json?count=3&callback=?', function(data){
		$.each(data, function(index, item){
			$('#twitter, #twitterSingle').append('<div class="tweet"><p>' + item.text.linkify() + '</p></div>');
			$('#tweet a').attr('target', '_blank');
			$('#twitterSingle .tweet:not(:first)').remove();
		});
	
	});
	
	//Turn tweeted links into links
	String.prototype.linkify = function() {
		return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
			return m.link(m);
		});
	};
		
	$.fn.slider = function() {
		$.inAnimation = false;
		$(window).bind("load", function(){
			if ($('#slideContainer .activeslide').length == 0)
			$('#slideContainer .slide:not(:first)').hide();
			$('#slideContainer .slide:first').addClass('activeslide');
			slideshow_interval = setInterval("nextslide()", 5000);
		});
	};
	
	//Randomise the photo order
	$.fn.shuffle = function() {
		return this.each(function(){
			var items = $(this).children().clone(true);
			return (items.length) ? $(this).html($.shuffle(items)) : this;
		});
	}
	
	$.shuffle = function(arr) {
		for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
		return arr;
	}

	
	$('#slideContainer').shuffle().slider();
	
	$('#main .gallery a').fancybox();
	
	var infoAccount = "info";
	var emailDomain = "picmebooths.com.au"
	
	var infoEmail = infoAccount + '@' + emailDomain;
	$('.infoEmail').html('<a href="mailto:' + infoEmail + '?subject=Message from picmebooths.com.au">' + infoEmail + '</a>');
	$('.picofthemonthEmail').html('<a href="mailto:' + infoEmail + '?subject=Pic of the month submission">Click here</a>');
	$('.employmentEmail').html('<a href="mailto:' + infoEmail + '?subject=Employment enquiry">Employment Opportunities</a>');
	
	
})(jQuery);


//Slideshow Next Slide
	function nextslide() {
		if($.inAnimation) return false;
		else $.inAnimation = true;
	    var currentslide = $('#slideContainer .activeslide');
	    currentslide.removeClass('activeslide');
		
	    if ( currentslide.length == 0 ) currentslide = $('#slideContainer .slide:last');
			
	    var nextslide =  currentslide.next().length ? currentslide.next() : $('#slideContainer .slide:first');
	    var prevslide =  nextslide.prev().length ? nextslide.prev() : $('#slideContainer .slide:last');
		
		$('#slideContainer .prevslide').removeClass('prevslide');
		prevslide.addClass('prevslide')
		prevslide.fadeOut(500);
		
	    nextslide.hide().addClass('activeslide')
	    nextslide.fadeIn(500, function(){$.inAnimation = false;});
	    
	}
	
	function currentLink() {

    //Look through all the links in the sidebar
   $("header nav ul li a").filter(function() {

      //Take the current URL and split it into chunks at each slash
      var currentURL = window.location.toString().split("/");

      //return true if the bit after the last slash is the current page name
      return $(this).attr("href") == currentURL[currentURL.length-1];

    //when the filter function is done, you're left with the links that match.
    }).addClass("current");
 }

currentLink()
