//Assumed that the Ad Sense javascript has already been loaded, aka
//you must always load http://partner.googleadservices.com/gampad/google_service.js before this file.

//==================================================================
// Purpose: strip GET parameters off the end of a URL.
// Params:  - [curURL] Any HTTP GET URL.
// Returns: Leftmost part of URL split at question mark of GET URL.
//==================================================================
function stripParams(curURL)
{
  if(curURL.indexOf("?") != -1)
    {
      var curTokens = new Array();
      curTokens = curURL.split("?");
      
      return curTokens[0];
    }
  else
    {
      return curURL;
    }
}

//==================================================================
// Purpose: Gets the current Ning major section, aka Mozzle, by
//          parsing it from the URL structure for use in Google
//          Adsense slot name.
// Params:  None.
// Returns: Ning major section or Mozzle.
//==================================================================
function getCurMozzle()
{
  var curTokens = new Array();
  curTokens = document.location.href.split("/");
 	
  switch(curTokens[3])
    {
    case "":        return "main"; break;
    case "main":    return "main"; break;
    case "gifts":
    case "profile": return "profile"; break;
    case "profiles":
 
      if(curTokens[4] == "members")
	{
	  return "profile";
	}
      else if(curTokens[4] == "blog" || curTokens[4] == "blogs")
	{
	  return "blog";
	}
      else 
	{
	  return "main";
	}

      break;

    case "photo":   return "photo"; break;
    case "video":   return "video"; break;
    case "events":  return "events"; break;
    case "group":
    case "groups":  return "groups"; break;
    case "chat":    return "chat"; break;
    case "page":    return "page"; break;
    case "notes":   return "notes"; break;
    case "forum":   return "forum"; break;
    default:        return "main"; break;
    }
} 

//==================================================================
// Purpose: Gets the current Ning minor section, or instance of an
//          entry within a Ning major section for use in creating
//          Google Adsense slots for a specific page in a major
//          section only if that minor section is listed in the 
//          curAdSlots array below.
// Params:  - [curMozzle] Current major section or Mozzle as
//          determined by getMozzle() function.
// Returns: Ning minor section or Mozzle entry.
//==================================================================
function getCurMozzleType(curMozzle)
{
  //This curAdSlots array is used to determine whether the minor section is "general" or a specific entry
  //within a Mozzle for creating targeted adds on that entry page. It is a two dimensional array, where the first
  //dimension consists of all possible returnable Mozzles from getMozzle() and the second dimension is the minor
  //entries for which Google Adsense slots have been created.
  //Note: If you add a new Mozzle section to the first dimension of the array, you MUST define an entry for it in the
  //switch statement to tell it how to parse the URL for the minor section, otherwise it will always return "general".
  var curAdSlots        = ["main", "profile", "blog", "photo", "video", "events", "groups", "chat", "page", "notes", "forum"];
  curAdSlots["main"]    = [];
  curAdSlots["profile"] = [];
  curAdSlots["blog"]    = [];
  curAdSlots["photo"]   = ["vip-game-lounge-april-2009"];
  curAdSlots["video"]   = ["yes-we-did-trailer"];
  curAdSlots["events"]  = [];
  curAdSlots["groups"]  = ["cooltechguys"];
  curAdSlots["chat"]    = [];
  curAdSlots["page"]    = [];
  curAdSlots["notes"]   = [];
  curAdSlots["forum"]   = ["test-1"];
  
  var curTokens = new Array();
   
  curTokens = document.location.href.split("/");
  curTokens.shift();
  curTokens.shift();
  curTokens.shift();
  curTokens.shift();
  
  if(curTokens.length == 0)
    {
      curTokens[0] = "";
    }

  switch(curMozzle)
    {
    case "photo":

      switch(curTokens[0])
	{
	case "albums":
 
	  if(curAdSlots["photo"].join().indexOf(stripParams(curTokens[1])) != -1)
	    {
	      return stripParams(curTokens[1]);
	    }
	  else
	    {
	      return "general";
	    } 

	  break;
	  
	case "":
	case "album":
	case "photo":  return "general"; break;
	default: 

	  if(curAdSlots["photo"].join().indexOf(stripParams(curTokens[0])) != -1)
	    {
	      return stripParams(curTokens[0]);
	    }
	  else
	    {
	      return "general";
	    }
 
	  break;  
	}
      
      break;    
      
    case "video":

      switch(curTokens[0])
	{
	case "":
	case "video": return "general"; break;
	default:
 
	  if(curAdSlots["video"].join().indexOf(stripParams(curTokens[0])) != -1)
	    {
	      return stripParams(curTokens[0]);
	    }
	  else
	    {
	      return "general";
	    }

	  break;
	} 

      break;

    case "groups":

      switch(curTokens[0])
	{
         
	case "": return "general"; break;
	default:
	  if(curAdSlots["groups"].join().indexOf(stripParams(curTokens[0])) != -1)
	    {
	      return stripParams(curTokens[0]);
	    }
	  else
	    {
	      return "general";
	    }
	  
	  break;
	} 

      break;

    case "page":

      switch(curTokens[0])
	{
	case "": 
	case "page": return "general"; break;
	default:
 
	  if(curAdSlots["page"].join().indexOf(stripParams(curTokens[0])) != -1)
	    {
	      return stripParams(curTokens[0]);
	    }
	  else
	    {
	      return "general";
	    }
 
	  break;
	} 
      
      break;

    case "notes":

      switch(curTokens[0])
	{
	case "": 
	case "index": return "general"; break;
	default:
 
	  if(curAdSlots["notes"].join().indexOf(stripParams(curTokens[0])) != -1)
	    {
	      return stripParams(curTokens[0]);
	    }
	  else
	    {
	      return "general";
	    }
 
	  break;
	} 
      
      break;

    case "forum":

      switch(curTokens[0])
	{
	case "topics":
 
	  if(curAdSlots["forum"].join().indexOf(stripParams(curTokens[1])) != -1)
	    {
	      return stripParams(curTokens[1]);
	    }
	  else
	    {
	      return "general";
	    }
	  
	  break;

	case "":
	default: return "general"; break;
	} 
      
      break;

    default: return "general"; break;
    }	
}

//==================================================================
// Purpose: Enables google ads for the current adslot or page with 
//          our public ad key.
// Note:    This function MUST be called by itself in its own 
//          html script tag. If you call this function along with
//          addGoogleAd, fetchGoogleAds, and generateGoogleAd then
//          the ad slot will not load and be created. Ideally, this
//          function would be called once, before those stated above
//          in the page header, but if that is not possible, then
//          call it first for each ad slot.
// Params:  None.
// Returns: None.
//==================================================================
function enableGoogleAds()
{
  GS_googleAddAdSenseService("ca-pub-6263261062029681");
  GS_googleEnableAllServices();
}

var ega = enableGoogleAds;

//==================================================================
// Purpose: Creates a slot for the named Google Adslot using our
//          public ad key.
// Note:    This function MUST be called by itself in its own 
//          html script tag. If you call this function along with
//          enableGoogleAds, fetchGoogleAds, and generateGoogleAd then
//          the ad slot will not load and be created. Ideally, this
//          function would be called once, after enableGoogleAds
//          in the page header, but if that is not possible, then
//          call it second for each ad slot.
// Params:  None.
// Returns: None.
//==================================================================
function addGoogleAd(curSite, curDimension, curAlignment)
{      
  //The curAdSlot defines a Google Adslot name that is to be loaded.
  //The ad slot name consists of 5 major parts:
  //Site name: Identifies the current site
  //Mozzle name: Identifies the Mozzle of the current page
  //Mozzle type: Identifies the minor section or entry for the current Mozzle
  //Dimension: Identifies the dimensions of the ad slot in pixels 
  //Alignment: Identifies the relative position on the page of the ad slot
  var curMozzle = getCurMozzle();
  var curAdSlot = curSite;
  curAdSlot += "_";
  curAdSlot += curMozzle;
  curAdSlot += "_";
  curAdSlot += getCurMozzleType(curMozzle);
  curAdSlot += "_";
  curAdSlot += curDimension;
  curAdSlot += "_";
  curAdSlot += curAlignment;

  GA_googleAddSlot("ca-pub-6263261062029681", curAdSlot);
}

var aga = addGoogleAd;

//==================================================================
// Purpose: Gets the ads for all the defined ad slots.
// Note:    This function MUST be called by itself in its own 
//          html script tag. If you call this function along with
//          enableGoogleAds, addGoogleAd and generateGoogleAd then
//          the ad slot will not load and be created. Ideally, this
//          function would be called once, after all addGoggleAd calls
//          in the page header, but if that is not possible, then
//          call it third for each ad slot.
// Params:  None.
// Returns: None.
//==================================================================
function fetchGoogleAds()
{
  GA_googleFetchAds();
}

var fga = fetchGoogleAds;

//==================================================================
// Purpose: Creates the ad slot div and loads the ad for the ad slot.
// Note:    This function MUST be called by itself in its own 
//          html script tag. If you call this function along with
//          enableGoogleAds, addGoogleAd and fetchGoogleAds then
//          the ad slot will not load and be created. Ideally, this
//          function would be called once, in the place where
//          the ad slot should appear on a page, but if that is not 
//          possible, then call it fourth for each ad slot.
// Params:  None.
// Returns: None.
//==================================================================
function generateGoogleAd(curSite, curDimension, curAlignment)
{
  //The curAdSlot defines a Google Adslot name that is to be loaded.
  //The ad slot name consists of 5 major parts:
  //Site name: Identifies the current site
  //Mozzle name: Identifies the Mozzle of the current page
  //Mozzle type: Identifies the minor section or entry for the current Mozzle
  //Dimension: Identifies the dimensions of the ad slot in pixels 
  //Alignment: Identifies the relative position on the page of the ad slot
  var curMozzle = getCurMozzle();
  var curAdSlot = curSite;
  curAdSlot += "_";	
  curAdSlot += curMozzle;
  curAdSlot += "_";
  curAdSlot += getCurMozzleType(curMozzle);
  curAdSlot += "_";
  curAdSlot += curDimension;
  curAdSlot += "_";
  curAdSlot += curAlignment;

  GA_googleFillSlot(curAdSlot);
}

var gga = generateGoogleAd;
