/********************************
 * 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      *
 ********************************/

/* From http://www.alistapart.com/articles/horizdropdowns/ */
function loadNavMenuHoverStates() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav");
		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 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 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 selected';

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

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 selected';

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


function changeGalleryImage(galleryName, index) {
    var target = galleryName + '_image_' + index;
    var caption = galleryName + '_caption_' + index;
    if (!$(target)) return false;

    var queue = Effect.Queues.get('global');
    queue.each(function(e) { e.cancel() });

    for (var i = 0; i < gallerySize[galleryName]; i++) {
        el = $(galleryName + '_image_' + i);
        if (el.id != target && el.style.display != 'none') Effect.Fade(el, { duration: 0.6 });

        el = $(galleryName + '_thumb_' + i);
        if (el) {
            if (i == index) {
                el.addClassName('current');
                el.onmouseover = null;
                el.onmouseout = null;
            } else {
                el.removeClassName('current');
                el.onmouseover = function () { $(this).addClassName('current'); };
                el.onmouseout = function () { $(this).removeClassName('current'); };
            }
        }
    }

    Effect.Appear(target, { duration: 0.6 });

    if($(caption) && $(caption).innerHTML != '') {
        $(galleryName + '_caption').style.display = 'block';
        $(galleryName + '_caption').innerHTML = $(caption).innerHTML;
    } else {
        $(galleryName + '_caption').style.display = 'none';
    }

    prevIndex = index == 0 ? gallerySize[galleryName] - 1 : index-1;
    nextIndex = index == gallerySize[galleryName] - 1 ? 0 : index+1;
    $(galleryName + '_prev_button').onclick = function () { changeGalleryImage(galleryName, prevIndex) };
    $(galleryName + '_next_button').onclick = function () { changeGalleryImage(galleryName, nextIndex) };
}



/* 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();
    }
  }
}

