/* moo.toolkit: MIT LICENSE */

/*---------------------------------------------------------------------------------------------------------*/
//# [2.5kb] prototype.lite: mini version of prototype, modified
/*---------------------------------------------------------------------------------------------------------*/

/*  Prototype JavaScript framework
 *  (c) 2005 Sam Stephenson <sam@conio.net>
 *  Prototype is freely distributable under the terms of an MIT-style license.
 *  For details, see the Prototype web site: http://prototype.conio.net/
/*--------------------------------------------------------------------------*/

var Class = {
	create: function() {
		return function() { 
			this.initialize.apply(this, arguments);
		}
	}
}

Object.extend = function(destination, source) {
	for (property in source) destination[property] = source[property];
	return destination;
}

Function.prototype.bind = function(object) {
	var __method = this;
	return function() {
		return __method.apply(object, arguments);
	}
}

Function.prototype.bindAsEventListener = function(object) {
var __method = this;
	return function(event) {
		__method.call(object, event || window.event);
	}
}

function $() {
	if (arguments.length == 1) return get$(arguments[0]);
	var elements = [];
	arguments.each(function(el){
		elements.push(get$(el));
	});
	return elements;
	
	function get$(el){
		if (typeof el == 'string') el = document.getElementById(el);
		return el;
	}
}

if (!window.Element) var Element = new Object();

Object.extend(Element, {
	remove: function(element) {
		element = $(element);
		element.parentNode.removeChild(element);
	},

	hasClassName: function(element, className) {
		element = $(element);
		if (!element) return;
		var hasClass = false;
		element.className.split(' ').each(function(cn){
			if (cn == className) hasClass = true;
		});
		return hasClass;
	},

	addClassName: function(element, className) {
		element = $(element);
		Element.removeClassName(element, className);
		element.className += ' ' + className;
	},
  
	removeClassName: function(element, className) {
		element = $(element);
		if (!element) return;
		var newClassName = '';
		element.className.split(' ').each(function(cn, i){
			if (cn != className){
				if (i > 0) newClassName += ' ';
				newClassName += cn;
			}
		});
		element.className = newClassName;
	},

	cleanWhitespace: function(element) {
		element = $(element);
		$e(element.childNodes).each(function(node){
			if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) Element.remove(node);
		});
	}
});

var Position = {
	cumulativeOffset: function(element) {
		var valueT = 0, valueL = 0;
		do {
			valueT += element.offsetTop  || 0;
			valueL += element.offsetLeft || 0;
			element = element.offsetParent;
		} while (element);
		return [valueL, valueT];
	}
};

/*---------------------------------------------------------------------------------------------------------*/
//# [1.5kb] moo.ajax: mini ajax class
/*---------------------------------------------------------------------------------------------------------*/

ajax = Class.create();
ajax.prototype = {
	initialize: function(url, options){
		this.transport = this.getTransport();
		this.postBody = options.postBody || '';
		this.method = options.method || 'post';
		this.onComplete = options.onComplete || null;
		this.update = $(options.update) || null;
		this.request(url);
	},

	request: function(url){
		this.transport.open(this.method, url, true);
		this.transport.onreadystatechange = this.onStateChange.bind(this);
		if (this.method == 'post') {
			this.transport.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			if (this.transport.overrideMimeType) this.transport.setRequestHeader('Connection', 'close');
		}
		this.transport.send(this.postBody);
	},

	onStateChange: function(){
		if (this.transport.readyState == 4 && this.transport.status == 200) {
			if (this.update) 
				setTimeout(function(){this.update.innerHTML = this.transport.responseText;}.bind(this), 10);
			if (this.onComplete) 
				setTimeout(function(){this.onComplete(this.transport);}.bind(this), 10);
			this.transport.onreadystatechange = function(){};
		}
	},

	getTransport: function() {
		if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
		else if (window.XMLHttpRequest) return new XMLHttpRequest();
		else return false;
	}
};

/*---------------------------------------------------------------------------------------------------------*/
//# [3kb] moo.fx: simple javascript effects
/*---------------------------------------------------------------------------------------------------------*/

var fx = new Object();
//base
fx.Base = function(){};
fx.Base.prototype = {
	setOptions: function(options) {
	this.options = {
		duration: 500,
		onComplete: '',
		transition: fx.sinoidal
	}
	Object.extend(this.options, options || {});
	},

	step: function() {
		var time  = (new Date).getTime();
		if (time >= this.options.duration+this.startTime) {
			this.now = this.to;
			clearInterval (this.timer);
			this.timer = null;
			if (this.options.onComplete) setTimeout(this.options.onComplete.bind(this), 10);
		}
		else {
			var Tpos = (time - this.startTime) / (this.options.duration);
			this.now = this.options.transition(Tpos) * (this.to-this.from) + this.from;
		}
		this.increase();
	},

	custom: function(from, to) {
		if (this.timer != null) return;
		this.from = from;
		this.to = to;
		this.startTime = (new Date).getTime();
		this.timer = setInterval (this.step.bind(this), 13);
	},

	hide: function() {
		this.now = 0;
		this.increase();
	},

	clearTimer: function() {
		clearInterval(this.timer);
		this.timer = null;
	}
}

//stretchers
fx.Layout = Class.create();
fx.Layout.prototype = Object.extend(new fx.Base(), {
	initialize: function(el, options) {
		this.el = $(el);
		this.el.style.overflow = "hidden";
		this.iniWidth = this.el.offsetWidth;
		this.iniHeight = this.el.offsetHeight;
		this.setOptions(options);
	}
});

fx.Height = Class.create();
Object.extend(Object.extend(fx.Height.prototype, fx.Layout.prototype), {	
	increase: function() {
		this.el.style.height = this.now + "px";
	},

	toggle: function() {
		if (this.el.offsetHeight > 0) this.custom(this.el.offsetHeight, 0);
		else this.custom(0, this.el.scrollHeight);
	}
});

fx.Width = Class.create();
Object.extend(Object.extend(fx.Width.prototype, fx.Layout.prototype), {	
	increase: function() {
		this.el.style.width = this.now + "px";
	},

	toggle: function(){
		if (this.el.offsetWidth > 0) this.custom(this.el.offsetWidth, 0);
		else this.custom(0, this.iniWidth);
	}
});

//fader
fx.Opacity = Class.create();
fx.Opacity.prototype = Object.extend(new fx.Base(), {
	initialize: function(el, options) {
		this.el = $(el);
		this.now = 1;
		this.increase();
		this.setOptions(options);
	},

	increase: function() {
		if (this.now == 1 && (/Firefox/.test(navigator.userAgent))) this.now = 0.9999;
		this.setOpacity(this.now);
	},
	
	setOpacity: function(opacity) {
		if (opacity == 0 && this.el.style.visibility != "hidden") this.el.style.visibility = "hidden";
		else if (this.el.style.visibility != "visible") this.el.style.visibility = "visible";
		if (window.ActiveXObject) this.el.style.filter = "alpha(opacity=" + opacity*100 + ")";
		this.el.style.opacity = opacity;
	},

	toggle: function() {
		if (this.now > 0) this.custom(1, 0);
		else this.custom(0, 1);
	}
});

//transitions
fx.sinoidal = function(pos){
	return ((-Math.cos(pos*Math.PI)/2) + 0.5);
	//this transition is from script.aculo.us
}
fx.linear = function(pos){
	return pos;
}
fx.cubic = function(pos){
	return Math.pow(pos, 3);
}
fx.circ = function(pos){
	return Math.sqrt(pos);
}

/*--------------------------------------------------------------------------*/
/*------------------behaviour.js--------------------------------------------*/
/*--------------------------------------------------------------------------*/

/*
   Behaviour v1.1 by Ben Nolan, June 2005.
*/   

var Behaviour = {
	list : new Array,
	
	register : function(sheet){
		Behaviour.list.push(sheet);
	},
	
	start : function(){
		Behaviour.addLoadEvent(function(){
			Behaviour.apply();
		});
	},
	
	apply : function(){
		for (h=0;sheet=Behaviour.list[h];h++){
			for (selector in sheet){
				list = document.getElementsBySelector(selector);
				
				if (!list){
					continue;
				}

				for (i=0;element=list[i];i++){
					sheet[selector](element);
				}
			}
		}
	},

/*
   The following code has been modified as per Dean Edwards suggestion.
   This ensures that Behaviour can be used before window.onload
   (Works in Firefox and IE6)
*/

	addLoadEvent : function(func){
         	if (this.applied) return;
	        this.applied = true;
		if (window.addEventListener) {
			window.addEventListener('DOMContentLoaded', func, false);
		};
                /*@cc_on @*/
                /*@if (@_win32)
	                document.write("<script defer src=ie_onload.js><"+"/script>");
                /*@end @*/
                window.onload = func;       
	}

/* end of modification */

}

Behaviour.start();

/*
   The following code is Copyright (C) Simon Willison 2004.
*/

function getAllChildren(e) {
  // Returns all children of element. Workaround required for IE5/Windows. Ugh.
  return e.all ? e.all : e.getElementsByTagName('*');
}

document.getElementsBySelector = function(selector) {
  // Attempt to fail gracefully in lesser browsers
  if (!document.getElementsByTagName) {
    return new Array();
  }
  // Split selector in to tokens
  var tokens = selector.split(' ');
  var currentContext = new Array(document);
  for (var i = 0; i < tokens.length; i++) {
    token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');;
    if (token.indexOf('#') > -1) {
      // Token is an ID selector
      var bits = token.split('#');
      var tagName = bits[0];
      var id = bits[1];
      var element = document.getElementById(id);
      if (tagName && element.nodeName.toLowerCase() != tagName) {
        // tag with that ID not found, return false
        return new Array();
      }
      // Set currentContext to contain just this element
      currentContext = new Array(element);
      continue; // Skip to next token
    }
    if (token.indexOf('.') > -1) {
      // Token contains a class selector
      var bits = token.split('.');
      var tagName = bits[0];
      var className = bits[1];
      if (!tagName) {
        tagName = '*';
      }
      // Get elements matching tag, filter them for class selector
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for (var j = 0; j < elements.length; j++) {
          found[foundCount++] = elements[j];
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b'))) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      continue; // Skip to next token
    }
    // Code to deal with attribute selectors
    if (token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)) {
      var tagName = RegExp.$1;
      var attrName = RegExp.$2;
      var attrOperator = RegExp.$3;
      var attrValue = RegExp.$4;
      if (!tagName) {
        tagName = '*';
      }
      // Grab all of the tagName elements within current context
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for (var j = 0; j < elements.length; j++) {
          found[foundCount++] = elements[j];
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      var checkFunction; // This function will be used to filter the elements
      switch (attrOperator) {
        case '=': // Equality
          checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); };
          break;
        case '~': // Match one of space seperated words 
          checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };
          break;
        case '|': // Match start with value followed by optional hyphen
          checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };
          break;
        case '^': // Match starts with value
          checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); };
          break;
        case '$': // Match ends with value - fails with "Warning" in Opera 7
          checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };
          break;
        case '*': // Match ends with value
          checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); };
          break;
        default :
          // Just test for existence of attribute
          checkFunction = function(e) { return e.getAttribute(attrName); };
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (checkFunction(found[k])) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      // alert('Attribute Selector: '+tagName+' '+attrName+' '+attrOperator+' '+attrValue);
      continue; // Skip to next token
    }
    
    if (!currentContext[0]){
    	return;
    }
    
    // If we get here, token is JUST an element (not a class or ID selector)
    tagName = token;
    var found = new Array;
    var foundCount = 0;
    for (var h = 0; h < currentContext.length; h++) {
      var elements = currentContext[h].getElementsByTagName(tagName);
      for (var j = 0; j < elements.length; j++) {
        found[foundCount++] = elements[j];
      }
    }
    currentContext = found;
  }
  return currentContext;
}

/*--------------------------------------------------------------------------*/
/*       Peter Jerie - written for rallyOZ.com (2005) :                     */
/*--------------------------------------------------------------------------*/

var pages = new Array();
var galleries = new Array();
galleries['bag06pix'] = 1960922;
galleries['obe07pix'] = 2986895;
galleries['for07pix'] = 3479262;
galleries['bag07pix'] = 3563735;
galleries['cor07pix'] = 3639687;
galleries['obe08pix'] = "4710296&AlbumKey=Eiv4n";
loading = "<div id='load'>... loading</div>";
// change to pagename = "" if photos are not to be loaded on the main page
var pagename = "for07pix";
var tempTR = "";
var tempImg = new Image();
// enable following 2 lines if photos are to be loaded on the main page
tempImg.src = 'http://rallyoz.smugmug.com/photos/random.mg?AlbumID=' + galleries[pagename] + '&Size=Medium&rand=' + 
              (Math.floor(Math.random()*9000) + 1000);
              
function showpage(pname){
        if (pages[pname] == null){
                new ajax(pname + '.txt',{
                        method: 'get', 
                        update: $('content'),
                        onComplete:function(){
                                pages[pname] = $('content').innerHTML;
                                Behaviour.apply();
                                myAppear('wrap')
                        }
                })
        }
        else{
                $('content').innerHTML = pages[pname];
                Behaviour.apply();
                myAppear('wrap')
        }
}

function myAppear(container){
        // disable next 5 lines if the photos are to be loaded on the main page
        if (pagename.substring(5) == 'pix'){
            tempImg.src = 'http://rallyoz.smugmug.com/photos/random.mg?AlbumID=' + galleries[pagename] + '&Size=Medium&rand=' + 
                          (Math.floor(Math.random()*9000) + 1000);
        }
         else{
            if ((pagename == 'gir06res')||(pagename == 'cor07res')||(pagename == 'cof08res')){
              document.body.style.width = "1220px";
            }    
            else{
                if (pagename == 'roc08res'){
                  document.body.style.width = "1320px";
                }
                else{ 
                  document.body.style.width = "970px";
                }  
            }
        }
        var myEffect = new fx.Opacity(container);
        myEffect.custom(0, 1)
}

// probably we don't need this function ever again
// function randomPix()
// {
//     $('content').innerHTML =  '<h2><span class="heavy">Wauchope (Bago "100") </span><span class="light">(September 30, 2006)</span></h2>' +
//                               '<div id="small">Random Photo</div><a class="pixlnk" href="http://rallyoz.smugmug.com/gallery/1960922" target="_blank">' +
//                               '<img src="http://rallyoz.smugmug.com/photos/random.mg?AlbumID=1960922&Size=Medium&rand=' + 
//                               (Math.floor(Math.random()*9000) + 1000) + '" border="0"></a>';
//     Behaviour.apply();
// }

var myrules = {
	'.tm' : function(el){
		el.onclick = function(){
                        this.style.backgroundImage = (this.nextSibling.style.display == "block") ? "url(plus.gif)" : "url(minus.gif)";
                        this.nextSibling.style.display = (this.nextSibling.style.display == "block") ? "none" : "block"
		}
	},
	'#rndlnk' : function(el){
		el.onclick = function(){
                        $('rndpix').src = tempImg.src;
                        tempImg.src = 'http://rallyoz.smugmug.com/photos/random.mg?AlbumID=' + galleries[pagename] + '&Size=Medium&rand=' + 
                                      (Math.floor(Math.random()*9000) + 1000);
                        Behaviour.apply();
   		}
	},
	'ul li ul li a' : function(el){
		el.onclick = function(){
                        pagename = this.parentNode.parentNode.className + this.className;
                        if (! /.pdf/.test(pagename)){
                             $('content').innerHTML = loading;
                            var myEffect = new fx.Opacity('load', {onComplete: function(){showpage(pagename)}});
                            myEffect.custom(0, 1)
                        }
    		}
	},
	'tbody tr' : function(el){
		el.onmouseover = function(){
                        tempTR = this.className;
			this.className = "ruled"
		}
		el.onmouseout = function(){
			this.className = tempTR
		}
	},
	'caption a' : function(el){
		el.onclick = function(){
                        this.parentNode.parentNode.className = "hide";
                        $(this.className).className = "show";
                        myAppear('wrap');
		}
	},
	'.season a' : function(el){
		el.onclick = function(){
                        $('content').innerHTML = "";
                        this.parentNode.parentNode.className = "hide";
                        $('mainMenu' + this.className).className = "show";
                        myAppear('wrp');
		}
	}
};

Behaviour.register(myrules);

