function toggleArrow(progID,broadcastType){

    var imageID = 'imageID_' + progID;
    var headerID = 'headerID_' + progID;
    
	if($(imageID).name == "closed") { 
		$(imageID).src = "http://www.opb.org/presentation/chrome/downArrow.gif";
		$(imageID).name = "open"
		$(headerID).style.borderBottom='1px solid #eeeeee';
		$(headerID).style.backgroundColor = '#E9F5E9';
		$('rolloverStatus').value = headerID;
        
        showScheduleInfo(progID,broadcastType);
        
	} else {
		$(imageID).src = "http://www.opb.org/presentation/chrome/rightArrow.gif";
		$(imageID).name = "closed";
		$(headerID).style.backgroundColor = '#f9faf3';
		$(headerID).style.borderBottom='1px solid #f9faf3';
		$('rolloverStatus').value = '';
	}
}

function over(progID) {
    var headerID = 'headerID_' + progID;

	$(headerID).style.backgroundColor='#E9F5E9'; 
	$(headerID).style.cursor='pointer';
}

function out(progID) {
    var headerID = 'headerID_' + progID;
    var elementID = 'programID_' + progID;
    
	if($(elementID).style.display == 'none') { 
		$(headerID).style.background='#f9fbf0';	
	}
}

function getSelectionId(text, li) {
    alert (li.id);
}


function gotoTop(progID) {
    var programID = 'programID_' + progID;
    new Effect.ScrollTo('findProgramTop',{offset: -200});
    toggleArrow(progID);
	Element.toggle(programID);
}


/**
 * function openProgram - allows for opening of a program info box and scrolling to that box
 *                      currently called by either clicking on a name that appears in the autocomplete div, or
 *                      by exteranl link (via querystring)
 *
 * var progID int - only used when in a query string (coming from another page, for example)
 * var li htmlInputElement - comes in as '1234_radio' (progid_broadcastType) from ajax response of autocompleter
 *
 * */
function openProgram(progID, li) {
   
	var li = (li == null) ? false : li;
	
	if(li) {
        // split up id of the li element passed from the ajax response of the autocomplete function
        var schedInfo = li.id;
    } else {
        var schedInfo = progID;
    }
    
    var progID_broadcastType = schedInfo.split('_');
    var progID = progID_broadcastType[0];
    var broadcastType = progID_broadcastType[1];
    
    
	var programID = 'programID_' + progID;
	var imageID = 'imageID_' + progID;
	var headerID = 'headerID_' + progID;
    
    // user has come from another page
    if(!li) {
        $(headerID).scrollTo();
    }
    
    // user has used the search field on same page
	if($(headerID) && li) {
		new Effect.ScrollTo(headerID,{offset: -24});
	}
   
    // open up the program info box
    toggleArrow(progID,broadcastType);
	Element.toggle(programID);
}

function showScheduleInfo(progID,broadcastType) {
    
    var replaceDiv = 'programSchedule_' + progID;
    $(replaceDiv).innerHTML = '<span style="color: #A7AE84">loading schedule information...</span><img src="http://www.opb.org/presentation/chrome/ajaxLoading.gif" />';
    
    var pars = 'ajax=true&ajaxMode=schedule&ajaxParameter=' + progID +'&broadcastType=' + broadcastType;
    new Ajax.Updater(
        'programSchedule_' + progID, 
        'php/findProgramAjax.php', 
        {
            method: 'post', 
            parameters: pars
        });
}


function findProgramWithAnchor() {
	if(window.location.hash) {
		childId = window.location.hash;
		childId = childId.replace('#','');
		childId = $(childId);
		
		// grab previous element, which is an image that happens to contain the program id
		var img=childId.previous();
		imgIdString = img.identify();
		progId = imgIdString.replace('imageID_','');
		
		// call function to open up tab and display program info
		openProgram(progId);
	}
}

