(function($) {	
	//Main Method
	$.fn.reportprogress = function(val,maxVal) {			
		var max=100;
		if(maxVal)
			max=maxVal;
		return this.each(
			function(){		
				var div=$(this);
				var innerdiv=div.find(".progress");
				
				if(innerdiv.length!=1) {
					innerdiv=$("<div class='progress'></div>");			
					div.append("<div class='text'>&nbsp;</div>");
					$("<span class='text'></span>").css("width",div.width()).appendTo(innerdiv);					
					div.append(innerdiv);			
				}
				if( val == 100 ) {
					var width= 98;
				} else {
					var width=Math.round(val/max*100);
				}
				innerdiv.css("width", width + "%");
				div.find(".text").html(val + "% of needs met");
			}
		);
	};
})(jQuery);

function createCookie(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 readCookie(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 eraseCookie(name) {createCookie(name,"",-1);}

$(function() {
	$('tr .expando').click(function() {
		if( $(this).parent().hasClass('twiddled') ) {
		    $(this).siblings('ul').animate({ height: 'hide', opacity: 'hide' }, 'fast');
			$(this).parent().removeClass('twiddled');			
		} else {
			$(this).siblings('ul').animate({ height: 'show', opacity: 'show' }, 'fast');
			$(this).parent().addClass('twiddled');
		}
		return false;
	});
});

$(function() {
	var x = readCookie('.gohub.comdont_show');
	if(x) {
		$('#coming-soon #email_verbage').hide();
		//$('#email-me').hide();
	}
});

$(function() {
	$('#email_me').click(function() {
		var email= $( '#notify-me' ).val();
		var date= $( '#date' ).val();
		$.post( "api/utils/email_store.php?email=" + email + '&date=' + date,
			function( data ) {
				if( data== 'TRUE' ) {
					$('#email_verbage').hide();
					$('#thanks').fadeIn();
					createCookie('.gohub.comdont_show','dont_show',7);
				} else if( data== 'FALSE' ) {
					$('#email_verbage').hide();
					$('#thanks').html('<strong>Looks like you have already registered with that email.</strong>').fadeIn();
				}
			}
		);
		return false;
	});
});

function searchPeople( includeDeleteLink ) {
	var name= $('#u_search').val();
	$.post('api/utils/personLookup.php?stub=' + name + '&method=directory' + '&include_delete_link=' + includeDeleteLink, 
		function( data ) {
			if( data != 'FALSE' ) {
				$('#results').html( data );
			}
		}
	);
	return false;
}

function searchGroup() {
	var name= $('#g_search').val();
	$.post('api/utils/groupLookup.php?stub=' + name + '&method=directory', 
		function( data ) {
			if( data != 'FALSE' ) {
				$('#results').html( data );
			}
		}
	);
	return false;
}

$(document).ready(function() {	
	// handle error messages
	setTimeout("$('.logError').fadeIn()", 400);
	if($('.logError').val()!=null) {
		$('#not_logged').hide();
		setTimeout("swapError()", 1300);
	}
	
	$('#post-need-link').click(function() {
		swapActive();
	});

 	$('#a').click(function() {
		$(this).attr('value', '');
	});
	
	$('#admin-pass').click(function() {
		$(this).attr('value', '');
	});
	
	$('#search_people').click(function() {
		$('#group_form').hide();
		$('#results').html('');
		$('#user_form').fadeIn();
		return false;
	});
	
	$('#search_groups').click(function() {
		$('#user_form').hide();
		$('#results').html('');
		$('#group_form').fadeIn();
		return false;
	});
	
	$('#search_my_people').click(function() {
		$('#my_group_form').hide();
		$('#groups').hide();
		$('#results').html('');
		$('#my_user_form').fadeIn();
		$('#people').fadeIn();
		return false;
	});
	
	$('#search_my_groups').click(function() {
		$('#my_user_form').hide();
		$('#people').hide();
		$('#results').html('');
		$('#my_group_form').fadeIn();
		$('#groups').fadeIn();
		return false;
	});
	
	$('#g_my_search_execute').click(function() {
		var name= $('#g_my_search').val();
		$.post('api/utils/groupLookup.php?stub=' + name + '&method=directory', 
			function( data ) {
				if( data != 'FALSE' ) {
					$('#results').html( data );
				}
			}
		);
		return false;
	});
	
	$('#u_my_search_execute').click(function() {
		var name= $('#u_my_search').val();
		$.post('api/utils/personLookup.php?stub=' + name + '&method=directory', 
			function( data ) {
				if( data != 'FALSE' ) {
					$('#results').html( data );
				}
			}
		);
		return false;
	});
	
	$('#u_search_execute').click(function() {
		var name= $('#u_search').val();
		$.post('api/utils/personLookup.php?stub=' + name + '&method=directory', 
			function( data ) {
				if( data != 'FALSE' ) {
					$('#results').html( data );
				}
			}
		);
		return false;
	});
	
	$('#g_search_execute').click(function() {
		var name= $('#g_search').val();
		$.post('api/utils/groupLookup.php?stub=' + name + '&method=directory', 
			function( data ) {
				if( data != 'FALSE' ) {
					$('#results').html( data );
				}
			}
		);
		return false;
	});
});

function next_page_people(page) {
	var name= $('#u_search').val();
	$.post('api/utils/personLookup.php?stub=' + name + '&method=directory&pageID=' + page, 
		function( data ) {
			if( data != 'FALSE' ) {
				$('#results').html( data );
			}
		}
	);
}

function next_page_groups(page) {
	var name= $('#g_search').val();
		$.post('api/utils/groupLookup.php?stub=' + name + '&method=directory', 
		function( data ) {
			if( data != 'FALSE' ) {
				$('#results').html( data );
			}
		}
	);
}

function next_page_my_people(page) {
	var name= $('#u_my_search').val();
	$.post('api/utils/personLookup.php?stub=' + name + '&method=directory&pageID=' + page, 
		function( data ) {
			if( data != 'FALSE' ) {
				$('#results').html( data );
			}
		}
	);
}

function next_page_my_groups(page) {
	var name= $('#g_my_search').val();
	$.post('api/utils/getGroups.php?stub=' + name + '&method=directory&pageID=' + page, 
		function( data ) {
			if( data != 'FALSE' ) {
				$('#results').html( data );
			}
		}
	);
}

function showEdit(n) {
	//$('#' + n + '_need').fadeOut();
	$('#' + n + '_needQuantity').fadeIn();
}

function close_need(p,n) {
	$.post('api/need/CloseNeed.php?id=' + p + '&need=' + n, 
		function( data ) {
		}
	);
	$('#action_butt_' + n).html('<strong>Filled</strong>');
}

function updateQuantity(p,n) {
	var newQuantity= $('#' + n + '_value').val();
//	$.post("api/need/EditNeed.php?project=" + p + '&need=' + n + '&quantity=' + newQuantity);
	
	$.get("/api/need/EditNeed.php", { project: p, need: n, quantity: newQuantity },
			  function(data){
					$('#' + n + '_need').html(data).fadeIn();
					$('#' + n + '_need_total').html(newQuantity).fadeIn();
					$('#' + n + '_needQuantity').fadeOut();

			  });	
}

function runSearch() {
	var term= $('#g2_input').val();
	$('#g2Wrapper').height('800');
	//$('#hughsWrapper').html('<iframe frameborder="0" style="overflow-x:none; overflow-y:auto;" src="http://www.viewzi.com/vfp.php?vfp_id=tr8c22tpdffk&amp;t=29&amp;v=gohub&amp;q=' + term + '" id="viewzi_iframe"/></iframe>');	
	//$('#g2Wrapper').html('<iframe frameborder="0" style="overflow-x:none; overflow-y:auto;" src="http://gohub.local/g2_search/results.php?q=' + term + '" id="g2_iframe"/></iframe>');	
	$('#g2_iframe').attr('src' , '/g2_search/results.php?q=' + term);
}

function swapActive() {
	$('#browse').removeClass('active-tab');
	$('#search').removeClass('active-tab');
	$('#post').addClass('active-tab');
};

function resetTabs() {
	$('#post').removeClass('active-tab');
	$('#browse').addClass('active-tab');	
};

function swapError() {
	$('#not_logged').hide();
	$('.logError').fadeOut();
	$('#login_form').fadeIn();
};

function swapLogin() {
	// swap in the login form when login link is clicked
	$('#not_logged').fadeOut();
	setTimeout("$('#login_form').fadeIn()", 400);
};

Date.format = 'yyyy-mm-dd';