﻿jQuery(function($){
    
    // Global Text Resize
    var textSizeItems = $("#textSize li");
    textSizeItems.find("a").click(function(e){
        e.preventDefault();
        
        var size = $(this).attr("href").substring(1);
        
        $.cookie("DHSFontSize", size, {
			path: "/",
			expires: 30
        });
        
		var applySizeTo;
		
		if( $("form #container").length > 0 ) {
			applySizeTo = $("form");
		} else { // no outer <form> tag on the page so #container is the high-level wrapper
			applySizeTo = $("#container");
		}
		
        applySizeTo
			.removeClass("small")
			.removeClass("medium")
            .removeClass("large")
            .addClass(size);

		textSizeItems.removeClass("active")
		$(this).parent().addClass("active");
        
    });  
    
    // Calendar Export
    var addToCal = $(".add-to-calendar");
    addToCal.hover(function(){
        addToCal.addClass("hover");
    }, function(){
        addToCal.removeClass("hover");
    });
    
    // Calendar Month Picker
    var chooseMonth = $("#chooseMonth");
    chooseMonth.unbind("click").bind("click", function() {
    	if(chooseMonth.hasClass("hover")) {
    		chooseMonth.removeClass("hover");
   		} 
   		else {
    		chooseMonth.addClass("hover");
    	}
    });
    chooseMonth.bind('mouseenter',function(){
        chooseMonth.addClass("hover");
    });
    chooseMonth.bind('mouseleave', function(){
        chooseMonth.removeClass("hover");
    });
 
});
    
jQuery.cookie = function (key, value, options) {
    
    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }
        
        value = String(value);
        
        var res = (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
        
        return res;
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

(function ($) {
    $.fn.enterpress = function (f) {

        return this.each(function () {
            $(this).keypress(function (e) {
                var code = (e.which) ? e.which : e.keyCode;
                if (code === 13) f(e);
            });
        });

    }
})(jQuery);

/*
example use:

$("#test").enterpress(function(){
   alert("fired");
});

*/
