/* style.js file for square */

function initializeStyle()
{
    addEvtHandlerToElement('div', '#mainLogo', "onclick", function() { document.location = '/'; return false; });
    addEvtHandlerToElement('div', '#secondBox', "onclick", function() { document.location = '/'; return false; });
    addEvtHandlerToElement('div', '#fourthBox', "onclick", function() { document.location = '/fatmixx-archives/'; return false; });
    addEvtHandlerToElement('div', '#thirdBox', "onclick", function() { document.location = '/about-fatmixx/'; return false; });
    addEvtHandlerToElement('div', '#seventhBox', "onclick", function() { document.location = '/wp-admin/post-new.php'; return false; });

    addEvtHandlerToElement('input', '#searchBox', "onfocus", function () { var myEle = document.getElementById("searchBox"); if (myEle.value == "Type terms, hit enter") { myEle.value = ""; } });

    //addEvtHandlerToElement('div', '.postExpandButton', "onclick", expandItem);
    //addEvtHandlerToElement('a', '.postExpandButtonAnchor', "onclick", expandItem);
}

function expandItem(e)
{
    if (!e) var e = window.event

    var rv = false;

    var target = (e.target) ? e.target : e.srcElement  

    var tagName = new String(target.tagName);

    if (tagName.toLowerCase() == "a")
    {
        target = target.parentNode;
    } 

    var dashPos = target.id.indexOf("-");
    
    if (dashPos > 0)
    {
        var postId = target.id.substr(dashPos+1);
        rv = rv || flipVisibility("postContentWrapper-"+postId, document.getElementById("postExpandButtonAnchor-"+postId));
    }

    if (rv == true)
    {
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();
        return false;
    }
    return true;
}

function flipVisibility(eleId, linkEle)
{
    if (document.getElementById)
    {
        var ele = document.getElementById(eleId);
        if (ele != null)
        {
            ele.style.display = (ele.style.display)?"":"block";
            linkEle.innerHTML = (ele.style.display)?"click to hide commentary":"click to read commentary";
            return true;
        }
    }

    return false;
}

function addEvtHandlerToElement(tagName, cssSelector, handler, funcPtr)
{
    if (!document.getElementsByTagName) return false;
   
    var tagProp = "className";

    if (cssSelector.charAt(0) == '#')
        tagProp = 'id';

    var elements = null;

    if (tagProp == 'id')
    {
        var theEle = document.getElementById(cssSelector.substr(1));
        if (theEle != null)
        {
            elements = new Array( theEle );
        }
    } else {
        elements = document.getElementsByTagName(tagName);
    }

    if (elements != null)
    {
        for (var i=0; i < elements.length; i++) {
            if (elements[i][tagProp].match(cssSelector.substr(1))) {
                elements[i][handler] = funcPtr;
            }
        }
    }
}


if (window.addEventListener)
{
    window.addEventListener("load", initializeStyle, false);
} else if (window.attachEvent)
{
    window.attachEvent("onload", initializeStyle);
}


