$(document).ready(function(){
	$('#srcssc_1').click(function(){ 
		$('#sfname').val($('#fname').val());
		$('#slname').val($('#lname').val());
		$('#semail').val($('#email').val());
		$('#semail_confirm').val($('#email_confirm').val());
    });
	$('#srcssc_2').click(function(){ 
		$('#sfname').val('');
		$('#slname').val('');
		$('#semail').val('');
		$('#semail_confirm').val('');
    });
	//$('#audience,#audience_additional').change(function(){
	$('#audience').change(function(){
		$('#audience_evaluators,#audience_schools,#audience_admins').css('display','none');
		switch($('#audience option:selected').val())
		{
			case '1': $('#audience_evaluators').css('display','block'); break;
			case '2': 
			case '3': 
			case '4': 
			case '5': $('#audience_schools').css('display','block'); break;
			case '6': $('#audience_admins').css('display','block'); break;
		}
		console.log('changed');
	});
	$('#available_change').click(function(){
		if (($('#available_sort')[0].selectedIndex != 0)&& ($('#available_options')[0].selectedIndex != 0))
		{
			if ($('#available_sort option:selected').val()=='All')
			{
				$('.tablesorter INPUT[type="checkbox"]').each(function(i){ $(this).attr('checked',$('#available_options option:selected').val()); });
			}
			else
			{
				var col;
				if ($('#available_sort option:selected').val().substr(0,6)=='Cohort') { col = 9; }
				if ($('#available_sort option:selected').val().substr(0,6)=='Region') { col = 12; }
				
				$('.tablesorter tr').each(function(i){
					var row = $(this);
					if ($('td:eq('+col+')',row).text()==$('#available_sort option:selected').val()) { $('td:eq(3) INPUT[type="checkbox"]',row).attr('checked',$('#available_options option:selected').val()); }
				});
			}
		}
	});
	$('#load_template').change(function(){
		if ($('#load_template')[0].selectedIndex != 0)
		{
			$('#loading_template').css('visibility','visible');
			$.post('/dashboard/admin/ajax_load_template',
			{ id: $('#load_template option:selected').val() },
			function(ret){
   				$('#content').val(ret);
				$('#loading_template').css('visibility','hidden');
 			});
			$.post('/dashboard/admin/ajax_load_subject',
			{ id: $('#load_template option:selected').val() },
			function(ret){
   				$('#subject').val(ret);
 			});
		}
		else
		{
			$('#content').val('');
			$('#subject').val('');
		}
	});
	$('#uncheck_radios_1').click(function(){
		$('#confirmed_1,#confirmed_2').removeAttr('checked');
	});
	$('#uncheck_radios_2').click(function(){
		$('#training_1,#training_2').removeAttr('checked');
		$('#training_data').css('display','none');
	});
	$('#uncheck_radios_3').click(function(){
		$('#reliability_1,#reliability_2').removeAttr('checked');
		$('#reliability_data').css('display','none');
	});
	$('#uncheck_radios_4').click(function(){
		$('#srcssc_1,#srcssc_2').removeAttr('checked');
	});
	$('#uncheck_radios_5').click(function(){
		$('#pptl_1,#pptl_2').removeAttr('checked');
	});
	$('#uncheck_radios_6').click(function(){
		$('#status_1,#status_2').removeAttr('checked');
	});
	$('#uncheck_radios_7').click(function(){
		$('#evaluation_1,#evaluation_2,#evaluation_3').removeAttr('checked');
	});
	$('#affiliations_all').click(function(){
		$('.checkboxes_1').each(function(i){
            $(this).attr('checked','checked');
        });
	});
	$('#compensations_all').click(function(){
		$('.checkboxes_2').each(function(i){
            $(this).attr('checked','checked');
        });
	});
	$('#regions_all').click(function(){
		$('.checkboxes_3').each(function(i){
            $(this).attr('checked','checked');
        });
	});
	$('#levels_all').click(function(){
		$('.checkboxes_4').each(function(i){
            $(this).attr('checked','checked');
        });
	});
	$('#months_all').click(function(){
		$('.checkboxes_5').each(function(i){
            $(this).attr('checked','checked');
        });
	});
	$('#years_all').click(function(){
		$('.checkboxes_6').each(function(i){
            $(this).attr('checked','checked');
        });
	});
	$('#zips_all').click(function(){
		$('.checkboxes_7').each(function(i){
            $(this).attr('checked','checked');
        });
	});
	$('#districts_all').click(function(){
		$('.checkboxes_8').each(function(i){
            $(this).attr('checked','checked');
        });
	});
	$('#district_nums_all').click(function(){
		$('.checkboxes_9').each(function(i){
            $(this).attr('checked','checked');
        });
	});
	$('#cohorts_all').click(function(){
		$('.checkboxes_10').each(function(i){
            $(this).attr('checked','checked');
        });
	});
	$('#load_report').change(function(){
		if ($('#load_report')[0].selectedIndex != 0)
		{
			$('#loading_report').css('visibility','visible');
			$.post('/dashboard/admin/ajax_load_report',
			{ id: $('#load_report option:selected').val() },
			function(ret){
				var obj = jQuery.parseJSON(ret);
				$('#audience_additional').val(obj.emails);
   				$('#report_audience_name').html(obj.audience);
				$('#report_audience').val(obj.audience);
				$('#report_audience_parent').css('display','block');
				$('#loading_report').css('visibility','hidden');
				
				$('#audience_evaluators,#audience_schools,#audience_admins').css('display','none');
				switch(obj.audience)
				{
					case 'Evaluators': $('#audience_evaluators').css('display','block'); break;
					case 'Admins': $('#audience_admins').css('display','block'); break;
					case 'School Registration Contacts': 
					case 'School SET Contacts': 
					case 'Principals':
					case 'PBIS Team Leaders': $('#audience_schools').css('display','block'); break;
					
				}
 			});
		}
		else
		{
			$('#audience_additional').val('');
			$('#report_audience_name').html('');
			$('#report_audience').val('');
			$('#report_audience_parent,#audience_evaluators,#audience_schools,#audience_admins').css('display','none');
			$('#loading_report').css('visibility','hidden');
		}
	});
	$('#training_1').click(function(){ $('#training_data').css('display','block'); });
	$('#training_2').click(function(){ $('#training_data').css('display','none'); });
	$('#reliability_1').click(function(){ $('#reliability_data').css('display','block'); });
	$('#reliability_2').click(function(){ $('#reliability_data').css('display','none'); });
	$('#email_back').click(function(){ history.go(-1); });
	
	$('#affiliation_0').click(function(){
		$('#affiliation_school_req').css('display',$(this).is(':checked')?'inline':'none');
	});
	$('#affiliation_1').click(function(){
		$('#affiliation_district_req').css('display',$(this).is(':checked')?'inline':'none');
	});
	$('#affiliation_2').click(function(){
		$('#affiliation_other_req').css('display',$(this).is(':checked')?'inline':'none');
	});
	
	// add new widget called repeatHeaders
	$.tablesorter.addWidget({
		// give the widget a id
		id: "repeatHeaders",
		// format is called when the on init and when a sorting has finished
		format: function(table) {
			// cache and collect all TH headers
			if(!this.headers) {
				var h = this.headers = []; 
				$("thead th",table).each(function() {
					h.push(
						"<th>" + $(this).text() + "</th>"
					);	
				});
			}
			// remove appended headers by classname.
			$("tr.repated-header",table).remove();
			// loop all tr elements and insert a copy of the "headers"	
			for(var i=0; i < table.tBodies[0].rows.length; i++) {
				// insert a copy of the table head every 10th row
				if((i%5) == 4) {
					$("tbody tr:eq(" + i + ")",table).before(
						$("<tr></tr>").addClass("repated-header").html(this.headers.join(""))
						
					);	
				}
			}
		}
	});

	$('.tablesorter').tablesorter({
		widgets: ['zebra','repeatHeaders'],
		headers: { 3: { sorter: false }, 29: { sorter: false } }
	});
	$('.delete').click(function(){
		return confirm('Are you sure you want to delete?');
	});
	$('.map').colorbox({transition:'fade', width:'80%', height:'80%', iframe:true});
});

 
