$.extend($.expr[":"], {
    "containsNC": function(elem, i, match, array) {
        return (elem.textContent || elem.innerText || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
	}
});

$.extend($.expr[":"], {
    "beginswith": function(elem, i, match, array) {
        return (new RegExp("^" + match[3]).test( elem.textContent || elem.innerText || "" ));
	}
});

function advancedlist(data){
		var schools = eval('(' + data + ')');
		var schoolslist = '<ul>';
		var i = 0;
		if(schools[0] == '0'){
			$("#comparelist").html('<p style="letter-spacing:-1px;">No schools found that match your criteria</p>');
		}else{
			for(var id in schools){
				schoolslist += '<li><a href="#" class="remove" id="' + id + '">X</a> ' + schools[id] + '<input type="hidden" name="schools[]" value="' + id + '" /></li>';	
				i++;
			}
			schoolslist += '</ul>';
			if(i > 0){
				//$("#instructions").hide();
				schoolslist += '<input type="submit" name="submit" value="Compare These Schools"  />';
			}else{
				//$("#instructions").show();
			}
			$("#comparelist").html(schoolslist);
			$("#comparelist ul li a").click(function(){
				var sid = $(this).attr('id');
				$('#school-comparison').append('<input type="hidden" name="exclude[]" value="' + sid + '" />');
				$(this).parent().remove();	
				return false;
			});
			$('.main > .content').css({'min-height':$('#comparelist').height() + 200});
		}
	
}

function generatelist(){
	var schoolslist = '<ul>';
	var i = 0;
	$("ul.schools input:checked").not("#selectall, #selectallbystate").each(function(){
		schoolslist += '<li><a href="#" class="remove" id="' + $(this).val() + '">X</a> ' + $(this).parent().children('a').html() + '</li>';
		i++;
	});
	schoolslist += '</ul>';
	
	if(i > 0){
		//$("#instructions").hide();
		schoolslist += '<input type="submit" name="submit" value="Compare These Schools"  />';
	}else{
		//$("#instructions").show();
	}
	
	if($("#school-comparison #controls").height() > $("ul.schools").height()){
		$("ul.schools").height($("#school-comparison #controls").height());
	}else{
		$("ul.schools").css('height','auto');
	}
	
	$("#comparelist").html(schoolslist);
	$("#comparelist ul li a").click(function(){
		var schoolid = $(this).attr('id');
		$("ul.schools input[type=checkbox]").each(function(){
			if($(this).val() == schoolid){
				$(this).attr('checked',false);
			}
		});
		$(this).parent().remove();
		
		return false;
	});	
}

function schoolslist(){
	$("ul.schools input[type=checkbox]").click(function(){
		generatelist();
	});
}


$(document).ready(function(){
	$("#compare > a").click(function(){
		$(this).parent().submit();
	});
	
	$(".seenote").click(function(){
		$(this).parent().parent().find(".note").slideToggle('slow', function(){
			if ($(this).is(':visible')) {
			    $(this).parent().find(".seenote").html('Hide Note');
			} else {
			    $(this).parent().find(".seenote").html('See Note');
			}
		});
		return false;
	});
	
	$(".showhide").click(function(){
		$(".hidden").children("li").toggle();
		if($(".hidden li:first").is(':visible')) {
		    $(".showhide").html('Hide List');
		} else {
		    $(".showhide").html('Show List');
		}
		return false;
	});
	
	$("#selectall, #selectallbystate").click(function(){
		var checked_status = this.checked;
		$("ul.schools input[type=checkbox]:visible").not("#selectall, #selectallbystate").each(function(){
			this.checked = checked_status;
		});
		generatelist();
	});
	
	generatelist();
	schoolslist();
	defaults();
	
	$("#comparison").tablesorter(); 
	
	$("#textfilter").focus(function(){
		$(this).val('');
		var term = $(this).val();
		var state = $("#statefilter").val();
		$("#selectall, #selectallbystate").attr('checked',false);
		$("#alphabetical li a").removeClass('current');
		
		if(term.length > 0){
			$("#school-comparison ul.schools li").show();
			if(term.toLowerCase() != "any school"){
				$("#school-comparison ul.schools li a").not(":containsNC('" + term + "')").parent().hide();
			}
		}else{
			$("#school-comparison ul.schools li").show();
		}
		if(state != ""){
			$("#school-comparison ul.schools li a").not("." + state).parent().hide();
		}
		
		generatelist();
	});
	
	$("#textfilter").keyup(function(){
		var term = $(this).val();
		var state = $("#statefilter").val();
		$("#selectall, #selectallbystate").attr('checked',false);
		$("#alphabetical li a").removeClass('current');
		
		if(term.length > 0){
			$("#school-comparison ul.schools li").show();
			if(term.toLowerCase() != "any school"){
				$("#school-comparison ul.schools li a").not(":containsNC('" + term + "')").parent().hide();
			}
		}else{
			$("#school-comparison ul.schools li").show();
		}
		if(state != ""){
			$("#school-comparison ul.schools li a").not("." + state).parent().hide();
		}
		
		generatelist();
	});
	
	$("#statefilter").change(function(){
		var term = $(this).val();
		var text = $("#textfilter").val();
		$("#selectall, #selectallbystate").attr('checked',false);
		$("#alphabetical li a").removeClass('current');
		
		if(term.length > 0){
			$("#school-comparison ul.schools li").show();
			if(term != ""){
				$("#school-comparison ul.schools li a").not("." + term).parent().hide();
			}
		}else{
			$("#school-comparison ul.schools li").show();
		}
		if(text.toLowerCase() != "any school"){
			$("#school-comparison ul.schools li a").not(":containsNC('" + text + "')").parent().hide();
		}
		
		generatelist();
	});
	
	$("#alphabetical li a").click(function(){
		var term = $(this).attr('id');
		$("#selectall, #selectallbystate").attr('checked',false);
		$("#alphabetical li a").removeClass('current');
		$("#textfilter, #statefilter").val("");
		$(this).addClass('current');
		
		$("#school-comparison ul.schools li").show();
		$("#school-comparison ul.schools li a").not(":beginswith('" + term + "')").parent().hide();
		if($("#school-comparison #controls").height() > $("ul.schools").height()){
			$("ul.schools").height($("#school-comparison #controls").height());
		}else{
			$("ul.schools").css('height','auto');
		}
		return false;
	});
	
	$('.region .toggle').click(function(){
		$(this).parents('table:first').find('tbody').toggle();
			if($(this).parents('table:first').find('tbody').is(':visible')) {
			    $(this).html('Hide');
			} else {
			    $(this).html('Show');
			}
		return false;
	});
	
	$('#school-comparison #parameters input').change(function(){
		//defaults();
		$.ajax({
			type: "POST",
			url: "/schools/advancedquery/",
			data: $(this).parents('form').serialize(),
			responseType: 'json',
			beforeSend: function(){
				$('#progress').html('<img src="/public/images/searching.gif">');
			},
			success: function(response){
				advancedlist(response);
				//$("#comparelist").html(response);
			},
			error: function(xhr,status){
				clearlist();
				//alert(status)
			},
			complete: function(){
				$('#progress').html('');
			}
		});
	});
	
	$('.find-school #schoolname').bind('focus', function(){
		$(this).val("");
		$('.find-school #state').val(0);	
		$('.find-school #region').val(0);
	});
	$('.find-school #state').bind('change', function(){
		$('.find-school #schoolname').val("NAME OF UNIVERSITY");
		$('.find-school #region').val(0);
	});
	
	$('.find-school #region').bind('change', function(){
		$('.find-school #schoolname').val("NAME OF UNIVERSITY");
		$('.find-school #state').val(0);
	});
});

function defaults(){
	var statecount = 0;
	var gradecount = 0;
	var subjectcount = 0;
	/*
	$('.region').parent().parent().parent().find(':input').each(function(){
		if($(this).attr('checked') == true){
			statecount ++;
		}
	});
	if(statecount == 0){
		$('.region').parent().parent().parent().find(':input').each(function(){
			$(this).attr('checked', true);
			$(this).parents('table:first').find('tbody').show();
		});
	}
	*/
	$('#grades').find(':input').each(function(){
		if($(this).attr('checked') == true){
			gradecount ++;
		}
	});
	
	if(gradecount == 0){
		$('#grades').find(':input').each(function(){
			$(this).attr('checked', true);
		});
	}
	if($('#tuition_min').val() == '') $('#tuition_min').val('0');
	if($('#tuition_max').val() == '') $('#tuition_max').val('100000');
	if($('#gradrate_min').val() == '') $('#gradrate_min').val('0');
	if($('#gradrate_max').val() == '') $('#gradrate_max').val('100');
	
	$('#subjects').find(':input').each(function(){
		if($(this).attr('checked') == true){
			subjectcount ++;
		}
	});
	
	if(subjectcount == 0){
		$('#subjects').find(':input').each(function(){
			$(this).attr('checked', true);
		});
	}
}

window.onscroll = function()
{
	if( window.XMLHttpRequest ) { // IE 6 doesn't implement position fixed nicely...
		if($("#school-comparison #controls.advanced").height() < window.innerHeight || $("#school-comparison #controls.advanced").height() < document.documentElement.clientHeight){
			if ((document.documentElement.scrollTop > 242 || self.pageYOffset > 242)) {
				$('#school-comparison #controls.advanced').css({'position':'fixed','top':10});
			}else{
				$('#school-comparison #controls.advanced').css({'position':'absolute','top':'auto'});
			}
		} else {
			$('#school-comparison #controls.advanced').css({'position':'absolute','top':'auto'});
		}
		
		if($("#school-comparison #controls.simple").height() < window.innerHeight || $("#school-comparison #controls.simple").height() < document.documentElement.clientHeight){
			if ((document.documentElement.scrollTop > 317 || self.pageYOffset > 317)) {
				$('#school-comparison #controls.simple').css({'position':'fixed','top':10});
			}else{
				$('#school-comparison #controls.simple').css({'position':'absolute','top':'auto'});
			}
		} else {
			$('#school-comparison #controls.simple').css({'position':'absolute','top':'auto'});
		}
	}
}

function rotateQuotes(id){
	jQuery('#quotes ul li').hide();
	jQuery('#quotes ul li:nth-child(' + id + ')').fadeIn('slow');

	if(id >= jQuery('#quotes ul li').size() - 1){
		id = 1;
	}else{
		id++;
	}
	setTimeout("rotateQuotes(" + id + ")",12000);	
}


jQuery(document).ready(function(){
	rotateQuotes(1);
});
