var searchbox = null;
var largertext = null;
var smallertext = null;
var searchbutton = null;

window.onload = function () {
	searchbox = document.getElementById('GoogleSearchText');
	searchbutton = document.getElementById('GoogleSearchButton');
	searchbox.onfocus = SearchIn;
	searchbox.onblur = SearchOut;
	searchbox.onkeypress=SubmitOnEnter;
	searchbutton.onclick = SearchSite;
	
	largertext = document.getElementById('LargerText');
	smallertext = document.getElementById('SmallerText');
	largertext.onclick = IncreaseText;
	smallertext.onclick = DecreaseText;
	
	SetAnchorList();
	
	Hightlight();
	SetDoItOnline();
    SetInformationFor();
    
    if (document.getElementById("CommunityServicesDirectory") != null){SetCSD();}
}

function Hightlight(){
//    var ul = document.getElementById("MainMenu");
//    var liArray = ul.getElementsByTagName("li");
//     
//    for (var i = 0; i < liArray.length; i++) {
//        var li = liArray[i];
//        if(li.className == "selected") {
            
            // Set MainMenu
//            li.className += " Tab" + (i+1); 
            
            // Set LandingPane
//            var lp = document.getElementById("LandingPane");
//            lp.className = "LandingPane" + (i+1);
//            lp.innerHTML = "<p class='LandingPaneText'>" + li.getElementsByTagName("a")[0].innerHTML + "</p>";
 
            // Set SubMenuArea
//            var sma = document.getElementById("SubMenuArea");
//            sma.className += " SubMenuArea" + (i+1);
//            var ul1 = sma.getElementsByTagName("ul");
//            var li1 = ul1[0].getElementsByTagName("li");
//            for (var j = 0; j < li1.length; j++) {
//                if (li1[j].getElementsByTagName("ul").length > 0) {
//                    li1[j].className += " Current";
//                }
//            }
//            var ul2 = ul1[0].getElementsByTagName("ul");
//            if (ul2.length > 0) {
//                var li2 = ul2[0].getElementsByTagName("li");
//                var k = li2.length -1;
//                li2[k].className += " Last";
//            }
            
//            return false;
//        }
//    } 
     
    // Otherwise, for the Homepage
//    SetSubMenuArea();
    SetLatestNews();
//    SetRightPane();
         
}

function SetLatestNews(){
    var ln = document.getElementById("LatestNews");
    if (ln != null){
        var lndivs = ln.getElementsByTagName("div");
        var j = lndivs.length -1;
        lndivs[j].className += " Last";    
    }
}
function SetDoItOnline(){
    var div = document.getElementById("DoItOnline");
    if (div != null){
        var li = div.getElementsByTagName("li");
        var j = li.length -1;
        li[j].className += " Last";    
    }
}
function SetInformationFor(){
    var div = document.getElementById("InformationFor");
    if (div != null){
        var li = div.getElementsByTagName("li");
        var j = li.length -1;
        li[j].className += " Last";    
    }
}
function SetRightPane(){
    var rp = document.getElementById("RightPane");
    if(rp != null)
    {
        rp.style.marginTop = "0px";
    }
}
function SetSubMenuArea(){
    var rp = document.getElementById("SubMenuArea");
    if(rp != null)
    {
        rp.style.visibility = "hidden";
    }
}




function SearchIn(){
	if (searchbox.value == "Search") {searchbox.value = ""; searchbox.style.color="#000000";}
	else {searchbox.style.color="#000000";}
} 
function SearchOut(){ 
	searchbox.style.color="#797a7b";
	if (searchbox.value == "") {searchbox.value = "Search"; }
}



function IncreaseText() {
	if (document.body.style.fontSize == "") { document.body.style.fontSize = "1.0em"; } 
	if (parseFloat(document.body.style.fontSize) < 2) { document.body.style.fontSize = parseFloat(document.body.style.fontSize) + 0.05 + "em"; }
}
function DecreaseText() {
	if (document.body.style.fontSize == "") { document.body.style.fontSize = "1.0em"; } 
	if (parseFloat(document.body.style.fontSize) > 0.5) { document.body.style.fontSize = parseFloat(document.body.style.fontSize) - 0.05 + "em"; }
}



function SearchSite(){
    window.location = "http://www.huttcity.govt.nz/SearchResults.aspx?cx=007490992888458835186%3aetsqrrutt5i&cof=FORID%3A10&ie=UTF-8&q=" +  searchbox.value + "&sa=Search";
    return false;
}
function URLEncode (clearString) {
    var output = '';
    var x = 0;
    clearString = clearString.toString();
    var regex = /(^[a-zA-Z0-9_.]*)/;
    while (x < clearString.length) {
      var match = regex.exec(clearString.substr(x));
      if (match != null && match.length > 1 && match[1] != '') {
          output += match[1];
        x += match[1].length;
      } else {
        if (clearString[x] == ' ')
          output += '+';
        else {
          var charCode = clearString.charCodeAt(x);
          var hexVal = charCode.toString(16);
          output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
        }
        x++;
      }
    }
    return output;
}
function SubmitOnEnter(e)
{
    if(!e)
    {
        e = window.event;
    }
    
    if (e.keyCode == 13) {
        SearchSite();
        return false;
    }  
}


function SetAnchorList(){
    var AnchorList = document.getElementById('AnchorList');
    var allAnchors = document.anchors;
    var atoz = document.getElementById('AtoZ');
    var count = 0;
    
    if (atoz != null){ // this is a A-Z page
        for(var i = 1; i < allAnchors.length; i++) { // [0] is for top
            var a = allAnchors[i];
            if(a.name != null && a.name!= ""){
                AnchorList.innerHTML += "<li><a href='#" + a.name + "'>" + a.name + "</a></li>";
            }
        }
        AnchorList.id = "AtoZAnchorList";
    }
    else {
    
        for(var i = 1; i < allAnchors.length; i++) { // [0] is for top
            var a = allAnchors[i];
            if(a.name != null && a.name!= ""){
                AnchorList.innerHTML += "<li><img src='/templates/hcc/images/anchorlistarrow.jpg' /><a href='#" + a.name + "'>" + a.name + "</a></li>";
                count = count + 1;
            }
        }
        //alert(count);
        if (count > 0) {
            AnchorList.innerHTML = "<p>On this page:</p>" + AnchorList.innerHTML + "<p></p>";
            AnchorList.style.display  = "block";
        }
    }
    
    // Anchors take vertical spaces, there reset the GetInTouch
    var GetInTouch = document.getElementById('GetInTouch');
    if (GetInTouch != null){ 
        GetInTouch.style.bottom = "10px";
    }
}






