$(function() {
	if($('#logout_link').size() > 0) {
		$('#logout_link').attr("rel", setTimeout("logout()",1200000));

		$('*').live('click', function(){
			var timerId = $('#logout_link').attr("rel");
			clearTimeout(timerId);
			$('#logout_link').attr("rel", setTimeout("logout()", 1200000));
		});
	}
	

/*
	$('a, input, select option').live('click', function() {
		var uri = window.location.pathname;
		var html_class = $(this).attr('class');
		var html_id = $(this).attr('id');
		var href = $(this).attr('href');
		var text = $(this).text();
		var tagname = this.tagName;
		
		var as = true;
		
		if(tagname.toLowerCase() == 'a' && href != '#') {
			as = false;
		}
		
		if(tagname.toLowerCase() == 'input' && $(this).attr('type') == 'submit') {
			if($(this).closest('form').attr('action') != '') {
				as = false;
			}
		}
		
		if(tagname.toLowerCase() == 'option') {
			html_id = $(this).parent().attr('id');
		}
		
		$.ajax({
			url: '/assets/ajax/log_click.php',
			type: 'POST',
			async: as,
			data: { 
				uri: uri,
				html_class: html_class,
				html_id: html_id,
				href: href,
				text: text,
				tagname: tagname,
				click_log: 'click_log'
			},
			dataType: 'html',
			success: function(data) {}
		});
	});
*/
	
	$('.tooltip').live('mouseover',function() {
		var pos_top = $(this).position().top - 30;
		var pos_left = $(this).position().left - 6;
		var txt = $(this).text();
		$(this).after('<div class="show_tooltip">'+txt+'</div>')
		$('div.show_tooltip').css('top', pos_top).css('left', pos_left);
	});
	$('.tooltip').live('mouseout',function() {
		$('div.show_tooltip').remove();
	});
	
	$('#bug_report').children('ul').hide();
	
	$('a#bug_report_label, #close_link').live('click', function() {
		$('#bug_report_container').children('ul, p').toggle('fast');
		return false;
	});
	
	$('#bug_report_container input[type=button]').live('click', function() {
		var bug_msg = $('textarea#bug').val();
		var myuri = window.location.pathname;
		
		$.ajax({
			url: '/assets/ajax/report_bug.php',
			type: 'POST',
			async: true,
			data: { 
				uri: myuri,
				msg: bug_msg
			},
			dataType: 'html',
			success: function(data) {
				$('#bug_report, #bug_report_container p').hide();
				$('textarea#bug').val('');
				alert('Thank you for your bug report submission.  We will fix the problem very soon!');
			}
		});
		return false;
	});
	
	
	$('.helpme').live('mouseover',function() {
		var txt = $(this).text();
		$(this).after('<div class="helpme_show">'+txt+'</div>');
		
		var ht = $('div.helpme_show').outerHeight();
		var wt = $('div.helpme_show').outerWidth();
		
		var pos_top = $(this).position().top - ht;
		var pos_left = $(this).position().left - (34);
		$('div.helpme_show').css('top', pos_top).css('left', pos_left);
		$('div.helpme_show').css('position', 'absolute');
		$('div.helpme_show').fadeIn('normal');
	});
	
	$('.helpme').live('mouseout', function() {
		$('div.helpme_show').fadeOut('fast', function() {
			$(this).remove();
		});
	});
	
});

function reload_flash() {
		$.ajax({
			url: '/assets/ajax/load_flash.php',
			type: 'POST',
			async: true,
			data: {},
			dataType: 'html',
			success: function(data) {
				$('div#flash_container').remove();
				$('div#no_javascript_warning').remove();
				$('div#session_identification').remove();
				$('div#content .wrapper').prepend(data);
				check_skill_threshhold();
				check_occupation_threshhold();
			}
		});
}

function partial(part_name, req_vars, async) {
	var code = "default";
	var uri = '/assets/js_partials/'+part_name+'.php';
	
	if(req_vars != null) {
		uri = uri + '?' + req_vars;
	}
	
	if(async == undefined)
		async = false;
	
	$.ajax({
		url: uri,
		type: 'POST',
		async: async,
		data: {},
		dataType: 'html',
		success: function(data) {
			code = data;
		}
	});
	
	return code;
}

function check_skill_threshhold() {
	var regex = /^\/account(\/)?/i;
	if(!regex.test(window.location.pathname)) {
		return false;
	}
	
		$.ajax({
			url: '/account/ajax/check_minimum_skill_threshhold.php',
			type: 'POST',
			async: true,
			data: {},
			dataType: 'html',
			success: function(data) {
				$('li#sidebar_account_skills a').children('img').remove();
				$('li#sidebar_account_skills a').prepend(data);
			}
		});
}

function check_occupation_threshhold() {
	var regex = /^\/account(\/)?/i;
	if(!regex.test(window.location.pathname)) {
		return false;
	}
	
		$.ajax({
			url: '/account/ajax/check_minimum_occupation_threshhold.php',
			type: 'POST',
			async: true,
			data: {},
			dataType: 'html',
			success: function(data) {
				$('li#sidebar_account_occupations a').children('img').remove();
				$('li#sidebar_account_occupations a').prepend(data);
			}
		});
}

function logout() {
	window.location = "/logout/";
}

