$(document).ready(function(){
	if($.browser.msie){
		$('#cc_chooser').tabs();
	}else{
		$('#cc_chooser').tabs({ fx: { opacity: 'toggle' } });
	}
	// $('#cc_chooser ul li:eq(2) a').mousedown(function(){
	// 	$('#cc_chooser').tabs('destroy');
	// 	$(this).attr('href', 'http://turboclaw.com');
	// });
	$('#cc_chooser ul li a').mousedown(function(){
		var myparent = $(this).parent();
		var whichcenter = $('#cc_chooser ul li').index(myparent);
		switch(whichcenter){
			case 0:
				$('#cc_chooser').tabs('destroy');
				$('#cc_descriptions').html('<img src="/images/loading.gif" alt="Loading" class="loading" /> Loading&hellip;');
				$(this).attr('href', 'http://www.bgclondon.ca/');
				break;
			case 2:
				$('#cc_chooser').tabs('destroy');
				$('#cc_descriptions').html('<img src="/images/loading.gif" alt="Loading" class="loading" /> Loading&hellip;');
				$(this).attr('href', 'http://www.pathwaysyorkregion.org/');
				break;
			case 3:
				$('#cc_chooser').tabs('destroy');
				$('#cc_descriptions').html('<img src="/images/loading.gif" alt="Loading" class="loading" /> Loading&hellip;');
				$(this).attr('href', 'http://www.ourplacekw.ca/');
				break;
			case 4:
				$('#cc_chooser').tabs('destroy');
				$('#cc_descriptions').html('<img src="/images/loading.gif" alt="Loading" class="loading" /> Loading&hellip;');
				$(this).attr('href', 'http://www.franklinhorner.org/');
				break;
			case 5:
				$('#cc_chooser').tabs('destroy');
				$('#cc_descriptions').html('<img src="/images/loading.gif" alt="Loading" class="loading" /> Loading&hellip;');
				$(this).attr('href', 'http://www.lechainon.org/');
				break;
			case 6:
				$('#cc_chooser').tabs('destroy');
				$('#cc_descriptions').html('<img src="/images/loading.gif" alt="Loading" class="loading" /> Loading&hellip;');
				$(this).attr('href', 'http://www.laruedesfemmes.com/');
				break;
			case 7:
				$('#cc_chooser').tabs('destroy');
				$('#cc_descriptions').html('<img src="/images/loading.gif" alt="Loading" class="loading" /> Loading&hellip;');
				$(this).attr('href', 'http://www.tyndalestgeorges.ca/');
				break;
		}
	});
	
	$('#cc_descriptions .previous').click(function(){
		var cc_length = $('#cc_descriptions .previous').length;
		var cc_index = $('#cc_descriptions .previous').index(this);
		if(cc_index == 0){
			$('#cc_chooser').tabs('select', cc_length-1);
		}else{
			$('#cc_chooser').tabs('select', cc_index-1);
		}
	});
	$('#cc_descriptions .next').click(function(){
		var cc_length = $('#cc_descriptions .next').length;
		var cc_index = $('#cc_descriptions .next').index(this);
		if(cc_index == cc_length-1){
			$('#cc_chooser').tabs('select', 0);
		}else{
			$('#cc_chooser').tabs('select', cc_index+1);
		}
	});
	
	$('#cc_chooser ul li a, #cc_descriptions .previous, #cc_descriptions .next').click(function(){
		$('#cc_descriptions #vote').removeClass('no_button');
		$('#cc_descriptions #vote input').hide();
		$('#cc_descriptions #vote .button').show();
	});
	
	$('#cc_descriptions input.text').focus(function(){
		if($(this).val() == "Your email address"){
			$(this).val('');
		}
	});
	$('#cc_descriptions input.text').blur(function(){
		if($(this).val() == ''){
			$(this).val("Your email address");
		}
	});
	
	$('#cc_descriptions form').submit(function(){
		var which_cc = $('#cc_descriptions .ui-tabs-panel:not(.ui-tabs-hide)').attr('id');
		$('#cc_descriptions select#cc').val(which_cc);
		
		var email_val = $('#cc_descriptions form #email').val();
		var center_id = $('#cc_descriptions select#cc').val().substr(2);
		
		$.ajax({
			type: "POST",
			dataType: "json",
			url: "/vote",
			data: "vote[email_address]="+email_val+"&vote[center_id]="+center_id,
			success: function(data, textStatus){
				if(typeof data.errors == "undefined"){
					location.replace(thankyou_url);
				}else{
					var error_str = data.errors.join("<br/>");
					$("#vote_status").empty().html("<span style=\"color: red\">"+error_str+"</span>");
				}
			}
		});
		
		return false;
	});
});
