/* ============================ Helper functions ============================ */

function extractHash(aURL)
{
  if (aURL)
  {
    /* next line is IE 6 compatibility */
    aURL = aURL.replace(/%23/g, '#');
    var hashPart = aURL.replace(/^([^&#]|&#|&)*[^&]#/, '#');
    if (hashPart.substr(0, 1) == '#')
    {
      return hashPart;
    }
  }
  return '';
}

/* ============================ Main functions ============================ */
function init()
{
  $('.pngfix').ifixpng();
  
  $('#optimizerbackground').click(
    function()
    {
      if (preventValue)
      {
        alert('#prevent:0');
        $('#optimizerslider').animate({left: '0px'}, 300, 'swing');
      }
      else
      {
        alert('#prevent:1');
        $('#optimizerslider').animate({left: '24px'}, 300, 'swing');
      }
      preventValue = !preventValue;
    }
  );
  
  $('#speedupbackground').click(
    function()
    {
      if (speedupValue)
      {
        alert('#speedup:0');
        $('#speedupslider').animate({left: '0px'}, 300, 'swing');
      }
      else
      {
        alert('#speedup:1');
        $('#speedupslider').animate({left: '24px'}, 300, 'swing');
      }
      speedupValue = !speedupValue;
    }
  );
  
  $('.taskbutton').click(
    function(event)
    {
      var clickedElement = $(event.target);
      var taskTarget     = extractHash(clickedElement.attr('href'));
      alert(taskTarget);
      event.preventDefault();
    }
  );
  
  $('.commandLinkButton').each(function(){

        id = $(this).attr('id');
        onclick = $(this).attr('onclick') +  '';
        if (onclick)
            onclick = onclick.replace("function anonymous()", "");
        else
            onclick = "";
   
        iconSrcNormal = $('img', this).attr('src');
        iconSrcHover  = $('img', this).attr('hoverSrc');
        
        if (!iconSrcNormal){
            iconSrcNormal = commandLinkImgDir+'command_link_normal.png';                      
            iconWidth  = '20px';
            iconHeight = '20px';
        }else{
          iconWidth   = $('img', this).get(0).offsetWidth + 'px';
          iconHeight  = $('img', this).get(0).offsetHeight + 'px';        
        }
        if (!iconSrcHover){
            iconSrcHover  = commandLinkImgDir + 'command_link_hover.png';
        }
        
        hintText = $(this).attr('title');        
        if (!hintText)
            hintText = '';
        
        html = '<a href="javascript:void(0);" class="commandLinkContent" tabindex="0" style="cursor:hand;" onclick= "alert(\'' + $(this).attr('cmd') + '\');">'+
               '<table id="' + id + '" cellspacing="0" cellpadding="0"  style="width:100%;"'+
               '     iconSrcNormal="' + iconSrcNormal + '"'+
               '     iconSrcHover= "' + iconSrcHover  + '"'+
	           '     onmouseenter="handleCommandLinkMouseEnter(this);" '+
	           '     onmouseleave="handleCommandLinkMouseLeave(this);"> '+
	           '   <tr>'+
		       '     <td id="' + id + '.iconCont' + '"  class="image" valign="top" width="' + iconWidth + '" style="padding:5px;  filter: progid:DXImageTransform.Microsoft.Fade(duration=0.450)">'+
		       '         <span class="commandLinkIcon" id="' + id + '.icon' + '" style="display:block; width:' + iconWidth + '; height:' + iconHeight + '; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=scale, src=\'' + iconSrcNormal + '\')"/>'+
		       '     </td>'+
		       '     <td valign="top" align="left" class="commandLinkTexts" style="padding-top:7px; padding-bottom:3px; padding-right:20px;">'+
		       '         <div id="' + id + '.actionText' + '" class="commandLinkAction">' + $(this).text() + '</div>'+
               '         <div class="commandLinkHint"  >' + hintText       + '</div>'+
		       '     </td>'+
	           '   </tr>'+
	           '</table>'+
	           '</a>';
	    //alert(html);
        $(this).replaceWith(html);
        
    }
   );  

   $('.commandLinkContent').LayerButton(
    {
     'hover':
      {
        'imageSource':  { 'prefix': commandLinkImgDir + 'command_link_hover_', 'suffix': '.png' },
        'imageBorders': { 'top': 4, 'right': 4, 'bottom': 4, 'left': 4 },
        'fadeIn':       { 'speed': 250 },
        'fadeOut':      { 'speed': 450 }
      }
    }
  );  

}

function changeStyle(selector, style, rs_state, rs_description)
{
  $(selector).attr("class",style);
}

var preventValue;
var speedupValue;

function showPreventSwitch(value)
{
  preventValue = value;
  
  if (preventValue)
  {
    $('#optimizerslider').css('left', '24px');
  }
}

function showSpeedUpSwitch(value)
{
  speedupValue = value;
  
  if (speedupValue)
  {
    $('#speedupslider').css('left', '24px');
  }
}

/* ============================== Command links ============================= */

var 
  commandLinkImgDir='CommandLinks/';

function getCommandLinkElement(childEl){
    if (childEl == null)
        return null;
    var nofMatches = $('.commandLink', childEl.parentElement).length;
    if (nofMatches> 0){
        return childEl;        
    }        
    else{
        return getCommandLinkElement(childEl.parentElement);
    }
}

function handleCommandLinkMouseEnter(el){
    id = el.id;
       
    aParent = getCommandLinkElement(el);
        
    var fadeInSpeed = 250;
    if (aParent)
        fadeInSpeed = aParent.LayerButton['hover'].fadeIn.speed;
    
    iconSrcHover = el.getAttribute('iconSrcHover');
    iconEl = document.getElementById(id + '.icon');
    iconContEl = document.getElementById(id + '.iconCont');
    iconContEl.filters(0).Duration = fadeInSpeed / 1000;
    iconContEl.filters(0).Apply();
    iconEl.filters(0).src = iconSrcHover;  
    iconContEl.filters(0).Play();
    currForeColor = el.currentStyle.color;    
    el.setAttribute('lastForeColor', currForeColor);
    $('.commandLinkTexts', el).animate({ color: '#2F4AE5'}, 250);
    
      
}

function handleCommandLinkMouseLeave(el){
    id = el.id;
    
    aParent = getCommandLinkElement(el);
    
    var fadeOutSpeed = 450;
    if (aParent)
        fadeOutSpeed = aParent.LayerButton['hover'].fadeOut.speed;
    
    iconSrcNormal = el.getAttribute('iconSrcNormal');  
    iconEl = document.getElementById(id + '.icon');
    iconContEl = document.getElementById(id + '.iconCont');
    iconContEl.filters(0).Duration = fadeOutSpeed / 1000;
    iconContEl.filters(0).Apply();
    iconEl.filters(0).src = iconSrcNormal;  
    iconContEl.filters(0).Play();
    lastForeColor = el.getAttribute('lastForeColor');
    $('.commandLinkTexts', el).animate({ color: lastForeColor}, 450);

}
