/* Daily Calendar View
*
*  Relies on: jquery 1.1.4, YUI 2.3.0
*  Callbacks located in: sb_calendar_trainer_callbacks.js
*  Callback files should be included in your document first
*/

var calendar_options;
var calendar;

function calendarInit(options){
	calendar_options = options;
}

function calendarInit_ready(){
	calendar = new YAHOO.widget.Calendar("date_select1","calendar_select1",calendar_options);
//	1=2;
    calendar.render();
	calendar.selectEvent.subscribe(onDateSelect);
	$('.calendar h2 a').click(function(){
		$("#calendar_select1").toggle();
	});
	var targetDay = new Date(calendar_options.selected);
	var previousDay = $('#link_prev').attr('href').split('date=')[1];
	var nextDay = $('#link_next').attr('href').split('date=')[1];

	$('.calendar .next').click(function(){
		$("#calendar_select1").hide();
                d = nextDay.split('-');
                $('#activity_date').val( d[1]
                                       + '/'
                                       + d[2]
                                       + '/'
                                       + d[0]);
		return getSchedule(nextDay);
	});
	$('.calendar .prev').click(function(){
		$("#calendar_select1").hide();
                d = previousDay.split('-');
                $('#activity_date').val( d[1]
                                       + '/'
                                       + d[2]
                                       + '/'
                                       + d[0]);
		return getSchedule(previousDay);
	});
	$('.calendar ol.events li').click( function() {li_click(this, targetDay);});
}

function onDateSelect(evt, info){
	$("#calendar_select1").hide();
        $('#activity_date').val( info[0][0][1]
                               + '/'
                               + info[0][0][2]
                               + '/'
                               + info[0][0][0]);
	getSchedule( info[0][0][0]
                   + "-"
                   + info[0][0][1]
                   + "-"
                   + info[0][0][2]);
}

function getSchedule(targetDate){
    if(calendar_ajax_action instanceof Function){
        calendar_ajax_action(targetDate);
        return false;
    }
	$('.calendar h2 a').css('display', 'none');
	$('.calendar .indicator').css('display', 'inline');
	$('.calendar .message').css('display', 'inline');
    $.ajax({ type: "POST",
        url: calendar_ajax_action,
        data: 'date=' + targetDate + '&trainer_id=' + trainer_id,
        success: function(data) {
            $('#first_col').empty().append(data);
	        var d = targetDate.split('-');
			calendarInit({pagedate: d[1] + '/' + d[0]
                            , selected: d[1] + '/' + d[2] + '/' + d[0]});
            calendarInit_ready();
       },
       error: function(req, type, ex) {
           window.location = error_page + '?date=' + targetDate;
	   }
    });
    $('div.flash').empty().removeClass()
	return false;
}

var sched_cancel = function () {
    window.location = error_page + '?date=' + $('#activity_date').val(); 
}

function showTrainersSelect(){
    $('#trainerSelect').toggle();
}
$(document).ready(function(){
	if(calendar_options){
		calendarInit_ready();		
	}
	$('#sched_cancel_link').click(sched_cancel);
	if($('.calTrainers')){
	    $('#trainerSelect').hide();
	    $('.calTrainers p').click(showTrainersSelect);
	}
})
