/* communities page script */

/* Explore control */
//initial values
var communityDefaultSearchString = "Search for";
var allDivs = document.getElementsByTagName("div");
var allTables = document.getElementsByTagName("table");
var shellFrameTop = 0;
var shellFrameLeft = 0;
var isIE6 = (navigator.appVersion.split(";")[1] == " MSIE 6.0");

//input field receives focus
function communitySrchInputFocus(cNode)
{
    if(cNode != null)
    {
	    cNode.className = "communitySrchInput communityEnabledText";
	    if(cNode.value == communityDefaultSearchString)
	    {
		    cNode.value = "";
		    return;
	    }
	    else
	    {
		    return;
	    }
    }
}

//input field is pressed
function communitySrchInputKeyPress(currentNode)
{
    return;
}

//input field is blurred
function communitySrchInputBlur(cNode)
{
    if(cNode != null)
    {
        cNode.value = communityTrimString(cNode.value);
		
        if(cNode.value == null || cNode.value == "")
        {
            cNode.className = "communitySrchInput communityDisabledText";
	        cNode.value = communityDefaultSearchString;
        }
    }
    return;
}

//trims strings
function communityTrimString(s)
{
    while(s.charAt(0) == " ")
    {
	    s = s.replace(s.charAt(0), "");
    }
	
    while(s.charAt(s.length -1) == " ")
    {
	    s = s.replace(s.charAt(s.length -1), "");
    }
	
    return s;
}

//displays explore filter menu
function communitySrchShowFilter(currentEvent)
{

    //get offset of shellFrame cell
    if(document.getBoxObjectFor == null && !isIE6)
    {
        //IE
        for(i=0; i < allTables.length; i++)
        {
            if(allTables[i].className == "glassFrame")
            {
                shellFrameTop = allTables[i].getClientRects()[0].top;
                break;
            }
        }
    }
    else
    {
        //IE6
        if(isIE6)
        {
            for(i=0; i < allDivs.length; i++)
            {
                if(allDivs[i].className == "zoneThreeDiv")
                {
                    shellFrameTop = allDivs[i].getClientRects()[0].top;
                    shellFrameLeft = allDivs[i].getClientRects()[0].left;
                    break;
                }
            }
        }
        else
        {
            //FF
            for(i=0; i < allDivs.length; i++)
            {
                if(allDivs[i].className == "zoneThreeDiv")
                {
                    shellFrameTop = document.getBoxObjectFor(allDivs[i]).y;
                    shellFrameLeft = document.getBoxObjectFor(allDivs[i]).x;
                    break;
                }
            }
        }
    }

    //get text version of filter selector
    var filterBtn2 = document.getElementById("communitySrchTextSelect");
    
    //show filter list
    communitySrchFilterPane.style.display = "block";
    
    if(document.getBoxObjectFor == null)
    {
        //IE
        communitySrchFilterPane.style.top = filterBtn2.getClientRects()[0].bottom - shellFrameTop;
        eval(communitySearchFilterPaneStringIE);
    }
    else
    {
        //FF
        communitySrchFilterPane.style.top = document.getBoxObjectFor(filterBtn2).y + document.getBoxObjectFor(filterBtn2).height - shellFrameTop;
        eval(communitySearchFilterPaneStringFF);
    }

    for(i=0; i<communitySrchFilterPane.childNodes.length; i++)
    {
        if(communitySrchFilterPane.childNodes[i].nodeName.toLowerCase() == "a" && communitySrchFilterPane.childNodes[i].innerHTML != null)
        {
            if(communitySrchFilterPane.childNodes[i].innerHTML.replace("&amp;","&") == filterBtn2.value)
            {
                communitySrchFilterPane.childNodes[i].className = "communitySrchFilterChoiceSelected";
            }
            else
            {
                communitySrchFilterPane.childNodes[i].className = "communitySrchFilterChoice";
            }
        }
    }
    return;
}

//applies explore filtering
function communitySrchSelectFilter(currentEvent)
{
    if(currentEvent.target != null)
    {
        //FF
        var currentNode = currentEvent.target;
    }
    else
    {
        //IE
        var currentNode = currentEvent.srcElement;
    }

    if(currentNode.className == "communitySrchFilterChoice")
    {
    
        if(currentNode.getAttribute("dg") != null && currentNode.getAttribute("dg") != "")
        {
            if(currentNode.getAttribute("dg") == "all")
            {
                document.communitySrchForm.communityDg.value = "";
            }
            else
            {
                document.communitySrchForm.communityDg.value = currentNode.getAttribute("dg");
            }
            document.communitySrchForm.communitySrchTextSelect.value = currentNode.innerHTML.replace("&amp;","&");
            document.communitySrchForm.communitySrchTextSelect.title = currentNode.innerHTML;
        }
    }

    if(currentNode.id != "communitySrchSelect" && currentNode.id != "communitySrchTextSelect")
    {
       // communitySrchFilterPane.style.display = "none";
    }
    return;
}

//updates the title field of the text filter selection element
function communitySetTextSelect()
{
    //document.communitySrchForm.communitySrchTextSelect.title = document.communitySrchForm.communitySrchTextSelect.value;
    return;
}

//checks explore string and submits form
function communityValidateSearch(cNode)
{
    if(cNode.value != null && cNode.value != "" && cNode.value != communityDefaultSearchString)
    {
        document.getElementById("communitySrchForm").submit();
    }
    return;
}

/* hero navigation control */
//global values
var communityCurrentHero;
var communityCurrentHeroImg;
var communityHeroNavInitialized = false;
var communityHeroNavCurrentIndex = 0;

//initialize hero nav
function communityInitHeroNav()
{
    //add existing hero properties to array
    if(document.getElementById("heroImageNav") != null && document.getElementById("heroImageNavSrc") != null)
    {
        communityCurrentHero = document.getElementById("heroImageNav");
        communityCurrentHeroImg = document.getElementById("heroImageNavSrc");
        
        if(heroCollection != null)
        {
            heroCollection.push(communityCurrentHero.getAttribute("href") + "," + communityCurrentHeroImg.getAttribute("src") + "," + communityCurrentHeroImg.getAttribute("alt"));
            communityHeroNavCurrentIndex = heroCollection.length - 1;
            communityHeroNavInitialized = true;
        }
        else
        {
        
        }
    }
}

function communityCycleHeroNav(direction)
{
    if(heroCollection != null && communityHeroNavInitialized)
    {
        //set new index value
        if(direction == "-")
        {
            if(communityHeroNavCurrentIndex == 0)
            {
                communityHeroNavCurrentIndex = heroCollection.length - 1;
            }
            else
            {
                communityHeroNavCurrentIndex --;
            }
        }
        else if(direction == "+")
        {
            if(communityHeroNavCurrentIndex == heroCollection.length - 1)
            {
                communityHeroNavCurrentIndex = 0;
            }
            else
            {
                communityHeroNavCurrentIndex ++;
            }
        }
        
        //update hero nav
        var heroProperties = heroCollection[communityHeroNavCurrentIndex].split(",");
        if(heroProperties.length == 3)
        {
            communityCurrentHero.href = heroProperties[0];
            communityCurrentHeroImg.src = heroProperties[1];
            communityCurrentHeroImg.alt = heroProperties[2];
            communityCurrentHeroImg.title = heroProperties[2];
        }
    }
    
}