// current section of the site
var currentSection = ""

// document ready functions
$(document).ready(function() {  
  // setup the listener
  SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
});


// ************************************************************
// handle the change
// ************************************************************
function handleChange(event) 
{
  swfValue = SWFAddress.getValue();
  
  var str = swfValue.substring(10);  // removes the /projects/ prefix.
  str = str.replace(/-+/g, ' ');     // removes the dashes.

  if (swfValue != "/") 
  {
    getContent(getAspxPage(swfValue), swfValue);
  document.title = "pollen digital - " + str;
  }
}

// ************************************************************
// check if cleanup is required
// ************************************************************
function checkforCleanup() 
{
// clean up if required
}

// ************************************************************
// get the document based on the swf address value
// ************************************************************
function getAspxPage(swfAddressValue) 
{
  result = "/"
  if (swfAddressValue != "/")
    result = swfAddressValue.substring(1,swfAddressValue.length) + ".aspx?noredirect=1";

  return result;
}

function getSwfValueFromPage(aspxUrl)
{
  result = "";
  result = aspxUrl.substr(0, aspxUrl.indexOf(".aspx"));
  return result;
}

// ************************************************************
// get some content based on the swf address
// ************************************************************
function getContent(url,swfValue) 
{
  // show the preloader if you want
  //showPreloader();        
  $.ajax({
    url: url,
    cache: false,
    success: function(html) {

      // clean up before removing
      checkforCleanup();

      //fade the panel out
      $('.noFlash .content').fadeTo(200, 0, function(){
    
      // replace content in the panel
      $("#featurePanelContent").html(html);
        
      // activate only the current project thumb
      thumbAction(swfValue);
        
      //fade the panel back in
      $('.noFlash .content').fadeTo(500, 1);
      })
    },
  error: function() {
      alert("document: " + url + " could not be loaded");
      //hidePreloader();
    }                  
  });
}

function thumbAction(swfValue)
{

  // remove active class from each project thumb
  $('.project').each(function(){
    $(this).removeClass('active');     
  })
  
  // make the current project page have an active thumb
  var currentThumb = $("a[rel='" + swfValue + "']");
  currentThumb.parent().addClass('active');
  
  $('.project').each(function(){
    if($(this).hasClass("active"))
    {
      // animate the elements in the active project thumb
      currentThumb.children('.projectImage').children('.projectImageHover').stop(true,true).fadeTo(500,1);
      currentThumb.children('.projectInfo').stop(true,true).animate({color: "#fff"});
    }
    else
    {
      // deactivate the elements in the active project thumb
      $(this).children('a').children('.projectImage').children('.projectImageHover').stop(true,true).fadeTo(500,.08);
      $(this).children('a').children('.projectInfo').stop(true,true).animate({color: "#393937"});  
    }
  })    
  
}

