var image_list = false;
var gallery = false;
var selected_image_id = false;

$(document).ready(function(){
    
    var imageBorisSpiezmanMouseOver = new Image();
    var imageBorisSpiezmanMouseOut = new Image();
    var imageArrowUp = new Image();
    var imageArrowUpMouseOver = new Image();
    var imageArrowDown = new Image();
    var imageArrowDownMouseOver = new Image();
    var topMenuBusy = false;
    var bottomMenuBusy = false;
    var grad = new Image();
    var left_timer;
    var left_timer_timeout=100;
    var right_timer;
    var right_timer_timeout=100;
    var available_roll_height = $("#page").height()-200-6;  
    var left_roll_height = 0;
    var right_roll_height = 0;                
    var text_timer;   
    var agt=navigator.userAgent.toLowerCase();
    var move_left_down_points = 1;
    var move_left_up_points = 1;
    var move_right_down_points = 1;
    var move_right_up_points = 1;
    
   // var selected_image_id;

    
    $(imageBorisSpiezmanMouseOver).attr("src","/images/boris_spiezman_mouse_over.gif");
    $(imageBorisSpiezmanMouseOut).attr("src","/images/boris_spiezman.gif");
    $(imageArrowUp).attr("src","/images/arrow_up.gif");
    $(imageArrowUpMouseOver).attr("src","/images/arrow_up_mouse_over.gif");
    $(imageArrowDown).attr("src","/images/arrow_down.gif");
    $(imageArrowDownMouseOver).attr("src","/images/arrow_down_mouse_over.gif");

   

    
    
     
    
    
    $(".roll_img_link").click(function(event){
        
        if(gallery)
        {
            event.preventDefault();
        }
        
    });
    
    if(image_list)
    {
        $('.center_text div.text a.thumb').lightBox({fixedNavigation:true});    
    }
    
        //

   
    
      
    if(gallery)
    {
        $('#center_image_link').click(function(event){
            
                if($("#left_roll1_img_"+$("#center_image_link").attr("rel") ).attr('id'))
                {
                    
                    $("#left_roll_lb_img_"+$("#center_image_link").attr("rel")).click();
                    //alert("#left_roll_lb_img_"+$("#center_image_link").attr("rel"));
                }
                if($("#right_roll1_img_"+$("#center_image_link").attr("rel") ).attr('id'))
                {
                    $("#right_roll_lb_img_"+$("#center_image_link").attr("rel")).click();
                }
                event.preventDefault();
            });
    }
    
    $(".img_boris_spiezman").mouseover(function()
    {
        this.src = imageBorisSpiezmanMouseOver.src;
    });
    
    $(".img_boris_spiezman").mouseout(function()
    {
        this.src = imageBorisSpiezmanMouseOut.src;
    });
    
    $(".img_arrow_up").mouseover(function()
    {           
        this.src = imageArrowUpMouseOver.src;
    });
    
    $(".img_arrow_up").mouseout(function()
    {
        this.src = imageArrowUp.src;
    });
    
    $(".img_arrow_down").mouseover(function()
    {
        this.src = imageArrowDownMouseOver.src;
    });
    
    $(".img_arrow_down").mouseout(function()
    {
        this.src = imageArrowDown.src;
    });
    
    
    
    
    $("#buttons_top").width(0);
           
    $("#buttons_top img").each(function()
    {
        $("#buttons_top").width($("#buttons_top").width()+100);
    });
           
    
    $("#buttons_top").mouseover(function()
    {
        
            $("#top_menu .horizontal_line").animate({
                top:'-16px'
            },{ queue:false, duration:999 });
          
            //$("#buttons_top").animate({height:'37px'},{ queue:false, duration:1500 })
        
        
            $("#buttons_top img").each(function()
            {
                $(this).animate({height:'27px'},{queue:false, duration:999});
            });
       
    });
    
    $("#buttons_top").mouseout(function()
    {
        //topMenuBusy = true;
        $("#top_menu .horizontal_line").animate({top:'0px'}, {queue:false, duration:1000, complete:function(){}});

        
        $("#buttons_top img").each(function()
        {
            $(this).animate({height:'11px'},{queue:false, duration:1000});
        });
    });
    
    $("#buttons_bottom").mouseover(function()
    {
        
            $("#bottom_menu .horizontal_line").animate({
                bottom:'-16px'
            },{ queue:false, duration:999 });
          
            //$("#buttons_top").animate({height:'37px'},{ queue:false, duration:1500 })
        
        
            $("#buttons_bottom img").each(function()
            {
                $(this).animate({height:'27px'},{queue:false, duration:999});
            });
       
    });
    
    $("#buttons_bottom").mouseout(function()
    {
        $("#bottom_menu .horizontal_line").animate({bottom:'0px'}, {queue:false, duration:1000, complete:function(){}});
       
        $("#buttons_bottom img").each(function()
        {
            $(this).animate({height:'11px'},{queue:false, duration:1000});
        });
    });
    
    var changed = false;
    $(".menu_img").mouseover(function()
    {
        if (this.src.lastIndexOf("_mouse_over")==-1)
        {
            changed = true;
            this.src = this.src.substr(0,this.src.lastIndexOf(".gif"))+"_mouse_over"+".gif";
        }
    });
    
    $(".menu_img").mouseout(function()
    {
        if(changed)
        {
            this.src = this.src.substr(0,this.src.lastIndexOf("_mouse_over"))+".gif";
            changed = false;
        }
    });
    
    
   

    $(".link_arrow_up.left").mouseover(function()
    {
        if(left_timer || admin_mode)
        {
            clearInterval(left_timer);
            if(agt.indexOf("msie")!=-1)
            {
                move_left_up_points = 3; 
            }
            else
            {
                move_left_up_points = 2; 
            }
            left_timer = setInterval(move_left_roll,3);      
        }
               
    });
    
    $(".link_arrow_up.left").mouseout(function()
    {
        clearInterval(left_timer);
        
        if(!admin_mode && (left_roll_height>available_roll_height))
        {
              
            move_left_up_points = 1; 
            left_timer = setInterval(move_left_roll_down,100);      
            
        }
    });
    
    
    $(".link_arrow_down.left").mouseover(function()
    {
        
        if(left_timer || admin_mode)
        {
            clearInterval(left_timer);
            if(agt.indexOf("msie")!=-1)
            {
                move_left_down_points = 3; 
            }
            else
            {
                move_left_down_points = 2; 
            }
            left_timer = setInterval(move_left_roll_down,3);      
        }
               
    });
    
    $(".link_arrow_down.left").mouseout(function()
    {
        
        clearInterval(left_timer);
        if(!admin_mode && (left_roll_height>available_roll_height))
        {
            move_left_down_points = 1;   
            left_timer = setInterval(move_left_roll_down,100);      
        }
    });
    
    $(".link_arrow_down.right").mouseover(function()
    {
        if(right_timer || admin_mode)
        {
            clearInterval(right_timer);
            if(agt.indexOf("msie")!=-1)
            {
                move_right_down_points = 3; 
            }
            else
            {
                move_right_down_points = 2; 
            }
            right_timer = setInterval(move_right_roll_down,3);      
        }
               
    });
    
    $(".link_arrow_down.right").mouseout(function()
    {
        clearInterval(right_timer);
        if(!admin_mode && (right_roll_height>available_roll_height))
        {
            
            move_right_down_points = 1; 
            right_timer = setInterval(move_right_roll_down,100);      
        }
    });
    
    
    $(".link_arrow_up.right").mouseover(function()
    {
       
        if(right_timer || admin_mode)
        {
            clearInterval(right_timer);
            if(agt.indexOf("msie")!=-1)
            {
                move_right_up_points = 3; 
            }
            else
            {
                move_right_up_points = 2; 
            }
            right_timer = setInterval(move_right_roll,3);      
        }
               
    });
    
    $(".link_arrow_up.right").mouseout(function()
    {
        clearInterval(right_timer); 
        
        if(!admin_mode && (right_roll_height>available_roll_height))
        {
            move_right_down_points = 1; 
            right_timer = setInterval(move_right_roll_down,100);      
        }
    });
    
    
 
        $(".left_roll_img").each(function()
        {
            left_roll_height+=$(this).height()+2;
            if(admin_mode)
            {
                left_roll_height+=40;
            }
        });
        
        $(".right_roll_img").each(function()
        {
            right_roll_height+=$(this).height()+2;
            if(admin_mode)
            {
                right_roll_height+=40;
            }
        });
    
        
        if(left_roll_height>available_roll_height)
        {
            if(admin_mode)
            {
                $("#left_roll").height(left_roll_height);
                $("#left_roll1").height(left_roll_height);
            }
            else
            {
                $("#left_roll").height(left_roll_height*2);
                $("#left_roll1").height(left_roll_height);
                $("#left_roll2").height(left_roll_height);
                $("#left_roll2").css("top",left_roll_height+'px');
        

                $(".left_roll_img").each(function()
                {
                    var src=$(this).attr("src");
                    var width=$(this).attr("width");
                    var height=$(this).attr("height");
                    var id = $(this).attr("id").replace("left_roll1_img_","left_roll2_img_");
                    $('<img class="roll_img" src="'+src+'" id='+id+' width='+width+' height='+height+' />').appendTo("#left_roll2");
                });
            
                left_timer = setInterval(move_left_roll_down,left_timer_timeout);
            }
            
        }
        else
        {
            $("#left_roll").height(left_roll_height);
            $("#left_roll1").height(left_roll_height); 
            $("#left_roll1").css("top",((available_roll_height-left_roll_height)/2+2)+'px');
        }
        
        
        if(right_roll_height>available_roll_height)
        {
            
            if(admin_mode)
            {
                $("#right_roll").height(right_roll_height);
                $("#right_roll1").height(right_roll_height);
            }
            else
            {
                $("#right_roll").height(right_roll_height*2);
                $("#right_roll1").height(right_roll_height);
                $("#right_roll2").height(right_roll_height);
                $("#right_roll2").css("top",right_roll_height+'px');
        

                $(".right_roll_img").each(function()
                {
                    var src=$(this).attr("src");
                    var width=$(this).attr("width");
                    var height=$(this).attr("height");
                    var id = $(this).attr("id").replace("right_roll1_img_","right_roll2_img_");
                    $('<img src="'+src+'" id='+id+' width='+width+' height='+height+' />').addClass("roll_img").addClass("right_roll_img").appendTo("#right_roll2");
                });
            
                right_timer = setInterval(move_right_roll_down,right_timer_timeout);
            }
            
        }
        else
        {
            $("#right_roll").height(right_roll_height);
            $("#right_roll1").height(right_roll_height); 
            $("#right_roll1").css("top",((available_roll_height-right_roll_height)/2)+'px');
        }        
        
     // bind();
         
         
    
    function move_left_roll_down()
    {
        var top1 = parseInt($("#left_roll1").css("top"));
        var height1 = $("#left_roll1").height();
       
        
       
        if(admin_mode)
        {
            if(top1+height1-available_roll_height>=0)
            {
                
                 $("#left_roll1").css("top",(top1-move_left_down_points)+'px');  
            }
        } 
        else
        {
            var top2 = parseInt($("#left_roll2").css("top"));
            var height2 = $("#left_roll2").height();
                               // = 2;
            //$("#name").attr("value",move_left_down_points);//alert();
            $("#left_roll1").css("top",(top1-move_left_down_points)+'px');    
            $("#left_roll2").css("top",(top2-move_left_down_points)+'px');
        
            if(top1+height1<0)
            {
                $("#left_roll1").css("top",(top2+height2)+'px');
            }
        
            if(top2+height2<0)
            {
                $("#left_roll2").css("top",(top1+height1)+'px');
            }
        }
    }
    
    function move_left_roll()
    {
        var top1 = parseInt($("#left_roll1").css("top"));
        var height1 = $("#left_roll1").height();
        
        if(admin_mode)
        {
            
            if(top1<0)
            {
                
                $("#left_roll1").css("top",(top1+move_left_up_points)+'px');
            }    
        }
        else
        {
            var top2 = parseInt($("#left_roll2").css("top"));
            var height2 = $("#left_roll2").height();
         
            $("#left_roll1").css("top",(top1+move_left_up_points)+'px');
            $("#left_roll2").css("top",(top2+move_left_up_points)+'px');
        
            if(top1>available_roll_height)
            {
                $("#left_roll1").css("top",(top2-height1+1)+'px');
            }
        
        
            if(top2>available_roll_height)
            {
                $("#left_roll2").css("top",(top1-height2+1)+'px');
            }
        }
    }
    
    
    function move_right_roll_down()
    {
        var top1 = parseInt($("#right_roll1").css("top"));
        var height1 = $("#right_roll1").height();         
        
       
        
        if(admin_mode)
        {
            if(top1+height1-available_roll_height>0)
            {
                 $("#right_roll1").css("top",(top1-move_right_down_points)+'px');  
            }
        } 
        else
        {
            var top2 = parseInt($("#right_roll2").css("top"));
            var height2 = $("#right_roll2").height();
              
            $("#right_roll1").css("top",(top1-move_right_down_points)+'px');
            $("#right_roll2").css("top",(top2-move_right_down_points)+'px');
        
            if(top1+height1<0)
            {
                $("#right_roll1").css("top",(top2+height2+1)+'px');
            }
        
            if(top2+height2<0)
            {
                $("#right_roll2").css("top",(top1+height1+1)+'px');
            }
        }
    }
    
    function move_right_roll()
    {
         
        
        var top1 = parseInt($("#right_roll1").css("top"));   
        var height1 = $("#right_roll1").height(); 
        
        if(admin_mode)
        {
            if(top1<0)
            {
                
                $("#right_roll1").css("top",(top1+move_right_up_points)+'px');
            }    
        }
        else
        {
            var top2 = parseInt($("#right_roll2").css("top"));
            var height2 = $("#right_roll2").height();
         
            $("#right_roll1").css("top",(top1+move_right_up_points)+'px');
            $("#right_roll2").css("top",(top2+move_right_up_points)+'px');
        
            if(top1>available_roll_height)
            {
                $("#right_roll1").css("top",(top2-height1)+'px');
            }
        
        
            if(top2>available_roll_height)
            {
                $("#right_roll2").css("top",(top1-height2)+'px');
            }
        }
    }
    
    var clicked_image;
    
    if(selected_image_id)
    {
       
        if($("#right_roll1_img_"+selected_image_id))
        {
            
        }
        if($("#left_roll1_img_"+selected_image_id).attr("id"))
        {
             
            clicked_image = $("#left_roll1_img_"+selected_image_id);
            $("#left_roll1_img_"+selected_image_id).addClass("roll_img_highlighted");

        }
        else if($("#right_roll1_img_"+selected_image_id).attr("id"))
        {
           // alert($("#right_roll1_img_"+selected_image_id).attr("id"));
            clicked_image = $("#right_roll1_img_"+selected_image_id);
            $("#right_roll1_img_"+selected_image_id).addClass("roll_img_highlighted");
        }
        else 
        {
            clicked_image = false;
        }
    }
    else
    {
         clicked_image = false;      
    }
    
    ///alert(clicked_image.id);
    $(".roll_img").mouseover(function()      
    {
        $(this).addClass("roll_img_highlighted");
    });
    
    $(".roll_img").mouseout(function()      
    {
        if(clicked_image)
        {
            if(this.id==$(clicked_image).attr("id"))
            {
            return true;
            }
        }
        $(this).removeClass("roll_img_highlighted");
    });
          
          
    $(".roll_img").click(function()
    {
        if(!gallery)
        {
            return true;
        }
        
        $(".roll_img").removeClass("roll_img_highlighted");
        $(this).addClass("roll_img_highlighted");
        clicked_image = this;
        if($(this).hasClass('right_roll_img'))     
        {
            var id = this.id.substr(16);
        }
        else
        {
            var id = this.id.substr(15);
        }
        var img_edit_link=""
        var thumb_edit_link;
        var thumb_href;
        var href;
        
        if(admin_mode && $("#center_image_edit_link"))
        {
            img_edit_link= $("#center_image_edit_link");
            img_edit_link_img = $("#center_image_edit_link img");
            thumb_edit_link= $("#center_image_thumb_edit_link");
            thumb_edit_link_img= $("#center_image_thumb_edit_link img");

            href = $(img_edit_link).attr("href");
            thumb_href = $(thumb_edit_link).attr("href");
            
            href = href.substr(0,href.lastIndexOf("/")+1)+big_img_id[id];
            thumb_href = thumb_href.substr(0,thumb_href.lastIndexOf("/")+1)+big_img_id[id];
            
            $(img_edit_link).attr("href",href); 
            $(thumb_edit_link).attr("href",thumb_href); 
            
            $(img_edit_link_img).css("left",-big_img_width[id]/2-20); 
            $(thumb_edit_link_img).css("left",-big_img_width[id]/2-40); 
            $(img_edit_link_img).css("top",-big_img_height[id]/2-20); 
            $(thumb_edit_link_img).css("top",-big_img_height[id]/2-20); 
            //alert($(img_edit_link).css("left")); 
        }
            
        if((big_img_type[id]=="image/gif")||(big_img_type[id]=="image/jpeg")||(big_img_type[id]=="image/pjpeg"))
        {

            $("#center_div").empty();
            $('<a rel="'+id+'" id="center_image_link" href="/'+source_img_path[id]+'"><img style="position:absolute;top:-'+big_img_height[id]/2+'px;left:-'+big_img_width[id]/2+'px;" width="'+big_img_width[id]+'" height="'+big_img_height[id]+'" src="/'+big_img_path[id]+'"></a>').appendTo("#center_div");
            //$('#center_image_link').lightBox({fixedNavigation:true,thumbnailsArray:[thumbnails[id]]});    
            
            $('#center_image_link').click(function(event){
            
                if($("#left_roll1_img_"+$("#center_image_link").attr("rel") ).attr('id'))
                {
                    
                    $("#left_roll_lb_img_"+$("#center_image_link").attr("rel")).click();
                    //alert("#left_roll_lb_img_"+$("#center_image_link").attr("rel"));
                }
                if($("#right_roll1_img_"+$("#center_image_link").attr("rel") ).attr('id'))
                {
                    $("#right_roll_lb_img_"+$("#center_image_link").attr("rel")).click();
                }
                event.preventDefault();
            });
            
            if(admin_mode)
            {
                
                img_edit_link.appendTo("#center_div");
                thumb_edit_link.appendTo("#center_div");
            }
                       
        }
        else if(big_img_type[id]=="application/x-shockwave-flash")
        {
            $("#center_div").empty();
            
           $('<div style="position:absolute;left:-205px;top:-105px;"><object width="410" height="210" id="flash"><param name="movie" value="/'+big_img_path[id]+'"><embed src="/'+big_img_path[id]+'" width="410" height="210"></embed></object></div>').appendTo("#center_div");
            
                
            if(admin_mode)
            {
                img_edit_link.appendTo("#center_div");
            }
           
                     
        }
        
        
        
        
    });     
    
    $("#text_arrow_up").mouseover(function(){
        //text_timer = setInterval(scroll_text_down(),1);    
        //scroll_text_down();
    });
    
    $("#text_arrow_down").mouseover(function(){
        text_timer = setInterval(scroll_text_down,1);    
    });
    $("#text_arrow_down").mouseout(function(){
        clearInterval(text_timer);
    });
    
    $("#text_arrow_up").mouseover(function(){
        text_timer = setInterval(scroll_text_up,1);    
    });
    $("#text_arrow_up").mouseout(function(){
        clearInterval(text_timer);
    });
    
    function scroll_text_down()
    {
        
        if (agt.indexOf("msie") != -1) 
        {
            $("div.center_text .text").scrollTop($("div.center_text .text").scrollTop()+2);
        }
        else
        {
            $("div.center_text .text").scrollTop($("div.center_text .text").scrollTop()+1);
        }
    }        

    function scroll_text_up()
    {
        
                
        if (agt.indexOf("msie") != -1) 
        {
            $("div.center_text .text").scrollTop($("div.center_text .text").scrollTop()-3);
        }
        else
        {   
            var a = $("div.center_text .text").scrollTop();
            var b = $("div.center_text .text").scrollTop()-1;
            $("div.center_text .text").scrollTop($("div.center_text .text").scrollTop()-2);
            var c = $("div.center_text .text").scrollTop();
            c = $("div.center_text .text").scrollTop()-1;
        }
        
    }    
    
    
    $(".delete_link").click(function(event)
    {
        if(!confirm("Are you sure?"))
        {
            event.preventDefault();    
        }
    });
    
    
    
         
});


function mailCheck(str) {
    
    
        var at="@"
        var dot="."
        var lat=str.indexOf(at)
        var lstr=str.length
        var ldot=str.indexOf(dot)
        if (str.indexOf(at)==-1){
          /// alert("Invalid E-mail ID")
           return false
        }

        if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
           //alert("Invalid E-mail ID")
           return false
        }

        if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
            //alert("Invalid E-mail ID")
            return false
        }

         if (str.indexOf(at,(lat+1))!=-1){
            //alert("Invalid E-mail ID")
            return false
         }

         if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
            //alert("Invalid E-mail ID")
            return false
         }

         if (str.indexOf(dot,(lat+2))==-1){
            //alert("Invalid E-mail ID")
            return false
         }
        
         if (str.indexOf(" ")!=-1){
            //alert("Invalid E-mail ID")
            return false
         }

          return true            
    }
    
    function validateMessage()
    {
            var name = document.getElementById("name");
            var mail = document.getElementById("mail");
            if ((name.value.length == 0)||(!mailCheck(mail.value)))
            {
                text = "";
                
                if (name.value.length == 0)
                {
                    text+=" Name";
                }    
                
                
                if (!mailCheck(mail.value))
                {
                    if(text.length>0)
                    {
                        text+=',';
                    }
                    
                    text+=" E-Mail";
                }    
                
                alert('Please fill the following fields: '+text);
                return false;
            }
                        
            document.getElementById('form1').submit();
            return true;
    }    