(function() 
{
	CreativeJobs = {
		
		cj_url: "",
		cj_admin_url: "",
		job_id: "",
		
		FixPng: function()
		{
			var arVersion = navigator.appVersion.split("MSIE");
			var version = parseFloat(arVersion[1]);

			if ((version >= 5.5) && (document.body.filters)) 
			{
			   for(var i=0; i<document.images.length; i++)
			   {
			      var img = document.images[i];
			      var imgName = img.src.toUpperCase();

			      if (imgName == this.cj_url.toUpperCase() + "IMG/BT-RSS.PNG")
			      {
			         var imgID = (img.id) ? "id='" + img.id + "' " : "";
			         var imgClass = (img.className) ? "class='" + img.className + "' " : "";
			         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
			         var imgStyle = "display:inline-block;" + img.style.cssText;
			         if (img.align == "left") imgStyle = "float:left;" + imgStyle;
			         if (img.align == "right") imgStyle = "float:right;" + imgStyle;
			         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
			         var strNewHTML = "<span " + imgID + imgClass + imgTitle;
			         strNewHTML += " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";";
			         strNewHTML += "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader";
			         strNewHTML += "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
			         img.outerHTML = strNewHTML;
			         i = i - 1;
			      }
			   }
			}

		},
		
		PerformSearch: function(url)
		{
			clearTimeout(window.search_timer);	
			$('#job-listings').load(url + encodeURIComponent($('#keywords').fieldValue()) + '/');	
			$("#indicator").hide();	
		},
		
		HandleLocationOutsideRo: function()
		{
			if (document.getElementById("location_outside_ro").style.display == "none")
			{
				document.getElementById("city_id").setAttribute("disabled", "disabled");
				$("div#location_outside_ro").show();
				document.getElementById("location_outside_ro_where").focus();
				$("a#other_location_label").html("selecione uma da lista");
			}
			else
			{
				document.getElementById("city_id").removeAttribute("disabled");
				$('#city_other').attr('value', '');
				$("div#location_outside_ro").hide();
				$("a#other_location_label").html("outro local");
			}
		},
		
		HandlePublisher: function(objValue)
		{
			if (objValue == "persona")
			{
				$("#company_persona").html("Seu nome");
			}
			else
			{
				$("#company_persona").html("Nome da empresa");
			}
		},
		
		CopyPasteUrl: function()
		{
			if (window.location.href.search("#") != -1)
			{
				var url = window.location.href.substring(0, window.location.href.length - 7);
			}
			else
			{
				var url = window.location.href;
			}
			document.getElementById("apply_url").value = url;
		},
		
		SendToFriend: {
			showHide: function()
			{
				$("#send-to-friend").toggle();
			},

			sendMsg: function()
			{
				$("#frm-send-to-friend").ajaxForm(function(responseText) { 
					if (responseText == "0")
					{
						var msg = "Ops, não conseguimos enviar a sua indicação. Você preencheu os dois e-mails?";
						$("#send-to-friend-response").css({ color: "red" });
					}
					else
					{
						var msg = "Sua indicação foi enviada! Vamos torcer pra não ser marcada como spam... :)";
						$("#frm-send-to-friend").clearForm();
						$("#send-to-friend-response").css({ color: "green" });
					}
					$("#send-to-friend-response").html(msg);
				});
			}
		},
		
		ReportSpam: function(url, job_id)
		{
			$.ajax({
			  type: "POST",
			  url: url,
			  data: "job_id=" + job_id,
			  success: function(msg) {
			   	if (msg == "0")
					{
						var status = "Agradecemos a sua intenção, mas já estamos de olho neste job.";
						$("#report-spam-response").css({ color: "red" });
					}
					else
					{
						var status = "Obrigado, ficaremos de olho neste job!";
						$("#frm-send-to-friend").clearForm();
						$("#report-spam-response").css({ color: "green" });
					}
					$("#report-spam-response").html(status);
			  }
			});
		},
		DeactivateLink: function()
		{	
			
			var url = CreativeJobs.cj_admin_url+'deactivate/';
			CreativeJobs.Deactivate(url, CreativeJobs.job_id);
			
		},
		ActivateLink: function()
		{	
			
			var url = CreativeJobs.cj_admin_url+'activate/';
			CreativeJobs.Activate(url, CreativeJobs.job_id, 0);
			
		},
		Activate: function(url, job_id, is_first_page)
		{
			$.ajax({
			  type: "POST",
			  url: url,
			  data: "job_id=" + job_id,
			  success: function(msg) {
			   	if (msg != "0")
					{
						var currentRowId = 'item'+job_id;
						var currentLinkId = 'activateLink'+job_id;
						if(is_first_page == 1)
						{
							$("#"+currentRowId).css({ display: "none" });
						}
						else
						{
							 CreativeJobs.job_id = job_id;
							 document.getElementById(currentLinkId).setAttribute('onclick', CreativeJobs.DeactivateLink);
							 document.getElementById(currentLinkId).onclick = CreativeJobs.DeactivateLink; 
							 document.getElementById(currentLinkId).innerHTML = '<img src="'+CreativeJobs.cj_url+'images/job_deactivate.gif" alt="deactivate" />';
							 document.getElementById(currentLinkId).id = 'deactivateLink'+job_id;
						}	
					}
			  }
			});
		},
		
		Deactivate: function(url, job_id)
		{
			$.ajax({
			  type: "POST",
			  url: url,
			  data: "job_id=" + job_id,
			  success: function(msg) {
			   	if (msg != "0")
					{
						var currentLinkId = 'deactivateLink'+job_id;
						CreativeJobs.job_id = job_id;
						document.getElementById(currentLinkId).setAttribute('onclick', CreativeJobs.ActivateLink);
						document.getElementById(currentLinkId).onclick = CreativeJobs.ActivateLink;
						document.getElementById(currentLinkId).innerHTML = '<img src="'+CreativeJobs.cj_url+'images/job_accept.gif" alt="activate" />';
						document.getElementById(currentLinkId).id = 'activateLink'+job_id;
					}
			  }
			});
		},
		
		Delete: function(url, job_id)
		{
			if(confirm('Are you sure you want to delete this post?'))
			{
				$.ajax({
				  type: "POST",
				  url: url,
				  data: "job_id=" + job_id,
				  success: function(msg) {
				   	if (msg != "0")
						{
							var currentJobId = 'item'+job_id;
							$("#"+currentJobId).css({ display: "none" });
						}
				  }
				});
			}
			else
				return false;
		}
	}
})();

var getCheckedValue = function(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
