/*
 * Cookie Functions
 * http://jquery-howto.blogspot.com/2010/09/jquery-cookies-getsetdelete-plugin.html
 * http://www.quirksmode.org/js/cookies.html
 */
function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function deleteCookie(name) {
    setCookie(name,"",-1);
}

$(document).ready(function() {
	$('a[rel*=external]').click( function() {
		this.target = "_blank";
	});
	
	$('body').addClass('hasJS');
	$('.jsHide').show();
	
	//jQuery Cycle
    $('#old').cycle({
		fx: 'scrollDown', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		pager: '#bannerNav',
		pause: 1,
		timeout: 7500
	});
	//Rotator Functions
	$('#banner').cycle({ 
		timeout: 7500,
		pause: 1,
		speed: 750,
		fx: 'fade',
		startingSlide: 1,
		pauseOnPagerHover: true,
		pager:  '#bannerNav',
		pagerAnchorBuilder: function(idx, slide) {return '#bannerNav li:eq(' +(idx)+ ') a';}
	});
	$('#goto1').click(function() { 
		$('#rotator').cycle(0); 
		return false; 
	}); 
	 
	$('#goto2').click(function() {  
		$('#rotator').cycle(1);  
		return false;  fus
	});
	$('#goto3').click(function() { 
		$('#rotator').cycle(2); 
		return false; 
	});
	$('#bannerNav a').click(function() { return false; });
	
	//jQuery FAQ
	$('#faq').makeFAQ();

	//jQuery Validation
	jQuery.validator.addMethod("phone", function(phone_number, element) {
		phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 9 &&
			phone_number.match(/^(1(.|-)?)?(\([2-9]\d{2}\)|[2-9]\d{2})(.|-)?[2-9]\d{2}(.|-)?\d{4}$/);
	}, "Invalid Phone Number");	
	jQuery.validator.messages.required = "Required";
	$("form.validate").validate({
		messages: {
			email: {
				email: "Invalid Email Address"
			}
		}
	});
	$("#commentform").validate();
	$("#submitForm").validate();
	//Enrollment Form
	$("#other").hide();
	$("select#hear").change(function () {
		var other = $("select#hear option:selected").val();
		if(other == 'other') { $("#other").show(); }
		else { $("#other").hide(); }
	});
	
	// Accessibility
	$('#accessibility .title').show();
	fontSize = 0;
	accSize = getCookie('accessibilitySize');
	if ( accSize ) {
		fontSize = accSize;
		$('#accessibility .reset').show();
		if ( accSize < 0 ) {
			accSizePos = Math.abs(accSize);
			$('p,li,:header,li a,li a span, label').each( function() {
				$(this).css('font-size','-=' + parseInt(accSizePos));
			});
			if ( accSize == -3 ) {
				$('#smallerFont').css({'cursor':'default','font-weight':'normal'});
			}
		} else {
			$('p,li,:header,li a,li a span').each( function() {
				$(this).css('font-size','+=' + parseInt(accSize));
			});
			if ( accSize == 6 ) {
				$('#largerFont').css({'cursor':'default','font-weight':'normal'});
			}
		}
	}
	accColor = getCookie('accessibilityColor');
	if ( accColor ) {
		$('body').addClass(accColor).addClass('accessibility');
		$('#linkhome').css({'text-indent':'0','font-size':'26px'});
		$('#accessibility .reset').show();
	}
	$('#accessibility .title').click(function() {
		$('#accessibility .contents, #accessibility .resetContainer').animate({
			height: 'toggle'
			}, 500, function() {
				// Animation complete.
		});
	});
	$("#accessibility p.color-change a").click( function (){
		color = $(this).attr('class');
		$('body').removeClass('black-white white-black black-yellow yellow-black blue-yellow yellow-blue black-pink black-pale-yellow black-pale-blue').addClass(color).addClass('accessibility');
		$('#linkhome').css({'text-indent':'0','font-size':'24px'});
		$('#accessibility .reset').fadeIn();
		setCookie('accessibilityColor', color, 90);
	});
	$("#smallerFont").click( function (){
		if ( fontSize != -3 ) {
			$('p,li,:header,li a,li a span, label').each( function() {
				$(this).css('font-size','-=1');
			})
			fontSize = fontSize - 1;
			if ( fontSize == -3 ) {
				$(this).css({'cursor':'default','font-weight':'normal'});
			}
			$('#largerFont').css({'cursor':'pointer','font-weight':''});
			$('#accessibility .reset').fadeIn();
		}
		setCookie('accessibilitySize', fontSize, 90);
	});
	$("#largerFont").click( function (){
		if ( fontSize != 6 ) {
			$('p,li,:header,li a,li a span, label').each( function() {
				$(this).css('font-size','+=1');
			})
			fontSize = fontSize + 1;
			if ( fontSize == 6 ) {
				$(this).css({'cursor':'default','font-weight':'normal'});
			}
			$('#smallerFont').css({'cursor':'pointer','font-weight':''});
			$('#accessibility .reset').fadeIn();
		}
		setCookie('accessibilitySize', fontSize, 90);
	});
	$('#accessibility .reset').click(function() {
		fontSize = 0;
		$('p,li,:header,li a,li a span').each( function() {
			$(this).css('font-size','');
		});
		$('body').removeClass('black-white white-black black-yellow yellow-black blue-yellow yellow-blue black-pink black-pale-yellow black-pale-blue accessibility');
		$('#linkhome').css({'text-indent':'','font-size':''});
		$('#accessibility .reset').fadeOut();
		deleteCookie('accessibilityColor');
		deleteCookie('accessibilitySize');
	});
});
