/* 
 * Functions to preload/swap images
 */


// detect browser capabilities
var capable = (document.getElementById && document.getElementsByTagName);


// MenuImage constructor
function MenuImage(img_id, img_out, img_in, img_act, swapped) 
{
    this.img_id = img_id;           // the image id
    this.img_out = new Image();     // the image to use when mouseout
    this.img_out.src = imageDir + img_out;
    this.img_in = new Image();      // the image to use when mouseover
    this.img_in.src = imageDir + img_in;
    this.img_act = new Image();     // the image when page active
    if(img_act) {
        this.img_act.src = imageDir + img_act;
    } else {
        this.img_act.src = '';
    }    
    this.swapped = swapped;         // register if the image is swapped
    this.over = 0;                  // register if the image is 'over'
}

// take a full or partial path, and return it's last part
function basename(path) 
{
    var parts = path.split('/');
    return parts[parts.length-1];
}

// get an image from an image_id
function get_image(id)
{
    for(var a=0; a<=MImages.length; a++)
    {
        if(MImages[a].img_id == id)
            return a;
    }
}

// swap in the image
function imgIn(image_id)
{
    var imgId = get_image(image_id);

    if(capable && MImages[imgId]) 
    {
        var theObj = MImages[imgId];
        var theImg = document.getElementById(theObj.img_id);
        var filename = basename(theImg.src);

        // don't swap if image is 'down' or currently active 
        if( filename.indexOf('_at') == -1 &&
           (filename.indexOf('_over') == -1 || filename.indexOf('_on') == -1) ) 
        {
            theImg.src = MImages[imgId].img_in.src;
            MImages[imgId].swapped = 1;
        }
    }
}

// swap out the image
function imgOut(image_id) 
{
    var imgId = get_image(image_id);

    if(capable && MImages[imgId]) 
    {
        theImg = document.getElementById(MImages[imgId].img_id);
        // only revert if "swapped" is true
        if(MImages[imgId].swapped) 
        {
            theImg.src = MImages[imgId].img_out.src;
            MImages[imgId].swapped = 0;
        }
    }
}


/*
 * Functions to swap the background color of a div 
 */

var savedBG = new Array();

function changeBG(id, color)
{
    if(capable && id && color)
    {
        var objStyle = document.getElementById(id).style;
        savedBG[id] = objStyle.backgroundColor;
        objStyle.backgroundColor = "#"+color;
    }
}

function changeBackBG(id)
{
    if(capable && id && savedBG[id])
    {
        var objStyle = document.getElementById(id).style;
        objStyle.backgroundColor = savedBG[id];
        savedBG[id] = ""; 
    }
}


/*
 * Functions to open links in a new window
 */
var newwin='';

// open a popup window
function popWindow(theUrl,width,height,full) {
    // use defaults if width and height were not supplied
    var ismoz = navigator.userAgent.indexOf("Gecko");
    var isie = navigator.userAgent.indexOf("MSIE");
    var default_width = 600;
    var default_height = 450;
    var offset_width = (isie != -1) ? 4 : 0;
    var offset_height = (isie != -1) ? 45 : 0;
    var popType = (full) ? ",scrollbars=yes,resizable=yes,status=yes,toolbar=yes,menubar=yes,location=yes,directories=yes" : ",scrollbars=no,resizable=no,status=no";
    
    var popWidth = (width) ? width + offset_width : default_width + offset_width;
    var popHeight = (height) ? height + offset_height : default_height + offset_height;
    var popLeft = self.screen.availWidth/2 - popWidth/2;
    var popTop = self.screen.availHeight/2 - popHeight/2;
    
    if(theUrl) {
        if(newwin)
            newwin.close();
        newwin = window.open(theUrl,'newwin','left='+popLeft+',top='+popTop+',width='+popWidth+',height='+popHeight+popType);
    }

    return;
}


$().ready(function(){
	$('#mmenu').accordion({active:false,alwaysOpen:false,autoheight:false,header:"a.menuitemOff"});
//	$('.greyfadecontent').gradient({from:'f3f3f3', to:'ffffff', direction:'horizontal', position:'top'});
	$('.greyfadecontent').gradient({from:'eeeeee', to:'ffffff', direction:'horizontal', position:'top'});
});
function clearForm(){
	$('.formfield').children('span').children('input').attr('checked', false);
	$('.formfield').children('span').children('textarea').val("");
	$('.queryleft').children('input').val("");
	$('.queryleft').children('span').children('input').val("");
	$('.queryleft_firstrow').children('input').val("");
	$('.queryleft_firstrow').children('span').children('input').val("");
	$('.formfield').children('span').children("input[class!='readonly']").val("");
	}
function cboxcheck(){
	if ($('input#cbxContent').is(':checked'))
	{	
		$('input[@type=checkbox].freq').removeAttr('disabled');
		$('input[@type=checkbox].addpub').removeAttr('disabled');
		$('input[@type=checkbox].dist').removeAttr('disabled');
		$('input[@type=checkbox].freq').removeAttr('readonly');
		$('input[@type=checkbox].addpub').removeAttr('readonly');
		$('input[@type=checkbox].dist').removeAttr('readonly');
	}
	else
	{	
		$('input[@type=checkbox].freq').removeAttr('checked');
		$('input[@type=checkbox].addpub').removeAttr('checked');
		$('input[@type=checkbox].dist').removeAttr('checked');
		$('input[@type=checkbox].freq').attr('disabled', 'disabled');
		$('input[@type=checkbox].addpub').attr('disabled', 'disabled');
		$('input[@type=checkbox].dist').attr('disabled', 'disabled');
		$('input[@type=checkbox].freq').attr('readonly', 'readonly');
		$('input[@type=checkbox].addpub').attr('readonly', 'readonly');
		$('input[@type=checkbox].dist').attr('readonly', 'readonly');
	}
}
function trainingcheck(){
	if ($('input#cbxTraining').is(':checked'))
	{	
		$('input[@type=radio]#training_0').attr('checked', 'checked');
		$('input[@type=radio].fsaspon').removeAttr('disabled');
		$('input[@type=radio].fsaspon').removeAttr('readonly');
	}
	else
	{	
		$('input[@type=radio].fsaspon').removeAttr('checked');
		$('input[@type=radio].fsaspon').attr('disabled', 'disabled');
		$('input[@type=radio].fsaspon').attr('readonly', 'readonly');

	}
}
function collectioncheck(){
	if ($('input[@type=radio]#radCollection_1').is(':checked'))
	{	
		$('select.asearch').attr('disabled', 'disabled');
		$('select.asearch').attr('readonly', 'readonly');
		$('select.asearch_mult').attr('disabled', 'disabled');
		$('select.asearch_mult').attr('readonly', 'readonly');
		$('select.asearch_mult option').removeAttr('selected', 'selected');
		$('h4.togglecolor').css('color', '#aaa');
		$('div.greyintro').css('color', '#aaa');
		$('select.asearch').css('color', '#aaa');
		$('select.asearch').css('background', '#f5f5f5');
		$('select.asearch option').css('background', '#f5f5f5');
		$('select.asearch_mult option').css('color', '#aaa');
		$('select.asearch_mult option').css('background', '#f5f5f5');
		$('select.asearch_mult').css('background', '#f5f5f5');
	}
	else
	{	
		$('select.asearch').removeAttr('disabled');
		$('select.asearch').removeAttr('readonly');
		$('select.asearch_mult').removeAttr('disabled');
		$('select.asearch_mult').removeAttr('readonly');
		$('h4.togglecolor').css('color', '#666');
		$('div.greyintro').css('color', '#666');
		$('select.asearch').css('color', '#666');
		$('select.asearch').css('background', '#fff');
		$('select.asearch option').css('background', '#fff');
		$('select.asearch_mult option').css('color', '#666');
		$('select.asearch_mult option').css('background', '#fff');
		$('select.asearch_mult').css('background', '#fff');
	}
}
function checkall_freq(){
	$('input[@type=checkbox].freq').attr('checked', 'checked');
	$('input#cbxContent').attr('checked', 'checked');
	cboxcheck();
}
function checkall_dist(){
	$('input[@type=checkbox].dist').attr('checked', 'checked');
	$('input#cbxContent').attr('checked', 'checked');
	cboxcheck();
}
function checkall_addpub(){
	$('input[@type=checkbox].addpub').attr('checked', 'checked');
	$('input#cbxContent').attr('checked', 'checked');
	cboxcheck();
}
function CloseWin(){
	if($.browser.mozilla){
		document.writeln("");
	}
	else if($.browser.safari || $.browser.opera || $.browser.msie){
		document.writeln("<input type='submit' class='btnCloseWindow_mnd' onmouseover='$(this).addClass('btnCloseWindow_mndOn');' onmouseout='$(this).removeClass('btnCloseWindow_mndOn');' onClick='javascript:window.close()' alt='Close Window' title='Close Window' value=' ' />");
	}
}
function CloseWinText(){
	if($.browser.mozilla){
		document.writeln("");
	}
	else if($.browser.safari || $.browser.opera || $.browser.msie){
		document.writeln("<p>(You may click on the 'CLOSE WINODW' button to return to the page with the external link, or click the 'CONTINUE' button to leave the IFAP Web site and the ED.gov domain, and visit the externally hosted site)</p>");
	}
}
function boxrollh2fix(){
	$('.boxroll_h2').css('margin-top','-33px');
	$('.boxroll_h2').css('color','#333');
	$('.boxroll_h2').css('font-size','12px');
}