document.observe('dom:loaded',setUp);
Event.observe(window,'load',adjustHeight);

var MenuParent;
var MenuParentHeight = 0;

function adjustHeight(){
	var contentHeight = $('content').getHeight();

	//fix content height issue
	if(MenuParentHeight > contentHeight) {
		$('content').setStyle({ 'height' : MenuParentHeight });
	}
}

function setUp() {

	setupCalendars();
	setupSiteWidth();
		
	//Handle back button click
	var backButton = $('back-button');
	if(backButton) {
		backButton.observe('click', function(){history.back(1)});
	}
	
	//Handle Page Controls
	var pageControlButton = $('edit-page-controls');
	if(pageControlButton) {
		pageControlButton.observe('click', function(ev){
		
			//Show and hide buttons
			var PageControl = $$('.page-control');
			PageControl.invoke('toggle');
			PageControl.invoke('highlight');
			
		});
	}
	
}

function setupSiteWidth() {
	//Fix width of container when content width extends beyond the viewport
	var navWidth;
	MenuParent = $('menu-parent');
	
	var contentWidth = $('content').getWidth();
	
	if(MenuParent) {
		navWidth = MenuParent.getWidth() + 12;
		MenuParentHeight = MenuParent.getHeight();
	}
	
	var adjustWidth = navWidth + contentWidth;
	
	//adjust size of page
	if(adjustWidth) {
		$('container').setStyle({ 'width' : adjustWidth + 'px' });
		
		var foot = $('footer');
		if(foot) foot.setStyle({ 'width' : adjustWidth + 'px' });
	}
}

//Added popup window functions for excel print and calendar
function PrintPopUp(slocation){
	window.open(slocation, "PrintableVersion", "top=10,left=10,resizable=yes,toolbar=yes,statusbar=yes,scrollbars=yes");
}
function ExcelPopUp(slocation){				
	window.open(slocation, "Excel", "width=500,height=340,top=10,left=10,resizable=yes,toolbar=yes,statusbar=yes,scrollbars=yes");				
}
function CalendarPopUp(slocation){			
	window.open(slocation, "remote", "width=200,height=240,top=10,left=10");
}

function AutocompleteHelper(grp, options) {
    var options = Object.extend({ 
		indicator: grp + '_busy',
		dynamParams: 'username',
		paramName: 'autoCompleteParam'
    }, options || {});

	new Ajax.Autocompleter(
		grp,
		grp + '_results',
		grp + '.asp',
		options
	)
}

function InPlaceEditHelper(grp) {
	pg = grp.split('_')[0]
	
	new Ajax.InPlaceEditor(
		grp,
		'save_' + pg + '.asp',
		{ cols: 10, highlightcolor: '#4784ff', cancelText: 'no', paramName: grp }
	)
}

function handleCalendarSelect(type, args, obj) {
	var dates = args[0];
	var date = dates[0];
	var year = date[0], month = date[1], day = date[2];
        		
	obj[0].value = month + '/' + day + '/' + year;
	obj[1].hide();
}


function setupCalendars() {

		        //get every input date field
		        $$('input.date').each(function(field, index){

		            var calendarContainer = new Element('div')
		                .setStyle({ position: 'absolute' })
		                .addClassName('calendar-container')
		                .hide();
		            
		            field.insert({ after: calendarContainer });

                    var calendar = new YAHOO.widget.Calendar(calendarContainer, {
                        title: 'Choose a date:',
                        close: true
                    });
                    calendar.selectEvent.subscribe(handleCalendarSelect, [field, calendar], true);  
                    calendar.render();

    		        field
    		            .writeAttribute('autocomplete', 'off')
    		            .addClassName('widgets-calendar-field')
    		            .observe('click', function(){
    		                $$('.calendar-container').invoke('hide');
    		                calendar.show();
    		            });
		        });
}