// page initialization
function init() {
    stripe_tables();

    if(document.forms[0]) {
        if(document.forms[0].name == "raids")
            form_raidsinit();
    }
}

// http://www.sergiopereira.com/articles/prototype.js.html

function toggle_me(obj) {
    parent = obj;

    while(parent.tagName != 'TR')
        parent = parent.parentNode;

    toon_name = parent.getElementsByTagName('TD')[0];

    //var toggleVar = new Ajax.Request(
}

function stripe_tables() {
    theTables = document.getElementsByTagName('TABLE');

    var i, j;

    for(i = 0; i < theTables.length; i++) {
        theTable = theTables[i];

        if(theTable.className) continue;

        theRows = theTable.getElementsByTagName('TR');

        var even_odd = "even";

        for(j = 0; j < theRows.length; j++) {
            even_odd = (even_odd == "even") ? "odd" : "even";

            theRows[j].className = even_odd;
        }
    }
}

/**
 * Wrapper function for apoc_save_field(), tailored for checkboxes.
 *
 * @param k   the key that identifies this record
 * @param f   the database field
 * @param o   the form object in question
 */
function apoc_save_checkbox(k, f, o) {
    var val;

    if(o.checked) val = 1;
    else val = 0;

    apoc_save_field(f, k, val, o);
}

/**
 * Save a field to the database via XMLHttpRequest.
 *
 * @param k   the key that identifies this record
 * @param f   the database field
 * @param v   the new value
 * @param o   the form object
 */
function apoc_save_field(f, k, v, o) {
    v.enabled = false;

    var responseSuccess = function(o) {
        o.argument.setAttribute('disabled','');
        o.argument.removeAttribute('disabled');

        // alert(o.responseText);
    }

    var responseFailure = function() {
        alert("Change could not be saved.");
    }

    var callback = {
        success: responseSuccess,
        failure: responseFailure,
        argument: o
    }

    var url = 'http://www.apocalypseguild.info/wow-includes/post/save-field.php?f='+escape(f)+'&v='+escape(v)+'&k='+escape(k);
    YAHOO.util.Connect.asyncRequest('GET',url,callback,null);
    o.setAttribute('disabled', 'disabled');
}

function smudge(obj) {
    if(obj.className == "active") { obj.className = ''; return; }

    obj.className = "active";
}

function toggle_sixties() {
    var theDivs = document.getElementsByTagName("DIV");
    var theSmudge;

    if(sixties_on_off == "on") sixties_on_off = "off";
    else sixties_on_off = "on";

    for(var i = 0; i < theDivs.length; i++) {
        if(theDivs[i].className == "smudge") {
            theSmudge = theDivs[i];
        }
    }

    if(!theSmudge) return;

    theLinks = theSmudge.getElementsByTagName("a");

    var theName;

    for(i = 0; i < theLinks.length; i++) {
        theName = theLinks[i].innerHTML;

        if(characters[theName] > 58) continue;

        if(sixties_on_off == "off") {
            theLinks[i].style.display = "block";
        } else {
            theLinks[i].style.display = "none";
        }
    }
}

function toggle_div(obj, name) {
    theDiv = document.getElementById(name);

    if(!theDiv) return;

    if(theDiv.style.display == "none") {
        theDiv.style.display = "block";
        obj.innerHTML = "Hide";
    } else {
        theDiv.style.display = "none";
        obj.innerHTML = "Show";
    }
}

item_cache = new Array();
var currentItem;

function set_item_text(s) {
    theDiv.innerHTML = s;
}

function show_item(obj,id) {
    if(!ns6 && !ie) return;

    currentItem = id;

    theBody = document.getElementsByTagName('body')[0];
    theDiv = document.getElementById('tbitem');

    if(!theDiv) {
        theDiv = document.createElement("div");
        theDiv.setAttribute("id", "tbitem");
        theBody.appendChild(theDiv);

        if (ie || ns6)
            tipobj=document.all? document.all["tbitem"] : document.getElementById? document.getElementById("tbitem") : "";
    }

    if(item_cache[id]) {
        enabletip = true;
        theDiv.style.display = "";
        set_item_text(item_cache[id]);
        return;
    }

    // if we got this far, we are fetching a new item

    enabletip = true;
    theDiv.style.display = "";
    set_item_text("<b>"+id+"</b><br><br>Loading...");

    var responseSuccess = function(o) {
        item_cache[id] = o.responseText;

        // don't display this new item if there has been a mouseover since the request started
        if(o.argument == currentItem) {
            set_item_text(item_cache[id]);
        }
    }

    var responseFailure = function(o) {
        theDiv.innerHTML = "<b>"+id+"</b><br><br>Failure fetching tooltip.";
    }

    var callback = {
        success: responseSuccess,
        failure: responseFailure,
        argument: id
    }

    var url = 'http://www.apocalypseguild.info/wow-includes/post/thott-item.php?id='+escape(id);
    YAHOO.util.Connect.asyncRequest('GET',url,callback,null);
}

function hide_item() {
    if(!ns6 && !ie) return;

    enabletip = false;

    theDiv = document.getElementById('tbitem');
    theDiv.style.display = "none";
}

/***********************************************
* Cool DHTML tooltip script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var offsetxpoint=10; //Customize x offset of tooltip
var offsetypoint=10; //Customize y offset of tooltip
var ie=document.all;
var ns6=document.getElementById && !document.all;
var enabletip=false;

var tipobj;

if (ie || ns6)
    var tipobj=document.all? document.all["tbitem"] : document.getElementById? document.getElementById("tbitem") : "";

function ietruebody(){
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function positiontip(e){
    if (enabletip) {
        var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
        var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;

        //Find out how close the mouse is to the corner of the window
        var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20;
        var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20;

        var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000;

        //if the horizontal distance isn't enough to accomodate the width of the context menu
        if (rightedge<tipobj.offsetWidth)
            //move the horizontal position of the menu to the left by it's width
            tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px";
        else if (curX<leftedge)
            tipobj.style.left="5px";
        else
            //position the horizontal position of the menu where the mouse is positioned
            tipobj.style.left=curX+offsetxpoint+"px";

        //same concept with the vertical position
        if (bottomedge<tipobj.offsetHeight)
            tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px";
        else
            tipobj.style.top=curY+offsetypoint+"px";

        tipobj.style.visibility="visible";
    }
}

document.onmousemove=positiontip;

/* END DYNAMIC DRIVE */
