function showActiveAreas() {
	if ($('#follow').length){
		$("#follow").addClass("inactive");
		$("#existing_alerts").addClass("inactive");
		$("#create_alert").addClass("inactive");
		$("#criteria").addClass("inactive");
	  $("#frequency").addClass("inactive");
	  $("#submit").addClass("inactive");
	  //var employer = getUrlVars()['employer'];
	  var split_url=document.URL.split('/');		
		var employer = (split_url[3]);
	  if (employer) {
	  	$.get("http://www.cbjobtweets.com/scripts/checkuser.php", { check: 'loggedin', employer: employer }, function(data) {
		   if (data == 1) {
		   	$("#follow").removeClass("inactive");
		   }
			 	$.get("http://www.cbjobtweets.com/scripts/checkuser.php", { check: 'following', employer: employer }, function(data) {
			   if (data == 1) {
			   	$("#existing_alerts").removeClass("inactive");
			   	$("#create_alert").removeClass("inactive");
			   	$("#criteria").removeClass("inactive");
			   }
			 	});
		 	});
		} else {
			$.get("http://www.cbjobtweets.com/scripts/checkuser.php", { check: 'loggedin' }, function(data) {
		   if (data == 1) {
		   	$("#follow").removeClass("inactive");
		   }
			 	$.get("http://www.cbjobtweets.com/scripts/checkuser.php", { check: 'following' }, function(data) {
			   if (data == 1) {
			   	$("#existing_alerts").removeClass("inactive");
			   	$("#create_alert").removeClass("inactive");
			   	$("#criteria").removeClass("inactive");
			   }
			 	});
		 	});
	 	}
		$('#keyword').change(function() {
			$("#frequency").removeClass("inactive");
		});
		$('#location').change(function() {
			$("#frequency").removeClass("inactive");
		});
		$('#daily').change(function() {
			$("#submit").removeClass("inactive");
		});
		$('#weekly').change(function() {
			$("#submit").removeClass("inactive");
		});
	}
	if ($('#companyinfo_form').length) {
		if ($('#public_tweets').attr("checked") == false) {
			$("#tweet_info").addClass("inactive");
		}
		$('#public_tweets').change(function () {
		    if ($(this).attr("checked")) {
					$("#tweet_info").removeClass("inactive");
		        return;
		    }
		   $("#tweet_info").addClass("inactive");
		});
		if ($('#direct_messages').attr("checked") == false) {
			$("#alert_info").addClass("inactive");
		}
		$('#direct_messages').change(function () {
		    if ($(this).attr("checked")) {
					$("#alert_info").removeClass("inactive");
		        return;
		    }
		   $("#alert_info").addClass("inactive");
		});
	}
	if ($('#companyinfo_form').length || $('#additionaltweet_form').length) {
		if ($('#tweet_info').hasClass('inactive') == false) {
			$('#summary_tweet').after('<a href="" id="summary_help" class="help_link">What can I put here?</a>');
			$('#detail_tweet').after('<a href="" id="detail_help" class="help_link">What can I put here?</a>');
		}
		if ($('#talent_network').attr("checked") == false) {
			$(".talent_network_did").hide('fast');
		}
		$('#talent_network').change(function () {
		    if ($(this).attr("checked")) {
					$(".talent_network_did").show('fast');
		        return;
		    }
		   $(".talent_network_did").hide('fast');
		});
		$('#summary_help').click(function() {
		
			return false;
		});
		$('#detail_help').click(function() {
		
			return false;
		});
	}
}

function hideEmployerLink() {
	var cookie = $.cookie("hide_employer_box");
	if (cookie == "true") {
		$('.employer_create').hide('fast');
	} else {
		$('#employer_signin').before('<a href="#" id="close_employer_box">Close</a>');
		$('#close_employer_box').click(function() {
			$('.employer_create').hide('fast');
			$.cookie("hide_employer_box", "true", { expires: 7 });
	  	return false;
		});
	}
}

function stopAlerts(alert_link,alert_id) {
	var queryvalue = alert_link.split('?');
	var alert_stop = queryvalue[1].split('=');
	var this_alert = alert_stop[1];
	$.get('http://www.cbjobtweets.com/scripts/editalerts.php', { stopalert: this_alert }, function(data) {
		$("#"+alert_id).hide();
		$("#"+alert_id).after('<span class="undo_alert_stop">This alert has been stopped. <a href="">Undo</a></span>');
		$(".undo_alert_stop").attr("id", "undostop"+alert_id);
		$('.undo_alert_stop a').click(function() {
			var restart_alert_id = $(this).parent().attr('id');
	  	undoStopAlerts(alert_id,restart_alert_id);
	  	return false;
		});
	});
}

function undoStopAlerts(alert_id,restart_alert_id) {
	var alert_stop = restart_alert_id.split('_');
	var this_alert = alert_stop[1];
	$("#"+restart_alert_id).hide();
	$("#"+alert_id).show();
	$.get('http://www.cbjobtweets.com/scripts/editalerts.php', { restartalert: this_alert });
}

function searchForm() {
	// default text
	var keytext = "Enter search keywords";
	var loctext = "Enter job location";
	if (document.getElementById('savesearch_form')) {
		var searchform = document.getElementById('savesearch_form');
		// set the text in the field to our default text
		searchform.keyword.value=keytext;
		$("#keyword").css("color","#808080");
		$("#location").css("color","#808080");
		searchform.location.value=loctext;
		// if field is default text clear it out, otherwise leave it
		$('input#keyword').focus( function(){
			if(searchform.keyword.value==keytext){
				searchform.keyword.value='';
				$(this).css("color","#333");
			}
		});
		$('input#location').focus( function(){
			if(searchform.location.value==loctext){
				searchform.location.value='';
				$(this).css("color","#333");
			}
		});
		// if it is empty put the default text
		$('input#keyword').blur( function(){
			if(searchform.keyword.value==''){
				searchform.keyword.value=keytext;
				$("#keyword").css("color","#808080");
			}
		});
		$('input#location').blur( function(){
			if(searchform.location.value==''){
				searchform.location.value=loctext;
				$("#location").css("color","#808080");
			}
		});
		// remove default text on submit
		$('form#savesearch_form').submit( function(){
			if(searchform.keyword.value==keytext){
				searchform.keyword.value='';
			}
			if(searchform.location.value==loctext){
				searchform.location.value='';
			}
		});
	}
}

function showSampleResults() {
	var keytext = "Enter search keywords";
	var loctext = "Enter job location";
	if (document.getElementById('savesearch_form')) {
		//var employer = getUrlVars()['employer'];
		var split_url=document.URL.split('/');		
		var employer = (split_url[3]);
		$('.search_title').click(function() {
			var search_value = $(this).html();
			$('#keyword').val(search_value);
			$("#frequency").removeClass("inactive");
			var keyval = $('#keyword').val();
			getJobResults(keyval,'',employer);
	  	return false;
		});
		$('#criteria').after('<table id="jobs_sample"></table>');
		$('#keyword').blur(function() {
			var keyval = $('#keyword').val();
			var locval = $('#location').val();
			if (keyval == keytext) {
				var keyval = '';
			}
			if (locval == loctext) {
				var locval = '';
			}
		  if (employer) {
		  	getJobResults(keyval,locval,employer);
			} else {
				getJobResults(keyval,locval,'');
		 	}
		});
		$('#location').blur(function() {
			var keyval = $('#keyword').val();
			var locval = $('#location').val();
			if (keyval == keytext) {
				var keyval = '';
			}
			if (locval == loctext) {
				var locval = '';
			}
	  	if (employer) {
		  	getJobResults(keyval,locval,employer);
			} else {
				getJobResults(keyval,locval,'');
		 	}
		});
	}
}

function getJobResults(keyword,location,employer) {
	$.get('http://www.cbjobtweets.com/scripts/jobresults.php', { keyword: keyword, location: location, employer: employer },
	function(data){
		var jobs = data;
		$('#jobs_sample').html(jobs);
	})
}

function showOverview() {
	$('.overview_link').click(function() {
		var content = $('div#overview').html();
		controlModal(content,550);
  	return false;
	});
}
function showVideo() {
	$('.video_link').click(function() {
		var content = $('div#video').html();
		controlModal(content,550);
  	return false;
	});
}
function showDirectMessage() {
	$('.directmessages_link').click(function() {
		var content = $('div#control_directmessages').html();
		controlModal(content,550);
  	return false;
	});
}
function controlModal(content, width) {
	if(width == null || width == '') {
		width = 550;
	}
	var myDiv = document.getElementById('alertDiv');
	var formDiv = document.getElementById('alertContent');
	var InnerDiv = document.getElementById('alertDivInner');
	if(myDiv.style.visibility == 'hidden') {
		myDiv.style.visibility = 'visible';
		myDiv.style.top = document.body.scrollTop; 
		myDiv.style.height = document.body.scrollHeight;
		InnerDiv.style.top = document.body.scrollTop + (document.body.clientHeight / 15);
		formDiv.style.width = width;
		formDiv.innerHTML = content;
	} else {
		myDiv.style.visibility = 'hidden';
	}
}

function getUrlVars() {
	var vars = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		vars[key] = value;
	});
	return vars;
}

$(document).ready(function(){	
	$("body").addClass("jquery"); // define as javascript ready
	showActiveAreas();
	hideEmployerLink();
	showOverview();
	showVideo();
	showDirectMessage();
	$('.alert_stop').click(function() {
		var alert_link = $(this).attr('href');
		var alert_id = $(this).attr('id');
  	stopAlerts(alert_link,alert_id);
  	return false;
	});
	searchForm();
	showSampleResults();
});
