﻿// Tailored expansion for page options
function pref(id, path, option)
{
    var expand = 'expand';
    var options = 'Master_options';
    var prefhead = 'prefhead';
    if (option == '2')
    {
        expand += '2';
        options += '2';
        prefhead += '2';
    }
    if (returnObjById(id).style.display == "block") // id is currently visible.
    {
        returnObjById(expand).style.backgroundImage = "url('http://www.growerz.com/images/expand.gif')";
        returnObjById(expand).setAttribute("href", "?pref=" + options);
        var s = returnObjById(id).offsetHeight;
        ac = null;
        var y = 5; //Milliseconds between steps
        var z = 30; //Step size
        if (navigator.appName == "Microsoft Internet Explorer")
        {
            y = 10;
            z = 15;
        }
        aniclose(id, 1, s, z, y, prefhead, 'borderBottom', 'none');      
    }
    else // id is currently collapsed
    {
        returnObjById(expand).style.backgroundImage = "url('http://www.growerz.com/images/contract.gif')";
        returnObjById(expand).setAttribute("href", "");
        returnObjById(prefhead).style.borderBottom = "solid 1px #dedede";
        returnObjById(id).style.visibility = "hidden";
        returnObjById(id).style.position = "absolute";
        returnObjById(id).style.display = "block";
        returnObjById(id).style.height = "auto";
        var e = returnObjById(id).offsetHeight;
        returnObjById(id).style.height = 1 + "px";
        returnObjById(id).style.position = "relative";
        returnObjById(id).style.visibility = "visible";
        ae = null;
        var y = 5; //Milliseconds between steps
        var z = 30; //Step size
        if (navigator.appName == "Microsoft Internet Explorer")
        {
            y = 10;
            z = 15;
        }
        aniexpand(id, e, 1, z, y);
    }
}

// Custom function to expand forum message.
function expandForum(id, classname, act)
{
    if (act && collapseClassForum(classname, id))
    {
        returnObjById(id).style.visibility = "hidden";
        returnObjById(id).style.position = "absolute";
        returnObjById(id).style.display = "block";
        var width = returnObjById('forumTable').offsetWidth;
        returnObjById(id).style.width = width + "px"; //Needed for text wrapping
        returnObjById(id).style.height = "auto";
        var e = returnObjById(id).offsetHeight;
        returnObjById(id).style.height = 1 + "px";
        returnObjById(id).style.position = "relative";
        returnObjById(id).style.visibility = "visible";
        var idHead = id.substring(0, id.length - 7) + "head";
        returnObjById(idHead).style.backgroundColor = "#9cc7e8";
        ae = null;
        aniexpand(id, e, 1, 30, 10);
    }
}

// Custom collapse class for the forum.
function collapseClassForum(classname, id)
{
    var getClass = new Array();
    var doExpand = "true";
    getClass = getElementsByClass(classname);
    for (i=0; i<getClass.length; i++)
    {
        if (getClass[i].style.display == "block") // This panel is visible.
        {
            if (getClass[i] == returnObjById(id)) // This visible panel has been clicked on. Do not execute expand for element.
                doExpand = "false";
            var idHead = getClass[i].id.substring(0, getClass[i].id.length - 7) + "head";
            returnObjById(idHead).style.backgroundColor = "#f3fff6";
            var s = getClass[i].offsetHeight;
            ac = null;
            aniclose(getClass[i].id, 1, s, 30, 10, 'none', 'n', 'n');
        }
    }
    if (doExpand == "true")
        return true;
    else
        return false;
}






// Function to expand one "main" div and collapse whatever is open.
function expand(id, classname, width)
{
    if (collapseClass(classname, id))
    {
        returnObjById(id).style.visibility = "hidden";
        returnObjById(id).style.position = "absolute";
        returnObjById(id).style.display = "block";
        if (width)
            returnObjById(id).style.width = width; //Needed for text wrapping
        returnObjById(id).style.height = "auto";
        var e = returnObjById(id).offsetHeight;
        returnObjById(id).style.height = 1 + "px";
        returnObjById(id).style.position = "relative";
        returnObjById(id).style.visibility = "visible";
        ae = null;
        aniexpand(id, e, 1, 30, 10);
    }
}

// Function used by news scripts that expand "extra" information but where the original id is never actually hidden.
function expandFullText(id, classname, heightFull, showCollapsed, showExpanded, heightBlurb)
{
    var getClass = new Array();
    var doExpand = true;
    getClass = getElementsByClass(classname);
    for (i=0; i<getClass.length; i++)
    {
        if (getClass[i].style.display == "inline") // This text is visible.
        {
            if (getClass[i] == returnObjById(showExpanded)) // This visible panel has been clicked on. Do not execute expand for element.
                doExpand = false;
            var pane = getClass[i].id.substring(0,19) + "pane";
            var more = getClass[i].id.substring(0,19) + "more";
            var blurb = getClass[i].id.substring(0,19) + "blurb";
            var full = getClass[i].id.substring(0,19) + "full";
            var s = returnObjById(pane).offsetHeight;
            var e;
            if (returnObjById(heightBlurb).innerHtml)
                e = returnObjById(heightBlurb).innerHtml;
            else
            {
                var newDiv = returnObjById(blurb).cloneNode(true);
                newDiv.style.visibility = "hidden";
                newDiv.style.position = "absolute";
	            newDiv.style.display = "block";
	            newDiv.id = "newDiv";
	            newDiv.style.width = returnObjById(blurb).offsetWidth + "px";
	            returnObjById(pane).appendChild(newDiv);
                e = returnObjById(heightBlurb).innerHtml = newDiv.offsetHeight;
                returnObjById(pane).removeChild(returnObjById('newDiv'));
            }
            ac = null;
            aniclose(pane, e, s, 30, 10, more+","+getClass[i].id, 'display'+","+'display', 'inline'+","+'none');
        }
    }
    if (doExpand == true)
    {
        var e;
        if (returnObjById(heightFull).innerHtml)
            e = returnObjById(heightFull).innerHtml;
        else
        {
            var newDiv = returnObjById(showExpanded).cloneNode(true);
            newDiv.style.visibility = "hidden";
            newDiv.style.position = "absolute";
            newDiv.style.display = "block";
            newDiv.id = "newDiv";
            newDiv.style.width = returnObjById(showExpanded.substring(0,19) + "blurb").offsetWidth + "px";
            returnObjById(showExpanded.substring(0,19) + "pane").appendChild(newDiv);
            e = returnObjById(heightFull).innerHtml = newDiv.offsetHeight;
            returnObjById(showExpanded.substring(0,19) + "pane").removeChild(returnObjById('newDiv'));
        }
        var start = returnObjById(id).offsetHeight; 
        returnObjById(heightBlurb).innerHtml = start;
        e = e + start;
        ae = null;
        returnObjById(showExpanded).style.display = "inline";
        returnObjById(showCollapsed).style.display = "none";
        aniexpand(id, e, start, 30, 10);
    }
}

// Check here which ID is expanded. If multiple, close all.
// Returns true if the the ID it was passed was not expanded
// Returns false if the ID it was passed was was expanded and has now been collapsed.
function collapseClass(classname, id)
{
    var getClass = new Array();
    var doExpand = "true";
    getClass = getElementsByClass(classname);
    for (i=0; i<getClass.length; i++)
    {
        if (getClass[i].style.display == "block") // This panel is visible.
        {
            if (getClass[i] == returnObjById(id)) // This visible panel has been clicked on. Do not execute expand for element.
            {
                doExpand = "false";
            }
            var s = getClass[i].offsetHeight;
            ac = null;
            aniclose(getClass[i].id, 1, s, 30, 10, 'none', 'n', 'n');
        }
    }
    if (doExpand == "true")
        return true;
    else
        return false;
}

// Resize a parent div if a child div has changed size
function resize(parent, child)
{
    var parentsize = returnObjById(parent).offsetHeight;
    var childsize = returnObjById(child).offsetHeight + 15;
    if (parentsize < childsize)
    {
        //Increase size of parent.
        ae = null;
        aniexpand(parent, childsize, parentsize, 30, 10);
    }
    else if (parentsize > childsize)
    {
        //Decrease size of parent
        ac = null
        aniclose(parent, childsize, parentsize, 30, 10, 'none', 'n', 'n');
    }
    else
    {
        //They're the same size. Return.
        return;
    }
    nowexpanded = parent;
}

// Show a div
function show(showid)
{
    returnObjById(showid).style.display = "block";
}

// Hide a div
function hide(hideid)
{
    returnObjById(hideid).style.display = "none";
}

// Animate the expansion
function aniexpand(id, e, s, step, rate)
{
    if ((s+step) >= e)
    {
        footerFix();
        returnObjById(id).style.height = e + "px";
        if (ae != null)
        {
            clearTimeout(ae);
        }
        return;
    }
    var s1 = s + step;
    returnObjById(id).style.height = s1 + "px";
    var ae=setTimeout("aniexpand('"+id+"', "+e+", "+s1+", "+step+", "+rate+")", rate);
}

// Animate the contraction
// fprop = final property
// fact = final property's value
// fid = id on which to set the final property
// id = id of div to collapse
// e = end size
// s = start size
// step = the size of the step
// rate = the rate of collapse (in ms)
function aniclose(id, e, s, step, rate, fid, fprop, fact)
{
    if ((s-step) <= e)
    {
        footerFix();
        if (fid != "none")
        {
            var fids = fid.split(",");
            var fprops = fprop.split(",");
            var facts = fact.split(",");
            for (var i=0; i<fids.length; i++)
            {
                switch (fprops[i])
                {
                    case "borderBottom":
                        returnObjById(fids[i]).style.borderBottom = facts[i];
                        break;
                    case "display":
                        returnObjById(fids[i]).style.display = facts[i];
                        break;
                    default:
                        break;
                }
            }
        }
        if (e < 5)
        {
            returnObjById(id).style.display = "none";
            returnObjById(id).style.height = "auto";
        }
        else
        {
            returnObjById(id).style.height = e + "px";
        }
        if (ac != null)
        {
            clearTimeout(ac);
        }
        return;
    }
    var s1 = s - step;
    returnObjById(id).style.height= s1 + "px";
    var ac=setTimeout("aniclose('"+id+"',"+e+", "+s1+", "+step+", "+rate+", '"+fid+"', '"+fprop+"', '"+fact+"')", rate);
}

// Returns the query variable passed to it or an empty string if not found
function getQueryVariable(variable)
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++)
    {
        var pair = vars[i].split("=");
        if (pair[0] == variable)
        {
            return pair[1];
        }
    } 
    return "";
}

// Used as text counter for TextBox fields
function textCounter(field,cntfield,maxlimit)
{
    if (field.value.length > maxlimit)
    {
        // It's too long, trim
        field.value = field.value.substring(0, maxlimit);
    }
    else
    {
        // Update counter
        cntfield.value = maxlimit - field.value.length;
    }
}

// Fix for IE to keep the footer at the bottom of the page.
function footerFix()
{
	var x = returnObjById('main').offsetHeight;
	var y = returnObjById('side').offsetHeight;
	var z = Math.max(x, y);
	returnObjById('mc').style.height = z + "px";
}