// is it a DOM-enabled browser?
domBrowser = document.getElementById ? true : false;

// redraw onload support
redraws = new Array();
// arbitrary function calls to perform onload
onloads = new Array();

function redraw( id, y, x ) {
   this.id = id;
   this.x = x;
   this.y = y;

   this.paint = function(offset) {
       setProperty( this.id, 'left', eval(this.x) + 'px' );
       setProperty( this.id, 'top',  eval(this.y)-offset + 'px' ); 
   };
}


function makeImg(imgname, prefix) {
        eval(imgname + "_on = new Image()");
        eval(imgname + "_off = new Image()");
        eval(imgname + "_on.src = prefix + '/images/" + imgname + "_on.gif'");
        eval(imgname + "_off.src = prefix + '/images/" + imgname + ".gif'");
}

function setUp( prefix ) {

  type_module_expand = new Image();
  type_module_collapse = new Image();
  type_module_link = new Image();
  type_module_expand.src = prefix + '/images/icons/type_module_expand.gif';
  type_module_collapse.src = prefix + '/images/icons/type_module_collapse.gif';
  type_module_link.src = prefix + '/images/icons/type_module_link.gif';

  // dynamic support for multiple onload functions
  for( i = 0; i < onloads.length; i++ ) {
     onloads[i]();
  }

  document.onmouseDown = mouseDn;

  // xhtml forbids the target attribute of the a tag
  // we move behavior to javascript instead
  // http://www.sitepoint.com/article/standards-compliant-world/
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
      var anchor = anchors[i];
      if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
        anchor.target = "_blank";
    }

}

function redrawAll(offset) {

  // redraw everything that's been queued up during the page load
  for( i = 0; i < redraws.length; i++ ) {
     redraws[i].paint(offset);
  } 

}

function mouseDn() {
}

// SET ANY STYLE OF DIV
function setProperty( id, property, value) {
        var styleObject = $( id );
        if (styleObject != null) {
                styleObject = styleObject.style;
                styleObject[ property ] = value;
        }
}

function getProperty( id, property ) {
        var styleObject = $( id );
        if (styleObject != null) {
                styleObject = styleObject.style;
                return styleObject[ property ];
        }
}

// FOR SETTING PROPERTY ON AN ENTIRE CSS CLASS (doesn't work in Safari?)
function addRule (selector, rule) {
  if (document.all)
    document.styleSheets[document.styleSheets.length - 1].addRule(selector, rule);
  else if (domBrowser)
    document.styleSheets[document.styleSheets.length - 1].insertRule(selector + '{ ' + rule + ' } ', document.styleSheets[document.styleSheets.length - 1].cssRules.length);
}

function confirmBox ( message, link ) {
    var answer = confirm( message );
    if( answer ) 
        window.location.href=link;
}

// expand / collapse modules in the Modules tab and in the editor WS
function toggleGroup( id, imgId ) {
  if( !$(id) ) { return; }
  Element.toggle( id );
  if( getProperty( id, 'display' ) == 'none' && imgId ) {
        $(imgId).src = type_module_expand.src;
  }
  else if( imgId ) {
        $(imgId).src = type_module_collapse.src;
  }
}

