ScheduleGrid = function(path, 
					imagePath, 
					stylesheetPath, 
					shouldZoomToTime, 
					listingHeightInPixelsPerMinute, 
					listingWidthPerThirdInPixels, 
					timeColumnWidthInPixels,
					scheduleRuntime)
{
	this.rightScrollDelay = 40;
	this.path = path;
	this.imagePath = imagePath;
	this.stylesheetPath = stylesheetPath;
	this.listingHeightInPixelsPerMinute = listingHeightInPixelsPerMinute;
	this.listingHeightInPixelsPerHalfHour = this.listingHeightInPixelsPerMinute * 30;
	// Only scroll to the current time if the user is viewing the current day's schedule.
    this.shouldZoomToTimeOnLoad = shouldZoomToTime; 
	this.listingWidthPerThirdInPixels = listingWidthPerThirdInPixels;
	this.timeColumnWidthInPixels = timeColumnWidthInPixels;
	this.runtime = scheduleRuntime;
	this.registerEvents();
} 

// load all schedule content
ScheduleGrid.prototype.display = function(pageId, theDate, isInitialLoad)
{

	afterLoadAction = (isInitialLoad) ? ScheduleGrid.hideProgressWidgetAndScroll : ScheduleGrid.hideProgressWidget;

	// populate schedule content
	params = 'view='+this.runtime.view+'&'+
			 'listingHeightInPixelsPerMinute='+this.listingHeightInPixelsPerMinute+'&' +
        	 'listingWidthPerThirdInPixels='+this.listingWidthPerThirdInPixels+'&' +
        	 'timeColumnWidthInPixels='+this.timeColumnWidthInPixels+'&' +
        	 'id='+pageId+'&' +
        	 'date=' + theDate;
	
	this.runtime.latestRequest = new Ajax.ScheduleUpdater(this,
		$('s1'), 
		this.path+'/?controller=ScheduleGridPage', 
		{
            onCreate: ScheduleGrid.showProgressWidget,
            onSuccess: afterLoadAction,
			method: 'post',
			parameters: params
        }
	);
		
	jsDate = Date.newFromString(theDate);

	$('date_line_1').update(jsDate.getFullDay().toLowerCase());
	$('date_line_2').update(jsDate.getPrettyString('/'));
                     
}
     
ScheduleGrid.prototype.displayScheduleForDate = function(theDate, isInitialLoad)
{
	if (isInitialLoad == null) isInitialLoad = false;
	this.runtime.focusDate = theDate;
	this.display(this.runtime.focusPageId, theDate, isInitialLoad);	
}
	
ScheduleGrid.prototype.displaySchedulePage = function(theDate, isInitialLoad)
{
	if (isInitialLoad == null) isInitialLoad = false;
	
	params = 'id='+this.runtime.focusPageId+'&' +
			 'view='+this.runtime.view+'&' +
        	 'listingWidthPerThirdInPixels='+this.listingWidthPerThirdInPixels;
			 
	// display default schedule page header
    new Ajax.Updater($('channel_header_container'), 
						this.path+'/?controller=ScheduleGridPage&action=displayHeader', 
						{
							evalScripts:true,
							method:'post',
							parameters:params
						}
	);
						
	this.displayScheduleForDate(theDate, isInitialLoad);
}
 
ScheduleGrid.prototype.initializeAutocomplete = function()
{
	if (this.runtime.user.browserSpecificSettings.useAutocompleteOnGridSchedulePage) 
		new Ajax.Autocompleter('search_key_words', 
						   	   'autocomplete_choices', 
						   	   this.path+'/?controller=Show', 
						   		{afterUpdateElement: ScheduleGrid.submitSearch}
		);
}

ScheduleGrid.prototype.doAfterRender = function()
{
	new Rollover('left_arrow');
	new Rollover('right_arrow');
	new Rollover('change_date');
	this.initializeAutocomplete();
	this.displaySchedulePage(this.runtime.focusDate, true);
}     

ScheduleGrid.prototype.displayProgressWidget = function() 
{
	if ($("loading_box") && !this.runtime._ajaxLoaded) 
	{
    	$("loading_box").update('<img src="'+this.imagePath+'/progress.gif">');
        new Effect.Appear($("loading_box"));
    }
}
 
ScheduleGrid.prototype.registerEvents = function()
{
	Event.observe(document, 'mousemove', ScheduleGrid.handleMouseMove);                               
	Event.observe(document, 'mouseup', ScheduleGrid.handleMouseUp);           
	Event.observe(document, 'click', ScheduleGrid.handleClick);
}       

ScheduleGrid.prototype.doScrollButtonPress = function(name)
{
	ScheduleGrid.handleClick();
	switch (name)
	{
		case 'bottom':this._startScroll(this.listingHeightInPixelsPerHalfHour);
					break;
		case 'top':this._startScroll(-this.listingHeightInPixelsPerHalfHour);
					break;
	}							
}
					
ScheduleGrid.prototype.doScrollButtonClick = function(name)
{
	ScheduleGrid.handleClick();
	switch (name)
	{
		case 'bottom':this._tweenBy(this.listingHeightInPixelsPerHalfHour);
					break;
		case 'top':this._tweenBy(-this.listingHeightInPixelsPerHalfHour);
					break;
	}							
}                         

ScheduleGrid.prototype.handleOnload = function()
{
	if ($('container')) {
		// Disable text selection within the schedule because it interferes with mouse drag scrolling.
		$('container').onselectstart = function(){
			return false;
		} // ie
		$('container').onmousedown = function(){
			return false;
		} // mozilla
		$('container').onkeypress = function(){
		};
	} 
    //
    this.registerListingEvents();
	this.runtime.user.browserSpecificSettings.functions;
}

ScheduleGrid.prototype.handleCustomScrollerMouseDown = function()
{
	ScheduleGrid.handleClick();
    this.runtime.scroll.isScrolling = true;
}

ScheduleGrid.prototype.handleCustomScrollerMouseUp = function()
{
    this.runtime.scroll.isScrolling = false;
}

ScheduleGrid.prototype.displayPopupBoxForListing = function(view, event, listingId)
{
	if (this.canDisplayPopup() || listingId != this.runtime.popup.id) 
		schedule.runtime.popup.id = schedule.runtime.popup.displayForListing(view, event, listingId, this.path);
}

ScheduleGrid.prototype.canDisplayPopup = function()
{
	return (this.runtime.isLoaded && this.runtime.scroll.scrollTween.idle());
}

ScheduleGrid.prototype.setMouseIsInPopup = function(bool)
{
    this.runtime.mouse.isInPopup = bool;
}  

ScheduleGrid.prototype.doScrollButtonRelease = function()
{
	this.runtime.scroll.scrollbar.stopScroll();
	this.runtime.scroll.scroller.stopScroll();
}

ScheduleGrid.prototype.displayPreviousDate = function()
{
	this._displayIncrementedDate(-1);
}

ScheduleGrid.prototype.displayNextDate = function(shouldZoomToBeginning)
{
	this._displayIncrementedDate(1);
	if (shouldZoomToBeginning)
		this.runtime.scroll.zoomToBeginning();
}

ScheduleGrid.prototype.isValidSearch = function(){
    return ($('search_key_words').value.length > 0);
}

ScheduleGrid.prototype.sanitizeSearchString = function(){
    $('search_key_words').value = $('search_key_words').value.trim();
}

ScheduleGrid.prototype.submitSearch = function(){
    this.sanitizeSearchString();
    if (this.isValidSearch()) 
        $('search_form').submit();
}

// virtual   
ScheduleGrid.prototype.registerListingEvents = function(){}

// private
ScheduleGrid.prototype._initScroller = function()
{
	this.runtime.isLoaded = this.runtime.scroll.init(this.shouldZoomToTimeOnLoad);	
}

ScheduleGrid.prototype._displayIncrementedDate = function(amount)
{
	var newDate = this._getIncrementedDate(amount);
	if (newDate)
    	this.displayScheduleForDate(newDate);	
}

ScheduleGrid.prototype._getIncrementedDate = function(amount)
{
    jsDate = Date.newFromString(this.runtime.focusDate);
    jsDate.incrementDay(amount);
	return (jsDate.isAcceptableForSchedule()) ? jsDate.getSensiblyIndexedAsString() : null;	
}

ScheduleGrid.prototype._tweenBy = function (y)
{
	this.runtime.scroll.scrollbar.tweenBy(y);
}

ScheduleGrid.prototype._startScroll = function(y)
{
	this.runtime.scroll.scrollbar.startScroll(y);
}

// class methods  

ScheduleGrid.submitSearch = function()
{
    schedule.submitSearch();
}
  
ScheduleGrid.hideProgressWidgetAndScroll = function()
{
	ScheduleGrid.hideProgressWidget();	
	schedule._initScroller();
}

ScheduleGrid.showProgressWidget = function()
{
	$('s1').hide();
    schedule.runtime._ajaxLoaded = false;
    window.setTimeout('schedule.displayProgressWidget()', 600);
}

ScheduleGrid.hideProgressWidget = function()
{
	$('s1').show();
    $('loading_box').hide();
    schedule.runtime._ajaxLoaded = true;
}   
 
ScheduleGrid.handleMouseMove = function(event)
{
	// record mouse movement
    schedule.runtime.mouse.position.x = event.clientX;
    schedule.runtime.mouse.position.y = event.clientY;
    if (schedule.runtime.popup.isInHideProcess)
		schedule._hidePopupBox();
	if (schedule.runtime.scroll.isScrolling)
    {
    	// scroll the schedule
        if (schedule.runtime.mouse.lastPosition.y == 0) 
			schedule.runtime.mouse.lastPosition.y = event.clientY;
	
        var dy = event.clientY - schedule.runtime.mouse.lastPosition.y;
        var moveBy = -((dy * schedule.listingHeightInPixelsPerHalfHour) / schedule.rightScrollDelay);
        schedule.runtime.mouse.lastPosition.y = event.clientY;
        schedule.runtime.scroll.scrollbar.scrollBy(moveBy);
    } else schedule.runtime.mouse.lastPosition.y = (event.clientY > 0) ? event.clientY : 0;
}

ScheduleGrid.handleMouseUp = function(event)
{
	// stop scrolling when the user releases the mouse button
	schedule.runtime.scroll.isScrolling = false;
}

ScheduleGrid.handleClick = function(event)
{
	if (!schedule.runtime.mouse.isInPopup) schedule.runtime.popup._hide();
}
