﻿$(function(){

    var  $l_container = $(".l-container");
    var win_width = $(".l-container").width();
    $(".home-slider > div > div").css("width", win_width+"px");

    function window_resize() {
        var win_width = $(".l-container").width();
        $(".home-slider > div > div").css("width", win_width+"px");

        var body_height = $(window).height();
        var content_height = $l_container.height();
        if (content_height < body_height ) {
            $l_container.css("minHeight", body_height+"px");
        }

      if ( $(window).width() < 1050 ) {
        $l_container.addClass("l-container-min")
      }
      else {
        $l_container.removeClass("l-container-min")
      }
    }

    window_resize();

    $(window).resize(function(){
        window_resize();
    });


    //шаги
    var steps = function(){

      var $page = $("#page"),
          $steps = $(".steps", $page),
          $step_tabs = $(".step-tabs", $steps),
          $tab = $("li", $step_tabs ),
          $tab_contents = $(".tabs-content", $steps),
          $item_content = $(" > div", $tab_contents),
          $button = $("button");

          $tab.click(function(){
            if ( $(this).is(".act") ) {}
            else {
                var num = $(this).attr("step-num");
                $tab.removeClass("act");
                $(this).addClass("act");
                $item_content.hide();
                $item_content.eq(num).show();
            }
            if ( $steps.is(".steps-house") ) {
              $page.attr("class","step"+num+"-house-bg");
            }
            else {
              $page.attr("class","step"+num+"-office-bg");
            }
            return false;
          });

          $button.click(function(){
            $(this).closest($item_content).hide().next().show();
            var $act_step = $(".act", $step_tabs),
                num = $act_step.next().attr("step-num");

            $act_step.removeClass('act').next().addClass("act");
            if ( $steps.is(".steps-house") ) {
              $page.attr("class","step"+num+"-house-bg");
            }
            else {
              $page.attr("class","step"+num+"-office-bg");
            }
          });

    }

    var num_random = function(el, sec) {
      var rand = Math.round(Math.random() * 9);
      this.ref = function(){ el.attr("class","c"+rand);    }
      var rands = Math.round(Math.random() * 1000);
      setTimeout(this.ref, sec);
    }
    //число подключившихся
    var abonents_size = function(){
      var $abonents = $(".abonents-size"),
          $num = $("span", $abonents);

      $num.each(function(){
        var n = $(this).html();
        var m = $(this);
        for ( i=0; i< 4500; i=i+100) {
          num_random(m, i)
        }

        this.ref1 = function(){ m.attr("class","c"+n)    }
        setTimeout(this.ref1, 4500);
      });
    }

    var page_loader = function() {

        var $top_menu_border = $(".top-menu-border"),
            $top_menu = $(".top_menu"),
            $logo = $(".logo"),
            $enter = $(".enter"),
            $l_content = $(".l-content"),
            $contacts_block = $(".contacts_block");

            //блог главной страницы
        var $home = $("#home"),
            $h2 = $("h2", $home),
            $arrows = $(".arrows"),
            $h3 = $("h3", $home),
            $news = $(".news", $home);

 /*       if ($.browser.msie && parseInt($.browser.version) < 9){
            $top_menu_border.show();
            $enter.show();
            $top_menu.show();
            $logo.show();
            $contacts_block.show();
            $l_content.css("visibility","visible");
            $h2.show();
            $arrows.show();
            $h3.show();
            $news.show();
        }
        else {   */
          $top_menu_border.animate({opacity: 'show'}, 500);
          $enter.animate({opacity: 'show'}, 500);
          $top_menu.animate({opacity: 'show'}, 500, function(){
              $logo.animate({opacity: 'show'}, 500, function(){
                  $contacts_block.animate({opacity: 'show'}, 500, function(){
                    //показываем контент
                    setTimeout(function() {
                      $l_content.css("visibility","visible");
                      $h2.animate({opacity: 'show'}, 500, function(){
                        $arrows.animate({opacity: 'show'}, 500, function(){
                          $h3.animate({opacity: 'show'}, 500);
                          //$news.animate({opacity: 'show'}, 500);
                          //показываем новости по очереди, код жесть но некогда )
                          $("li",$news).eq(0).animate({opacity: 'show'}, 500,function(){
                            $("li",$news).eq(1).animate({opacity: 'show'}, 500,function(){
                                $("li",$news).eq(2).animate({opacity: 'show'}, 500,function(){
                                    $("li",$news).eq(3).animate({opacity: 'show'}, 500,function(){
                                        $("li",$news).eq(4).animate({opacity: 'show'}, 500,function(){

                                        });
                                    });
                                });
                            });
                          });
                        });
                      });

                    }, 500);
                  });
              });
          });
      /*  }  */


    }


    //при загрузке страницы
    steps();
    abonents_size();
    page_loader();

    /*
    var home_slider = function(){

        var $page = $("#page");
            $home_slider = $(".home-slider"),
            $item = $(" > div", $home_slider),
            $arrows = $(".arrows", $item);
            $arrow = $("a", $arrows),
            me = this;

        this.slider = function(element){
          $item.hide();
          //alert(element) ;
          $page.attr("class", element+"-bg")
          $("#"+element).animate({opacity: 'show'}, 800);
          abonents_size();
        }

        $arrow.click(function(){
            me.slider($(this).attr("target"));
            return false;
        });
    }
    */

    var home_slider = function(){

        var $page = $("#page");
            $home_slider = $(".home-slider"),
            $item = $(" > div", $home_slider),
            $arrows = $(".arrows", $item),
            $arrow_left = $(".left a", $arrows),
            $arrow_right = $(".right a", $arrows),
            me = this;

        this.slider_right = function(element){
          var $qqq = $(" > div > div:visible", $home_slider);
          $page.attr("class", element+"-bg");
          $("#"+element).prependTo($(".home-slider2")).css("left", "-"+win_width+"px").show().animate({left: '0'}, 500);
          $qqq.css("left", "0px").animate({left: win_width+'px'}, 500, function(){ $(this).hide() });
          abonents_size();
        }

        this.slider_left = function(element){
          var $qqq = $(" > div > div:visible", $home_slider);
          $page.attr("class", element+"-bg");
          $("#"+element).appendTo($(".home-slider2")).css("left", win_width+"px").show().animate({left: '0'}, 500);
          $qqq.css("left", "0px").animate({left: "-"+win_width+'px'}, 500).hide();
          abonents_size();
        }

        $arrow_right.click(function(){
            me.slider_right($(this).attr("target"));
            return false;
        });

        $arrow_left.click(function(){
            me.slider_left($(this).attr("target"));
            return false;
        });
    }

    home_slider();


    //всплывающие окна
    var $popup = $(".popup"),
        $page_override = $(".page-override"),
        $auth_form = $(".auth-form"),
        $feedback_form = $(".feedback-form"),
        $contacts = $(".contacts"),
        $popup_close = $(".popup-close"),
        $enter = $(".enter"),
        $contacts_link = $(".contacts-link"),
        $message_link = $(".message-link");

    var popup_open = function(element){
        $page_override.show();
        $popup.show();
        element.show();
    }
    var popup_close = function(){
        $page_override.hide();
        $auth_form.hide();
        $contacts.hide();
        $feedback_form.hide();
    }

    $popup_close.click(function(){
      popup_close();
    });

    $page_override.click(function(){
      popup_close();
    });

    $enter.click(function(){
        popup_open($auth_form);
    });

    $contacts_link.click(function(){
        popup_open($contacts);
    });

    $message_link.click(function(){
        popup_open($feedback_form);
    });

    var tatif_type_switcher = function() {
      var $tarif_switch = $(".tarif-switch"),
          $li = $("li", $tarif_switch),
          $link = $("a", $tarif_switch);

      $link.click(function(){
        if ( $(this).parent().hasClass("act") ) {}
        else {
            $li.removeClass("act");
            $(this).parent().addClass("act");
            $(".calc-tarifs, .see-tarifs").hide();

            var data_type = $(this).attr("data-type");
            $("."+data_type).show();
        }
        return false;
      });
    }

    tatif_type_switcher();


    



});
