// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function add_load_event(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
function add_resize_event(func) {
  var oldonresize = window.onresize;
  if (typeof window.onresize != 'function') {
    window.onresize = func;
  } else {
    window.onresize = function() {
      if (oldonresize) {
        oldonresize();
      }
      func();
    }
  }
}
function hide_spinner() {
    Effect.Fade($('loading'));
    l = $('loading_overlay');
    if (l) {
        Effect.Fade(l);
    }

}
function show_spinner() {
    Element.show($('loading'));
}

add_load_event(hide_spinner);

function center(object)  {
    o = $(object);
    o.style.position = "absolute";
    o.style.left = "50%";
    o.style.top = "50%";
    o.style.marginLeft = "-" + parseInt(o.offsetWidth / 2) + "px";
    o.style.marginTop = "-" + parseInt(o.offsetHeight / 2) + "px";

}
function toggle_xmap_list_item(title_id, list_type, animate) {
    e_id = 'xv_' + title_id + '_' + list_type;
    Effect.toggle($(e_id), 'slide');
    image_id = 'xv_tree_' + title_id + '_' + list_type
    if($(image_id).className == "tree_open") {
        $(image_id).className = "tree_closed"
        $(image_id).src = "/images/tree_closed.png"
    } else {
        $(image_id).className = "tree_open"
        $(image_id).src = "/images/tree_open.png"
    }
}

function initHighlightHelper(t)
{
    var allfields = document.getElementsByTagName(t);

    // loop through all input tags and add events
    for (var i=0; i<allfields.length; i++){
        var field = allfields[i];
        if ((field.getAttribute("type") == "text") || (field.getAttribute("type") == "password") || (t == "textarea") || (t == "iframe") ) {
            if (t != "iframe") {
                field.className = "text_input";
                field.onfocus = function () {this.className = 'text_input_active'; this.focus()}
                field.onblur = function () {this.className = 'text_input';}
            } else {
                field.className = "widgIframe text_input";
                field.onfocus = function () {this.className = 'widgIframe text_input_active'; this.focus()}
                field.onblur = function () {this.className = 'widgIframe text_input';}
            }
        }
    }
}

function init_step_blinder()
{
    // Requires mouseenter.js
    // see: http://blog.stchur.com/2007/03/15/mouseenter-and-mouseleave-events-for-firefox-and-other-non-ie-browsers/

    var steps = $$('.step_animate_hook');
    for (var i=0; i < steps.length; i++) {
        xb.addEvent(steps[i], 'mouseenter', show_step, false);
        xb.addEvent(steps[i], 'mouseleave', hide_step, false);
        steps[i].theChild = get_step_child(steps[i]);
        steps[i].theChildType = get_step_type(steps[i].theChild);
        steps[i].theChild.style.display = "none";
    }
}
function get_step_child(e) {
    r = Element.down(e, ".internal_step_animate_container");
    if (r != null) {
        return r;
    }
    return Element.down(e, ".external_step_animate_container");
}
function get_step_type(c) {
    return c.className.replace("_step_animate_container","");
}

function is_insert_step(e, x) {
    return (Element.descendantOf(e,Element.down(x, ".internal_step_insert")) ||
            Element.descendantOf(e,Element.down(x, ".external_step_insert")) );
}

function animate_insert(xmap_container, direction, delta) {
        $('scroller').scrollLeft += 100 * delta * direction;
        time_line = Element.down(xmap_container, ".time");
        xmap_container.style.width = (200 + (200 * delta * ((direction + 1) / 2) )) + "px";
        time_line.style.width = (200 + (200 * delta * ((direction + 1) / 2) )) + "px";
        Element.down(xmap_container, ".external_step_insert").style.marginLeft = (100 * delta * ((direction + 1) / 2) ) + "px";
        Element.down(xmap_container, ".internal_step_insert").style.marginLeft = (100 * delta * ((direction + 1) / 2) ) + "px";
}

function show_step(e) {
    if (this.theChildType == "internal") {
        Effect.SlideDown(this.theChild, {duration:0.15, queue: {position:'end', scope: 'show_step', limit:2} });
    } else {
        Effect.BlindDown(this.theChild, {duration:0.15, queue: {position:'end', scope: 'show_step', limit:2} });
    }

    xmap_container = Element.up(this, ".xmap_step_container");

    if  (is_insert_step(this, xmap_container) ) {
        animate_insert(xmap_container, 1, 1);
    }
}

function hide_step(e) {
    if (this.theChildType == "internal") {
        Effect.SlideUp(this.theChild, {duration:0.15, queue: {position:'end', scope: 'show_step', limit:2} });
    } else {
        Effect.BlindUp(this.theChild, {duration:0.15, queue: {position:'end', scope: 'show_step', limit:2} });
    }

    xmap_container = Element.up(this, ".xmap_step_container");

    if  (is_insert_step(this, xmap_container) ) {
        animate_insert(xmap_container, -1, 1);
    }
}
/* Disabled for performance reasons: add_load_event(init_step_blinder); */

function update_times() {
    var time_items = $$('.time_item_hook');
    total_time = 0;
    for (var i=0; i < time_items.length; i++) {
        var time = Element.down(time_items[i], ".elapsed_time").innerHTML.split(" ")
        elapsed_time = parseInt(time[0]);
        if (time[1] == "day" || time[1] == "days") {
            elapsed_time *= 8;
        }
        total_time += elapsed_time;
        var e = Element.down(time_items[i], "span.total_time");
        days = Math.floor(total_time / 8);
        hours = total_time % 8;
        e.innerHTML =  days + " day" + (days == 1 ? " " : "s ") + hours + " hour" + (hours == 1 ? "" : "s");
        e.title = total_time + " hours";
    }
}

function initHighlight() {
    if (!document.getElementsByTagName){ return; }
    initHighlightHelper("input");
    initHighlightHelper("textarea");
    initHighlightHelper("iframe");
}
add_load_event(initHighlight);

function change_title_color(i) {
    $('header').className = "header_color_" + i;
}

function toggle_time(id) {
    Effect.toggle("time_display_" + id, "blind");
    Effect.toggle("time_controls_" + id, "blind");
}
function endNodeIeFix() {
    setTimeout("endNodeIeFixPart2()",100);
}
function endNodeIeFixPart2() {
    e = $$('div.unique_end_node');
    for (var i=0; i < e.length; i++) {
        e[i].onmouseover = function() {Element.addClassName(this, "unique_end_node_hover")}
        e[i].onmouseout = function() {Element.removeClassName(this, "unique_end_node_hover")}
    }
}
add_load_event(endNodeIeFix);

function getWindowHeight() {
    var windowHeight=0;
    if (typeof(window.innerHeight)=='number') {
        windowHeight=window.innerHeight;
    }
    else {
        if (document.documentElement&&
            document.documentElement.clientHeight) {
            windowHeight=
                document.documentElement.clientHeight;
        }
        else {
            if (document.body&&document.body.clientHeight) {
                windowHeight=document.body.clientHeight;
            }
        }
    }
    return windowHeight;
}

function set_footer() {
    //    alert($('fixed_footer_1').getHeight() );
    //alert($('content_container').getHeight() );

    if (document.getElementById) {
        var windowHeight=getWindowHeight();
        if (windowHeight>0) {
            var contentHeight=
                document.getElementById('content_container').offsetHeight;
            var footerElement=
                document.getElementById('fixed_footer_1');
            var footerHeight=footerElement.offsetHeight;
            if (windowHeight-(contentHeight+footerHeight)>=0) {
                footerElement.style.position='relative';
                footerElement.style.top=(windowHeight-
                                         (contentHeight+footerHeight))+'px';
            }
            else {
                footerElement.style.position='static';
            }
        }
    }
}
add_load_event(set_footer)
add_resize_event(set_footer)
