$(document).ready( function() {
	$("div#schoolList").hide();
	if(queryString("school") != "false"){
	    document.location = document.location.protocol+"//"+document.location.host+"/base.html?school="+queryString("school");
	    return;
	}
// 	if(readCookie("school")) {
// 	    var school = readCookie("school");
// 	    eraseCookie("school");
// 	    document.location = document.location.protocol+"//"+document.location.host+"/base.html?school="+school;
// 	    return;
// 	}
	getUniv();
	$("a").click(function() {
		if ($(this).attr("href") == "http://ninjabooks.catchthebest.com/jobs/") {
		    try {
			pageTracker._trackPageview('/outgoing/jobs');
		    }catch(err){}
		}
	    });
    } );

var Where = {};

function getUniv() {
    $.getJSON("/js/school.json", function (data) {
	    universitySelector(data);
	});
}


function universitySelector(data) {
    $("#suggest1").autocomplete(data,
				{
				    matchContains:true /* Match doesn't have to start at first character */
					,matchWords:true   /* My enhancement: must match start of a word */
					,max:30 /* Number of items in the scroll list */
					,formatItem:function (data,i,total){
					return data[1];
				    }
				    ,cacheLength:100
					 ,selectFirst:true
					 ,width:400
					 })
	.blur(function(){
		if($(this).val() == "" || $(this).val() == "Enter name of School") {
		    $(this).css({color:'#CCCCCC'}).val("Enter name of School");
		}
	    })
	.result(function(a,b) {
		document.location = document.location.protocol+"//"+document.location.host+"/base.html?school="+ b[1].replace(new RegExp(" ","g"),"_");
	    })
	.focus(function(){
		if($(this).val() == "Enter name of School") $(this).val("");
		$(this).css({color:'black'});
	    }).val("Enter name of School").css({color:'#CCCCCC'}).show();
    
    $("input#goButton").click(function() {
	var result = $("#suggest1").val();
	var uni = -1;
	for(var u=0;u<data.length;u++){
	    if (result == data[u][1]) {
		uni = data[u][0];
		break;
	    }
	}
	if (uni == -1){
	    alert("Select a school from the list.");
	} else {
	    document.location = document.location.protocol+"//"+document.location.host+"/base.html?school="+ result.replace(new RegExp(" ","g"),"_");
	}
    });
};


function PageQuery(q) {
    if(q.length > 1) this.q = q.substring(1, q.length);
    else this.q = null;
    this.keyValuePairs = new Array();
    if(q) {
	for(var i=0; i < this.q.split("&").length; i++) {
	    this.keyValuePairs[i] = this.q.split("&")[i];
	}
    }
    this.getKeyValuePairs = function() { return this.keyValuePairs; }
    this.getValue = function(s) {
	for(var j=0; j < this.keyValuePairs.length; j++) {
	    if(this.keyValuePairs[j].split("=")[0] == s)
		return this.keyValuePairs[j].split("=")[1];
	}
	return false;
    }
    this.getParameters = function() {
	var a = new Array(this.getLength());
	for(var j=0; j < this.keyValuePairs.length; j++) {
	    a[j] = this.keyValuePairs[j].split("=")[0];
	}
	return a;
    }
    this.getLength = function() { return this.keyValuePairs.length; } 
}
function queryString(key){
    var page = new PageQuery(window.location.search); 
    return unescape(page.getValue(key)); 
}

