/************* COG - a few built-in functions *************\
 Image popups, float tidying. That sort of thing!
\**********************************************************/

COG = {};

COG.POPUP = {};
COG.POPUP.window = "";
COG.POPUP.doPop = function (url, W, H) {
	// URL *might* be from a link:
	url = (typeof (url.href) == "undefined") ? url : url.href;
	
	// Add some to the dimensions to fit:
	W += 30;
	H += 50;
	
	// Default the scroll to true:
	S = (typeof (S) == "undefined") ? "1" : S;
	
	// Calculate the position onscreen:
	L = (screen.width-W)/2;
	T = (screen.height-H)/2;
	
	// Create the window:
	if (COG.POPUP.window.close) {
		COG.POPUP.window.close ();
	}
	COG.POPUP.window = window.open (url, 'cogPopup', 'width=' + W + ', height=' + H + ', left=' + L + ', top=' + T + ', scrollbars=' + S + ', menubar=0, toolbar=0, directories=0, resizable=0, location=0, status=0');
	if (window.focus) {
		COG.POPUP.window.focus();
	}
	
	// Add on an opener if we need it:
	if (!COG.POPUP.window.opener) {
		COG.POPUP.window.opener = self;
	}
};

COG.POPUP.doImage = function (url, W, H) {
  // Open
  COG.POPUP.doPop ("", W, H);
  // Modify:
  var doc = COG.POPUP.window.document;
  doc.open ();
  doc.write ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
  doc.write ('<html xmlns="http://www.w3.org/1999/xhtml" lang="en">');
  doc.write ('<head>');
  doc.write ('<title>Preview Image</title>');
  doc.write ('<style type="text/css">* {margin: 0; padding: 0; border: 0;} html {height: 100%;} body {height: 100%; overflow: hidden; background: center center url(' + url + ') no-repeat;}</style>');
  doc.write ('</head>');
  doc.write ('<body>');
  doc.write ('</body>');
  doc.write ('</html>');
  doc.close ();
};

/******* TIDYFLOATS - automatically clearing floats *******\
 Seeks out all the elements of class "tidyfloats" and
  clears the float every "N" where "N" is part of another
  class "everyN"...
\**********************************************************/

COG.TIDYFLOATS = {};
//
// Regex to find "n" of everyn
//
COG.TIDYFLOATS.regex = new RegExp ("(^|\\s)every([0-9]+)($|\\s)");
//
// Finds those floating buggers!
//
COG.TIDYFLOATS.init = function () {
  // Get all the "tidyfloats" elements
  var tidies = getElementsByClassName ("tidyfloats");
  for (var t=0; t<tidies.length; t++) {
    var tidy = tidies[t];
    // How often do we clear?
    var every = COG.TIDYFLOATS.regex.exec (tidy.className);
    if (!every) {
      continue;
    }
    every = every[2];
    // Height levelling
    h = 0;
    row = [];
    // Generate a list of the nodes we need to check
    var nodes = [];
    for (var c=0, cl=tidy.childNodes.length; c<cl; c++) {
      var node = tidy.childNodes[c];
      // Only "element" nodes to be checked
      if (node.nodeType == 1) {
        // Add to the list
        nodes.push (node);
      }
    }
    // Loop the list and level them!
    for (var n=0, nl=nodes.length; n<nl; n++) {
      // Reference the node
      var node = nodes[n];
      // Clear on new rows
      if ((n % every) == 0) {
        node.style.clear = "both";
      }
      // Add to the row
      row.push (node);
      // Max height?
      var nh = node.offsetHeight;
      if (nh > h) {
        h = nh;
      }
      // Level heights at the end of rows
      //  (or at the end of the whole lot)
      if (((n + 1) % every) == 0 || n == (nl - 1)) {
        // Every item in the row
        for (var r=0, rl=row.length; r<rl; r++) {
          row[r].style.height = h + "px";
        }
        // Empty for the next row
        h = 0;
        row = [];
      }
    }
  }
};

/***************** getElementsByClassName *****************\
 Developed by Robert Nyman, http://www.robertnyman.com
 Code/licensing: http://code.google.com/p/getelementsbyclassname/
\**********************************************************/

if (!window.getElementsByClassName) {
  var getElementsByClassName = function (className, tag, elm){
    if (document.getElementsByClassName) {
      getElementsByClassName = function (className, tag, elm) {
        elm = elm || document;
        var elements = elm.getElementsByClassName(className),
          nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
          returnElements = [],
          current;
        for(var i=0, il=elements.length; i<il; i+=1){
          current = elements[i];
          if(!nodeName || nodeName.test(current.nodeName)) {
            returnElements.push(current);
          }
        }
        return returnElements;
      };
    }
    else if (document.evaluate) {
      getElementsByClassName = function (className, tag, elm) {
        tag = tag || "*";
        elm = elm || document;
        var classes = className.split(" "),
          classesToCheck = "",
          xhtmlNamespace = "http://www.w3.org/1999/xhtml",
          namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
          returnElements = [],
          elements,
          node;
        for(var j=0, jl=classes.length; j<jl; j+=1){
          classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
        }
        try  {
          elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
        }
        catch (e) {
          elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
        }
        while ((node = elements.iterateNext())) {
          returnElements.push(node);
        }
        return returnElements;
      };
    }
    else {
      getElementsByClassName = function (className, tag, elm) {
        tag = tag || "*";
        elm = elm || document;
        var classes = className.split(" "),
          classesToCheck = [],
          elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
          current,
          returnElements = [],
          match;
        for(var k=0, kl=classes.length; k<kl; k+=1){
          classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
        }
        for(var l=0, ll=elements.length; l<ll; l+=1){
          current = elements[l];
          match = false;
          for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
            match = classesToCheck[m].test(current.className);
            if (!match) {
              break;
            }
          }
          if (match) {
            returnElements.push(current);
          }
        }
        return returnElements;
      };
    }
    return getElementsByClassName(className, tag, elm);
  };
}

