/* Author: 

*/



//--DISPLAY RANDOM
	this.randomTestimonial = function(){
		var pause = 3000; // define the pause for each tip (in milliseconds) Feel free to make the pause longer so users can have time to read the tips :)
		var length = $("#testimonials li").length; 
		var temp = -1;		
	
		this.getRan = function(){
			// get the random number
			var ran = Math.floor(Math.random()*length) + 1;
			return ran;
		};
		this.show = function(){
			var ran = getRan();
			// to avoid repeating tips we need to check 
			while (ran == temp){
				ran = getRan();
			}; 
			temp = ran;
			$("#testimonials li").hide();	
			$("#testimonials li:nth-child(" + ran + ")").fadeIn("fast");		
		};
		// initiate the script and also set an interval
		show(); //setInterval(show,pause);
	};
	

//--LOAD RANDOM SAYING
	$(document).ready(function(){
		if ($('#testimonials').length) {
			randomTestimonial();
		};
	});



//--NIVO SLIDER
	$(".slider").livequery( function(){	 // when slider is created
	//$(window).load(function() {
		// add caption and rel attribute to gallery images for thumbnails
		$('.slider img').each(function(){
		    var src = $(this).attr('src'),
		    	caption = $(this).next('.caption').text();
		    $(this).next('.caption').remove();
		    $(this).attr('rel',src);
		    if (caption) {
		    	$(this).attr('title',$.trim(caption));
		    } else {
		    	$(this).removeAttr('title');
		    }
		});
		// activate slider
		$(this).nivoSlider({
		//$(".slider").nivoSlider({
	        effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
	        captionOpacity:1.0,
	        directionNavHide:false,
	        animSpeed:200, // Slide transition speed
	        //controlNavThumbs:true, // Use thumbnails for Control Nav
	        //controlNavThumbsFromRel:true, // Use image rel for thumbs
	        manualAdvance:true, // Force manual transitions
	        afterLoad: function(){ // Triggers when slider has loaded
	        	$('.nivo-control').last().addClass('last');
	        	// hide prev/next controls when hovering over thumbnails
	        	$('.nivo-control').hover(function() {
	        		$('.nivo-directionNav a').addClass('hide');
	        	}, function() {
	        		$('.nivo-directionNav a').removeClass('hide');
	        	});
	        }
	    });
	});
	
	
	$("#featured-slider").livequery( function(){	 // when slider is created
	//$(window).load(function() {

		// activate slider
		$(this).nivoSlider({
		//$(".slider").nivoSlider({
	        effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
	        captionOpacity:1.0,
	       	pauseTime:5000,
	       	directionNavHide:false,
	        animSpeed:200, // Slide transition speed
	        directionNav:false,
	        pauseOnHover:false,
	        afterLoad: function(){ // Triggers when slider has loaded
	        	$("#featured-slider").append("<div id='slider-mask'></div>");
	        
	        	$('.nivo-control').last().addClass('last');
	        	// hide prev/next controls when hovering over thumbnails
	        	$('.nivo-control').hover(function() {
	        		$('.nivo-directionNav a').addClass('hide');
	        	}, function() {
	        		$('.nivo-directionNav a').removeClass('hide');
	        	});
	        }
	    });
	});



//--AJAX POST LOADING - http://www.deluxeblogtips.com/2010/05/how-to-ajaxify-wordpress-theme.html
	jQuery(document).ready(function($) { 
	    
	    $(document).delegate("a.ajax-tab", "click", function() {
	        var sectionContainer = "#" + $(this).attr("id") + "-container";
	        
	        $(".active-tab").removeClass("active-tab");
	        $(this).addClass("active-tab");
	        
	        $(".portfolio-items-container").each(function(){
	        	$(this).addClass("hidden");
			});
	        
			$(sectionContainer).removeClass("hidden");
			
			if (!($(sectionContainer).hasClass("loaded"))) {
							
				$(sectionContainer).find(".portfolio-project-container").each(function(){
					var url = $(this).attr("data-url") + " #content";

					$(this).html("<div id='ajax-loader'></div>")
		    		$(this).load(url, function() { 
			    		$(this)
			    			.hide()
			    			.height("auto") //reset height
			        		.animate({opacity: "toggle"});
			        	$(sectionContainer).addClass("loaded");	
			    	});
				});

			}
	        return false;
	    }); 
	    
	    	    
	});
	


//--SCROLL TO TOP
//--http://davidwalsh.name/jquery-top-link
	jQuery.fn.topLink = function(settings) {
	  settings = jQuery.extend({
	    min: 1,
	    fadeSpeed: 200
	  }, settings);
	  return this.each(function() {
	    //listen for scroll
	    var el = $(this);
	    el.hide(); //in case the user forgot
	    $(window).scroll(function() {
	      if($(window).scrollTop() >= settings.min)
	      {
	        el.fadeIn(settings.fadeSpeed);
	      }
	      else
	      {
	        el.fadeOut(settings.fadeSpeed);
	      }
	    });
	  });
	};
	
	//usage w/ smoothscroll
	$(document).ready(function() {
	  //set the link
	  $('#top-link').topLink({
	    min: 400,
	    fadeSpeed: 500
	  });
	  //smoothscroll
	  $('#top-link').click(function(e) {
	    e.preventDefault();
	    $.scrollTo(0,300);
	  });
	});
