$(document).ready(function(){
	
	// Login button in the header
	$('#login-button').click(function(){
		
		$('#login-form').show();
		$('#login-form input:first').focus();
		$(this).parent().hide();
		$(this).blur();
		return false;
	});
	
	// Tabs
	$('div.title-box a').not('span a').click(function(){
		
		var next_div = $(this).closest('div').next();
		
		next_div.html(loading_icon);
		next_div.load($(this).attr('href'), false, function(){ 
																job_pagination_init(); 
																init_watches(); 
																init_forms(); 
																init_edits();
																init_tabs();

																if ($('#uploader').length > 0){

																	init_uploads();
																}

																if (typeof news_pagination_init == 'function'){
																	
																	news_pagination_init();
																}

																if (typeof tweets_pagination_init == 'function'){
																	
																	tweets_pagination_init();
																}
															});
		
		$(this).closest('ul').find('a').removeClass('active');
		$(this).addClass('active');
		$(this).blur();
		return false;
	});

	if (isset('redirect')){
		
		$('div.title-box a[href="' + redirect + '"]:first').each(function(){
		
			var next_div = $(this).closest('div').next();
			
			next_div.html(loading_icon);
			next_div.load($(this).attr('href'), false, function(){ 
																	job_pagination_init(); 
																	init_watches(); 
																	init_forms(); 
																	init_edits();
																	init_tabs();

																	if ($('#uploader').length > 0){

																		init_uploads();
																	}

																	if (typeof news_pagination_init == 'function'){
																		
																		news_pagination_init();
																	}

																	if (typeof tweets_pagination_init == 'function'){
																		
																		tweets_pagination_init();
																	}
																});
			
			$(this).closest('ul').find('a').removeClass('active');
			$(this).addClass('active');
			$(this).blur();
			return false;
		});
	}
	
	// Search
	$('input#search').data('orig', $('input#search').val());
	$('input#zip').data('orig', $('input#zip').val());

	$('input#search, input#zip').focus(function(){
		
		$(this).val($(this).val() == $(this).data('orig') ? '' : $(this).val());
	});

	$('input#search, input#zip').blur(function(){
		
		$(this).val($(this).val() == '' ? $(this).data('orig') : $(this).val());
	});


	// Home Page Live Search

	$('.home-search input#search').keyup(function(){
		
		if ($(this).val().length > 2){
			
			$('.big-box:first').html(loading_icon);
			$('.big-box:first').load('/search/', { q : $(this).val() });
		}
	});


	// Watch buttons / links
	init_watches();
	init_forms();
	init_tabs();

	if ($('#uploader').length > 0){ init_uploads();	}
	

	// Pre's
	$('pre').each(function(){
		
		$(this).after('<textarea class="pre">' + $(this).text() + '</textarea>');
	});

	$('pre').remove();

	$('textarea.pre').click(function(){
		
		$(this).select();
	});
});

var init_tabs = function(){
	
	$('span.tab a').click(function(){
		
		$('#tab-content').html(loading_icon);
		$('#tab-content').load($(this).attr('href'), false, function(){ 
																		init_forms();
																		if ($('#uploader').length > 0){
																			init_uploads();
																		}		
																	});
		$(this).closest('.tabs').find('span').removeClass('active');
		$(this).parent().addClass('active');
		return false;
	});
}

var init_edits = function(){

	$('span.edit-job a').click(function(){
		
		$('#account-cont').html(loading_icon);
		$('#account-cont').load($(this).attr('href'), false, function(){ init_forms(); init_edits(); });
		return false;
	});
}

var init_forms = function(){
	
	if ($('input.date').length > 0) {

		$('input.date').datePicker();
	}
	
	$('form.ajax').submit(function() { 
		
		$('input.date').removeAttr('disabled');

		$(this).ajaxSubmit(
							{ 
								url: $(this).attr('action'), 
								type: 'POST', 
								target: $(this).closest('div.big-box'), 
								beforeSubmit: function(){

									$('input.date').removeAttr('disabled');
									$('#account-cont').html(loading_icon);
								},
								success: function(){
									
									init_forms();
								}
							}
						);

		window.location='#';
		return false; 
	});

	$('form.ajax-tab').submit(function() { 
		
		$('input.date').removeAttr('disabled');

		$(this).ajaxSubmit(
							{ 
								url: $(this).attr('action'), 
								type: 'POST', 
								target: $('#tab-content'), 
								beforeSubmit: function(){

									$('input.date').removeAttr('disabled');
									$('#tab-content').html(loading_icon);
								},
								success: function(){
									
									init_forms();
								}
							}
						);

		window.location='#';
		return false; 
	});
}

var init_watches = function(){

	$('a.watch').click(function(){
			
		var parent = $(this).closest('div.to-watch');

		$('div.' + alert_class).remove();
		$('<div class="' + alert_class + ' success">Adding to watch list...</div>').insertBefore(parent);
		$(this).addClass('active');

		$.post($(this).attr('href'), false, function(data){
			
			render_alert_inline(1, data.status_message);
			init_watches();

		}, 'json');
		
		$(this).blur();
		return false;
	});

	$('a.stop').click(function(){
		
		$.get($(this).attr('href'));
		$(this).closest('div.to-watch').slideUp('fast');

		if ($('div.to-watch:visible').length == 1){
			
			window.location = '/account/watches/';
		}

		return false;
	});
}

var alert_class	= 'inline-flash';

function render_alert(status, message){ // unused.
	
	$('#flash-cont').html('<div class="success">' + message + '</div>');
}

function render_alert_inline(status, message, target){
	
	$('div.' + alert_class).html(message);
	
}

// utilities

function isset(varname)  {
	if(typeof( window[ varname ] ) != "undefined") return true;
	else return false;
}
