﻿/* OtakuElite Javascript Class Library */
/* Release v0.1.8.22.2009              */

//Creates Library name spaces.
var otakuelite = {
    ajax: {},
    animation: {},
    event: {},
    form: {
        tools: {
            search: {}
        }
    },
    string: {},
    style: {}
};
/*-------------------------------------------------------------------------------------------------------------------------------------------------*/
/* otakuelite.ajax */
// Sets the xmlHTTP var for ajax calls.
otakuelite.ajax.setXMLHTTP = new function() {
    var xmlHttp;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msx12.XMLHTTP");
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    return xmlHttp;
}

/*-------------------------------------------------------------------------------------------------------------------------------------------------*/
/* otakuelite.animation */
//Changes the opacity of an element over a set time limit. Note: If hide == 1, display is set to none.
otakuelite.animation.fade = function(id, opacStart, opacEnd, millisec, hide) {
    if (!hide) hide = 0;
    if (navigator.appName == "Microsoft Internet Explorer") millisec = millisec / 3;
    var speed = Math.round(millisec / 100);
    var timer = 0;
    if (opacStart > opacEnd) {
        for (i = opacStart; i >= opacEnd; i--) {
            setTimeout("otakuelite.style.opacity('" + id + "'," + i + ")", (timer * speed));
            timer++;
        }
    } else if (opacStart < opacEnd) {
        for (i = opacStart; i <= opacEnd; i++) {
            setTimeout("otakuelite.style.opacity('" + id + "'," + i + ")", (timer * speed));
            timer++;
        }
    }

    if (hide == 1) setTimeout("document.getElementById('" + id + "').style.display='none';", millisec);
}

/*-------------------------------------------------------------------------------------------------------------------------------------------------*/
/* otakuelite.event */
//Determines if mouse leaves or enters parent element.
otakuelite.event.isMouseLeaveOrEnter = function(e, handler) {
    if (e.type != 'mouseout' && e.type != 'mouseover') return false;
    var reltg = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement;
    while (reltg && reltg != handler) reltg = reltg.parentNode;
    return (reltg != handler);
}

/*-------------------------------------------------------------------------------------------------------------------------------------------------*/
/* otakuelite.form */
//Collapse cells in checkboxlist with table layout
otakuelite.form.tools.search.cbxTable = function(targetID, searchValue) {
    var Results = 0;
    var ParDOM = targetID;
    var iID;
    for (i = 0; i < document.getElementById(ParDOM).childNodes.length; i++) {
        if (document.getElementById(ParDOM).childNodes[i].nodeName == "TBODY") {
            for (x = 0; x < document.getElementById(ParDOM).childNodes[i].childNodes.length; x++) {
                if (document.getElementById(ParDOM).childNodes[i].childNodes[x].nodeName == "TR") {
                    for (y = 0; y < document.getElementById(ParDOM).childNodes[i].childNodes[x].childNodes.length; y++) {
                        if (document.getElementById(ParDOM).childNodes[i].childNodes[x].childNodes[y].nodeName == "TD") {
                            for (z = 0; z < document.getElementById(ParDOM).childNodes[i].childNodes[x].childNodes[y].childNodes.length; z++) {
                                if (document.getElementById(ParDOM).childNodes[i].childNodes[x].childNodes[y].childNodes[z].nodeName == "LABEL") {
                                    if (document.getElementById(ParDOM).childNodes[i].childNodes[x].childNodes[y].childNodes[z].innerHTML.toLowerCase().indexOf(searchValue.toLowerCase()) != -1) {
                                        document.getElementById(ParDOM).childNodes[i].childNodes[x].childNodes[y].childNodes[z].style.visibility = "visible";
                                        iID = document.getElementById(ParDOM).childNodes[i].childNodes[x].childNodes[y].childNodes[z].htmlFor;
                                        document.getElementById(iID).style.visibility = "visible";
                                        Results++;
                                    }
                                    else {
                                        document.getElementById(ParDOM).childNodes[i].childNodes[x].childNodes[y].childNodes[z].style.visibility = "collapse";
                                        iID = document.getElementById(ParDOM).childNodes[i].childNodes[x].childNodes[y].childNodes[z].htmlFor;
                                        document.getElementById(iID).style.visibility = "collapse";
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return Results;
};

/*-------------------------------------------------------------------------------------------------------------------------------------------------*/
/* otakuelite.string */
//Formats URI string for unbroken URL.
otakuelite.string.uriEncode = function(myStr) {
    var myOutput = myStr.replace("%", "%37");
    for (var i = 0; i < myStr.length; i++) {

        if (myStr.charAt(i).match(/[a-zA-Z0-9%]/) == null) {
            myOutput = myOutput.replace(myStr.charAt(i), "%" + myStr.charCodeAt(i));
        }
    }
    return myOutput;
}


otakuelite.string.htmlEncode = function(str, strict) {
    var div = document.createElement('div');
    var text = document.createTextNode(str);
    div.appendChild(text);

    str = div.innerHTML;
    var myOutput = "";
    for (var i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) < 32 || str.charCodeAt(i) > 126) {
            myOutput += "&#" + str.charCodeAt(i) + ";";
        } else {
            myOutput += str.charAt(i);
        }
    }
    return myOutput;
} 


/*-------------------------------------------------------------------------------------------------------------------------------------------------*/
/* otakuelite.style */
//Sets the opacity of an element.
otakuelite.style.opacity = function(id, opacity) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
    
}

//Next four functions are used to detect browser values
function clientWidth() {
    return CrossBrowserResults(
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function clientHeight() {
    return CrossBrowserResults(
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function scrollLeft() {
    return CrossBrowserResults(
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function scrollTop() {
    return CrossBrowserResults(
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function winHeight() {
    var myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myHeight = document.body.clientHeight;
    }
    return myHeight;
}

function winWidth() {
    var myWidth = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
    }
    return myWidth;
}

//Support Function for clientWidth, clientHeight, scrollLeft, and scrollTop
function CrossBrowserResults(RsltinnerWidth, RsltdocElement, Rsltbody) {
    var SetRslt = RsltinnerWidth ? RsltinnerWidth : 0;
    if (RsltdocElement && (!SetRslt || (SetRslt < RsltdocElement)))
        SetRslt = RsltdocElement;
    return Rsltbody && (!SetRslt || (Rsltbody > SetRslt)) ? Rsltbody : SetRslt;
}


/*-------------------------------------------------------------------------------------------------------------------------------------------------*/
/* misc */
function getElementsByClass(searchClass, node, tag) {
    var classElements = new Array();
    if (node == null)
        node = document;
    if (tag == null)
        tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp('(^|\\\\s)' + searchClass + '(\\\\s|$)');
    for (i = 0, j = 0; i < elsLen; i++) {
        if (pattern.test(els[i].className)) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}

function clickElement(elementid) {
    var e = document.getElementById(elementid);
    if (typeof e == 'object') {
        if (document.createEvent) {
            var evObj = document.createEvent('MouseEvents');
            evObj.initEvent('click', true, true);
            e.dispatchEvent(evObj);
            return false;
        }
        else if (document.createEventObject) {
            e.fireEvent('onclick');
            return false;
        }
        else {
            e.click();
            return false;
        }
    }
}                                                    

