﻿(function() 
{
	Jobjob = {
		app_url: "",
		
		/* Function acts as main gateway/router
		*  Expects 2 params:
		*  @action  string  Specifies the action that should be performed
		*  @params  json    Array of parameters
		*/
		Gateway: function(action, params)
		{
			switch(action)
			{
				// launch on page load
				case "init":
					Jobjob.app_url = params.app_url;
					
					$('a[rel*=facebox]').facebox({
						loading_image: '{/literal}{$APP_URL}{literal}img/loading.gif'
					});
					
					if (params.section == "home")
					{
						
						if (document.getElementById("mycarousel"))
						{
							$("#mycarousel").jCarouselLite({
								btnNext: ".next",
								btnPrev: ".prev",
								mouseWheel: true,
								visible: 4,
								speed: 600,
								easing: "backout"
							});	
							
							// show carousel only after load
							document.getElementById("mycarousel").style.visibility = "visible";
							document.getElementById("car_prev").style.visibility = "visible";
							document.getElementById("car_next").style.visibility = "visible";
						}

						// add event handlers (onclick) for job categories and cities toggle
						$("#toggle-categs").bind("click", function(e) {
							var elem = "#categorii";
							if ($("#orase").css("display") == "block")
							{
								$("#orase").hide();
								$("#ors").removeClass("adroph");
							}
							if ($(elem).css("display") == "none")
							{
								$("#cat").addClass("adroph");
								$(elem).show();
							}
							else
							{
								$("#cat").removeClass("adroph");
								$(elem).hide();
							}
						});
						$("#toggle-cities").bind("click", function(e) {
							var elem = "#orase";
							if ($("#categorii").css("display") == "block")
							{
								$("#categorii").hide();
								$("#cat").removeClass("adroph");
							}
							if ($(elem).css("display") == "none")
							{
								$("#ors").addClass("adroph");
								$(elem).show();
							}
							else
							{
								$("#ors").removeClass("adroph");
								$(elem).hide();
							}
						});	
					}
					
					else if (params.section == "job")
					{
						$("a#apply_link").click(function() {
							$.ajax({
							  type: "POST",
							  url: Jobjob.app_url + 'page.proxy.inc.php',
							  data: "job_id=" + $("#job_id").html() + "&ajax=1"
							});
							var redirect_url = this.href;
							setTimeout(function() { window.location = redirect_url; }, 800);
							return false;
						});
					}
					
					else if (params.section == "publish")
					{
						$('#title').focus();
					
						$("#publish_form").validate({
							rules: {
								title: { required: true },
								description: { required: true },
								jobtypes: { required: true,minLength: 1 }
							},
							messages: {
								title: ' <img src="' + Jobjob.app_url + 'img/icon-delete.png" alt="" />',
								description: ' <img src="' + Jobjob.app_url + 'img/icon-delete.png" alt="" />',
								jobtypes: ' <img src="' + Jobjob.app_url + 'img/icon-delete.png" alt="" />'
							}
						});	
					}
					
					else if (params.section == "list")
					{
						// postings listing hover
						$(".job-posts tr").mouseover(function() {
							$(this).toggleClass("listing-hover");
						}).mouseout(function() {
							$(this).toggleClass("listing-hover");
						});

						// subscribe via email (show/hide)
						$(".anchor-email-subscription").bind("click", function(e) {
							$("#form-container-email-subscription").toggle();
							$("#abonare").toggleClass("abonare-active");
							$("#subscribe_email").focus();
						});
						
						// ajaxify subscribe-via-email form
						$("#form-email-sub").validate({
							rules: {
								subscribe_email: { required: true, email: true }
							},
							messages: {
								subscribe_email: '<div class="form-validation-error">' + fes_error_email_msg + '</div>'
							},
							submitHandler: function() {
								$("#form-email-sub").ajaxSubmit(function(responseText) { 
									if (responseText == "notok")
									{
										var msg = fes_error_try_again;
										$("#email-sub-resp").css({ color: "red" });
									}
									else if (responseText == "already-subscribed")
									{
										var msg = fes_error_already_subscribed;
										$("#email-sub-resp").css({ color: "red" });
									}
									else
									{
										var msg = fes_error_ok_subscribed;
										$("#form-email-sub").clearForm();
										$("#email-sub-resp").css({ color: "green" });
									}
									$("#email-sub-resp").html(msg);
								});
							}
						});

						// show/hide filters column
						$(".hideFltr").bind("click", function(e) {
							$("#leftCln").toggle();
							$("#middleCln").toggleClass("expanded");
							$("#customize").toggleClass("visible");
							$("#page").toggleClass("noBG");
						});
						$(".showFltr").bind("click", function(e) {
							$("#leftCln").toggle();
							$("#middleCln").toggleClass("expanded");
							$("#customize").toggleClass("visible");
							$("#page").toggleClass("noBG");
						});

						// toggle independent filters
						$("#toggle-filter-types").bind("click", function(e) {
							$("#filter_types").toggle();
						});
						$("#toggle-filter-titles").bind("click", function(e) {
							$("#filter_titles").toggle();
						});
						$("#toggle-filter-similartitles").bind("click", function(e) {
							$("#filter_similartitles").toggle();
						});
						$("#toggle-filter-days").bind("click", function(e) {
							$("#filter_days").toggle();
						});
						$("#toggle-filter-provider").bind("click", function(e) {
							$("#filter_provider").toggle();
						});
						if ($("#toggle-filter-location"))
						{
							$("#toggle-filter-location").bind("click", function(e) {
								$("#filter_location").toggle();
							});	
						}
						
						if (!document.getElementById("job-posts"))
						{
							$(".hideFltr").trigger("click");
						}	
					}
					
					if (document.getElementById("press_form"))
					{
						document.getElementById("press_email").focus();
						
						$("#press_form").validate({
							rules: {
								press_email: { required: true, email: true }
							},
							messages: {
								press_email: '<div class="form-validation-error">' + fes_error_email_msg + '</div>'
							},
							submitHandler: function() {
								$("#press_form").ajaxSubmit(function(responseText) { 
									if (responseText == "notok")
									{
										var msg = fes_error_try_again;
										$("#send-press-resp").css({ color: "red" });
									}
									else if (responseText == "ok")
									{
										var msg = press_ok;
										$("#press_form").clearForm();
										$("#send-press-resp").css({ color: "green" });
									}
									else
									{
										var msg = responseText;
										$("#send-press-resp").css({ color: "red" });
									}
									$("#send-press-resp").html(msg);
								});
							}
						});
					}
						
					
					// location checkboxes filter
					$("input.location-checkboxes").bind("click", function(e) {
						var url = document.getElementById("current_url").innerHTML;
						
						if (url.match(/l-((.)*?)\//))
						{
							var existing_locations = url.match(/l-((.)*?)\//);
							var no_location_url = url.replace(/l-((.)*?)\//g, "");
						}
						else
						{
							var existing_locations = "";
							var no_location_url = url;
						}
						
						// add location
						if (this.checked)
						{
							var new_location_params = (existing_locations != "") ? (existing_locations[1] + ", " + this.value) : (this.value);
						}
						// remove location
						else
						{
							var tmp = existing_locations[1] + ", ";
							var myRegExp = new RegExp(this.value + ", ","i");
							var remove_selected_loc = tmp.replace(myRegExp, "");
							var new_location_params = remove_selected_loc.substring(0, remove_selected_loc.length - 2);
						}

						if (new_location_params == "" || this.id=="city_all")
						{
							window.location.href = no_location_url;	
							$("#city_all").attr("checked","checked");
						}
						else
						{
							window.location.href = no_location_url + "l-" + new_location_params + "/";
						}
					});
					
					
					// contact form, if we're on the contact page
					if (document.getElementById("form-contact"))
					{
						$("#form-contact").validate({
							rules: {
								contact_name: { required: true },
								contact_email: { required: true, email: true },
								contact_msg: { required: true }
							},
							messages: {
								contact_name: '<div class="form-validation-error">' + cf_name + '</div>',
								contact_email: '<div class="form-validation-error">' + cf_error_email_msg + '</div>',
								contact_msg: '<div class="form-validation-error">' + cf_error_msg + '</div>'
							}
						});
					}
					break;
					
					
				case "refresh_jobs_count":
					
					//$.cookie('jobjob_jobs_count', 'the_value', { expires: 30 });
					
					var current_count = parseInt($("#jobs_count").html());
					var count_real = parseInt($("#jobs_count_real").html());
					
					current_count += 5;
					if (current_count < count_real)
					{
						$("#jobs_count").html(current_count);
						update(current_count.toString(), 1000);
						setTimeout("Jobjob.Gateway('refresh_jobs_count', {});", 1500);	
					}
					break;
				
				case "fake_job":
					if (!document.getElementById("fakejob_" + params.job_id))
					{
						var fakejob_form = '<tr id="fakejob_' + params.job_id + '" class="stf">';
						fakejob_form += '<td>&nbsp;</td><td colspan="4">';
						fakejob_form += '<div style="position: relative;"><a href="javascript: void(0);" class="fakejob-close" onclick="$(\'#fakejob_' + params.job_id + '\').remove();"></a></div>';
						fakejob_form += '<table class="stf-table" cellpadding="2">';
						fakejob_form += '<tr><td colspan="2"><strong>' + stf_recommend + '</strong></td></tr>';
						fakejob_form += '<tr><td><label for="friend_email_' + params.job_id + '">' + stf_his_address + '</label></td>';
						fakejob_form += '<td><input type="text" name="friend_email" id="friend_email_' + params.job_id + '" size="30" /></td></tr>';
						fakejob_form += '<tr><td><label for="your_email_' + params.job_id + '">' + stf_your_address + '</label></td>';
						fakejob_form += '<td><input type="text" name="your_email" id="your_email_' + params.job_id + '" size="30" /><input type="hidden" name="job_url" value="' + params.job_url + '" /></td></tr>';
						fakejob_form += '<tr><td colspan="2"><input type="submit" name="submit" value="' + stf_send_recom + '" /></td></tr>';
						fakejob_form += '<tr><td colspan="2"><div id="email-friend-resp-' + params.job_id + '"></div></td></tr>';
						fakejob_form += '</table><input type="hidden" name="the_url" value="' + Jobjob.app_url + Jobjob._u.list.url + '/' + $("#search_string").val() + 'r-' + params.job_id + '/" /></td></tr>';
						$("#posting_row_" + params.job_id).after(fakejob_form);

						//$.post(JobjobAdmin.app_root + "admin/tag_actions");
						
					}
					else
					{
						$("#fakejob_" + params.job_id).remove();
					}
				break;	
					
				case "send_to_friend":
					if (!document.getElementById("stf_" + params.job_id))
					{
						var stf_form = '<tr id="stf_' + params.job_id + '" class="stf">';
						stf_form += '<td>&nbsp;</td><td colspan="4">';
						stf_form += '<div style="position: relative;"><a href="javascript: void(0);" class="stf-close" onclick="$(\'#stf_' + params.job_id + '\').remove();"></a></div>';
						stf_form += '<form id="stf-form-' + params.job_id + '" method="post" action="' + Jobjob.app_url + 'email-friend/">';
						stf_form += '<table class="stf-table" cellpadding="2">';
						stf_form += '<tr><td colspan="2"><strong>' + stf_recommend + '</strong></td></tr>';
						stf_form += '<tr><td><label for="friend_email_' + params.job_id + '">' + stf_his_address + '</label></td>';
						stf_form += '<td><input type="text" name="friend_email" id="friend_email_' + params.job_id + '" size="30" /></td></tr>';
						stf_form += '<tr><td><label for="your_email_' + params.job_id + '">' + stf_your_address + '</label></td>';
						stf_form += '<td><input type="text" name="your_email" id="your_email_' + params.job_id + '" size="30" /><input type="hidden" name="job_url" value="' + params.job_url + '" /></td></tr>';
						stf_form += '<tr><td colspan="2"><input type="submit" name="submit" value="' + stf_send_recom + '" /></td></tr>';
						stf_form += '<tr><td colspan="2"><div id="email-friend-resp-' + params.job_id + '"></div></td></tr>';
						stf_form += '</table><input type="hidden" name="the_url" value="' + Jobjob.app_url + Jobjob._u.list.url + '/' + $("#search_string").val() + 'r-' + params.job_id + '/" /></form></td></tr>';
						$("#posting_row_" + params.job_id).after(stf_form);
						$("#friend_email_" + params.job_id).focus();

						$("#stf-form-" + params.job_id).validate({ 
							rules: {
								friend_email: { required: true, email: true },
								your_email: { required: true, email: true }
							},
							messages: {
								friend_email: "<div class='form-validation-error'>" + stf_error_email_msg + "</div>",
								your_email: "<div class='form-validation-error'>" + stf_error_email_msg + "</div>"
							},
							submitHandler: function() {
								$("#stf-form-" + params.job_id).ajaxSubmit(function(responseText) { 
									if (responseText == "notok")
									{
										var msg = stf_error_try_again;
										$("#email-friend-resp-" + params.job_id).css({ color: "red" });
									}
									else
									{
										var msg = stf_error_send_ok;
										$("#stf-form-" + params.job_id).clearForm();
										$("#email-friend-resp-" + params.job_id).css({ color: "green" });
									}
									$("#email-friend-resp-" + params.job_id).html(msg);
								});
							}
						});
					}
					else
					{
						$("#stf_" + params.job_id).remove();
					}
					break;

				case "report_job":
					if (!document.getElementById("rep_" + params.job_id))
					{
						var stf_form = '<tr id="rep_' + params.job_id + '" class="stf">';
						stf_form += '<td>&nbsp;</td><td colspan="4">';
						stf_form += '<div style="position: relative;"><a href="javascript: void(0);" class="stf-close" onclick="$(\'#rep_' + params.job_id + '\').remove();"></a></div>';
						stf_form += '<form id="rep-form-' + params.job_id + '" method="post" action="' + Jobjob.app_url + 'report-job/">';
						stf_form += '<p><strong>' + rep_title + '</strong></p>';
						stf_form += '<div class="choices" style="padding-top: 10px;"><input type="submit" name="submit" value="' + rep_but_yes + '" />';
						stf_form += ' <span class="conj">' + rep_but_or + '</span> <a class="reddish" href="javascript: void(0);" onclick="$(\'#rep_' + params.job_id + '\').remove();">' + rep_but_no + '</a></div>';
						stf_form += '<div class="clear" id="rep-resp-' + params.job_id + '"></div>';
						stf_form += '<input type="hidden" name="job_id" value="' + params.job_id + '" />';
						stf_form += '</form>';
						$("#posting_row_" + params.job_id).after(stf_form);

						$("#rep-form-" + params.job_id).validate({ 
							
							submitHandler: function() {
								$("#rep-form-" + params.job_id).ajaxSubmit(function(responseText) { 
									if (responseText == "notok")
									{
										var msg = rep_error_try_again;
										$("#rep-resp-" + params.job_id).css({ color: "red" });
									}
									else if(responseText == "already reported")
									{
										var msg = rep_error_already_reported;
										$("#rep-resp-" + params.job_id).css({ color: "red" });
									}
									else
									{
										var msg = rep_send_ok;
										$("#rep-form-" + params.job_id).clearForm();
										$("#rep-resp-" + params.job_id).css({ color: "green" });
									}
									$("#rep-form-" + params.job_id).find("div.choices").hide();
									$("#rep-resp-" + params.job_id).html(msg);
								});
							}
						});
					}
					else
					{
						$("#rep_" + params.job_id).remove();
					}
					break;
					
				// map php strings localization to JS
				case "localize_strings":
					Jobjob._s = params.s;
					break;

				// map php urls localization to JS
				case "localize_urls":
					Jobjob._u = params.u;
					break;

				default: 
					break;
			}
		}
	}
})();