﻿function init()
{    
    enableTabs();        
    setupPopupSections();  
}

function initAdmin()
{    
    enableTabs();        
}

function getDate()
{
    var d8 = new Date();
    return d8.toDateString();
}

function setupLayout()
{
    if (document.all)
    {
        var left = document.getElementById('ctl00_leftAndCentre');
        var right = document.getElementById('ctl00_right');

        
        if (left && right)
        {
            if (left.offsetHeight < right.offsetHeight)
            {

                left.style.height = parseInt(right.offsetHeight) + "px";
            }
            else
            {

                right.style.height = parseInt(left.offsetHeight) + "px";
            }
        }
    }
}

function setupRollovers()
{
    var imgs = document.getElementsByTagName("IMG");
    
    for (var i = 0; i < imgs.length; i++)
    {
        var img = imgs[i];

        // set current button on
        
        if (img.parentNode && img.parentNode.href && (unescape(img.parentNode.href) == unescape(location.href)))
        {
            img.src = img.src.replace('_off', '_on');
        }
        else if (img.src.match(/.*_off/))
        {        
            img.onmouseover = rollOn;
            img.onmouseout = rollOff;
        }
        
    }
}


function rollOn()
{
    this.src = this.src.replace(/_off/, '_on');
}

function rollOff()
{
    this.src = this.src.replace(/_on/, '_off');
}

function updateLabel(destId, textbox)
{
    var val = textbox.value;
    
    val = val.replace(/[^a-zA-Z0-9\-]/g, '').toLowerCase();
 
    textbox.value = val;   
    document.getElementById(destId).innerHTML = val;
}

function clearSearchBox(box)
{
    // clear on focus
    if (box.value.length > 0 && box.value.substr(0, 1) == '[')   
    {
        box.value = '';
    }
        
    box.style.color = '';
    
}

function addTag(tag, id)
{
    var o = document.getElementById(id);
    
    if (o)
    {
        if (o.value.length > 0)
            o.value += ' ';
          
        if (tag.indexOf(' ') >= 0)  
            o.value += '"' + tag + '"';
        else
            o.value += tag;
    }
}

function addBookmark() 
{
    var title = document.title;
    var url = window.location.href;

    var isMac=(navigator.userAgent.toLowerCase().indexOf('mac')!=-1);
    var button = (isMac ? 'Command/Cmd' : 'Ctrl');

    if (window.sidebar) // firefox
    {
	    // window.sidebar.addPanel(title, url, ""); // opens in sidebar
	    alert('Please press Ctrl+D to bookmark our site');
	}
    else if(window.opera && window.print) // opera
    { 
	    var elem = document.createElement('a');
	    elem.setAttribute('href',url);
	    elem.setAttribute('title',title);
	    elem.setAttribute('rel','sidebar');
	    elem.click();
	}
	else if(window.home) // Netscape
	{ 
      alert('Please press '+ button + ' + D to bookmark our site.');
    }
    else if(!window.print || isMac) // IE5/Mac and Safari 1.0
    { 
      alert('Please press Command/Cmd + D to bookmark our site.');    
	}
    else if(document.all) // ie
    {    
	    window.external.AddFavorite(url, title);
	}
	else
	{
	    alert('Please use your browser Add Bookmark feature to add us');
	}
}

function setHomePage() 
{
   var url = window.location.href;

   var isMac=(navigator.userAgent.toLowerCase().indexOf('mac')!=-1);

  if (document.all)
  {
        document.body.style.behavior = "url(#default#homepage)";
        document.body.setHomePage(url);
  }
  else if (window.home)
  {
    alert("Please drag the icon in the address bar over your home button.");
  }
  else if (isMac)
  {
    alert('Please use your browser menu Safari/Preferences/General/Set To Current Page');  
  }  
  else
  {
    alert('Sorry - only available in IE. Please use your browser menu to set us as your Home Page');
  }
}  

function showItemContent(buttonId, panelId)
{

    var panel = document.getElementById(panelId);
    var btn = document.getElementById(buttonId);
    
    if (btn.src.match(/up\.png/))
    {
        panel.style.display = 'none';
        btn.src = btn.src.replace(/up\.png/, 'down.png');
    }
    else
    {
        panel.style.display = 'block';
        btn.src = btn.src.replace(/down\.png/, 'up.png');
    }
}

function swapButtons(div1Id, div2Id)
{    
    var div1 = document.getElementById(div1Id);
    var div2 = document.getElementById(div2Id);
    
    if (div1.className.match(/Visible/))
    {
        div1.className = div1.className.replace(/Visible/, 'Hidden');
        div2.className = div2.className.replace(/Hidden/, 'Visible');
    }
    else
    {
        div2.className = div2.className.replace(/Visible/, 'Hidden');
        div1.className = div1.className.replace(/Hidden/, 'Visible');
    }
    
    // for toolbar, hide Choose button when toolbar 2 is shown
    
    if (getCookie('form2') == '109')
    {
        if (div1Id == 'toolbarDiv1')
        {
            if (div1.className.match(/.*Visible/))
            {
                document.getElementById('chooser').style.display = '';                
            }
            else
            {
                document.getElementById('chooser').style.display = 'none';            
            }
        }
    }
}

function updateTextbox(fromId, toId)
{
    var from = document.getElementById(fromId);
    var to = document.getElementById(toId);
    
    if (to.value.length == 0)
        to.value = from.value;
}

function goSearch(searchPage, textboxId, selectId)
{
    var text = document.getElementById(textboxId).value;
    
    
    if (text.length > 0)
    {
        var searchWhat = document.getElementById(selectId).options[document.getElementById(selectId).selectedIndex].value.toLowerCase();
    
        switch(searchWhat)
        {
        
            case "google":
                window.open("http://www.google.com/search?q=" + text);
                break;

            case "alpha":
                window.open("http://au.alpha.yahoo.com/search/web?p=" + text);
                break;

            case "youtube":
                window.open("http://www.youtube.com/results?search_query=" + text + "&search=Search");
                break;

            case "flickr":
                window.open("http://www.flickr.com/search/?q=" + text + "&w=all");
                break;

            case "wikipedia":
                window.open("http://en.wikipedia.org/wiki/" + text);
                break;

            default:
                location.href = searchPage + '?q=' + text;                
                break;
        }                        
    }
    
    return false; // no postback
}

function republish(id)
{
    document.getElementById(id).value = '(new)';
    
    return false;
}

function setCookie(name, value) 
{ 
	var today = new Date();
	var expiry = new Date(today.getTime() + 30 * 24 * 60 * 60 * 1000); // 30 days

	document.cookie = name + "=" + escape(value) + ";expires=" + expiry.toGMTString() + ';path=/';	
}

function setupPopupSections()
{
    var selectedButtons = getCookie('toolbarButtons', '');
    
    var i = 0;
    
    while(true)
    {
        var chk = document.getElementById('ctl00_popupSectionsList_' + i.toString());
        
        if (chk == null)
            break;
            
        var section = chk.parentNode.childNodes[1].innerHTML;
        
        if (selectedButtons.length == 0 || selectedButtons.indexOf(section + '::') >= 0)
            chk.checked = true;
            
        i++;
    }
}

function showRow(incr, linkPrefix, hidId, tblId)
{
    var hid = document.getElementById(hidId);
    var tbl = document.getElementById(tblId)
    
    var maxVal = tbl.rows.length;
    
    var currentRow = parseInt(hid.value);
    var nextRow = currentRow + incr;
    
    if (nextRow >= 0 && nextRow < maxVal)
    {
        tbl.rows[currentRow].className = 'Hidden';
        tbl.rows[nextRow].className = '';
        hid.value = nextRow;

        if (nextRow > 0)
        {
            document.getElementById(linkPrefix + 'Prev').className = ''
        }   
        else
        {
            document.getElementById(linkPrefix + 'Prev').className = 'Hidden'

        }
        
        if (nextRow < maxVal - 1)
        {
            document.getElementById(linkPrefix + 'Next').className = ''
        }   
        else
        {
            document.getElementById(linkPrefix + 'Next').className = 'Hidden    '
        }   

        if (nextRow > 0 && nextRow < maxVal - 1)    
        {
            document.getElementById(linkPrefix + 'Separator').className = '';
        }
        else
        {
            document.getElementById(linkPrefix + 'Separator').className = 'Hidden';
        }
    }
    
}

function getElementsByClassName(node, classname)
{
    var a = [];
    var re = new RegExp('(^| )'+classname+'( |$)');
    
    var els = node.getElementsByTagName("*");
    
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
        
    return a;
}

function querystring(key)
{
	var s = '';
	
	var sQS = location.href.split('?');
	
	if (sQS.length > 1)
	{
		var vecQS = sQS[1].split('&');
	
		for (var i = 0; i < vecQS.length; i++)
		{
			var vecQ = vecQS[i].split('=');
			
			if ((vecQ[0] == key) && (vecQ.length > 1))
			{
				s = vecQ[1];
				break;
			}
		}
	}	 
	
	return s;
}

function showAds()
{
    if (querystring('show') == 'ads')
    {
        document.getElementById('googleAds1').className = 'GoogleAds';
    }    
}


// store mouse position

document.onmousemove = getMouseXY;

var mouseX = 0;
var mouseY = 0;

function getMouseXY(e) 
{
  if (document.all) 
  { 
    // ie7 uses document.documentElement
    var sLeft = document.documentElement ? document.documentElement.scrollLeft : document.body.scrollLeft;
    var sTop = document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop;
  
    mouseX = event.clientX + sLeft;
    mouseY = event.clientY + sTop;
  } 
  else 
  { 
    mouseX = e.pageX;
    mouseY = e.pageY;
  } 

  if (mouseX < 0){mouseX = 0;}
  if (mouseY < 0){mouseY = 0;}  
  
  return true;
}

// process json for button popups

function processArticleData(data)
{
      if (data==null)
      {
        alert('error');
      }
      else
      {
        for (var i = 0; i < data.Items.length; i++)
        {
            var item = data.Items[i];
            
            var sectionLC = item.Section.toLowerCase().replace(/[\W]/, '');
            var divId = 'buttonHover' + sectionLC;
            
            // find or create button div
            var div = document.getElementById(divId)
            
            if (! div)
            {
                div = document.createElement('DIV');
                div.id = divId;
                div.className = 'HoverButtonDiv';
                div.style.display = 'none';
                div.style.position = 'absolute';
                document.body.appendChild(div);

                var imgTop = document.createElement('IMG');
                div.appendChild(imgTop);
                imgTop.src = data.BaseUrl + '/images/hoverButtonTop.png';
                                        
                var div2 = document.createElement('DIV');
                div.appendChild(div2);
                div2.className = 'HoverButtonInnerDiv';

                var img = document.createElement('IMG');
                div2.appendChild(img);
                img.src = data.BaseUrl + '/images/buttons/HPD_' + sectionLC + '_40px.gif';
                img.align = 'left';
                img.className = "reflect rheight20"; 
                
                var ul = document.createElement('UL');
                div2.appendChild( ul );

                var imgBottom = document.createElement('IMG');
                div.appendChild(imgBottom);
                imgBottom.src = data.BaseUrl + '/images/hoverButtonBottom.png';
                
                div.onmouseover = descheduleHide;
                div.onmouseout = hoverButtonOffByMouse;
            }
            
            var ul = div.getElementsByTagName('UL')[0];
            var li = document.createElement('LI');
            ul.appendChild(li);
            var link = document.createElement('A');
            li.appendChild(link);
            link.href = item.Url;
            link.appendChild( document.createTextNode(item.Title) );            
        }
        
        // set up hover event
        
        var btns = document.getElementById('toolbarStrap').getElementsByTagName('IMG');
        
        for (var i = 0; i < btns.length; i++)
        {
            var btn = btns[i];
            
            btn.onmouseover = scheduleHover;
            btn.onmouseout = descheduleHover;

        }
        
        //if (typeof(addReflections) == 'function')
        //    addReflections();
      }
}

var activeDiv = 0;
var hideActiveDivTimer = 0;
var scheduleHoverTimer = 0;

function scheduleHover()
{
    if (scheduleHoverTimer)
        clearTimeout(scheduleHoverTimer);

    scheduleHoverTimer = setTimeout('hoverButtonOn(\'' + this.id + '\')', 300);
}

function descheduleHover()
{
    if (scheduleHoverTimer)
    {
        clearTimeout(scheduleHoverTimer);
        scheduleHoverTimer = 0;
    }
}

function descheduleHide()
{
    if (hideActiveDivTimer)
    {
        clearTimeout(hideActiveDivTimer);
        hideActiveDivTimer = 0;
    }

}

function hoverButtonOn(imgId)
{
    scheduleHoverTimer = 0;
    
    if (activeDiv)
    {
        if (hideActiveDivTimer)
        {
            clearTimeout(hideActiveDivTimer);
            hideActiveDivTimer = 0;
        }
        
        hoverButtonOff();
    }
    
    var img = document.getElementById(imgId);
    
    var sectionLC = img.src.replace(/(.*HPD_button_)(.*)(.jpg)/, '$2').toLowerCase();
    
    var divId = 'buttonHover' + sectionLC;            

    var div = document.getElementById(divId);
    
    if (div)
    {
        var xy = GetPageXY(img);
        
        div.style.left = (xy[0] + 30) + 'px';
        div.style.top = (xy[1] + 85) + 'px';
        div.style.display = '';
        
        activeDiv = div;
        resetHoverHideTimer();
    }
}

function resetHoverHideTimer()
{
    if (hideActiveDivTimer)
        clearTimeout(hideActiveDivTimer);
        
    hideActiveDivTimer = setTimeout(hoverButtonOff, 4000);
}

function hoverButtonOff()
{
    if (activeDiv)
    {
        activeDiv.style.display = 'none';
        activeDiv = 0;
        hideActiveDivTimer = 0;
    }
}

function hoverButtonOffByMouse()
{
    // if outside active div, turn off
    
    if (activeDiv)
    {        
        var xy = GetPageXY(activeDiv);
        
        if (mouseX <= xy[0] + 8 || mouseY < xy[1] + 8 || mouseX >= xy[0] + activeDiv.clientWidth - 8 || mouseY >= xy[1] + activeDiv.clientHeight - 8)
        {
            hoverButtonOff();
        }        
    }    
}

function GetPageXY(obj) 
{
	var layer = obj;
	var x=0;
	var y=0;
	
	while (layer.offsetParent) 
	{  
		x += layer.offsetLeft; 
		y += layer.offsetTop; 
		layer = layer.offsetParent; 
	}
	
    x += layer.offsetLeft;
    y += layer.offsetTop;
    
    var arr = new Array();
    arr[0] = x;
    arr[1] = y;
    
    return arr;
}	
