function resizeTab() {
	var h = $('#column-left').height();
	var oh = $('#tab-content').height();
	var div = $('#tab-pages')[0];
	if(div) div.style.minHeight = (Math.max(oh, h - 80)) + 'px';
}

function toggler(x) {
	var w = $(x).next(".toggler")[0];
	if(w.className == 'toggler') {
		x.className = (x.className == 'closed' ? 'open' : 'closed');
		if(x.className == 'closed') $(w).slideUp(function(){
			resizeTab();
		}); else $(w).slideDown(function(){
			resizeTab();
		});
	}
}

function changeColour(elementId) {
    var interval = 1000;
    var colour1 = "#ff0000"
    var colour2 = "#000000";
    if (document.getElementById) {
        var element = document.getElementById(elementId);
        element.style.color = (element.style.color == colour1) ? colour2 : colour1;
        setTimeout("changeColour('" + elementId + "')", interval);
    }
}
function switchTab(id) {
	$('#tab-headers').children().removeClass('tab-active');
	$('#ptab-'+id).addClass('tab-active');

	$('#tab-pages').children().hide();
	$('#ctab-'+id).show();
	$(".prettyform").prettyform();
}
function invite_friend() {
	name = $("#invite_name").val();
	email = $("#invite_email").val();
	$('#friend_response').load('/portal/send_invite', {name:name,email:email}, function(data){
		if(data == '<b>Your invitation has been sent successfully!</b>') {
			
		$("#invite_name").val('');
		$("#invite_email").val('');
		$("#invite_ir").val($("#invite_ir").val()-1);
		}
	});
}
$(document).ready(function(){
	

	
	if(typeof jQuery.fn.prettyPhoto == 'function') {
		$("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});
	}
	$(".prettyform").prettyform();
	$(".size").autosize();
	$(".alert,.message,.error").fadeIn('slow', function(){
		$(this).oneTime(2000, function(){
			$(this).fadeOut('slow');
		}).mouseover(function(){
			$(this).stopTime();
		}).mouseout(function() {
			$(this).oneTime(2000, function(){
				$(this).fadeOut('slow');
			});
		});;
	})
	$('a[rel*=facebox]').facebox({
	  loading_image : '/static/facebox/loading.gif',
	  close_image   : '/static/facebox/closelabel.gif'
	})
$(document).bind('reveal.facebox', function() { 
	$(".prettyform").prettyform();
	if(typeof impact_rating != 'undefined') {
		count = $('input:radio[name=count]:checked').val() == 'other' ? $('input:text[name=count_input]').val() : $('input:radio[name=count]:checked').val();
		amount = $('input:text[name=recurring_amount]').val();
		impact = (count * amount * 12) /(10/impact_rating);
		$("#monthly_billing").html('$'+roundNumber(count*amount,2));
		$("#recurring_impact").html(roundNumber(impact,0));
		// date night stuff
		$("input:text[name=recurring_amount]").keyup(function(){
			count = $('input:radio[name=count]:checked').val() == 'other' ? $('input:text[name=count_input]').val() : $('input:radio[name=count]:checked').val();
			amount = $(this).val();
			impact = (count * amount * 12) /(10/impact_rating);
			$("#monthly_billing").html('$'+roundNumber(count*amount,2));
			$("#recurring_impact").html(roundNumber(impact,0));
		});
		$("input:text[name=count_input]").keyup(function(){
			$('#other_count')[0].checked=true;
			count = $('input:text[name=count_input]').val();
			amount = $('input:text[name=recurring_amount]').val();
			impact = (count * amount * 12) /(10/impact_rating);
			$("#monthly_billing").html('$'+roundNumber(count*amount,2));
			$("#recurring_impact").html(roundNumber(impact,0));
		}).focus(function(){
			$('#other_count')[0].checked=true;
			count = $('input:text[name=count_input]').val();
			amount = $('input:text[name=recurring_amount]').val();
			impact = (count * amount * 12) /(10/impact_rating);
			$("#monthly_billing").html('$'+roundNumber(count*amount,2));
			$("#recurring_impact").html(roundNumber(impact,0));
		});
		$("input:radio[name=count]").change(function(){
			count = $('input:radio[name=count]:checked').val() == 'other' ? $('input:text[name=count_input]').val() : $('input:radio[name=count]:checked').val();
			amount = $('input:text[name=recurring_amount]').val();
			impact = (count * amount * 12) /(10/impact_rating);
			$("#monthly_billing").html('$'+roundNumber(count*amount,2));
			$("#recurring_impact").html(roundNumber(impact,0));
		});
		
	}
 })
});

function tb(x) {
	var cb = $(x).find('input')[0];
	cb.checked = !cb.checked;
	x.className = (cb.checked ? 'c-selected' : '');
}
function submit_donate() {
	serialize = $("#donate_form").serialize();
	jQuery.facebox(function() { 
	  jQuery.get('/financial/donate?'+serialize, function(data) {
	    jQuery.facebox(data);
	  })
	})
}
function selectAll() {
	$('#contact_import_result').children('div').each(function(x, y) {
		y.className = 'c-selected';
		$(y).find('input')[0].checked = true;
	});
}

function selectNone() {
	$('#contact_import_result').children('div').each(function(x, y) {
		y.className = '';
		$(y).find('input')[0].checked = false;
	});
}

function friendList() {
	$('#contact_import_mailpage').slideUp();
	$('#contact_import_resultpage').slideDown();
}

function mailPage() {
	$('#contact_import_resultpage').slideUp();
	$('#contact_import_mailpage').slideDown();
}


function donateRecurring() {
	$('#recurring_donate_dform').slideUp();
	$('#recurring_donate_loading').slideDown(function(){
		$.ajax({
			url: '/portal/donate_recurring',
			type: 'POST',
			dataType: 'json',
			data: $('#recurring_donate_form').serialize(),
			error: function(x) {
				$('#recurring_donate_dform').slideDown();
				$('#recurring_donate_loading').slideUp();
				$('#contact_import_error').html('Connection failed, please try again').slideDown();
			},
			success: function(x) {
				if(x.error) {
					$('#recurring_donate_dform').slideDown();
					$('#recurring_donate_loading').slideUp();
					$('#contact_import_error').html(x.error).slideDown();
				} else {		
					$('#recurring_donate_dform').slideDown();
					$('#recurring_donate_loading').slideUp();	
					$('#contact_import_error').html(x.error).slideDown();
					return;
					$('#recurring_donate_dform').slideUp();
					$('#recurring_donate_loading').slideUp();
					$('#recurring_donate_success').slideDown();
				}
			}
		});	
	});
}
function closeFacebox() {
	$(document).trigger('close.facebox');
}

var canceled = false;

function loadContacts() {
	canceled = false;
	$('#contact_import_error').slideUp();
	$('#contact_import_dform').slideUp();
	$('#contact_import_loading').slideDown();
	$.ajax({
		url: '/tools/contacts_import',
		type: 'POST',
		dataType: 'json',
		data: $('#contact_import_form').serialize(),
		error: function(x) {
   		$('#contact_import_error').html('Connection failed, please try again').slideDown();
			cancelContacts();
		},
		success: function(x) {
			if(canceled) return;
    	if(x.error) {
    		$('#contact_import_error').html(x.error).slideDown();
				cancelContacts();
    	} else {
    		var html = '';
				for(i=0; i<x.contacts.length; i++) {
					html += '<div onclick="tb(this)" class="xsel"><span class="e-n"><input type="checkbox" onclick="this.checked=!this.checked");return:false;" />'+x.contacts[i].name+'</span><span class="e-e">'+x.contacts[i].email+'</span></div>';
				}
				$('#contact_import_loading').slideUp();
				$('#contact_import_result').html(html);
				$('#contact_import_resultpage').slideDown();
    	}
		}
	});
}

function cancelContacts() {
	canceled = true;
	$('#contact_import_resultpage').slideUp();
	$('#contact_import_loading').slideUp();
	$('#contact_import_dform').slideDown();
}

var ucanceled = false;
function cancelUpdate() {
	$('#update_posting').slideUp();
	$('#update_postpage').slideDown();
}

function postUpdate() {
	ucanceled = false;
	$('#update_postpage').slideUp();
	$('#update_posting').slideDown();
	$.ajax({
  	url: '/portal/update',
		type: 'POST',
		dataType: 'json',
		data: 'text=' + escape($('#update-text').val()),
		error: function(x) {
   		$('#update_error').html('Connection failed, please try again').slideDown();
			cancelUpdate();
		},
		success: function(x) {
			if(ucanceled) return;
    	if(x.error) {
    		$('#update_error').html(x.error).slideDown();
    	} else {
				$('#update-text').val('');
    		$(x.html).insertAfter('#update_posting');
				$('#'+x.newId).slideDown();
    	}
			cancelUpdate();
		}
	})
}
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
function deleteUpdate(id) {
	$.ajax({
  	url: '/portal/delete_update/'+id,
		dataType: 'json',
		error: function(x) {
   		alert('Error deleting update');
		},
		success: function(x) {
    	if(x.error) {
   			alert('Error deleting update: '+x.error);
    	} else {
				$('#post-'+x.id).slideUp();
    	}
		}
	})
}

function projectSearch(x) {
	$('#team_project_search').load('/admin/projects/'+team_id+'?search='+encodeURIComponent(x.value), function() {$(this).slideDown()});
}

function memberSearch(key, x) {
	$('#member_search').load('/admin/people/'+key+'/'+team_id+'?search='+encodeURIComponent(x.value), function() {$('#member_search').slideDown()});
}

function impact() {
	
		$(function(){
			$.fn.supersized.options = {  
				startwidth: 500,  
				startheight: 500,
				vertical_center: 1,
				slideshow: 1,
				navigation: 0,
				transition: 0, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
				pause_hover: 0,
				slide_counter: 1,
				slide_captions: 1,
				slide_interval: 3000  
			};
	        $('#supersize').supersized(); 
	    });
}