// * *
// * rayogram.js version 0.2.1
// * by eric@groundzero.com
// *
// * if you are thinking about changing this file please stop and ask eric
// * first. page-specific scripts should go in a seperate file. thanks
// *


// * *
// * constants you can set
// *

var ROLLOVERON = "_on";
var FRAMERATE = 12;


// * *
// * start-up
// *

var BIGNUMBER = 999999999;
var unnamed_element_count = 0;

var DIRBASE;


var is_msie;

if(navigator.userAgent.indexOf("MSIE") != -1){
	is_msie = true;
}


var scroll_string;

if(document.documentElement && document.documentElement.scrollTop){
	scroll_string = "document.documentElement.scroll";
}
else {
	scroll_string = "document.body.scroll";
}


var event_listener_table = new Array();

var preloaded_images = new Array();
var preloaded_images_count = 0;

// * *
// * system functions
// *

function preload(url){
	preloaded_images[preloaded_images_count] = new Image();
	preloaded_images[preloaded_images_count].src = url;
	preloaded_images_count++;
}

function respond(e){
	var actions = this.event_listener_table[e.type];
	var execute_string = "";
	
	if(actions){		
		for(i=0; i<actions.length; i++){
			execute_string += actions[i] + ";";
		}
	}
		
	eval(execute_string);
}

function addResponse(event, process_string){
	process_string = process_string.toString().replace(/this/g, this.id);
	
	
	if(!this.event_listener_table[event]){
		this.event_listener_table[event] = new Array();
		
		this.event_listener_table[event][0] = process_string;
	
		if(this.addEventListener){
			this.addEventListener(event, respond, true);
		}
		else if(this.attachEvent){
			this.attachEvent("on" + event, function(){if(window.event.srcElement){window.event.srcElement.respond(window.event);}else{window.respond(window.event);}});
		}
		else {
			eval("this.on" + event + " += ';' + function(){if(window.event.srcElement){window.event.srcElement.respond(window.event);}else{window.respond(window.event);}}");
		}
	}
	else{
		this.event_listener_table[event][this.event_listener_table[event].length] = process_string;
	}
	
}



// * *
// * style functions
// *

function getApparentStyle(property, include_units){
	var rval;
	
	if(!property){
		property = "";
	}
	else{
		property = "." + property;
	}
	
	if(this.currentStyle){
		rval = eval("this.currentStyle" + property);
	}
	else {
		rval = eval("document.defaultView.getComputedStyle(this,\"\")" + property);
	}
	
	if(!include_units && parseInt(rval.charAt(0)) >= 0){
		rval = parseInt(rval);
	}
	
	return rval;
}

function getStyle(property, include_units){
	var rval;
	
	if(!property){
		rval = this.style;
	}
	else{
		rval = eval("this.style." + property);
	}
	
	if(!include_units && parseInt(rval.charAt(0))>=0){
		rval = parseInt(rval);
	}
	
	return rval;
}
	
function setStyle(property, value){
	eval("this.style." + property + " = '" + value + "'");
}


// * *
// * movement functions
// *

function moveTo(x, y){
	this.setStyle("left", x);
	this.setStyle("top", y);
}

function moveToScreenFloor(){
	var fix;
	
	if(navigator.appName == "Microsoft Internet Explorer" && navigator.platform == "Win32"){
		fix = 4;	
	}
	else{
		fix = 0;
	}
	
	if(window.innerHeight){
		this.setStyle("top", window.innerHeight - this.getApparentStyle("height") - fix);
	}
	else {
		this.setStyle("top", document.body.offsetHeight - this.getApparentStyle("height") - fix);
	}
}

function moveToScreenRight(){
	var fix;
	
	if(navigator.appName == "Microsoft Internet Explorer" && navigator.platform == "Win32"){
		fix = 0;	
	}
	else{
		fix = 0;
	}
	
	if(window.innerWidth){
		this.setStyle("left", window.innerWidth - this.getApparentStyle("width") - fix + "px");
	}
	else {
		this.setStyle("left", document.body.offsetWidth - this.getApparentStyle("width") - fix + "px");
	}
}


// * *
// * visibility functions
// *

function hide(){
	this.setStyle("visibility", "hidden");
}

function show(){
	this.setStyle("visibility", "visible");
}


// * *
// * text functions
// *

function changeTextSize(increment){
	var all_children, font_sizes, line_heights;
	
	all_children = this.getElementsByTagName("*");
	font_sizes = new Array();
	line_heights = new Array();
	
	for(i=0; i<all_children.length; i++){
		
		if(typeof all_children[i].style != "undefined"){
			var original_style;
			
			if(all_children[i].currentStyle){
				original_style = all_children[i].currentStyle;
			}
			else {
				original_style = document.defaultView.getComputedStyle(all_children[i],"");
			}
			
			font_sizes[i] = parseInt(original_style.fontSize);
			line_heights[i] = parseInt(original_style.lineHeight);
		}
		else{
			font_sizes[i] = line_heights[i] = -1;
		}
	}
	
	for(i=0; i<all_children.length; i++){
		if(font_sizes[i] > 0){		
			var new_font_size, new_line_height;
			
			new_font_size = font_sizes[i] + increment;
			new_line_height = Math.round(new_font_size * line_heights[i] / parseInt(font_sizes[i]));
			all_children[i].setStyle("lineHeight", new_line_height + "px");
			all_children[i].setStyle("fontSize", new_font_size + "px");
		}
	}
}


// * *
// * layout management
// *

function columnizeFull(elements, max){
	var visible_width;
	if(is_msie){
		//visible_width = elements[0].parentNode.getApparentStyle("width");
		visible_width = document.body.clientWidth;
	}
	else {
		visible_width = elements[0].parentNode.getApparentStyle("width");
	}
	
	var sizeable_elements = elements.length;
	var current_total_width = 0;
	var minimum_total_width = 0;
	
	var maxed = new Array();
	var mined = new Array();
	
	var new_element_width = new Array();
	
	for(i = 0; i<elements.length; i++){
		if(!elements[i].min_width){
			elements[i].min_width = elements[i].getApparentStyle("width");
		}
		
		minimum_total_width += elements[i].min_width;
		current_total_width += elements[i].getApparentStyle("width");
		
		mined[i] = maxed[i] = false;
		
		new_element_width[i] = elements[i].min_width;
	}
	
	
			var working_total_width;

	
	if(visible_width < minimum_total_width){
		for(i = 0; i < elements.length; i++){
			elements[i].setStyle("width", elements[i].min_width + "px");
		}
	}
	else {
		var working_width = minimum_total_width;
		var working_visible_width = visible_width;
		
		var percentage_change = working_visible_width / working_width;

			
		while( (Math.abs(working_visible_width - working_width) > sizeable_elements) || (working_width > working_visible_width)){
			working_width = working_total_width = 0;
	
			for(i = 0; i < elements.length; i++){
				
				if(!mined[i] && !maxed[i]){
				
					var candidate_width = Math.floor(new_element_width[i] * percentage_change);
					
					
					if(candidate_width < elements[i].min_width){
						candidate_width = elements[i].min_width;
						working_visible_width -= candidate_width;
						mined[i] = true;
					}
					else if((candidate_width >= max[i]) && (max[i] != -1)){
						candidate_width = max[i];
						working_visible_width -= candidate_width;
						maxed[i] = true;
					}
					else working_width += candidate_width;
					
					new_element_width[i] = candidate_width;
				}
				
				working_total_width += new_element_width[i];
			}
			
			percentage_change = working_visible_width / working_width;
		}
	
		for(i = 0; i < elements.length; i++){
			if((!maxed[i]) && (working_total_width != visible_width)){
				new_element_width[i]++;
				working_total_width++;
			}
			elements[i].setStyle("width", new_element_width[i] + "px");
		}
	
	}
}


function getCurrentWindowWidth(){
	if(window.innerWidth){
		return window.innerWidth;
	}
	
	return document.body.offsetWidth;
}

function setMinimumWidth_core(){
	this.setStyle("width", this.width_functional);
	
	if(this.offsetWidth < this.width_minimum){
		this.setStyle("width", this.width_minimum);
	}
	
}

function setMinimumWidth(width){
	this.width_functional = this.getStyle("width", true);
	this.width_minimum = width;
	
	this.setMinimumWidth_core();
	window.addResponse("resize", this.id + ".setMinimumWidth_core()");
}


function matchHeights(elements){
	var tallest = 0;

	for(i=0; i<elements.length; i++){
		var current_height = elements[i].offsetHeight;
		
		if(current_height > tallest){
			tallest = current_height;
		}
	}

	for(i=0; i<elements.length; i++){
		elements[i].setStyle("height", tallest + "px");
	}
}

// * *
// * initialization
// *

function rayogramInitialize(){
	var all_tags;

	all_tags = document.getElementsByTagName("*");
	
	for(i=0; i<all_tags.length; i++){
		if(!all_tags[i].id){
			all_tags[i].id =  "unnamed" + unnamed_element_count;
			unnamed_element_count++;
		}
		
		if(all_tags[i].id && eval("typeof " + all_tags[i].id + "== 'undefined'")){
			eval(all_tags[i].id + " = all_tags[i]");
		}
					
		all_tags[i].event_listener_table = new Array();
		all_tags[i].addResponse = addResponse;
		all_tags[i].respond = respond;
		
		all_tags[i].getApparentStyle = getApparentStyle;
		all_tags[i].getStyle = getStyle;
		all_tags[i].setStyle = setStyle;
		
		all_tags[i].moveTo = moveTo;
		all_tags[i].moveToScreenFloor = moveToScreenFloor;
		all_tags[i].moveToScreenRight = moveToScreenRight;
		
		all_tags[i].show = show;
		all_tags[i].hide = hide;
		
		all_tags[i].changeTextSize = changeTextSize;
		all_tags[i].setMinimumWidth = setMinimumWidth;
		all_tags[i].setMinimumWidth_core = setMinimumWidth_core;
		
		if((all_tags[i].id.indexOf("_rollover") != -1) || (all_tags[i].className.indexOf("_rollover") != -1)){
			var file_suffix_location, new_src;
			
			file_suffix_location = all_tags[i].src.lastIndexOf(".");
			
			new_src = all_tags[i].src.substring(0,file_suffix_location)
				+ ROLLOVERON
				+ all_tags[i].src.substring(file_suffix_location, all_tags[i].src.length);
				
			preload(new_src);
				
			all_tags[i].addResponse("mouseover", "this.src='" + new_src + "'");
			all_tags[i].addResponse("mouseout", "this.src='" + all_tags[i].src + "'");	
		}
		
		if(all_tags[i].id.indexOf("_rollclass") != -1){
			all_tags[i].addResponse("mouseover", "this.className='" + all_tags[i].className + ROLLOVERON + "'");
			all_tags[i].addResponse("mouseout", "this.className='" + all_tags[i].className + "'");	
		}
		
		if(all_tags[i].id.indexOf("_pullquote") != -1 && (all_tags[i].innerHTML.replace(/<\![^>]*>/gm, "") != false)){
			var content, top, bottom;
			
			content = all_tags[i].innerHTML;
			
			top = all_tags[i].id.indexOf("top") != -1;
			bottom = all_tags[i].id.indexOf("bottom") != -1;
			
			if(!(top || bottom)){
				top = bottom = true;
			}
			
			all_tags[i].innerHTML = "";
			
			if(top){
				all_tags[i].innerHTML = "<img src=\"" + DIRBASE + "images/" + all_tags[i].className + "_top.gif\">";
			}
			
			all_tags[i].innerHTML += "<div class=\"" + all_tags[i].className + "_text\">" + content + "</div>";
			
			if(bottom){
				all_tags[i].innerHTML += "<img src=\"" + DIRBASE + "images/" + all_tags[i].className + "_bottom.gif\">";
			}
		}
		
		if((all_tags[i].id.indexOf("_text") != -1)||(all_tags[i].id.indexOf("drop") != -1)||(all_tags[i].className.indexOf("drop") != -1)){
			for(var j in all_tags[i].parentNode.event_listener_table){
				all_tags[i].addResponse(j,all_tags[i].parentNode.event_listener_table[j]);
			}
		}
		
		if(all_tags[i].tagName){
			if(all_tags[i].tagName.toLowerCase() == "input"){		
				all_tags[i].helptext = all_tags[i].value;
				
				all_tags[i].addResponse("focus","if(this.value==this.helptext){this.value='';}");
				all_tags[i].addResponse("blur","if(!this.value){this.value=this.helptext;}");
			}
		}
	}
}

var smallwords = new Array();
smallwords["of"] = true;
smallwords["a"] = true;
smallwords["the"] = true;
smallwords["and"] = true;
smallwords["an"] = true;
smallwords["or"] = true;
smallwords["but"] = true;
smallwords["nor"] = true;


function inTitleCase(str){
	var words = new Array();
	var rval;
	
	words = str.split(' ');
	
	
	
	for(i=0; i<words.length; i++){
		words[i] = words[i].toLowerCase();
		if((!smallwords[words[i]])||(i==0)){
			words[i] = words[i].substring(0,1).toUpperCase() + words[i].substring(1, words[i].length);
		}
		
	}
	
	rval = words[0];
	
	for(i=1; i<words.length; i++){
		rval += " " + words[i];
	}
	
	return rval;
}