var CampusSaver = function() {
    var ck = this;
    ck.checkBox = null;
    ck.chooseCampus = null;
    ck.slug = null;
    
    ck.load = function() {
        ck.slug = $('#header div.logo a').attr('href');    
        ck.checkBox = $('#header .campus_pref input[type="checkbox"]');        
        ck.chooseCampus = $('#header #utility-links .campus');
                        
        // set initial checkBox state
        ck.checkBox.attr("checked", false);
        var cookieRe = new RegExp("campus="+ck.slug);
        if(cookieRe.test(document.cookie)) ck.checkBox.attr("checked", true);
        
        // wire-up checkbox
        ck.checkBox.change(function(evt) {            
            var state = $(this).attr("checked");
            if(state == true) ck.update();
            else ck.clear();
        });
        
        // wire-up choose your campus link
        ck.chooseCampus.click(function(evt) {            
            ck.clear();
        });
        
    }
    
    ck.clear = function() {
        document.cookie = "campus=; path=/";
    }
    
    ck.update = function() {
        var date = new Date();
		date.setTime(date.getTime()+(365*24*60*60*1000));
		// set expiration to 1 year out
        document.cookie = "campus=" + ck.slug + "; expires="+date.toGMTString() + "; path=/";
    }
    
    ck.load();
};


$(document).ready(function(){
    new CampusSaver();
});
