$(document).ready(function(){
  $("a.toggle_nav").hover(function(){
    $(this).html('Navigation');
  }, function(){
    $(this).html('');
  });
  
  $("a.toggle_nav").click(function(){
    if ($("#sub_nav").is(':visible'))
		{
      $(this).addClass('down');
      $("#sub_nav").slideUp(1000);
    }
    else
		{
      $(this).removeClass('down');
      $("a.toggle_nav").html('');
      $("#sub_nav").slideDown(1000);
    }
    
		return false;
  });
  
  $("a.toggle_project").click(function(){
    if ($(".featured_project_details").is(":visible"))
		{
      $(".featured_project_info").animate({height:"20px"}, 1000);
      $(".featured_project_details").fadeOut(1000);
    }
    else
		{
      stopSlideshow();
      $(".featured_project_details").fadeIn(1000);
      $(".featured_project_info").animate({height:"79px"}, 1000); 
    }

		return false;
  });
  
  $(".featured_project_thumbs a").click(featuredImageSwap);
  
  $("a.next_project").click(function(){
    stopSlideshow();
    changeFeatured($(this).attr('rel'));
    return false;
  });
  
  startSlideshow();
}); // end document.ready

var slideshow = 0;

function startSlideshow()
{
  slideshow = setInterval(changeFeatured, 15000);
}

function stopSlideshow()
{
  clearInterval(slideshow);
}

function featuredImageSwap()
{
  var image = 'url(\'' + $(this).attr('rel') + '\')';
  
	if (!$(this).hasClass('active'))
	{
    $(".featured_project_thumbs a.active").removeClass('active');
    $(this).addClass('active');
    $(".featured_project").css('background-image', image);
    
    // $("#top").fadeOut('normal', function(){
    //   $("#top").css('background-image', image);
    //   $("#top").fadeIn();
    // });
  }

  return false;
}

function changeFeatured(id)
{
  if (id <= 0 || typeof(id) == 'undefined')
	{
    var id = $("a.next_project").attr('rel');
  }
  
	// generate random number to prevent caching
	var d = new Date();
  var r = d.getTime();
  
	$.getJSON(WWW_ROOT + "home/project/" + id + '?' + r, function(json)
	{
    var image = 'url(\'' + WWW_ROOT + json.Image[0].fullsize + '\')';
    var title = '<strong><span class="bullet">&bull;</span> Featured Project:</strong> ' + json.Project.title;
    var category = json.Project.category;
    var completion = json.Project.completion;
    var thumbs = '';
    var length = Math.min(json.Image.length, 4);

    for (var i = 0; i < length; i++)
		{
      thumbs += '<a href="" rel="' + WWW_ROOT + json.Image[i].fullsize + '"><img src="' + WWW_ROOT + json.Image[i].thumbnail + '" height="74" alt="" /></a>\n';
    }

    $(".featured_project").fadeOut(500, function()
		{
      $(".featured_project_details p.left").slice(0,1).html(category + '<br />' + completion);
      $(".featured_project").fadeIn(500);
      $(".featured_project_thumbs").html(thumbs);
      $("a.toggle_project").html(title);
      $("a.view_project").attr('href', WWW_ROOT + '/portfolio/view/' + json.Project.id);
      $("a.next_project").attr('rel', json.Project.id);
      $(".featured_project").css('background-image', image);
      $(".featured_project_thumbs a").click(featuredImageSwap);

			// Quotes
			if (typeof json.Quote != 'undefined')
			{
				$("#quote_body").html(json.Quote.body);
				$("#quote_section").html(json.Quote.section);
				$("#quote_author").html(json.Quote.author);
				$("#quote_caption").html(json.Quote.caption);
				$("#quote_image").attr('src', WWW_ROOT + 'img/' + json.Quote.photo);
				//console.log(json.Quote);
			}
    });
  });
}
