/*$.fn.egrep = function(pat) {
 var out = [];
 var textNodes = function(n) {
  if (n.nodeType == Node.TEXT_NODE) {
   var t = typeof pat == 'string' ?
    n.nodeValue.indexOf(pat) != -1 :
    pat.test(n.nodeValue);
   if (t) {
    out.push(n.parentNode);
   }
  }
  else {
   $.each(n.childNodes, function(a, b) {
    textNodes(b);
   });
  }
 };
 this.each(function() {
  textNodes(this);
 });
 return out;
};

$.fn.MakeItalic = function() {
	var regexp = /Weg /g;
	this.each(function() {
	 	$(this).html(
	 		$(this).html().replace(regexp,"<i>Weg</i> ")
		);
 	});
 return $(this);
}	

$(document).ready(function(){
	var n = $('body').egrep(/Weg /g);
	for (var i = 0; i < n.length; ++i) {
		$(n[i]).MakeItalic();
	}
});
*/


$(document).ready(function(){	
	$("#favourites").click(function() {
		$(".bere").css("visibility","hidden");
	});

	$("#meldaandrop").livequery('click',function() {
		$("#meld-aan").css("visibility","visible");
	});
	$("#close").livequery('click',function() {
		$("#meld-aan").css("visibility","hidden");
	});

	$("#registreerdrop").hover(function() {
		$("#register").css("visibility","visible");
		$("#meld-aan").css("visibility","hidden");
	},
	function() {
		$("#register").css("visibility","hidden");
	});

	$("#manufacturer").change(function(){
                $.getJSON('/categories/listCategory/' + $(this).val(), function(data) {
                        $('#vehiclemodel').empty();
                        $('#vehiclemodel').append("<option value=\"\">Model</option>");
                        $.each(data, function(optionIndex, option) {
                                var html = "<option value=\"" + option['id'] + "\">" + option['name'] + "</option>";
                                $('#vehiclemodel').append(html);
                        })      
                })
        });		
        
		$("#country_id").change(function(){
                var value = $(this).val();
                if(value != '183'){
                         $("#region_id").hide();
                         $('#town_id').hide();
                }else{
                        $("#region_id").show();
                        $('#town_id').show();
                }
        });

	$("#region_id").change(function(){
        	$.getJSON('/towns/listTown/' + $(this).val(), function(data) {
			$('#town_id').empty();
			$('#town_id').append("<option value=\"\">Town</option>");
            		$.each(data, function(optionIndex, option) {
                		var html = "<option value=\"" + option['id'] + "\">" + option['name'] + "</option>";
                		$('#town_id').append(html);
            		})
        	}) 
    	});

});

$(document).ready(function(){
	$(".expand").hide();
	$(".expandable").click(function(){
		$(this).next(".expand").slideToggle(300);
		if($(this).attr("class") == "on"){
			$(this).attr("class", 'off');
		}else{
			$(this).attr("class", 'on');
		}
	});
});

$(document).ready(function(){
	//$('#keyword_value')
	$("#keyword").click(function(){
		if($(this).attr('value') == $('#keyword_value').attr('value')){
			$(this).attr('value','');
		}
	});
});

$(document).ready(function(){
	//$('#keyword_value')
	$("#keyword").click(function(){
		if($(this).attr('value') == $('#keyword_value').attr('value')){
			$(this).attr('value','');
		}
	});
});

$(document).ready(function(){
	var keyword;
	keyword = $("#keyword");
	keyword.click(function(){
		if($(this).attr('value') == $(this).attr('default')){
			$(this).attr('value','');
		}
	});
	keyword.blur(function(){
		if($(this).attr('value') == ''){
			$(this).attr('value',$(this).attr('default'));
		}
	});
});

function get_subcategories(selected_subcategory){
	if(selected_subcategory == null){
		selected_subcategory = false;
	}
		
	main_cat =  $("select[name='data[Classified][category_id]'] :selected").attr('value');
	var subcategories = $("#subcategories");
	subcategories.find('option')
				//.remove()
				.end();
	subcategories.hide();
	$('#ajax_loader').show();
	
	$.ajax({
		dataType: 'xml',
		url: '/classifieds/get_sub_categories/'+ main_cat + '/.xml',
		success: function(xml) {
			var count = 0;
			var options = '';
			$('sub_categories category', xml).each(function() {
				count++;
				var selected = '';
				if($('id', this).text() == selected_subcategory){
					selected = 'selected';
					
				}
				//subcategories.append(new Option($('name', this).text(), $('id', this).text(), selected));
				options += '<option value="' + $('id', this).text() + '" '+ selected +' >' + $('name', this).text() + '</option>';
			});
			subcategories.html(options);
			$('#ajax_loader').hide();
			if(count == 0){
				$("select[name='data[Classified][sub_category_id]']").append(new Option('disabled'));
				$("select[name='data[Classified][sub_category_id]']").attr('disabled', 'true');
			}else{
				subcategories.show();
				subcategories.removeAttr('disabled');
			}
			
		}
	});
}

function clear_keyword_field(){
	var keyword;
	alert('here');
	keyword = $("#keyword");
	if(keyword.attr('value') == keyword.attr('default')){
		keyword.attr('value','');
	}
}

function fotoreel_initCallback(carousel)
{
  // Disable autoscrolling if the user clicks the prev or next button.
  $("#carousel-next").click(function() {
      carousel.next();
      return false;
  });

  $("#carousel-prev").click(function() {
      carousel.prev();
      return false;
  });
};

function sponsor_fotoreel_initCallback(carousel)
{
  // Disable autoscrolling if the user clicks the prev or next button.
  $("#carousel-next").click(function() {
      carousel.next();
      return false;
  });

  $("#carousel-prev").click(function() {
      carousel.prev();
      return false;
  });
};

function latest_initCallback(carousel)
{
  // Disable autoscrolling if the user clicks the prev or next button.
  $("#nextBtn2").click(function() {
      carousel.next();
      return false;
  });

  $("#prevBtn2").click(function() {
      carousel.prev();
      return false;
  });
  
  
  carousel.clip.hover(function() {
      carousel.stopAuto();
  }, function() {
      carousel.startAuto();
  });  
};

function shop_initCallback(carousel)
{
  // Disable autoscrolling if the user clicks the prev or next button.
  
  carousel.clip.hover(function() {
      carousel.stopAuto();
  }, function() {
      carousel.startAuto();
  });  
};

function news_and_events_initCallback(carousel)
{
  // Disable autoscrolling if the user clicks the prev or next button.
  $("#nextBtn").click(function() {
      carousel.next();
      return false;
  });

  $("#prevBtn").click(function() {
      carousel.prev();
      return false;
  });
  
  carousel.clip.hover(function() {
      carousel.stopAuto();
  }, function() {
      carousel.startAuto();
  });
  
};

jQuery(document).ready(function() {
    jQuery('#carousel').jcarousel({
        scroll: 1,
        wrap: 'both',
        initCallback: fotoreel_initCallback
    });
	
	jQuery('#ARTICLEcarousel_slider').jcarousel({
		scroll: 1,
		wrap: 'both',
		initCallback: fotoreel_initCallback
	});

    jQuery('#carousel_slider').jcarousel({
        scroll: 1,
        wrap: 'both',
        auto: 8,
        initCallback: latest_initCallback
    });
    
    jQuery('#carousel_shop').jcarousel({
        scroll: 1,
        wrap: 'both',
        auto: 8,
        initCallback: shop_initCallback
    });	

    jQuery('#slider').jcarousel({
        scroll: 1,
        wrap: 'both',
        auto: 8,
        initCallback: news_and_events_initCallback
    });
	
    jQuery('#sponsor-fotoreel').jcarousel({
        scroll: 1,
        wrap: 'both',
        initCallback: sponsor_fotoreel_initCallback
    });	
});
