/*  ###########################################################################
    company:    http://42.nl/
    client:     Samen in Zaken
    date:       September 2009
    author:     Egor Kloos
    author:     ---
    ########################################################################### */


	
	
    $(document).ready(function() { 
        
        $.getScript("/scripts/validate/localization/messages_nl.js", function(){
            $.validator.addMethod("dateNL", function(value, element) {
    			return this.optional(element) || /^\d\d?\-\d\d?\-\d\d\d\d?$/.test(value);
    		}, "Vul een geldige datum in.");
            $.validator.setDefaults({ dateNL: true });
        });

        function runInterface(){          
            
            runValidation();
            
        }
		runInterface()
		
        function runValidation(){
            $(':input[name="required[]"]').attr('name',function(index){return "required[" + index + "]"}); 
            $("label span.required").parent().next(":input").addClass("required");
            $("form").each(function(){$(this).validate()}); 
        }
        
        
        
        var galThumbs = $("#gallery ul").clone();
        galThumbs.find("a").each(function(a){
            var m = $("#gallery li")
            var t = $(this)
            var i = t.attr('href')
            var s = i.replace("/images/uploads/gallery/", "/images/uploads/gallery/thumbs/")
            s = s.replace(".jpg", "_thumb.jpg")
            var e = t.replaceWith('<img src="'+s+'" id="photo-'+a+'" alt="' + t.attr('title') + '" />');
        })
        
        $("#gallery li a").each(function(a){
             var t = $(this)
             var i = t.attr('href')
             t.replaceWith('<img src="'+i+'" id="id-'+a+'" alt="' + t.attr('title') + '" />');
        })
        
        $("#photos #sidebar").html(galThumbs)
        
        var sizHash = location.hash;
        if (sizHash) {
            var num = sizHash.split("-")[1];
            $("#gallery li").hide();
            $("#gallery li").eq(num).show();
            $("#sidebar .image-list img").eq(num).addClass("active")
        } else {
            $("#gallery li:not(:first)").hide()
            $("#sidebar .image-list img").eq(0).addClass("active")
        }
        $("#sidebar .image-list img").click(function(){
            var h = $(this).attr("id")
            location.hash = h;
            $("#sidebar .image-list .active").removeClass("active");
            $(this).addClass("active");
            $("#gallery li").hide();
            $("#gallery li").eq(h.split("-")[1]).show();
        })
        
        
    }); 
