$(document).ready(function(){
      Netural.Popup.init();
      
      $("#SL").slider();
	  Netural.FormValidation.init();
});

(function($) {
    $.fn.slider = function(settings) {
        var config = {};
        
        if (settings) $.extend(config, settings);
        
        this.each(function() {

            // element width
            var container = $(this).children(".SL-container");
            var slider = container.children(".SL-slider");
            var first_element = slider.find("li:first");
            var element_width = first_element.width();
            
            // set container width
            slider.width(slider.children("li").length * element_width).attr("current", 1);
            
            // check for overflow content
            if (first_element.children(".SL-more").length != 0) {
                var o = first_element.children(".SL-more");
                o.appendTo(this).fadeIn(200);
            }
            
            // marker setup
            var markerDiv = $("<div>").addClass("SL-marker");
            var marker = $("<ul>");
            var count = 1;
            $("<li>").addClass("lt").attr("rel", "lt").text('<').appendTo(marker);
                
            slider.find("li").each(function() {
                $("<li>").attr("rel", count).text(count).appendTo(marker);
                count++;
            });
            $("<li>").addClass("gt").attr("rel", "gt").text('>').appendTo(marker);
            marker.appendTo(markerDiv);
            marker.children("li:eq(1)").addClass("active");
            $("<p>").appendTo(markerDiv);
            markerDiv.appendTo(this);
                        
            // more events
            $(this).find(".SL-more").live("click", function() {
                if (!$(this).hasClass("active")) {
                    $(this).addClass("active");
                } else {
                    $(this).removeClass("active");
                }
                return false;
            });
            
            // click events
            function action() {
                var rel = $(this).attr("rel");
                var current = parseInt(slider.attr("current"));
                var items = slider.find("li").length;
                if (rel == "lt") {
                    rel = (current - 1 <= 0) ? items : current - 1;
                }
                if (rel == "gt") {
                    rel = (current + 1 > items) ? 1 : current + 1;
                }
                
                animate(rel);
                
                clearInterval(intval);
                
                return false;
            }
            
            function animate(rel) {
                slider.animate({
                    left: (rel - 1) * element_width * -1
                }, 500, function() {
                    slider.attr("current", rel);
                });
                
                markerDiv.find("li").removeClass("active");
                markerDiv.find("li[rel=" + rel + "]").addClass("active");
            }
            
            function slideshow() {
                var current = parseInt(slider.attr("current"));
                var items = slider.find("li").length;
                rel = (current + 1 > items) ? 1 : current + 1;
                
                animate(rel);
            
            }
            var intval = setInterval(slideshow, 5000);
            
            $(this).find(".SL-marker li").live("click", action);
            

        });
        
    return this;
    };
 
 })(jQuery);

var Netural = {
    Popup: {
        init: function() {
            $(".popup-link").bind("click",Netural.Popup.open);
            $(".overlay").bind("click",Netural.Popup.close);
            $(".popup a.close").bind("click",Netural.Popup.close);
        },
        
        open: function() {
            /*$(this).attr("href")*/
            var o = $(".overlay");
            var p = $(".popup");
            var c = p.children("#"+$(this).attr("href"));
            c.show();
            o.fadeIn(500);
            p.fadeIn(500);
            return false;
        },
        
        close: function() {
            var o = $(".overlay");
            var p = $(".popup");
            p.fadeOut(500,function(){
                $(".popup .element").hide();
            });
            o.fadeOut(500);
        }
	},
	FormValidation: {
		init: function() {

			
			$("input.text").live("focus", function() {
				$(this).parent().children(".behindscenes").fadeOut(300);
			});
			
			$("textarea.text").live("focus", function() {
				$(this).parent().children(".behindscenes").fadeOut(300);
			});
			
			$("input.text").live("blur", function() {
				if($(this).val() == "")
					$(this).parent().children(".behindscenes").fadeIn(350);
			});
			
			$("textarea.text").live("blur", function() {
				if($(this).val() == "")
					$(this).parent().children(".behindscenes").fadeIn(350);
			});
			
			$("input.submit").live("click", function() {
				Netural.FormValidation.validate();
			});

		},
		
		validate: function() {
			alert("test");
			var error = false, globalError = false, form, items;
			form = $("#haendler-werden");
			
			items = form.find(".required");
        	items.each(function() {
        		if ($.trim($(this).val()) == "") {
                	error = true, globalError = true;
            	}
            	if ($(this).hasClass("email") && !/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/.test($(this).val())) {
                	error = true, globalError = true;
            	}
            	
            	 if (error) {
                	$(this).addClass("error");
            	}
        	});
        	
        	if(!globalError) {	            
	            form.submit();
	            return true;
	        }
	        return false;
        	
		}
	}
}
