var BTS = BTS || {};

BTS.init = function() {
	/*  --------------------------------------------------
	Pop Up Boxes ----------------------------------------- */
	/*$(".show_login").fancybox({
		'autoDimensions': false,
		'centerOnScroll': true,
		'hideOnContentClick': false,
		'width': '603px',
		'height': '294px'
	})*/;
	
	$(".show_enter_contest").fancybox({
		'overlayColor': '#000',
		'autoDimensions': false,
		'hideOnContentClick': false,
		'width': '900px',
		'height': '250px'
	});
	
	$(".show_vip").fancybox({
		'overlayColor': '#000',
		'autoDimensions': false,
		'hideOnContentClick': false,
		'width': '808px',
		'height': '640px'
	});
	
	/* Trivia form */
	var $question = $("#question"),
		$correct = $("#correct"),
		$incorrect = $("#incorrect");
	
	if(typeof(has_vote_o) == 'undefined') {
		$(".show_trivia").fancybox({
			'overlayColor': '#000',
			'autoDimensions': false,
			'hideOnContentClick': false,
			'width': '900px',
			'height': '330px'
		});
	}
	
	$("#frm_trivia input[type=submit]").bind('click', function(event) {
		event.preventDefault();
		
		var selected = $('#frm_trivia input:radio:checked').val();
		if(selected == "oct7") {
			$question.fadeOut('slow', function() {
				$correct.fadeIn('slow');
				BTS.CastVote('o');
				$('#sidebar .show_trivia').addClass('show_trivia_complete');
			});
		} else if(selected == "four") {
			$question.fadeOut('slow', function() {
				$correct.fadeIn('slow');
				BTS.CastVote('o');
				$('#sidebar .show_trivia').addClass('show_trivia_complete');
			});
		} else if(selected == "student_bundle") {
			$question.fadeOut('slow', function() {
				$correct.fadeIn('slow');
				BTS.CastVote('o');
				$('#sidebar .show_trivia').addClass('show_trivia_complete');
			});
		} else {
			$question.fadeOut('slow', function() {
				$incorrect.fadeIn('slow');
			});
		}
	});
	
	$("#trivia_popup #answer_again").bind('click', function(event) {
		$incorrect.fadeOut('slow', function() {
			$question.fadeIn('slow');
		});
	});
	
	/* Popup form validation */
	$('#frm_login').validate({
		rules: {
			uemail: {
				required: true,
				email: true
			},
			upassword: {
				required: true
			}
		},
		showErrors: function(errorMap, errorList) {
			this.defaultShowErrors();
		},
		errorPlacement: function(error,element) {
			return true;
		}
	});
	
	$('#frm_trivia').validate({
		rules: {
			answer: {
				required: true,
				number: true
			}
		},
		showErrors: function(errorMap, errorList) {
			this.defaultShowErrors();
		},
		errorPlacement: function(error,element) {
			return true;
		}
	});
	
	/* ---------------------------------------------------
	Add vote for branch locator -------------------------- */
	$('#find_branch a').bind('click', function(e) {
		e.preventDefault();
		$link = $(this);
		if(typeof(has_vote_b) == 'undefined') {
			BTS.CastVote('b', function() {
				window.open($link.attr('href'), '_blank');
				$link.addClass('complete');
			});
		} else {
			window.open($link.attr('href'), '_blank');
		}
	});
	
	/* ---------------------------------------------------
	Launch Facebook authentication ----------------------- */
	$('#enter_popup a:first').bind('click', function(e) {
		e.preventDefault();
		BTSFacebookRegister();
	});
	
	/* ---------------------------------------------------
	Launch Facebook login function ----------------------- */
	$('#fb_login a').bind('click', function(e) {
		e.preventDefault();
		BTSFacebookLogin();
		
		//$('#loading_overlay').show();
		//$('#loading_overlay').height($(document).height());
	});
	
	/* ---------------------------------------------------
	Facebook Login  -------------------------------------- */
	function BTSFacebookRegister() {
	  FB.login(function(response) {
		  if (response.session) {
			  if (response.perms) {
				  // user is logged in and granted some permissions.
				  //perms is a comma separated list of granted permissions
				  var fbsess = response.session;
				  redirectUrl = nextUrl + "/t/" + fbsess.access_token;
				  //alert(redirectUrl);
				  window.location = redirectUrl;
			  }
		  }
	  }, {perms:'email'});
  }
  function BTSFacebookLogin() {
	  FB.login(function(response) {
		  if (response.session) {
			  var fbsess = response.session;
			  redirectUrl = "/login/t/" + fbsess.access_token;
			  //alert(redirectUrl);
			  window.location = redirectUrl;
		  }
	  }, { perms: 'email'});
  }
};

/* -----------------------------------------------------
Facebook Publish --------------------------------------- */
BTS.streamPublish = function (name, description, hrefTitle, hrefLink, userPrompt, imageSrc, imageUrl) {
	FB.ui({
		method: 'stream.publish',
		message: '',
		attachment: {
		media: [{type: 'image',src: imageSrc,href: imageUrl}], 
		name: name,
		caption: '',
		description: (description),
		href: hrefLink
	},
	action_links: [{ text: hrefTitle, href: hrefLink }],
	user_prompt_message: userPrompt
	});
}

/* -----------------------------------------------------
Vote function: takes vote type param ------------------- */
BTS.CastVote = function (voteType, callback) {
  $.ajax({
	  url: '/vote/' + voteType,
	  success: function() {
		  if(voteType == 'v') {
			  $('#need_vote').fadeOut('slow', function() {
				$('#nofb_thanks').fadeIn('slow');
				$('#share_on_fb').fadeIn('slow');
			  });
		  } else {
			  window.location.reload()
		  }
		  
		  if(callback instanceof Function) { callback(); }
	  }
  });
}

/* -------------------------------------------------------
Open Enter Contest Modal box function (flash) ------------ */
BTS.EnterContestModal = function() {
	$('.show_enter_contest').trigger('click');
};

BTS.matchup = function() {
	$('#vote_area > a').bind('click', function() {
		BTS.CastVote('v');
		
		$('#vote_area > a').fadeOut('slow', function() {
			$('#get_more_votes').fadeIn('slow');
		});
	});
}

BTS.media = function() {
	/* ---------------------------------------------------
	Video swticher on media page ------------------------- */
	$('#video_list a').click(function(e) {
		e.preventDefault();
		$('#video iframe').attr('src', $(this).attr('href'));
	});
};

BTS.register = function() {
	/* ---------------------------------------------------
	School switcher (based on Province) ------------------ */
	$('#select_province').bind('change', function() {
		province = $(this).val();
		if(province == "select") {
			$("#BTSuserForm_school_id option").show();
			$("#BTSuserForm_school_id option:not(.select)").attr('disabled', '');
		} else {
			// Hide/disable all options
			$("#BTSuserForm_school_id option:not(.select)").hide();
			$("#BTSuserForm_school_id option:not(.select)").attr('disabled', 'disabled');
			
			// Enable schools from the province of users choice
			$("#BTSuserForm_school_id ." + province).show();
			$("#BTSuserForm_school_id ." + province).attr('disabled', '');
			
			// Remove users selection
			$("#BTSuserForm_school_id").val("select");
		}
	});
	
	/* Select Province based on school */
	$('#BTSuserForm_school_id').bind('change', function() {
		provinceId = $(this).val();
		province = $('option[value="' + provinceId + '"]').attr('class');
		if(province.length != 0) {
			$("#select_province ." + province).attr('selected', 'selected');
			
			$("#BTSuserForm_school_id option:not(.select)").hide();
			$("#BTSuserForm_school_id option:not(.select)").attr('disabled', 'disabled');
			
			$("#BTSuserForm_school_id ." + province).show();
			$("#BTSuserForm_school_id ." + province).attr('disabled', '');
		} else {
			$('#select_province').val('select');
			$("#BTSuserForm_school_id option").attr('disabled', '');
		}
	});
	
	/*  --------------------------------------------------
	Form Validation -------------------------------------- */
	/* Add a method to add not equal rule */
	jQuery.validator.addMethod("notEqual", function(value, element, param) {
		return this.optional(element) || value !== param;
	});
	
	$('#frm_facebook_register input[type=submit]').bind('click', function() {
		$('#frm_facebook_register').validate({
			rules: {
				select_province: {
					required: true,
					notEqual: "select"
				},
				"BTSuserForm[school_id]": {
					required: true,
					notEqual: "select"
				},
				"BTSuserForm[first_name]": {
					required: true
				},
				"BTSuserForm[last_name]": {
					required: true
				},
				"BTSuserForm[rules]": {
					required: true
				}
			},
			showErrors: function(errorMap, errorList) {
				this.defaultShowErrors();
			},
			errorPlacement: function(error, element) {
				error.appendTo($('#jqvalid_error_list'));
				$('em > strong').addClass('error');
				$('h2.error').show();
				return true;
			}
		});
	});
	
	$('#frm_register input[type=submit]').bind('click', function() {
		$('#frm_register').validate({
			rules: {
				select_province: {
					required: true,
					notEqual: "select"
				},
				"BTSuserForm[school_id]": {
					required: true,
					notEqual: "select"
				},
				"BTSuserForm[first_name]": {
					required: true
				},
				"BTSuserForm[last_name]": {
					required: true
				},
				"BTSuserForm[user_email]": {
					required: true,
					email: true
				},
				"BTSuserForm[password]": {
					required: true
				},
				"BTSuserForm[password_confirm]": {
					required: true,
					equalTo: "#BTSuserForm_password"
				},
				"BTSuserForm[rules]": {
					required: true
				},
				"BTSuserForm[captcha]": {
					required: true
				}
			},
			showErrors: function(errorMap, errorList) {
				this.defaultShowErrors();
			},
			errorPlacement: function(error,element) {
				error.appendTo($('#jqvalid_error_list'));
				$('em > strong').addClass('error');
				$('h2.error').show();
				return true;
			}
		});
	});
}

BTS.login = function() {
	/*  --------------------------------------------------
	Form Validation -------------------------------------- */
	$('#frm_login_page input[type=submit]').bind('click', function() {
		$('#frm_login_page').validate({
			rules: {
				uemail: {
					required: true,
					email: true
				},
				upassword: {
					required: true
				}
			},
			showErrors: function(errorMap, errorList) {
				this.defaultShowErrors();
			},
			errorPlacement: function(error, element) {
				error.appendTo($('#jqvalid_error_list'));
				return true;
			}
		});
	});
}

BTS.resetpass = function() {
/*  --------------------------------------------------
	Form Validation -------------------------------------- */
	$('#frm_forgot_password input[type=submit]').bind('click', function() {
		$('#frm_forgot_password').validate({
			rules: {
				'BTSforgotPasswordForm[user_email]': {
					required: true,
					email: true
				}
			},
			showErrors: function(errorMap, errorList) {
				this.defaultShowErrors();
			},
			errorPlacement: function(error, element) {
				error.appendTo($('#jqvalid_error_list'));
				return true;
			}
		});
	});
	
	$('#frm_change_password input[type=submit]').bind('click', function() {
		$('#frm_change_password').validate({
			rules: {
				'BTSuserForm[password]': {
					required: true
				},
				'BTSuserForm[password_confirm]': {
					required: true,
					equalTo: "#BTSuserForm_password"
				}
			},
			showErrors: function(errorMap, errorList) {
				this.defaultShowErrors();
			},
			errorPlacement: function(error, element) {
				error.appendTo($('#jqvalid_error_list'));
				return true;
			}
		});
	});
}

BTS.resend_email = function() {
/*  --------------------------------------------------
	Form Validation -------------------------------------- */
	$('#frm_resend_email input[type=submit]').bind('click', function() {
		$('#frm_resend_email').validate({
			rules: {
				'user_email': {
					required: true,
					email: true
				}
			},
			showErrors: function(errorMap, errorList) {
				this.defaultShowErrors();
			},
			errorPlacement: function(error, element) {
				error.appendTo($('#jqvalid_error_list'));
				return true;
			}
		});
	});
}
