/********************************
 * site-specific settings       *
 ********************************/

var showRolloverMenuEffect = Effect.BlindDown;
var showRolloverMenuEffectOptions = { duration: 0.2 };
var hideRolloverDelay = 150;
var hideRolloverMenuEffect = Effect.BlindUp;
var hideRolloverMenuEffectOptions = { duration: 0.2 };
var featureFadeDelay = 8000;
var featureRotateTimeout = null;

/********************************
 * site-specific functions      *
 ********************************/

function autoResizeFeaturesDiv() {
    featuredivs = $$('#features .feature');
    masterDiv = $('features');
    masterDiv.style.display = 'block';

    var max = 0;

    featuredivs.each(function (feature) {
        feature.show();
        if (parseInt(masterDiv.scrollHeight) > parseInt(feature.scrollHeight)) {
            max = Math.max(max, parseInt(masterDiv.scrollHeight) - 30);
        } else {
            max = Math.max(max, parseInt(feature.scrollHeight) - 0);
        }
        feature.hide();
    });

    masterDiv.style.height = '' + max + 'px';
    featuredivs[0].show();
}


function resizeText() {
	var body = document.body;
	// var curSize = body.style.fontSize, newSize = '';
	var curSize = 'normal', newSize = '';
	
	if (body.className.indexOf('text-x-large') != -1)
		curSize = 'x-large';
	else if (body.className.indexOf('text-large') != -1)
		curSize = 'large';

	switch(curSize) {
		case 'normal':
			newSize = 'large';
			break;
		case 'large':
			newSize = 'x-large';
			break;
		case 'x-large':
			newSize = 'normal';
			break;
		default:
			newSize = 'large';
	}

	// save text size in cookie
	var date = new Date();
	date.setTime(date.getTime() + (352 * 24 * 60 * 60 * 1000));
	document.cookie = 'textSize=' + newSize + '; expires=' + date.toGMTString() + '; path=/';
	
	
	swapTextSizes(curSize, newSize);
}

function swapTextSizes(curSize, newSize) {
	$(document.body).removeClassName('text-' + curSize);
	$(document.body).addClassName('text-' + newSize);
}

function jailbreakDialog(prefix, id) {
	var d = $(prefix + '_' + id);
	d = d.parentNode.removeChild(d);
	$('dialog-temp').appendChild(d);
}

function jailDialogs(prefix, id) {
	var d = $('dialog-temp');
	d = document.body.removeChild(d);
	$('page_content_form').appendChild(d);
}

/* From http://www.reconn.us/content/view/43/64/, modified by BB */
function bookmark_us(url, title) {
    if (window.sidebar && window.sidebar.addPanel) { // firefox
	window.sidebar.addPanel(title, url, "");
    } else if (window.opera && window.print) { // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
    } else if(document.all) { // ie
        window.external.AddFavorite(url, title);
    }
}

function toggleTextSize(size) {
	var cur = document.body.className.match(/size-\w+/);
}

/* From http://www.alistapart.com/articles/horizdropdowns/ */
function loadNavMenuHoverStates() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav-list");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}

function toggleContent(elt, key) {
	$A($(elt).parentNode.parentNode.childNodes).each(function(el) {
		try {
			switch (el.className) {
				case 'toggle-' + key:
					el.innerHTML = '';
					break;
					
				case 'answer':
					$(el).show();
					break;
			}
		} catch (e) {
			alert(elt + ': ' + e);
		}
	});
	return false;
}

function rotateFeatures(nextIndex) {
    clearTimeout(featureRotateTimeout);
    var list_items = $$('#features .feature');
    var nav_items = $$('#features .nav');

    if (list_items.length != nav_items.length) return false;
    if (list_items.length < 1 || nav_items.length < 1) return false;

	if (list_items.length == 1) {
		list_items[0].setAttribute('style', '');
    	nav_items[0].className = 'nav nav-selected';
	} else if (list_items.length < 1) {
		return false;
	} else {
		if (typeof(nextIndex) != 'undefined') {
	        nextFeature(nextIndex)
	    } else {
	        nextFeature();
	    }

	    featureRotateTimeout = setTimeout(function () { rotateFeatures(); }, featureFadeDelay);
	}
}

function nextFeature(feature_id) {
    var list_items = $$('#features .feature');
    var nav_items = $$('#features .nav');

    var currentIndex = null;

	if (typeof(feature_id) == 'undefined') {
		nextIndex = feature_id
	} else {
		var nextIndex = 0;

		// map feature ID to nextIndex
		for (var i = 0; i < list_items.length; i++) {
			temp = list_items[i].getAttribute('id').split('_');
			id = temp[temp.length - 1];
			if (id == feature_id) {
				nextIndex = i;
				break;
			}
		}
	}

    // hide all selected
	for (var i = 0; i < list_items.length; i++) {
        if (list_items[i].className == 'feature selected') {
            if (!currentIndex) currentIndex = i;
            if (typeof(nextIndex) == 'undefined' || i != nextIndex) {
                Effect.Fade(list_items[i], { duration: 0.3 });
            }
        }

        list_items[i].className = 'feature';
        nav_items[i].className = 'nav';
    }

    if (currentIndex == null) {
        currentIndex = list_items.length - 1;
    }
    if (typeof(nextIndex) == 'undefined') {
        nextIndex = currentIndex + 1;
        if (nextIndex > (list_items.length-1)) nextIndex = 0;
    }

    list_items[nextIndex].className = 'feature selected';
    nav_items[nextIndex].className = 'nav nav-selected';
    
    Effect.Appear(list_items[nextIndex], { duration: 0.4 })
    setTimeout(function () { $(list_items[nextIndex]).style.opacity = 1.0; }, 450);
}

function prevFeature(feature_id) {
    var list_items = $$('#features .feature');
    var nav_items = $$('#features .nav');

    var currentIndex = null;

	if (typeof(feature_id) == 'undefined') {
		nextIndex = feature_id
	} else {
		var nextIndex = 0;

		// map feature ID to nextIndex
		for (var i = 0; i < list_items.length; i++) {
			temp = list_items[i].getAttribute('id').split('_');
			id = temp[temp.length - 1];
			if (id == feature_id) {
				nextIndex = i;
				break;
			}
		}
	}

    // hide all selected
	for (var i = 0; i < list_items.length; i++) {
        if (list_items[i].className == 'feature selected') {
            if (!currentIndex) currentIndex = i;
            if (typeof(nextIndex) == 'undefined' || i != nextIndex) {
                Effect.Fade(list_items[i], { duration: 0.3 });
            }
        }

        list_items[i].className = 'feature';
        nav_items[i].className = 'nav';
    }

    if (currentIndex == null) {
        currentIndex = list_items.length - 1;
    }
    if (typeof(nextIndex) == 'undefined') {
        nextIndex = currentIndex + 1;
        if (nextIndex > (list_items.length-1)) nextIndex = 0;
    }

    list_items[nextIndex].className = 'feature selected';
    nav_items[nextIndex].className = 'nav nav-selected';

    Effect.Appear(list_items[nextIndex], { duration: 0.4 })
}


/* Very good, very popular, unobtrusive event loader
 * that executes functions on page load.
 * - by JS guru Simon Willison.
 * http://simonwillison.net/2004/May/26/addLoadEvent/
 */

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
