$(document).ready(function() {
	//menu show-hide
	$('a#menu').click(function(){
		$('#mainNav').fadeIn(300);
	});
	
	$('a#close').click(function(){
		$('#mainNav').fadeOut(300);
	});
	
	//Home carousel
	$('#homeshow').cycle({
		fx:      'fade',
		//prevent grey background in IE
		cleartypeNoBg: true,
		timeout:  4500,
		speed:	 1250
	});

	//Portfolio carousel
	
// Single Story pagination / Cycle Plugin
// -----------------------------------------------------------------------------------------------------

function getStartingSlide(divToGrab){
    // jQuery index function returns -1
    // if it doesn't find <div>
    if ( $(divToGrab).index() !== -1 )
    {
        return $(divToGrab).index();
    } else {
        // Without this validation stories with unknown or incorrect
        // URLs would break the javascript. This way the first story
        // (index 0) is shown if the inputted one isn't found
        return 0;
    }
}

var hash = window.location.hash;
var myStartSlide = getStartingSlide(hash);
var lastSlide = 0;
var initialLoad = 1;
var currentSlide = 0;

// Actual Cycle logic (with number grabbed from url)
$('#projects').cycle({
    before: function(curr,next,opts) {
    	//get vertical scroll position
    	position = $(window).scrollTop();
    },
    fx:     'scrollHorz',
    timeout: 0,
    speed: 600,
    next: '.next',
    prev: '.prev',
    startingSlide: myStartSlide,    // Custom function which will look for #ID of slide based off of url paramter and return number
    after: function(curr,next,opts) {
        // Change the url to show the new (url-friendly) story title + beneficiary
        window.location.hash = next.id;

        //return window to previous position to prevent jumping
        $(window).scrollTop(position);
                        
        // For Firefox, update link to favicon after window.location
        // from http://kilianvalkhof.com/2010/javascript/the-case-of-the-disappearing-favicon/
          var link = $('link[type=image/ico]').remove().attr("href");
          $('<link href="'+ link +'" rel="shortcut icon" type="image/ico" />').appendTo('head');
        
		// Also nice to change window title, so we grab current one
		var currentTitle = document.title;

		//Grab the string after the hash to identify the current slide
		var currentProject = window.location.hash;
		// Remove everything to the left of ' | ' (in case that needs to change later on)
        currentTitle = currentTitle.substring(currentTitle.indexOf(' | '));

		// Grab the title from the current slide, and prepend that to window title
		var projectTitle = $(currentProject+' p.hidden').text();
		document.title = projectTitle + currentTitle;

     }
    
});

	//Testimoial carousel
	$('#testimonials').cycle({
		fx:      'scrollHorz',
		timeout:  0,
		prev:    '.prev',
		next:    '.next',
		speed:	 600,
		timeout:  0,
		pager:	 '#pager'
	});

	//Make portfolio hub li's clickable and follow the link within
	$("#content .category").click(function(){
		window.location=$(this).find("a").attr("href"); return false;
	});
	
	//Maralyn's Media pagination
	$("#articleList").pagination();

});


