/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// | Geeklog 1.3                                                               |
// +---------------------------------------------------------------------------+
// | Commmon javascript functions                                              |
// |                                                                           |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2005,2006 by the following authors:                         |
// |                                                                           |
// |            Blaine Lang - blaine@portalparts.com                           |
// +---------------------------------------------------------------------------+
// |                                                                           |
// | This program is free software; you can redistribute it and/or             |
// | modify it under the terms of the GNU General Public License               |
// | as published by the Free Software Foundation; either version 2            |
// | of the License, or (at your option) any later version.                    |
// |                                                                           |
// | This program is distributed in the hope that it will be useful,           |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
// | GNU General Public License for more details.                              |
// |                                                                           |
// | You should have received a copy of the GNU General Public License         |
// | along with this program; if not, write to the Free Software Foundation,   |
// | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
// |                                                                           |
// +---------------------------------------------------------------------------+

// -------------------------------------------------------------------
// caItems(form object)
// Check All Items - generic function that can be used to check and un-check all items in a list
// Used in the Admin Lists - like on the moderation page
// -------------------------------------------------------------------
   function caItems(f) {  
       var n=f.elements.length;
       for (i=0;i<n; i++) {
           var field=f.elements[i];
           if (field.type == 'checkbox' && field.name.match("delitem")) {
                if (f.chk_selectall.checked) {
                    field.checked=true;
                } else {
                    field.checked=false;
                }
           }

       }
   }




// Basic function to show/hide (toggle) an element - pass in the elment id
    function elementToggle(id) {
        var obj = document.getElementById(id);
        if (obj.style.display == 'none') {
            obj.style.display = '';
        } else {
            obj.style.display = 'none';
        }
    }

// Basic function to show/hide an element - pass in the elment id and option.
// Where option can be: show or hide or toggle
    function elementShowHide(id,option) {
        var obj = document.getElementById(id);
        if (option == 'hide') {
            obj.style.display = 'none';
        } else if (option == 'show') {
            obj.style.display = '';
        } else if (option == 'toggle') {
            elementToggle(id);
        }
    }

var X = 0;
var Y = 0;

// document is ready
$(document).ready( function() {

	// admin menu duct tape
	$('#adminmenu').hide();
	$('#aausercontrol.signedon').mouseover(function(){
		var pos = $(this).offset();
                $("#adminmenu").css({'top' : (pos.top +  $(this).height()), 'left' : pos.left});
		$("#adminmenu").css({'width' : ($(this).width()-2), 'height' : '240px'}); 
		$('#adminmenu').show();
    	}).mouseout(function(event){
		setTimeout('autohide_admin()',500);
	});
	$('#adminmenu').mouseout(function(event){
		var menuoffset = $('#adminmenu').offset();
		if(event.pageX<menuoffset.left || event.pageX>(menuoffset.left+$('#adminmenu').width())) {
			$('#adminmenu').fadeOut('slow');
		}
		if(event.pageY<menuoffset.top || event.pageY>(menuoffset.top+$('#adminmenu').height())) {
                        $('#adminmenu').fadeOut('slow');
                }
        });

	// ARRAOverview menu 
	$('#ARRAOverview').hide();
	$('#ARRAOverview-menu.signedon').mouseover(function(){
		var pos = $(this).offset();
                $("#ARRAOverview").css({'top' : (pos.top +  $(this).height()), 'left' : pos.left});
		$("#ARRAOverview").css({'width' : '150px', 'height' : '115px'}); 
		$('#ARRAOverview').show();
    	}).mouseout(function(event){
		setTimeout('autohide_ARRAOverview()',300);
	});
	$('#ARRAOverview').mouseout(function(event){
		var menuoffset = $('#ARRAOverview').offset();
		if(event.pageX<menuoffset.left || event.pageX>(menuoffset.left+$('#ARRAOverview').width())) {
			$('#ARRAOverview').fadeOut('fast');
		}
		if(event.pageY<menuoffset.top || event.pageY>(menuoffset.top+$('#ARRAOverview').height())) {
                        $('#ARRAOverview').fadeOut('fast');
                }
        });

	// AcctTrans menu 
	$('#AcctTrans').hide();
	$('#AcctTrans-menu.signedon').mouseover(function(){
		var pos = $(this).offset();
                $("#AcctTrans").css({'top' : (pos.top +  $(this).height()), 'left' : pos.left});
		$("#AcctTrans").css({'width' : '150px', 'height' : '110px'}); 
		$('#AcctTrans').show();
    	}).mouseout(function(event){
		setTimeout('autohide_AcctTrans()',300);
	});
	$('#AcctTrans').mouseout(function(event){
		var menuoffset = $('#AcctTrans').offset();
		if(event.pageX<menuoffset.left || event.pageX>(menuoffset.left+$('#AcctTrans').width())) {
			$('#AcctTrans').fadeOut('fast');
		}
		if(event.pageY<menuoffset.top || event.pageY>(menuoffset.top+$('#AcctTrans').height())) {
                        $('#AcctTrans').fadeOut('fast');
                }
        });

	// Community Forum menu 
	$('#Community').hide();
	$('#Community-menu.signedon').mouseover(function(){
		var pos = $(this).offset();
                $("#Community").css({'top' : (pos.top +  $(this).height()), 'left' : pos.left});
		$("#Community").css({'width' : '150px', 'height' : '75px'}); 
		$('#Community').show();
    	}).mouseout(function(event){
		setTimeout('autohide_Community()',300);
	});
	$('#Community').mouseout(function(event){
		var menuoffset = $('#Community').offset();
		if(event.pageX<menuoffset.left || event.pageX>(menuoffset.left+$('#Community').width())) {
			$('#Community').fadeOut('fast');
		}
		if(event.pageY<menuoffset.top || event.pageY>(menuoffset.top+$('#Community').height())) {
                        $('#Community').fadeOut('fast');
                }
        });

	// signed in user menu
	$('.usermenucontent').hide();
	if($('.usermenucontent').length>0) {
	$('.usermenucontent').each(function(i) {
		$('#userbar .items').append($(this).html());
	});
	$('#userbar').show();
	}

	// track mouse coords
	$().mousemove(function(e)
	{
	    X = e.pageX;
	    Y = e.pageY;
	});

});

function autohide_admin(execute) {
		
		var menuoffset = $('#adminmenu').offset();
                if(X<menuoffset.left || X>(menuoffset.left+$('#adminmenu').width())) {
                        $('#adminmenu').fadeOut('fast');
                }
                if(Y<(menuoffset.top-30) || Y>(menuoffset.top+$('#adminmenu').height())) {
                        $('#adminmenu').fadeOut('fast');
                }
}
function autohide_ARRAOverview(execute) {
		
		var menuoffset = $('#ARRAOverview').offset();
                if(X<menuoffset.left || X>(menuoffset.left+$('#ARRAOverview').width())) {
                        $('#ARRAOverview').fadeOut('fast');
                }
                if(Y<(menuoffset.top-30) || Y>(menuoffset.top+$('#ARRAOverview').height())) {
                        $('#ARRAOverview').fadeOut('fast');
                }
}
function autohide_AcctTrans(execute) {
		
		var menuoffset = $('#AcctTrans').offset();
                if(X<menuoffset.left || X>(menuoffset.left+$('#AcctTrans').width())) {
                        $('#AcctTrans').fadeOut('fast');
                }
                if(Y<(menuoffset.top-30) || Y>(menuoffset.top+$('#AcctTrans').height())) {
                        $('#AcctTrans').fadeOut('fast');
                }
}
function autohide_Community(execute) {
		
		var menuoffset = $('#Community').offset();
                if(X<menuoffset.left || X>(menuoffset.left+$('#Community').width())) {
                        $('#Community').fadeOut('fast');
                }
                if(Y<(menuoffset.top-30) || Y>(menuoffset.top+$('#Community').height())) {
                        $('#Community').fadeOut('fast');
                }
}
// hide and show content

var slideSpeed = 50;	// Higher value = faster
var timer = 2;	// Lower value = faster

var objectIdToSlideDown = false;
var activeId = false;
var slideInProgress = false;
function showHideContent(e,inputId)
{
	if(slideInProgress)return;
	slideInProgress = true;
	if(!inputId)inputId = this.id;
	inputId = inputId + '';
	var numericId = inputId.replace(/[^0-9]/g,'');
	var answerDiv = document.getElementById('a' + numericId);

	objectIdToSlideDown = false;
	
	if(!answerDiv.style.display || answerDiv.style.display=='none'){		
		if(activeId &&  activeId!=numericId){			
			objectIdToSlideDown = numericId;
			slideContent(activeId,(slideSpeed*-1));
		}else{
			
			answerDiv.style.display='block';
			answerDiv.style.visibility = 'visible';
			
			slideContent(numericId,slideSpeed);
		}
	}else{
		slideContent(numericId,(slideSpeed*-1));
		activeId = false;
	}	
}

function slideContent(inputId,direction)
{
	
	var obj =document.getElementById('a' + inputId);
	var contentObj = document.getElementById('ac' + inputId);
	height = obj.clientHeight;
	if(height==0)height = obj.offsetHeight;
	height = height + direction;
	rerunFunction = true;
	if(height>contentObj.offsetHeight){
		height = contentObj.offsetHeight;
		rerunFunction = false;
	}
	if(height<=1){
		height = 1;
		rerunFunction = false;
	}

	obj.style.height = height + 'px';
	var topPos = height - contentObj.offsetHeight;
	if(topPos>0)topPos=0;
	contentObj.style.top = topPos + 'px';
	if(rerunFunction){
		setTimeout('slideContent(' + inputId + ',' + direction + ')',timer);
	}else{
		if(height<=1){
			obj.style.display='none'; 
			if(objectIdToSlideDown && objectIdToSlideDown!=inputId){
				document.getElementById('a' + objectIdToSlideDown).style.display='block';
				document.getElementById('a' + objectIdToSlideDown).style.visibility='visible';
				slideContent(objectIdToSlideDown,slideSpeed);				
			}else{
				slideInProgress = false;
			}
		}else{
			activeId = inputId;
			slideInProgress = false;
		}
	}
}
function initShowHideDivs()
{
	var divs = document.getElementsByTagName('DIV');
	var divCounter = 1;
	for(var no=0;no<divs.length;no++){
		if(divs[no].className==('question' || 'questionSmall')){
			divs[no].onclick = showHideContent;
		 	divs[no].id = 'q'+divCounter;
			var answer = divs[no].nextSibling;
			while(answer && answer.tagName!='DIV'){
				answer = answer.nextSibling;
			}
			answer.id = 'a'+divCounter;	
			contentDiv = answer.getElementsByTagName('DIV')[0];
			contentDiv.style.top = 0 - contentDiv.offsetHeight + 'px'; 	
			contentDiv.className='answer_content';
			contentDiv.id = 'ac' + divCounter;
			answer.style.display='none';
			answer.style.height='1px';
			divCounter++;
		}		
	}	
}
window.onload = initShowHideDivs;
