$(function()
   {
      function parseStr(val)
      {
         return '' + val;
      }
      $('.cotop').each(function()
         {

            var overlay = false;
            var slider = false;
            var width = 600;
            var formDiv = $('.callback-form');
            var form = formDiv.find('form');

            $('.arrow-button', form).click(function()
               {
                  form.trigger('submit');
                  return false;
               }
            );

            form.submit(function()
               {
                  var phone = parseStr($('input[name=phone]', this).val());
                  var autor = parseStr($('input[name=autor]', this).val());
                  var time = parseStr($('input[name=time]', this).val());

                   
                  if (autor.length <= 0)
                  {
                     $('input[name=autor]', this).parent().prev().css(
                        {
                        color: 'red'
                        }
                     );
                     $('input[name=autor]', this).focus();
                     return false;
                  }
                  $('input[name=autor]', this).parent().prev().css(
                     {
                     color: ''
                     }
                  );

                  if (phone.length <= 0)
                  {
                     $('input[name=phone]', this).parent().prev().css(
                        {
                        color: 'red'
                        }
                     );
                     $('input[name=phone]', this).focus();
                     return false;
                  }

                  $('input[name=phone]', this).parent().prev().css(
                     {
                     color: ''
                     }
                  );

                  var data =
                  {
                  autor: autor,
                  phone: phone,
                  time: time
                  };

                  $.post('/quicker.php', data, function(json)
                     {
                      $('.restext').html(json);
                     }
                  );

                  return false;
               }
            );

            $(this).click(function()
               {

                  var contents = $('<div />');

                  if (!overlay)
                  {
                     overlay = $('<div id="opacity" />')
                     .css(
                        {
                        left: 0,
                        top: 0,
                        'z-index': 100,
                        width: $(window).width(),
                        height: $('.primary').height(),
                        position: 'absolute'
                        }
                     )
                     .appendTo(document.body);

                     overlay.append(
                        $('<img src="/t/img/white.gif" width="1" height="1" />')
                        .css(
                           {
                           left: 0,
                           top: 0,
                           width: '100%',
                           height: '100%'
                           }
                        )
                     );

                     overlay.find('img').css(
                        {
                        opacity: 0.8
                        }
                     );

                     $(document.body).click(function(e)
                        {
                           if ($(e.target).parents().index(slider.children('div')[0]) == -1)
                           {
                              overlay.hide();
                              slider.hide();
                           }
                        }
                     );
                  }

                  if (!slider)
                  {

                     contents.append(formDiv.show());

                     slider = $('<div />')
                     .css(
                        {
                        position: 'absolute',
                        'z-index': 101,
                        top: 0,
                        left: 0,
                        width: $(window).width()
                        }
                     )
                     .appendTo(document.body)
                     .append(
                        $('<div />')
                        .css(
                           {
                           margin: '15px 0 0',
                           marginLeft: ($(window).width() - width)/2,
                           backgroundColor: 'white',
                           width: width,
                           '-moz-box-shadow': '0 3px 5px #666666',
                           '-webkit-box-shadow': '0 3px 5px #666666',
                           'box-shadow': '0 3px 5px #666666',
                           borderBottom: $.browser.msie? '2px solid #cccccc': '',
                           borderRight: $.browser.msie? '2px solid #cccccc': '',
                           borderLeft: $.browser.msie? '1px solid #cccccc': '',
                           borderTop: $.browser.msie? '1px solid #cccccc': '',
                           padding: 15
                           }
                        )
                        .append(
                           $('<div style="position:absolute;margin:15px 0 0 500px; " />')

                           .append(
                              $('<span class="close" style="cursor:pointer;font-size:13px;color:#0862BC;border-bottom:1px dotted #0862BC">Закрыть</span>')
                           )
                        )
                        .append(contents)
                     );

                     slider.find('.close').click(function()
                        {
                           overlay.hide();
                           slider.hide();
                        }
                     );

                  }
                  else
                  {


                  }



                  overlay.show();
                  slider.show();

                  return false;
               }
            );

         }
      );

      $('.callback-trigger').click(function()
         {
            $('.callback').triggerHandler('click');
            $(window).scrollTop(0);
            return false;
         }
      );








   }
);
