$(document).ready( function(){
	$(".input-wrapper input, .input-wrapper textarea, .input-wrapper select").focus( function(){
		$(".explanation").each( function(){
			if(!$(this).hasClass("visible")){
				$(this).hide();
			}
		});
		var explanation = $(this).siblings(".explanation");
		if (!explanation.hasClass("visible")) {
			explanation.show();
		}
	});
	
	$(".input-wrapper select").change( function(){
		var inputValue = this.value;
		var showClass = $(this).find("option[value="+inputValue+"]").attr("class");
		$(this).find("option").each( function(){
			var hideClass = $(this).attr("class");
			if(hideClass && hideClass != showClass)
				$("div."+hideClass).fadeOut("fast");
		});
		
		if(showClass){
			$(this).parent().siblings("div."+showClass).fadeIn("slow");
		}
	});
	
	$(".input-wrapper input:checkbox").change( function(){
		$(this).removeClass("multiSelection");
		var showClass = $(this).attr("class");
		$(this).addClass("multiSelection");
		if (showClass) {
			if ($(this).is(':checked')) {
				$(this).parent().siblings("."+showClass).fadeIn("slow");
			}
			else{
				$(this).parent().siblings("."+showClass).fadeOut("slow");
			}
		}
	});
	$(".input-wrapper input:radio").change( function(){
		$(this).removeClass("radioGroup");
		var showClass = $(this).attr("class");
		$(this).addClass("radioGroup");
		if (showClass) {
		$(this).siblings(".hidden-inputs").fadeOut("slow");
			if ($(this).is(':checked')) {
				$(this).siblings("."+showClass).fadeIn("slow");
			}
		}
	});
	
	
	$(".input-wrapper select").each( function(){
		if (this.value) {
			var showClass = $(this).find("option:selected").attr("class");
			if(showClass){
				$(this).parent().siblings("div."+showClass).fadeIn("fasts");
			}
		}
	});
	
	$(".input-wrapper input:checkbox").each( function(){
		if(this.checked){
			var showClass = $(this).attr("class");
			$(this).parent().siblings("."+showClass).fadeIn("fast");
		}
	});
	
	$(".input-wrapper input:radio").each( function(){
		if(this.checked){
			$(this).removeClass("radioGroup");
			var showClass = $(this).attr("class");
			$(this).addClass("radioGroup");
			$(this).siblings("."+showClass).fadeIn("fast");
		}
	});
	
	$(".saveJobButton").click(function() {
		var jobID = $("#jobID").attr("value");
		var jobURL = $("#jobURL").attr("value");
		 $.ajax({
			   type: "GET",
			   url: "/salveaza-job/?job_id="+jobID+"&RETURN_TO="+jobURL,
			   success: function(data){
		 		if (data)
			 		{
			 			dataArray = eval('(' + data + ')');
			 			alert(dataArray["message"]);
			 			if (dataArray["url"] != undefined)
			 			{
			 				window.location = dataArray["url"];
			 			}
			 		}
			   }
			 });
		return false;
	});
	
	$(".sendToFriendButton").click(function() {
		$("#sendToFriend").fadeIn("slow");
		return false;
		
	});
	
	$(".ApplyToJobBtn").click(function() {
		$("#applyToJob").fadeIn("slow");
		return false;
	});	
	
	$("#apply_cv").submit(function() {
		var jobID = $("#jobID").attr("value");
		var cvID = $("#fldcv").attr("value");
		$("#cv-loader").show("fast", function () {
		 $.ajax({
			   type: "GET",
			   url: "/aplica-job/?job_id="+jobID+"&cv_id="+cvID,
			   success: function(data){
		 		if (data)
			 		{
			 			dataArray = eval('(' + data + ')');
			 			
			 			alert(dataArray["message"]);
			 			if (dataArray["url"] != undefined)
			 			{
			 				window.location = dataArray["url"];
			 			}
			 			$("#cv-loader").hide();
			 			$("#applyToJob").fadeOut("slow");
			 		}
			   }
			 });
		});
		 $("#cv-loader").hide();
		return false;
	});
	
	if ($("#cvContactUser").css("display") == "block")
	{
		var top = $('#cvContactUser').offset().top;
		$('html,body').animate({scrollTop:top},1000);
	}
	
	$(".CvContactBtn").click(function() {
		$("#cvContactUser").fadeIn("slow");		
		return false;
	});
	
	$("#companyCategories").click(function() {
		$("#categorii_firme").show();
		$("#companyCategories").addClass("selectedTab");

		$("#categorii_produse_noi, #categorii_produse_second, #categorii_joburi").hide();
		$("#jobCategories, #productCategoriesSecond, #productCategoriesNew").removeClass("selectedTab");
		return false;
	});

	$("#productCategoriesNew").click(function() {
		$("#categorii_produse_noi").show();
		$("#productCategoriesNew").addClass("selectedTab");
		
		$("#categorii_joburi, #categorii_firme, #categorii_produse_second").hide();
		$("#jobCategories, #companyCategories, #productCategoriesSecond").removeClass("selectedTab");
		return false;
	});

	$("#productCategoriesSecond").click(function() {
		$("#categorii_produse_second").show();
		$("#productCategoriesSecond").addClass("selectedTab");
		
		$("#categorii_joburi, #categorii_firme, #categorii_produse_noi").hide();
		$("#jobCategories, #companyCategories, #productCategoriesNew").removeClass("selectedTab");
		return false;
	});

	$("#jobCategories").click(function() {
		$("#categorii_joburi").show();
		$("#jobCategories").addClass("selectedTab");
		
		$("#categorii_produse_noi, #categorii_produse_second, #categorii_firme").hide();
		$("#productCategoriesNew, #companyCategories, #productCategoriesSecond").removeClass("selectedTab");
		return false;
	});
	
	$("#newProducts").click(function() {
		$("#produse_second").hide();
		$("#produse_noi").show();
		$("#oldProducts").removeClass("selectedTab");
		$("#newProducts").addClass("selectedTab");
		return false;
	});

	$("#oldProducts").click(function() {
		$("#produse_second").show();
		$("#produse_noi").hide();
		$("#oldProducts").addClass("selectedTab");
		$("#newProducts").removeClass("selectedTab");
		return false;
	});
	$("#latestnewProducts").click(function() {
		$("#ultimele_produse_second").hide();
		$("#ultimele_produse_noi").show();
		$("#latestoldProducts").removeClass("selectedTab");
		$("#latestnewProducts").addClass("selectedTab");
		return false;
	});

	$("#latestoldProducts").click(function() {
		$("#ultimele_produse_second").show();
		$("#ultimele_produse_noi").hide();
		$("#latestoldProducts").addClass("selectedTab");
		$("#latestnewProducts").removeClass("selectedTab");
		return false;
	});
});

