$(function(){
   $('*').focus(function(event){event.preventDefault(); event.stopPropagation(); return false;})
   var step = 660;
   var proceed_now;
   var slide = $('#slider_div');
   var size  = $('#slider_div li').size();
   var max_steps = Math.ceil(size / 3)-1;
   var current_step = 0;
   var left  = 0;

   function move_left(event){
      event.preventDefault();
      if(current_step <= 0) {
         left= step*max_steps;
         left *= -1; 
         slide.animate({
           left: left
         })
         current_step = max_steps; return; 
      }
      
      if(proceed_now) return;
      proceed_now = true;
      
      current_step--;
            
      left += step;
      slide.animate({
         left: left
      }, 'slow', function(){
         proceed_now = false;
      })
   }
   
   function move_right(event){
      event.preventDefault();
      if(current_step >= max_steps) {
         left = 0   
         slide.animate({
           left: left
         })
         current_step=0; return;
      }
      
      if(proceed_now) return;
      proceed_now = true;
      
      current_step++;
      
      left -= step;
      slide.animate({
         left: left
      }, 'slow', function(){
         proceed_now = false;
      })
   }

   
   $('#master_prev').click(move_left);
   $('#master_next').click(move_right);
   
   
   
 //make order bubbling form
   if($.browser.msie && parseInt($.browser.version) == 6) {
     var maket = $('#back_grad');
     
     $(window).resize(function(){
       if(document.documentElement.clientWidth < 1374) maket.width(1374)
       else maket.css('width', 'auto') 
     })
   }
   
   
   var pageWidth  = $('#back_grad').width();
   var pageHeight = $('#back_grad').height();
   //alert(pageHeight)
   var fo_za_ma_wrap  = $('#forma_zakaza_master_wrap');
   var container_form = $('#forma_zakaza_master_wrap #container');
   
   fo_za_ma_wrap.width(1).height(pageHeight).css({
     left: '50%',
     top: 0,
     height: pageHeight
   });
   
   $('#zvonok').click(function(event){
     event.preventDefault();
     fo_za_ma_wrap.show().animate({
       left: 0,
       top: 0,
       width: '100%'
     }, 'slow', function(){
        container_form.fadeIn('slow');
        if($.browser.msie && parseInt($.browser.version) == 6) {
           var top = document.documentElement.scrollTop + 100;
           container_form.animate({top: top})
        }
     })
   })
   
   $('#close_form_order').click(function(event){
     event.stopPropagation();
     event.preventDefault();
     container_form.fadeOut('fast', function(){
        fo_za_ma_wrap.fadeOut('fast', function(){
          fo_za_ma_wrap.css({
            left: '50%',
            top: 0,
            width: 0
          })
        });
     });
   })
   
   $('#opacBack').mousedown(function(){
     container_form.fadeOut('fast', function(){
        fo_za_ma_wrap.fadeOut('fast', function(){
          fo_za_ma_wrap.css({
            left: '50%',
            top: 0,
            width: 0
          })
        });
     });   
   })
   
   
   if($.browser.msie && parseInt($.browser.version) == 6) {
      $(window).scroll(function(){
                if(container_form.is(':visible')){
                    container_form.stop();
                    var top = document.documentElement.scrollTop + 100;
                    container_form.animate({top: top}, 'slow')
                }
      })
   }
   
   
   // form input clck
   $('form [name="name"]').focus(function(){
      if(this.value == 'Ваше имя') this.value = '';
   }).blur(function(){
      if(this.value == '') this.value = 'Ваше имя';
   })
   $('form [name="name"]').attr('value', 'Ваше имя');
   
   
   $('form [name="phone"]').focus(function(){
      if(this.value == 'Ваш телефон') this.value = '';
   }).blur(function(){
      if(this.value == '') this.value = 'Ваш телефон';
   })
   $('form [name="phone"]').attr('value', 'Ваш телефон');
   
   

   $('form [name="message"]').focus(function(){
      if(this.value == 'Комментарий') this.value = '';
   }).blur(function(){
      if(this.value == '') this.value = 'Комментарий';
   })
   $('form [name="message"]').attr('value', 'Комментарий');
   
   
   $('form [name="email"]').focus(function(){
      if(this.value == 'Ваша почта') this.value = '';
   }).blur(function(){
      if(this.value == '') this.value = 'Ваша почта';
   })
   $('form [name="email"]').attr('value', 'Ваша почта');
   
   
   
   //about us
   function coords(e){
      var ob = {left: 0, top: 0};
      while(e) {
          ob.left += e.offsetLeft;
          ob.top  += e.offsetTop;
          e = e.offsetParent;
      }
      return ob;
   }
    
   $('<div id="message" style="position: absolute; width: 720px; display: none; z-index: 40; left: 0; top: 0;"><div id="ugol"></div><div id="wrap_mes"></div></div>').appendTo('#back_grad')
   var container_for_message = $('#wrap_mes')
   var message_block = $('#message')

   $('#personas img').hover(function(){
                        var left = coords(this).left
                        var top  = coords(this).top
                        var needLeft = left + 126
                        var needTop  = top  + 10
                        left += 200;
                        top -=  100;
                        var html = $(this).nextAll('.mes').html();
                        
                        message_block.fadeOut(function(){
                           container_for_message.html(html);
                            message_block.css({left: left, top: top}).animate({
                              left: needLeft,
                              top:  needTop,
                              opacity: 'show'
                            })
                        })
                      }, 
                      function(event){
                        //if(event.relatedTarget.id == 'ugol') return
                        var left = coords(this).left
                        var top  = coords(this).top
                        left += 200;
                        top -=  126;
                        message_block.animate({
                          left: left,
                          top: top,
                          opacity: 'hide'
                        })
                      })
                      
   message_block.mouseout(function(){
      message_block.fadeOut();
   })
   
   
   //portin button
   $('#next button, #prev button').click(function(){
      if(this.className == 'disable') return;
      location = $(this).next('a')[0].href
   });
   
   //fifti fifti
   $('#safariTop').css('position', 'relative');
   
   var shakeInterval;

   function shake(){
     shakeInterval = setInterval(function(){
                                        $('#nokia').animate({left: -181, top: -101}, 50).animate({left: -180, top: -100}, 50).animate({left: -181, top: -101}, 50).animate({left: -180, top: -100}, 50).animate({left: -181, top: -101}, 50).animate({left: -180, top: -100}, 50).animate({left: -181, top: -101}, 50).animate({left: -180, top: -100}, 50)
                               }, 1500)
   }
   
 /*  if( (!$.browser.msie && !parseInt($.browser.version) == 6) || ($.browser.msie && parseInt($.browser.version) > 6) ||
(!$.browser.msie) ) {
      shake();
  
      $('#nokia').rotate({maxAngle:0,minAngle:-55, preservePosition: true,
	bind:
	[
	{"mouseover":function(){
                       clearInterval(shakeInterval)
                       $(this).rotateAnimation(-35);
                       
                     }},
	{"mouseout":function(){
                       $(this).rotateAnimation(0);
                       shake();
                    }}
	]
	});
   }

   */
  //rotate nokia
  /* old variant
  var shakeInterval;
  $('#nokia').rotate({maxAngle:0,minAngle:-55, preservePosition: true,
	bind:
	[
	{"mouseover":function(){
                       $(this).rotateAnimation(-35);
                       var thisNokia = this;
                       shakeInterval = setInterval(function(){
                                        $(thisNokia).animate({left: -56, top: -64}, 50).animate({left: -53, top: -61}, 50).animate({left: -56, top: -64}, 50).animate({left: -53, top: -61}, 50).animate({left: -56, top: -64}, 50).animate({left: -53, top: -61}, 50).animate({left: -56, top: -64}, 50).animate({left: -53, top: -61}, 50)
                                           
                                        }, 1500)
                     }},
	{"mouseout":function(){
                       $(this).rotateAnimation(0);
                       clearInterval(shakeInterval)
                    }}
	]
	});
   
       end old variant */
  



     /* uslugi zaderjka pri navedenii */
     $('#inner_uslugi td').hover(
        function(){
           var e = this; 
           setTimeout(function(){$('object, embed', e).css('visibility', 'visible')}, 500)
        },
        function(){
           var e = this;
           setTimeout(function(){$('object, embed', e).css('visibility', 'hidden')}, 2000)
        }
     )
     /* konec uslugi zaderjka pri navedenii */
     
       
});

function changePic(element, src) {
var img = element.getElementsByTagName("IMG")[0];
img.src = src;
return;
}
