/****************************************************
All functionality related to the buff and debuff stacking + highlighting
****************************************************/

// Called every time a change is made in the raid comp
function generateStacker()
{
	var comp = '';
	
	for(i = 1; i <= 8; i++)
		comp += getComp('group' + i);
	
	$.get("stacker.php", {setup: comp}, function(data) { $('#stacker').html(data); tooltips(); });
      
      enableCatHighlights("#layout .block");
      generateCount(comp);
}

/*************************
Category highlights
*************************/
function enableCatHighlights(selector)
{
      $(selector).hover(function() {
		tmp = ($(this).children().attr("class")).split(' ', 2);
		
		highlightCat(classtokens[tmp[0]][tmp[1]]);
		
	}, function() {
		tmp = ($(this).children().attr("class")).split(' ', 2);
		
		restoreCat(classtokens[tmp[0]][tmp[1]]);
	});
}

// Highlight function for categories
function highlightCat(spectoken)
{
      for(i = 0; i < urltoken2categories[spectoken].length; i++)
      {
            //$('#' + urltoken2categories[spectoken][i] + ' .catlink').css('color', '#41a5dc');
            $('#' + urltoken2categories[spectoken][i]).css({backgroundImage: 'url(img/category_highlight.jpg)', backgroundRepeat: 'repeat'});     
      }
}

// Restore function for categories
function restoreCat(spectoken)
{
      for(i = 0; i < urltoken2categories[spectoken].length; i++)
      {
            //$('#' + urltoken2categories[spectoken][i] + ' .catlink').css('color', '');
            $('#' + urltoken2categories[spectoken][i]).css('backgroundImage', '');  
      }
}

/*************************
Spec highlights
*************************/
function highlightSpec(cat)
{
      t = new Array();
      for(i = 0; i < categorytooltip2providers[cat].length; i++)
      {
            t = categorytooltip2providers[cat][i].split(" ", 2);
            
            $("#classes ." + t[0]).each(function()
            {
                  if($(this).hasClass(t[1]))
                        $(this).parent().css({backgroundImage: 'url(img/baseblock_highlight.jpg)'});
            });
            
            $("#layout ." + t[0]).each(function()
            {
                  if($(this).hasClass(t[1]))
                        $(this).parent().css({backgroundImage: 'url(img/baseblock_highlight.jpg)'});
            });
      }
}
// restore function for specs
function restoreSpec(cat)
{
      t = new Array();
      for(i = 0; i < categorytooltip2providers[cat].length; i++)
      {
            t = categorytooltip2providers[cat][i].split(" ", 2);
            
            $("#classes ." + t[0]).each(function()
            {
                  if($(this).hasClass(t[1]))
                        $(this).parent().css({backgroundImage: 'url(img/baseblock.jpg)'}); 
            });
            
            $("#layout ." + t[0]).each(function()
            {
                  if($(this).hasClass(t[1]))
                        $(this).parent().css({backgroundImage: 'url(img/baseblock.jpg)'}); 
            });
      }
}

/*************************
Tooltips
*************************/
function tooltips()
{
	$("#stacker .tooltipwrapper").hide();
      
      $(".catlink").each(function()
      {
                  $(this).composertip({
                  fluff: 15,
                  top: -10,
                  side: 'left',
                  runArgs: getTipID(this),
                  runOver: function() { highlightSpec(this.runArgs)},
                  runOut: function() { restoreSpec(this.runArgs) }
            });
      });
}

/*************************
Count box
*************************/
function generateCount(comp)
{
      var count = new Array();
      var manatide = 0;
      var rebirth = 0;
      var soulstone = 0;
      var curse = 0;
      var disease = 0;
      var magic = 0;
      var poison = 0;
      
      count['Tank'] = 0;
      count['Healer'] = 0;
      count['Ranged DPS'] = 0;
      count['Melee DPS'] = 0;
          
      // Do all the counting
      for(i = 0; i < comp.length; i++)
      {
            // the character
            c = comp.charAt(i);
            
            // Count roles
            count[urltoken2role[c]]++;
            
            
            // Shamans
            if(c == 'm' || c == 'n' || c == 'o')
            {
                  poison++;
                  disease++;
            }

            // Mages
            if(c == 'a' || c == 'b' || c == 'c')
            {
                  curse++;
            }
            
            // Priests
            if(c == 'g' || c == 'h' || c == 'i')
            {
                  disease++;
                  magic++;
            }

            // Paladins
            if(c == 'd' || c == 'e' || c == 'f')
            {
                  disease++;
                  magic++;
                  poison++;
            }
            
            // Druids
            if(c == '4' || c == '5' || c == '6')
            {
                  rebirth++;
                  curse++;
                  poison++;
            }
                  
            if(c == 'p' || c == 'q' || c == 'r')
                  soulstone++;
            
            
            // Resto shaman
            if(c == 'o')
            {
                  manatide++;
                  curse++;
            }
      }
      
      // Update result
      
      str = '<span>' + count['Tank'] + '</span>&nbsp;Tank';     
      if(count['Tank'] != 1)
            str = str + 's';
            
      $("#tanks").html(str);
      
      
      str = '<span>' + count['Healer'] + '</span>&nbsp;Healer';     
      if(count['Healer'] != 1)
            str = str + 's';
            
      $("#healers").html(str);

      $("#rDPS").html('<span>' + count['Ranged DPS'] + '</span>&nbsp;Ranged DPS');
      $("#mDPS").html('<span>' + count['Melee DPS'] + '</span>&nbsp;Melee DPS');
      
 
       if(manatide == 1)
            tmp = 'Tide';
      else
            tmp = 'Tides';
      
      $("#ManaTide").html('<span>' + manatide + '</span>&nbsp;Mana ' + tmp);
      
       if(rebirth == 1)
            tmp = '';
      else
            tmp = 's';
      
      $("#Rebirth").html('<span>' + rebirth + '</span>&nbsp;Rebirth' + tmp);
      
      if(soulstone == 1)
            tmp = '';
      else
            tmp = 's';
      
      $("#Soulstone").html('<span>' + soulstone + '</span>&nbsp;Soulstone' + tmp);
      
      
      $("#rCurse").html('<span>' + curse + '</span>&nbsp;Remove Curse');
      $("#rMagic").html('<span>' + magic + '</span>&nbsp;Remove Magic');
      $("#rPoison").html('<span>' + poison + '</span>&nbsp;Remove Poison');
      $("#rDisease").html('<span>' + disease + '</span>&nbsp;Remove Disease');
}