$(document).ready(function(){
	
	var base_url = $('input[name=base_url]').val();
	
	
	// Get cities from country_code
	$('select#countries').change(function(){
		
		var country_code = $(this).val();
		
		if (country_code != null) {
			$.ajax({
				type: "POST",
			  	url: base_url + "ajax/get_cities_from_country_code",
			  	data: "country_code=" + country_code,
				success: function(response) {
					$('#cities').html(response);
				}
			})
		}
	});
	
	
	// Get filtered job offers from search
	$("#search_jobs").submit(function(){
		
		var keywords = $('#keywords').val();
		var country_code = $('#countries').val();
		var city_id = $('#cities').val();
		var post = $('#posts').val();
		
		$.ajax({
			type: "POST",
			url: base_url + 'ajax/search_jobs',
			data: {
				"keywords": keywords, 
				"country_code": country_code,
				"city_id": city_id,
				"post": post },
			beforeSend: function() {
				$("#results").fadeOut("fast");
			},
			success: function(response) {
				$("#results").html(response).fadeIn();
			}
		})
		return false;
		
	});
	
	
	// Get filtered services offers from search
	$("#search_services").submit(function(){
		
		var keywords = $('#keywords').val();
		var country_code = $('#countries').val();
		var city_id = $('#cities').val();
		
		$.ajax({
			type: "POST",
			url: base_url + 'ajax/search_services',
			data: {
				"keywords": keywords, 
				"country_code": country_code,
				"city_id": city_id },
			beforeSend: function() {
				$("#results").fadeOut("fast");
			},
			success: function(response) {
				$("#results").html(response).fadeIn();
			}
		})
		return false;
		
	});
	
	
	// Get filtered castings offers from search
	$("#search_castings").submit(function(){
		
		var keywords = $('#keywords').val();
		var country_code = $('#countries').val();
		var city_id = $('#cities').val();
		var type = $('#types').val();
		
		$.ajax({
			type: "POST",
			url: base_url + 'ajax/search_castings',
			data: {
				"keywords": keywords, 
				"country_code": country_code,
				"city_id": city_id,
				"type": type },
			beforeSend: function() {
				$("#results").fadeOut("fast");
			},
			success: function(response) {
				$("#results").html(response).fadeIn();
			}
		})
		return false;
		
	});
	
	
	// Job alerts subscription
	$('#alerts').submit(function(){
		
		var email = $('#email').val();
		var cat = $('#cats').val();
		var country_code = $('#countries').val();
		
		$.ajax({
			type: "POST",
			url: base_url + 'ajax/alert',
			data: {
				"email" : email,
				"cat": cat,
				"country_code": country_code
			},
			success: function(response){
				if (response == "ok") {
					$('#alerts fieldset').html('<div class="success"><p>You now will be alerted once a week with jobs matching your criterias.</p></div>').fadeIn("slow");	
				} else {
					$('#alerts fieldset').before('<div class="errors"><p>'+ response +'</p></div>');
					setTimeout("errors()", 2000);
				}
			}
		})
		
		return false;	
	});
	
	
	// Delete Company Logo
	$('.delete-logo').click(function(){
		
		var company_id = $(this).attr("id").substr(11);
		$.ajax({
			type: "POST",
			url: base_url + 'ajax/logo',
			data: { "company_id": company_id },
			success: function(){
				$('#logo').slideUp("normal", function(){
					$('#logo').after('<input type="file" name="logo" id="company_logo" />');
					$('#company_logo').fadeIn("fast");
					$('#logo').remove();
				});
			}
		})
		
		return false;
	});

	
	if ($('.success').length > 0) {
		setTimeout("success()", 2500);
	}
})


function success()
{
	$('.success').slideUp(function(){$(this).remove();})
}

function errors()
{
	$('.errors').slideUp(function(){$(this).remove();})
}


//---------------------------------//
//----------Advertise Form---------//
//---------------------------------//
$(document).ready(function() {
	
	$("div#ad_image").hide();
	$("div#ad_code").hide();
	
	$("#ad_type").change(function(){
		var value = $(this).val();
		
		$("div#ad_image").slideUp();
		$("div#ad_code").slideUp();
		
		$("#ad_"+ value).slideDown();
	});
		
	//Home Page Order desabelity
	$("select.image_page").change(function(){
		var value = $(this).val();
		if(value == "home")
		{
			$("select.image_position").attr("disabled","disabled");
			$("select#image_order").attr("disabled","disabled");
		}
		else
		{
			$("select.image_position").attr("disabled","");
		}
	});
	
	//Home Page Order desabelity
	$("select.image_position").change(function(){
		var value = $(this).val();
		if(value == "top" || value == "bottom")
		{
			$("select#image_order").attr("disabled","disabled");
		}
		else
		{
			$("select#image_order").attr("disabled","");
		}
	});
	
	//Home Page Order desabelity
	$("select.code_page").change(function(){
		var value = $(this).val();
		if(value == "home")
		{
			$("select.code_position").attr("disabled","disabled");
			$("select#code_order").attr("disabled","disabled");
		}
		else
		{
			$("select.code_position").attr("disabled","");
		}
	});
	
	//Home Page Order desabelity
	$("select.code_position").change(function(){
		var value = $(this).val();
		if(value == "top" || value == "bottom")
		{
			$("select#code_order").attr("disabled","disabled");
		}
		else
		{
			$("select#code_order").attr("disabled","");
		}
	});
	
});
