// Wordpress retarget media Theme - Custom Javascript
jQuery(document).ready(function($) {
	
	// Header Image Cycle
	$('#area2').cycle({
		fit: 1,
		fx: 'fade',
		pause: 3
	});
	
	// Slide down Neuigkeiten excerpts
	//$("#primary").css({display: "none"});
	//$("#primary").slideDown("1000");
		
	//$('#primary li a').appendTo("<br />");
	//$(".gallery").css('padding-bottom','0px', function() {});
	$("br").removeAttr("style");
	
	// Navigation drop down etc.
	//$("#nav ul").css({display: "none"}); // Opera Fix
	//$("#nav li ul").css({visibility: "hidden"});
	$("#nav li").hover(function(){
		$(this).find('ul:first').css({visibility: "visible", display: "none"}).show(); }, function(){ 
		$(this).find('ul:first').css({visibility: "hidden"}); //fadeOut("100")  .slideDown("650")
	});	
	
    $("#nav > :nth-child(1)").children("a").attr('href', "javascript:void(0)");
	$("#nav > :nth-child(2)").children("a").attr('href', "javascript:void(0)");
	$("#nav > :nth-child(3)").children("a").attr('href', "javascript:void(0)");
	$("#nav > :nth-child(4)").children("a").attr('href', "javascript:void(0)");
	$("#nav > :nth-child(5)").children("a").attr('href', "javascript:void(0)");
	$("#nav > :nth-child(6)").children("a").attr('href', "javascript:void(0)");
	
	$('#nav > :nth-child(1)').css('width','166px'); //166
	$('#nav > :nth-child(2)').css('width','178px'); //178
	$('#nav > :nth-child(3)').css('width','176px'); //176
	$('#nav > :nth-child(4)').css('width','144px'); //144
	$('#nav > :nth-child(5)').css('width','160px'); //160
	$('#nav > :nth-child(6)').css('width','136px'); //136
	
	$('#nav > :nth-child(1) ul').css('width','166px'); //166
	$('#nav > :nth-child(2) ul').css('width','178px'); //178
	$('#nav > :nth-child(3) ul').css('width','176px'); //176
	$('#nav > :nth-child(4) ul').css('width','144px'); //144
	$('#nav > :nth-child(5) ul').css('width','160px'); //160
	$('#nav > :nth-child(6) ul').css('width','136px'); //136

	$('#nav > :nth-child(1)').children("a").hover(function() {
		$(this).css('cursor','default');
	}, function() {
	 	$(this).css('cursor','auto');
	});
	
	$('#nav > :nth-child(2)').children("a").hover(function() {
		$(this).css('cursor','default');
	}, function() {
	 	$(this).css('cursor','auto');
	});
	$('#nav > :nth-child(3)').children("a").hover(function() {
		$(this).css('cursor','default');
	}, function() {
	 	$(this).css('cursor','auto');
	});
	$('#nav > :nth-child(4)').children("a").hover(function() {
		$(this).css('cursor','default');
	}, function() {
	 	$(this).css('cursor','auto');
	});
	$('#nav > :nth-child(5)').children("a").hover(function() {
		$(this).css('cursor','default');
	}, function() {
	 	$(this).css('cursor','auto');
	});
	$('#nav > :nth-child(6)').children("a").hover(function() {
		$(this).css('cursor','default');
	}, function() {
	 	$(this).css('cursor','auto');
	});
	
	
	swapValue = [];
	$(".swap-value").each(function(i){
	   swapValue[i] = $(this).val();
	   $(this).focus(function(){
		  if ($(this).val() == swapValue[i]) {
			 $(this).val("");
		  }
		  $(this).addClass("focus");
	   }).blur(function(){
		  if ($.trim($(this).val()) == "") {
			 $(this).val(swapValue[i]);
		 $(this).removeClass("focus");
		  }
	   });
	});	
	
	// Sort Archive Items
	$("#archive-wrapper").height($("#archive-pot").height());

	$("#archive-browser select").change(function() {
	
		$("#archive-pot")
			.empty()
			.html("<div style='text-align: center; padding: 30px;'><img src='../../wp-content/themes/erlenschule/images/ajax-loader.gif' /></div>");
	
		var dateArray = $("#month-choice").val().split("/");
		var y = dateArray[5];
		var m = dateArray[6];
		var c = $("#cat").val();
		
		$.ajax({
			url: "../../archive-getter/",
			dataType: "html",
			type: "POST",
			data: ({
				"digwp_y": y,
				"digwp_m" : m,
				"digwp_c" : c
			}),
			success: function(data) {
				$("#archive-pot").html(data);
				
				$("#archive-wrapper").animate({
					height: $("#archives-table tr").length * 50
				});
			}
		});	
	});
	
	$('#searchform').submit(function(e){		
		if($('#s').val() == '') {
			e.preventDefault();
			alert("Bitte geben Sie mindestens ein Schlagwort ein, Vielen Dank!");
		}
	});
	
	if(typeof(hls_query) != 'undefined'){
	  $("#search-wrapper").highlight(hls_query, 1, "hls");
	}
	
});

function setActive(element) {
	jQuery('#nav > :nth-child(' + element + ')').css('background-color','#F8D12C');
	jQuery('#nav > :nth-child(' + element + ')').children("a").css('color','#623530');
}

jQuery.fn.extend({
  highlight: function(search, insensitive, hls_class){
	var regex = new RegExp("(<[^>]*>)|(\\b"+ search.replace(/([-.*+?^${}()|[\]\/\\])/g,"\\$1") +")", insensitive ? "ig" : "g");
	return this.html(this.html().replace(regex, function(a, b, c){
	  return (a.charAt(0) == "<") ? a : "<strong class=\""+ hls_class +"\">" + c + "</strong>";
	}));
  }
});
