//var path = '/02_proiecte_in_lucru/rbcatalog.ro/site/web/frontend_dev.php';

$(document).ready(function() {
	
	// external links
	$("a[rel='external']").click(function() {
		return !window.open($(this).attr("href"));
	});
	
	// non-clickable links
	$(".return-false").click( function() {
		return false;
	});

	$("#locality").autocomplete(path + '/ajax/autocomplete-locality', {
		multiple: false,
		minChars: 0,
		mustMatch: false,
		scroll: true,
		selectFirst: true,
		cacheLength:0,
		extraParams: {
       		county_id: function() { return $("#county_id").val(); }
		},
		parse: function(data) {
			return $.map(eval(data), function(row) {
				return {
					data: row,
					value: row,
					result: row
				}
			});
		},
		formatItem: function(item) {
			return (item);
		}
	}).result(function(event, data, formatted) {
		if (data) {
			$('#locality').val(data);
		}
	});
	
	
	$('.hide-with-js').hide(); // use this class to hide the message "please open this link in new window"
	
	  $("#login_link").click(function () { 
	      $("#login").hide();
	      $("#login_form").show();
	      return false;
	    });
	
	// GRAY HOVER ON ITEM WRAPPERS
	$(".hover-wrapper").hover(
		function () {
			$(this).css("background-color","#f1f1f1");
		},
		function () {
			$(this).css("background-color","transparent");
		}
	);
	
	// LINK TOGGLE
	$(".link-toggle").click(function(){
		
		// init Vars
		var classOpened = "opened";
		var classClosed = "closed";
		var opened = $(this).hasClass(classOpened);
		var textOld = $(this).text();
		var textNew = $(this).attr("rel");
		
		// switch Text
		//$(this).text(textNew);
		//$(this).attr("rel", textOld);
		$(".link-toggle").text(textNew);
		$(".link-toggle").attr("rel", textOld);
		
		// switch Class
		if (opened) {
			//$(this).removeClass(classOpened);
			//$(this).addClass(classClosed);
			$(".link-toggle").removeClass(classOpened);
			$(".link-toggle").addClass(classClosed);
		} else {
			//$(this).removeClass(classClosed);
			//$(this).addClass(classOpened);
			$(".link-toggle").removeClass(classClosed);
			$(".link-toggle").addClass(classOpened);
		}
		
		return false;
	});
	
	// ADVANCED SEARCH
	var advSearchOpen = false;
	$("#advanced-search-inner .link-toggle").click(function(){
		if (advSearchOpen) {
			// close panel
			$("#advanced-search-inner").animate({ 
				top: "32px"
			}, 300);
			// hide form (because of <select> appears on top)
			$("#advanced-search-inner form").fadeOut(200); 
			
			advSearchOpen = false;
		} else {
			// open panel
			$("#advanced-search-inner").animate({ 
				top: "165px"
			}, 300);
			// show form
			setTimeout(function(){
				$("#advanced-search-inner form").css("visibility","visible"); 
				$("#advanced-search-inner form").fadeIn(150); 
			}, 150);
			advSearchOpen = true;
		}
		return false;
	});
	
	// IMAGES NAVIGATION
	$("#business-premium-listing .numbers li a").click(function(){
		
		$(this).parent().parent().find("a").removeClass("active");
		var elem = $(this).attr("class");
		$(this).addClass("active");
		
		$(this).parent().parent().parent().parent().find("ul.image li").removeClass("active");
		$("#"+elem).addClass("active");
		return false;
	});
	$("#company-gallery .numbers li a").click(function(){
		
		$(this).parent().parent().find("a").removeClass("active");
		var elem = $(this).attr("class");
		$(this).addClass("active");
		
		$(this).parent().parent().parent().parent().parent().parent().find("ul.image li").removeClass("active");
		$("#"+elem).addClass("active");
		return false;
	});
	// VIDEO
	$("#view-video").click(function(){
		
		$(".numbers a").removeClass("active");
		
		$(".image li").removeClass("active");
		$("#video-content").addClass("active");
		return false;
	});
	
	$("#advanced-search input, #form-search input, #login_form input, #form-newsletter-footer input").each(function(i){
		var inputValue = $(this).val();
		
		$(this).focus(function(){ 
			if ($(this).val()==inputValue) {
				$(this).val("");
			}
		});
		$(this).blur(function(){ 
			if ($(this).val()=="") {
				$(this).val(inputValue);
			}
		});
	});
	
	//PREVIEW
	$("#business_name").keyup(function(e){
        $("#preview_business_name").html($("#business_name").val());
    });
	
	$("#business_cod_fiscal").keyup(function(e){
        $("#preview_cod_fiscal").html($("#business_cod_fiscal").val());
    });
	$("#business_nr_inregistrare").keyup(function(e){
        $("#preview_nr_inregistrare").html($("#business_nr_inregistrare").val());
    });
	
	$("#business_locality").keyup(function(e){
        $("#preview_locality").html($("#business_locality").val());
    });
	$("#business_zip").keyup(function(e){
        $("#preview_zip").html($("#business_zip").val());
    });
	
	$("#business_street").keyup(function(e){
        $("#preview_street").html($("#business_street").val());
    });
	$("#business_street_no").keyup(function(e){
        $("#preview_street_no").html($("#business_street_no").val());
    });
	$("#business_bloc").keyup(function(e){
        $("#preview_bloc").html($("#business_bloc").val());
    });
	$("#business_scara").keyup(function(e){
        $("#preview_scara").html($("#business_scara").val());
    });
	$("#business_etaj").keyup(function(e){
        $("#preview_etaj").html($("#business_etaj").val());
    });
	$("#business_apartament").keyup(function(e){
        $("#preview_apartament").html($("#business_apartament").val());
    });
	
	$("#business_email").keyup(function(e){
        $("#preview_email").html($("#business_email").val());
    });
	$("#business_web").keyup(function(e){
        $("#preview_web").html($("#business_web").val());
    });
	$("#business_phone").keyup(function(e){
        $("#preview_phone").html($("#business_phone").val());
    });
	$("#business_fax").keyup(function(e){
        $("#preview_fax").html($("#business_fax").val());
    });
	$("#business_mobile").keyup(function(e){
        $("#preview_mobile").html($("#business_mobile").val());
    });
	
	$("#business_locality").autocomplete(path + '/ajax/autocomplete-locality', {
		multiple: false,
		minChars: 0,
		mustMatch: false,
		scroll: true,
		selectFirst: true,
		cacheLength:0,
		extraParams: {
       		county_id: function() { return $("#business_county_id").val(); }
		},
		parse: function(data) {
			return $.map(eval(data), function(row) {
				return {
					data: row,
					value: row,
					result: row
				}
			});
		},
		formatItem: function(item) {
			return (item);
		}
	}).result(function(event, data, formatted) {
		if (data) {
			$('#business_locality').val(data);
			$("#preview_locality").html(data);
		}
	});
	
	//POPULATE SUBCATEGORY
	$("select#business_category_id").change(function(){
		//alert($(this).val());
	    $.getJSON(path + "/ajax/populate-subcategory",{id: $(this).val()}, function(j){
	      var options = '<option value="">Selectati subdomeniul</option>';
	      for (var i = 0; i < j.length; i++) {
	        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
	      }
	      $("select#business_subcategory_id").html(options);
	    });
		
		 if ($(this).val()) {
		 	$("#preview_category").html($('#business_category_id :selected').text());
		 	$("#preview_subcategory").html("");
		 }
		 else {
		 	$("#preview_category").html("");
		 	$("#preview_subcategory").html("");
		 }
  	});
	
	$("select#business_subcategory_id").change(function(){
		 if ($(this).val()) {
		 	$("#preview_subcategory").html($('#business_subcategory_id :selected').text());
		 }
		 else $("#preview_subcategory").html("");
	
  	});
	
	$("select#business_county_id").change(function(){
		 if ($(this).val()) {
		 	$("#preview_county").html($('#business_county_id :selected').text());
		 }
		 else $("#preview_county").html("");
  	});
	
});

function save_favorita(id){
	$('#tooltip').remove();
	$('#popup-html').remove();
	$.ajax({
	    type: "POST",
	    url: path + "/ajax/save-favorita",
	    data: {
	        id: id
	    },
		dataType: 'json',
	    success: function(data){
	        if (data.success) {
	            $('#favorita_' + id).removeClass('favorita-false');
	            $('#favorita_' + id).addClass('favorita-true');
	            $('#favorita_' + id).html('<span>Sterge din favorite</span>');
	            $('#favorita_' + id).attr('title', 'Sterge din favorite');
	        }
	        else {
				$('#favorita_' + id).removeClass('favorita-true');
	            $('#favorita_' + id).addClass('favorita-false');
	            $('#favorita_' + id).html('<span>Salveaza ca favorita</span>');
	            $('#favorita_' + id).attr('title', 'Salveaza ca favorita');
	        }
	    }
	});
}