// applying to grade
var _gradeType = {
	freshman : {
		parkerhall : {
			'title' : 'Parker Hall'
		}
	},
	sophomore : {
		hallowellhall : {
			'title' : 'Hallowell Hall'
		},
		abbotthall : {
			'title' : 'Abbott Hall'
		}
	},
	junior : {
		hallowellhall : {
			'title' : 'Hallowell Hall'
		},
		abbotthall : {
			'title' : 'Abbott Hall'
		}
	},
	senior : {
		whittierhall : {
			'title' : 'Whittier Hall'
		},
		ramseyhall : {
			'title' : 'Ramsey Hall'
		}
	}
}
// change grade select
function selectGrade(){
	/*
	if($('select#grade').length && $('select#dormitory').length) {
		$('select#grade').change(function(){
			var _opts = '';
			var _val = $(this).val();
			for (_item in _gradeType[_val]) {
				_opts += '<option value="' + _item + '">' + _gradeType[_val][_item].title + '</option>';
			}
			$('select#dormitory').html(_opts);
		});
	}
	*/
	// hardcode select errors
	var _dorm = $('select#dormitory');
	var _grad = $('select#grade');
	if(_dorm.length && _grad.length) {
		var _currDorm = _dorm.find('option').index(_dorm.find('option').filter(':selected'));
		_dorm.change(function(){
			if($(this).val()=='parkerhall') {
				if(_grad.val()=='freshman') {
					_currDorm = _dorm.find('option').index(_dorm.find('option').filter(':selected'));
				}else{
					_dorm.find('option').eq(_currDorm).attr('selected',true);
					$(this).parents('.col').find('.formError2').fadeIn(400);
				}
			}
			if($(this).val()=='hallowellhall') {
				if(_grad.val()=='sophomore' || _grad.val()=='junior') {
					_currDorm = _dorm.find('option').index(_dorm.find('option').filter(':selected'));
				}else{
					_dorm.find('option').eq(_currDorm).attr('selected',true);
					$(this).parents('.col').find('.formError2').fadeIn(400);
				}
			}
			if($(this).val()=='abbotthall') {
				if(_grad.val()=='sophomore' || _grad.val()=='junior') {
					_currDorm = _dorm.find('option').index(_dorm.find('option').filter(':selected'));
				}else{
					_dorm.find('option').eq(_currDorm).attr('selected',true);
					$(this).parents('.col').find('.formError2').fadeIn(400);
				}
			}
			if($(this).val()=='whittierhall') {
				if(_grad.val()=='senior') {
					_currDorm = _dorm.find('option').index(_dorm.find('option').filter(':selected'));
				}else{
					_dorm.find('option').eq(_currDorm).attr('selected',true);
					$(this).parents('.col').find('.formError2').fadeIn(400);
				}
			}
			if($(this).val()=='ramseyhall') {
				if(_grad.val()=='senior') {
					_currDorm = _dorm.find('option').index(_dorm.find('option').filter(':selected'));
				}else{
					_dorm.find('option').eq(_currDorm).attr('selected',true);
					$(this).parents('.col').find('.formError2').fadeIn(400);
				}
			}
		}).mousedown(function(){
			$(this).parents('.col').find('.formError2').hide();
		});
	}
}
// validation function
function initFormValidation() {
	if(typeof $().validationEngine === 'function') {
		if($("#content form").length){
			$("#content form").validationEngine({
				success : false,
				failure : function() {}
			});
		}
	}
}
function initTable(){
	$('input[name=radio01]').click(function(){
		$('.formError2').hide();
		if($(this).attr('class').indexOf('white')!=-1) {
			if($('input[name=radio02]').filter(':checked').attr('class')){
				if($('input[name=radio02]').filter(':checked').attr('class').indexOf('grey')!=-1) {
					if(Math.abs($('input[name=radio02]').val()-$(this).val()<=1)) {
						$(this).parent().find('.formError2').fadeIn(200);
						$(this).attr('checked',false);
						return false;
					}
				}
			}
			if($('input[name=radio03]').filter(':checked').attr('class')){
				if($('input[name=radio03]').filter(':checked').attr('class').indexOf('grey')!=-1) {
					if(Math.abs($('input[name=radio03]').val()-$(this).val()<=1)) {
						$(this).parent().find('.formError2').fadeIn(200);
						$(this).attr('checked',false);
						return false;
					}
				}
			}
		}else{
			if($('input[name=radio02]').filter(':checked').attr('class')){
				if($('input[name=radio02]').filter(':checked').attr('class').indexOf('white')!=-1) {
					if(Math.abs($('input[name=radio02]').val()-$(this).val()<=1)) {
						$(this).parent().find('.formError2').fadeIn(200);
						$(this).attr('checked',false);
						return false;
					}
				}
			}
			if($('input[name=radio03]').filter(':checked').attr('class')){
				if($('input[name=radio03]').filter(':checked').attr('class').indexOf('white')!=-1) {
					if(Math.abs($('input[name=radio03]').val()-$(this).val()<=1)) {
						$(this).parent().find('.formError2').fadeIn(200);
						$(this).attr('checked',false);
						return false;
					}
				}
			}
		}
	});
	$('input[name=radio02]').click(function(){
		$('.formError2').hide();
		if($(this).attr('class').indexOf('white')!=-1) {
			if($('input[name=radio01]').filter(':checked').attr('class')){
				if($('input[name=radio01]').filter(':checked').attr('class').indexOf('grey')!=-1) {
					if(Math.abs($('input[name=radio01]').val()-$(this).val()<=1)) {
						$(this).parent().find('.formError2').fadeIn(200);
						$(this).attr('checked',false);
						return false;
					}
				}
			}
			if($('input[name=radio03]').filter(':checked').attr('class')){
				if($('input[name=radio03]').filter(':checked').attr('class').indexOf('grey')!=-1) {
					if(Math.abs($('input[name=radio03]').val()-$(this).val()<=1)) {
						$(this).parent().find('.formError2').fadeIn(200);
						$(this).attr('checked',false);
						return false;
					}
				}
			}
		}else{
			if($('input[name=radio01]').filter(':checked').attr('class')){
				if($('input[name=radio01]').filter(':checked').attr('class').indexOf('white')!=-1) {
					if(Math.abs($('input[name=radio01]').val()-$(this).val()<=1)) {
						$(this).parent().find('.formError2').fadeIn(200);
						$(this).attr('checked',false);
						return false;
					}
				}
			}
			if($('input[name=radio03]').filter(':checked').attr('class')){
				if($('input[name=radio03]').filter(':checked').attr('class').indexOf('white')!=-1) {
					if(Math.abs($('input[name=radio03]').val()-$(this).val()<=1)) {
						$(this).parent().find('.formError2').fadeIn(200);
						$(this).attr('checked',false);
						return false;
					}
				}
			}
		}
	});
	$('input[name=radio03]').click(function(){
		$('.formError2').hide();
		if($(this).attr('class').indexOf('white')!=-1) {
			if($('input[name=radio02]').filter(':checked').attr('class')){
				if($('input[name=radio02]').filter(':checked').attr('class').indexOf('grey')!=-1) {
					if(Math.abs($('input[name=radio02]').val()-$(this).val()<=1)) {
						$(this).parent().find('.formError2').fadeIn(200);
						$(this).attr('checked',false);
						return false;
					}
				}
			}
			if($('input[name=radio01]').filter(':checked').attr('class')){
				if($('input[name=radio01]').filter(':checked').attr('class').indexOf('grey')!=-1) {
					if(Math.abs($('input[name=radio01]').val()-$(this).val()<=1)) {
						$(this).parent().find('.formError2').fadeIn(200);
						$(this).attr('checked',false);
						return false;
					}
				}
			}
		}else{
			if($('input[name=radio02]').filter(':checked').attr('class')){
				if($('input[name=radio02]').filter(':checked').attr('class').indexOf('white')!=-1) {
					if(Math.abs($('input[name=radio02]').val()-$(this).val()<=1)) {
						$(this).parent().find('.formError2').fadeIn(200);
						$(this).attr('checked',false);
						return false;
					}
				}
			}
			if($('input[name=radio01]').filter(':checked').attr('class')){
				if($('input[name=radio01]').filter(':checked').attr('class').indexOf('white')!=-1) {
					if(Math.abs($('input[name=radio01]').val()-$(this).val()<=1)) {
						$(this).parent().find('.formError2').fadeIn(200);
						$(this).attr('checked',false);
						return false;
					}
				}
			}
		}
	});
}
// ajax gallery
function initAjaxGallery(){
	$('div.gallery').each(function(){
		// options
		var _slideTime = 1000; // 1 sec slide animation duration
		// vars
		var _holder = $(this);
		var _prevBtn = _holder.find('.prev-arrow');
		var _nextBtn = _holder.find('.next-arrow');
		var _ajaxian = _holder.find('.ajax-links a');
		var _hold = _holder.find('.hold');
		var _ul = _hold.find(' > ul');
		var _li = _ul.find(' > li');
		var _animated = false;
		var _step = parseInt(_hold.width());
		// reset
		_prevBtn.find('span:eq(0)').addClass('disabled');
		var _currEl = 0;
		// click 'prev' button
		_prevBtn.click(function(){
			if(!$(this).find('span:eq(0)').hasClass('disabled') && !_animated) {
				_currEl--;
				_animated = true;
				_nextBtn.find('span:eq(0)').removeClass('disabled');
				_ul.animate({
					marginLeft:'+='+_step
				},_slideTime,function(){
					if(_currEl==0) _prevBtn.find('span:eq(0)').addClass('disabled');
					_animated = false;
				});
			}
			return false;
		});
		// click 'next' button
		_nextBtn.click(function(){
			if(!$(this).find('span:eq(0)').hasClass('disabled') && !_animated) {
				_currEl++;
				if(!_li.eq(_currEl).length){
					var _url = _ajaxian.eq(_currEl).attr('href');
					if(_url){
						$('span.ajax-loading').show();
						$.ajax({
							url: _url,
							success: function(msg){
								$('span.ajax-loading').hide();
								_animated = true;
								_prevBtn.find('span:eq(0)').removeClass('disabled');
								_ul.append('<li>' + msg + '</li>');
								_li = _ul.find(' > li');
								_ul.animate({
									marginLeft:'-='+_step
								},_slideTime,function(){
									if(_currEl==_ajaxian.length-1) _nextBtn.find('span:eq(0)').addClass('disabled');
									_animated = false;
								});
							},
							error: function(){
								$('span.ajax-loading').hide();
								alert('Error ajax loading...');
							}
						});
					}
				}else{
					_animated = true;
					_prevBtn.find('span:eq(0)').removeClass('disabled');
					_ul.animate({
						marginLeft:'-='+_step
					},_slideTime,function(){
						if(_currEl==_ajaxian.length-1) _nextBtn.find('span:eq(0)').addClass('disabled');
						_animated = false;
					});
				}
				
			}
			return false;
		});
	});
}
$(function(){
	selectGrade();
	initFormValidation();
	initTable();
	initAjaxGallery();
});