$('document').ready(function() {
     new image_navigation();
     $('.datepicker').datepicker({dateFormat:'yy-mm-dd'});

     $('input.submit').click(function() {
        ret = false;
        $('input.required').each(function() {
           
           if($(this).val()=='') {
               if (lang=='de') {
                   alert("Bitte alle mit * markierten Felder ausfüllen!");
               } else {
                   alert("Please fill in all with * marked fields!");
               }
               ret = true;
               return false;
           }
           
        });
        if(ret) { return false; }
     });

});

var image_navigation = function() {
    var me = this;
    var start = 1;
    var lis = $('ul li','#images_small');
    var ul = $('ul','#images_small');
    var counter = lis.length;
    var show = 3;
    var topUnactive = false;
    var bottomUnactive = false;
    var height = parseInt((lis.css('height')).replace("px",""))+parseInt((lis.css('margin-top')).replace("px",""))+parseInt((lis.css('margin-bottom')).replace('px',""));

    this.initTop = function() {
        $('#images_small_navi_top img')
        .unbind("click")
        .click(function() {
            start--;
            ul.animate({marginTop:'+='+height+'px'});
            me.checkActive();
        })
        .unbind("mouseover")
        .mouseover(function() {
            $(this).css("cursor","pointer");
            return false;
        })
        .unbind("mouseout")
        .mouseout(function() {
            $(this).css("cursor","");
            return false;
        });
       
    }

    this.initBottom = function() {
        $('#images_small_navi_bottom img')
        .unbind("click")
        .click(function() {
            start++;
            ul.animate({marginTop:'-='+height+'px'});
            me.checkActive();
            return false;
        })
        .unbind("mouseover")
        .mouseover(function() {
            $(this).css("cursor","pointer");
            return false;
        })
        .unbind("mouseout")
        .mouseout(function() {
            $(this).css("cursor","");
            return false;
        });
        
    }
    
    this.checkActive = function() {
        me.initBottom();
        me.initTop();
        if ((start+show-1)==counter && bottomUnactive==false) {
            $('#images_small_navi_bottom img').fadeTo('fast',0.1).unbind("click").unbind("mouseover");
            bottomUnactive=true;
        }
        if (bottomUnactive==true && (start+show-1)<counter) {
            $('#images_small_navi_bottom img').fadeTo('fast',1);
            bottomUnactive=false;
        }
        if (start==1 && topUnactive==false) {
            $('#images_small_navi_top img').fadeTo('fast',0.1).unbind("click").unbind("mouseover");
            topUnactive=true;
        }
        if (start!=1 && topUnactive==true) {
            $('#images_small_navi_top img').fadeTo('fast',1);
            topUnactive=false;
        }

    }

    this.init = function() {
        this.initTop();
        this.initBottom();
        this.checkActive();
    }
    this.start = function() {
        var img_small = $('#images_small');
        $('li img',img_small).each(function() { if(!$(this).hasClass("active")){
            $(this).fadeTo('fast','0.5');
        }})
        .bind("mouseenter",function() { if (!$(this).hasClass('active')) { $(this).fadeTo('fast',1); } })
        .bind("mouseout",function() { if (!$(this).hasClass('active')) { $(this).fadeTo('fast','0.5'); } })
        .click(function() {
            if (!$(this).hasClass('active')) {
                var src_new = $(this).attr("bigsrc");
                var img_id = 1;
                $('#image_big img.bigimage')
                    .fadeOut('fast',function(){
                        var objtoload = $(this);
                        var img_new = new Image();
                        //$(this).attr("src",src_new);
                        $(img_new)
                        .hide()
                        .attr("id","image_id_"+img_id).attr("class","bigimage")
                        .load(function() {
                            var refe = '#image_id_'+img_id;
                            $(objtoload).replaceWith(this);
                            $(refe).fadeIn('normal');
                            img_id++;
                        })
                        .attr("src",src_new);
                    });
                    
                $('img.active').removeClass('active').fadeTo('fast','0.5');
                $(this).addClass('active');

            }
        });
        if($('ul > li',img_small).length > 3) {
            var navi_top = $('<div id="images_small_navi_top" class="images_small_navi"><img src="/img/images_arrow_top.gif" /></div>');
            var navi_bottom = $('<div id="images_small_navi_bottom" class="images_small_navi"><img src="/img/images_arrow_bottom.gif" /></div>');
            img_small.prepend(navi_top).append(navi_bottom);
            me.init();
        }
    }
    this.start();
}
