if(frames){if(top.frames.length>0)top.location.href=self.location;}
/* Util and IE Fixes */


Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (this._timer)  clearInterval(this._timer);
    
    this._readyCallbacks.each(function(f) { f() });
    this._readyCallbacks = null;
},
  onDOMReady : function(f) {
    if (!this._readyCallbacks) {
      var domReady = this._domReady.bind(this);
      
      if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);
        
        /*@cc_on @*/
        /*@if (@_win32)
            document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
            document.getElementById("__ie_onload").onreadystatechange = function() {
                if (this.readyState == "complete") domReady(); 
            };
        /*@end @*/
        
        if (/WebKit/i.test(navigator.userAgent)) { 
          this._timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) domReady(); 
          }, 10);
        }
        
        Event.observe(window, 'load', domReady);
        Event._readyCallbacks =  [];
    }
    Event._readyCallbacks.push(f);
  }
});

function getXMLNodeSerialisation(xmlNode) {
  var text = false;
  try {
    // Gecko-based browsers, Safari, Opera.
    var serializer = new XMLSerializer();
    text = serializer.serializeToString(xmlNode);
  }
  catch (e) {
    try {
      // Internet Explorer.
      text = xmlNode.xml;
    }
    catch (e) {}
  }
  return text;
}


if (typeof DOMParser == "undefined") {
   DOMParser = function () {}

   DOMParser.prototype.parseFromString = function (str, contentType) {
      if (typeof ActiveXObject != "undefined") {
         var d = new ActiveXObject("MSXML.DomDocument");
         d.loadXML(str);
         return d;
      } else if (typeof XMLHttpRequest != "undefined") {
         var req = new XMLHttpRequest;
         req.open("GET", "data:" + (contentType || "application/xml") +
                         ";charset=utf-8," + encodeURIComponent(str), false);
         if (req.overrideMimeType) {
            req.overrideMimeType(contentType);
         }
         req.send(null);
         return req.responseXML;
      }
   }
}

var RotatingImage = Class.create();
RotatingImage.prototype = {
  	// Constructor
  	initialize: function(divId, images, links, duration) {
		this.div = $(divId);
		this.hyperlink = this.div.down('a');
		this.buffer = images[0];
		this.back_buffer = images[1];

		this.buffer.addClassName("buffer");
		this.back_buffer.addClassName("back_buffer");
		
		this.images = images.collect(function(image) { return image.src});
		this.links = links.collect(function(link) { return link.href});

		this.image_current = 1;
		
		this.back_buffer.src = this.images[this.image_current]
		
		this.fade = this._fade.bindAsEventListener(this);
		this.flip = this._flip.bindAsEventListener(this);
		
		var oClass = this
		setTimeout(function(){
			oClass.fade();
			setInterval(oClass.fade, 8000);
		}, 1000)
	},
	
	_fade: function() {
		Effect.Fade(this.buffer, {duration:0.5, delay:6, afterFinish:this.flip})
	},
	
	_flip: function() {
		var temp = this.buffer
		this.buffer = this.back_buffer
		this.back_buffer = temp
		
		this.buffer.removeClassName("back_buffer")
		this.back_buffer.addClassName("back_buffer")
		
		this.buffer.addClassName("buffer")
		this.back_buffer.removeClassName("buffer")
		
		this.hyperlink.href = this.links[this.image_current]
		
		Element.show(this.back_buffer)
	
	
		if (this.image_current + 1 == this.images.length)
			this.image_current = 0
		else
			this.image_current++
	
		this.back_buffer.src = this.images[this.image_current]
	}
}

Event.observe(window, "load", function(){
	if ($(document.body).hasClassName("home") && $$("#main_image img").length > 1 && $$("#main_image .editable").length == 0) {
		new RotatingImage('main_image', $$("#main_image img"), $$("#main_image a"));

	}
})

Event.onDOMReady(function(){

	if ($("callout") && $("callout").innerHTML.strip() == "") {
		$("callout").hide();
	}

	if ($$(".articles").length > 0)
	{
		paragraphs = $$(".articles .current p");
		if (paragraphs.length > 0) {
			paragraphs[0].addClassName("intro")
		}
	}
	
	$$(".print").each(function(el){
		el.onclick = function(){
			window.print();
			return false;
		}
	})

	function navigationUnexepandSiblings(li) {
		li.siblings().each(function(sibling){
			if (sibling.hasClassName("expanded")) {
				if (sibling.hasClassName("ancestor") == false) {// And doesn't contain the current page
					sibling.toggleClassName('expanded');
					sibling.toggleClassName('unexpanded');
				}
				children = new Array();
				child = sibling.down("li")
				children.push(child)
				while (child.next("li"))
				{
					child = child.next("li")
					children.push(child)
				}
				children.each(function(child){
					navigationUnexepandSiblings(child);
				})
			}
		})
	}
	
	$$("#navigation a.expand_navigation").each(function(a){
		a.observe("click", function(el){
			li = $(a.parentNode)
			if (li.hasClassName('unexpanded')) { // Close other expanded menus on the same level
				navigationUnexepandSiblings(li)
				
				// Close any ancestor's syblings
				parentLi = li.up('li')
				while(parentLi) {
					navigationUnexepandSiblings(parentLi)
					parentLi = parentLi.up('li')
				}
			}
			
			li.toggleClassName('expanded');
			li.toggleClassName('unexpanded');
			
			return false;
		})
	})
	
})


