/*
shadows.js.php

Copyright (c) Gareth Hadfield 2008
*/


var SHADOW_CLASS_PREFIX = ".shadow";

function shadow_div(aSize, aLeft, aTop, aWidth, aHeight, aShadowDir, aType,
  aShadowOpacity, aStartColor, aEndColor, aRounded){
  // To use one of the default shadow files, specify aShadowDir and aType and
  // don't specify aStartColor or aEndColor
  // Otherwise use aStartColor and aEndColor to create a dynamic shadow image

  var aImage;

  if(is_color(aStartColor) && is_color(aEndColor)){
    if(aRounded=="true"){
      aImage = "url("+HTTP_FMD+"rounded_border.php?width="+aSize+"&height="+aSize+
        "&start_color="+aStartColor.substr(1)+"&end_color="+aEndColor.substr(1)+
        "&type="+aType+")";
    }
    else{
      aImage = "url("+HTTP_FMD+"shadow.php?width="+aSize+"&height="+aSize+
        "&start_color="+aStartColor.substr(1)+"&end_color="+aEndColor.substr(1)+
        "&type="+aType+")";
    }
  }
  else{
    aImage = "url("+aShadowDir+"/shadow_"+aType+".gif)";
  }

  var result = document.createElement("div");
  result.shadow = true;
  result.style.position = "absolute";
  result.style.left = aLeft;
  result.style.top = aTop;
  result.style.width = aWidth;
  result.style.height = aHeight;
  result.style.backgroundImage = aImage;
  //result.style.backgroundColor = "red"; // $HTTP_FULL_MAIN_DIR
  result.innerHTML = "<img alt='' src='http://www.gtdesktop.com/opendots/images/blank.gif' style='width:"+aWidth+";height:"+aHeight+";'>";
  setOpacity(result, aShadowOpacity);
  //alert(getAttributeText(result, "style"));
  //alert(result.innerHTML);

//  debug(getAttributeText(result, "style"));
//  debug(result.innerHTML);

  return(result);
}

function add_shadow(aDot, aShadowNumber, aShadowOpacity, aStartColor, aEndColor, aSize, aRounded, aWhichShadows){
  if(aSize == undefined){
    aSize = 10;
  }

  if(aShadowNumber==undefined){
    aShadowNumber = "02";
  }

  if(aShadowOpacity==undefined){
    if(aShadowNumber=="04"){
      aShadowOpacity = 40;
    }
    else{
      aShadowOpacity = 100;
    }
  }

  if(aWhichShadows == undefined){
    aWhichShadows = "blrt";
  }

  var aBottom = in_array("b", aWhichShadows);
  var aLeft = in_array("l", aWhichShadows);
  var aRight = in_array("r", aWhichShadows);
  var aTop = in_array("t", aWhichShadows);

  var aShadowDir = "http://www.gtdesktop.com/opendots/images/shadow"+aShadowNumber;

  remove_shadows(aDot);

  if(aBottom){
    aDot.appendChild(shadow_div(aSize, "0px", "100%", "100%", aSize+"px", aShadowDir, "bottom", aShadowOpacity, aStartColor, aEndColor, aRounded));
  }
  if(aLeft){
    aDot.appendChild(shadow_div(aSize, "-"+aSize+"px", "0px", aSize+"px", "100%", aShadowDir, "left", aShadowOpacity, aStartColor, aEndColor, aRounded));
  }
  if(aRight){
    aDot.appendChild(shadow_div(aSize, "100%", "0px", aSize+"px", "100%", aShadowDir, "right", aShadowOpacity, aStartColor, aEndColor, aRounded));
  }
  if(aTop){
    aDot.appendChild(shadow_div(aSize, "0px", "-"+aSize+"px", "100%", aSize+"px", aShadowDir, "top", aShadowOpacity, aStartColor, aEndColor, aRounded));
  }

  if(aTop && aLeft){
    aDot.appendChild(shadow_div(aSize, "-"+aSize+"px", "-"+aSize+"px", aSize+"px", aSize+"px", aShadowDir, "top_left", aShadowOpacity, aStartColor, aEndColor, aRounded));
  }
  if(aTop && aRight){
    aDot.appendChild(shadow_div(aSize, "100%", "-"+aSize+"px", aSize+"px", aSize+"px", aShadowDir, "top_right", aShadowOpacity, aStartColor, aEndColor, aRounded));
  }

  if(aBottom && aLeft){
    aDot.appendChild(shadow_div(aSize, "-"+aSize+"px", "100%", aSize+"px", aSize+"px", aShadowDir, "bottom_left", aShadowOpacity, aStartColor, aEndColor, aRounded));
  }
  if(aBottom && aRight){
    aDot.appendChild(shadow_div(aSize, "100%", "100%", aSize+"px", aSize+"px", aShadowDir, "bottom_right", aShadowOpacity, aStartColor, aEndColor, aRounded));
  }

  // overflow must be visible since the borders are inside the div and offset
  if(getCSSAttribute(aDot, "overflow") != "visible"){
    setCSSAttribute(aDot, "overflow", "visible");
  }

//  warning - dont use setCSSAttribute - recursion!

/*
  if(IS_IE && (OPENDOTS_MODE==OPENDOTS_DISPLAY_MODE)){
    // kicks IE into calculating the height properly
    var aHeight = getCSSAttribute(aDot, "height");
    if( (aHeight == undefined) || isNaN(parseInt(aHeight, 10)) ){
      var aMinSize = "100px";
      setCSSAttribute(aDot, "height", aMinSize);
    }
  }
*/
}

function remove_shadows(aDot){
  var aChildren = all_divs(aDot, false);
  var aChild;
  for(var iChild=0; iChild<aChildren.length; iChild++){
    aChild = aChildren[iChild];
    if(aChild.shadow == true){
      aDot.removeChild(aChild);
    }
  }
}

function init_shadows(aShadowNumber, aShadowOpacity, aStartColor, aEndColor, aSize, aRounded, aWhichShadows){
  // find any dots with sub_class="shadow" an add the shadows
  var aDots = all_dots();
  var aClassName;
  for(var i=0; i<aDots.length; i++){
    aClassName = aDots[i].getAttributeText("sub_class");
    if(aClassName==SHADOW_CLASS_PREFIX){
      add_shadow(aDots[i], aShadowNumber, aShadowOpacity, "#"+aStartColor, "#"+aEndColor, aSize, aRounded, aWhichShadows);
    }
    else if(is_shadow_class(aClassName) && (aStartColor != undefined)){
      add_shadow(aDots[i], aShadowNumber, aShadowOpacity, "#"+aStartColor, "#"+aEndColor, aSize, aRounded, aWhichShadows);
    }
  }
}

function is_shadow_class(aClassName){
  var result = (aClassName != undefined);
  if(result){
    var aStartColor = query_string(aClassName, "start_color");
    var aEndColor = query_string(aClassName, "end_color");
  //  var aSize = query_string(aClassName, "size");
    var aShadowNumber = query_string(aClassName, "number");

    result = (aClassName==SHADOW_CLASS_PREFIX) ||
      (
      head_is(aClassName, SHADOW_CLASS_PREFIX) &&
      (aShadowNumber != undefined)
      ) ||
      (
      head_is(aClassName, SHADOW_CLASS_PREFIX) &&
      is_color("#"+aStartColor) &&
      is_color("#"+aEndColor)
      );
  }
  return(result);
}
