
var Changer = function(){

  var object = this,
    timer,
    $changerContainer,
    $changerSlogan,
    $changerList,
    $changerUp,
    $changerDown,
    $changerImages,
    changerHeight,
    lock = false,
    duration = 400; // "normal"

  this.next = function(){
    moveDown();
  };
  this.previous = function(){
    moveUp();
  };
  this.start = function(){
    this.stop();
    timer = setInterval(this.next, 10000);
  };
  this.stop = function(){
    clearInterval(timer);
  };
  this.restart = function(){
    this.start();
  };
  this.initialize = function(){

    $changerContainer = $("#changerContainer");
    $changerSlogan = $("#changerSlogan", $changerContainer);
    $changerList = $("#changerList", $changerContainer);
    $changerUp = $("#changerUp", $changerContainer);
    $changerDown = $("#changerDown", $changerContainer);
    $changerImages = $("#changerImages");
    changerHeight = $changerList.outerHeight();

    $changerList.css("overflow", "hidden");
    addIndexes();

    switch($("li", $changerList).length){
      case 0:
      case 1:
        $changerUp.hide();
        $changerDown.hide();
        return this;
      case 2:
      case 3:
        fillList();
      default:
        fixPositions();
        addScroll();
    }

    $changerUp.click(function(){
      object.next();
    });

    $changerDown.click(function(){
      object.previous();
    });
  };

  function addIndexes(){
    $("li", $changerList).each(function(index){
      $(this).attr("index", index);
    });
  }

  function fillList(){
    $("li", $changerList).clone()
      .removeClass("active")
      .find(".activeBg").hide().end()
      .appendTo($changerList);
  }

  function fixPositions(){ // change positioning to absolute
    $("li", $changerList).each(function(index){
      var top = $(this).position().top + (index * $(this).outerHeight(true));
      if(index > 2){
        top = changerHeight;
      }
      $(this).css("top", top).css("position", "absolute");
    });
  }

  function changeImage($img, condition){
    if(condition){
      $img.show(0, function(){
        $("img.active", $changerImages).removeClass("active").fadeOut(duration * 1.5, function(){
          $img.addClass("active");
        });
      });
    } else {
      $img.fadeIn(duration * 1.5, function(){
        $("img.active", $changerImages).removeClass("active").hide(0, function(){
          $img.addClass("active");
        });
      });
    }
  }

  function addScroll(){
    $changerList.mousewheel(function(event, delta) {
      if(delta > 0){
        moveDown();
      } else if(delta < 0){
        moveUp();
      }
      return false;
    });
  }

  function moveUp(){
    if(lock) return;
    lock = true; // lock
    object.restart();
    var $current = $("li.active", $changerList).removeClass("active").addClass("current"),
      distance = $current.outerHeight(true),
      speed = distance / duration,
      paddingTop = parseInt($changerList.css("padding-top")),
      paddingBottom = changerHeight - (distance * 3 + paddingTop),
      index = $current.next().attr("index");
    $current.find(".activeBg").fadeOut(duration);
    $("li:eq(3)", $changerList).animate({top: "-="+paddingBottom}, paddingBottom / speed, "linear", function(){
      $("li.active", $changerSlogan).removeClass("active", duration); // hide slogan
      $img = $("img:eq("+index+")", $changerImages);
      changeImage($img, !$img.prev().length); // change image
      $("li:lt(4)", $changerList).animate({top: "-="+distance}, duration, "linear", function(){
        $("li.current", $changerList).removeClass("current")
          .animate({top: (distance * -1)}, ($current.position().top + distance) / speed, "linear", function(){
            $(this).appendTo($changerList).css("top", changerHeight);
            $("li:eq("+index+")", $changerSlogan).addClass("active", duration); // show slogan
          }).next().addClass("active").find(".activeBg").fadeIn(duration, function(){
            lock = false; // unlock
          });
      });
    });
  }

  function moveDown(){
    if(lock) return;
    lock = true; // lock
    object.restart();
    var $active = $("li.active", $changerList).removeClass("active"),
      $last = $("li:last", $changerList),
      distance = $active.outerHeight(true),
      speed = distance / duration,
      paddingTop = parseInt($changerList.css("padding-top")),
      paddingBottom = changerHeight - (distance * 3 + paddingTop),
      index = $last.attr("index");
      $("li:eq(2)", $changerList).addClass("current");
    $active.find(".activeBg").fadeOut(duration);
    $last.prependTo($changerList)
      .css("top", (distance * -1))
      .animate({top: "+="+ paddingTop}, paddingTop / speed, "linear", function(){
        $("li.active", $changerSlogan).removeClass("active", duration); // hide slogan
        $img = $("img:eq("+index+")", $changerImages);
        changeImage($img, $img.next().length); // change image
        $("li:lt(4)", $changerList).animate({top: "+="+distance}, duration, "linear", function(){
          $("li.current", $changerList).removeClass("current")
            .animate({top: "+="+paddingBottom}, paddingBottom / speed, "linear", function(){
            $("li:eq("+index+")", $changerSlogan).addClass("active", duration); // show slogan
            $last.addClass("active").find(".activeBg").fadeIn(duration, function(){
              lock = false; // unlock
            });
          });
        });
      });
  }
}

var Realisation = function(){
  var lock = false,
    $realisations;

  this.initialize = function(){
    $realisations = $("#realisations .content");
    $("#realisationsLeft").click(moveLeft);
    $("#realisationsRight").click(moveRight);

    $realisations.mousewheel(function(event, delta) {
      if(delta > 0){
        moveLeft();
      } else if(delta < 0){
        moveRight();
      }
      return false;
    });
  }

  function moveLeft(){
    if(lock) return;
    lock = true;
    $active = $("li.active", $realisations);
    $prev = $active.prev();
    if($prev.length){
      $prev.show(0, function(){
        $active.fadeOut("normal", function(){
          lock = false;
        }).removeClass("active");
      }).addClass("active");
    } else {
      $("li:last", $realisations).fadeIn("normal", function(){
        $active.hide().removeClass("active");
        lock = false;
      }).addClass("active");
    }
  }

  function moveRight(){
    if(lock) return;
    lock = true;
    $active = $("li.active", $realisations);
    $next = $active.next();
    if($next.length){
      $next.fadeIn("normal", function(){
        $active.hide().removeClass("active");
        lock = false;
      }).addClass("active");
    } else {
      $("li:first", $realisations).show(0, function(){
        $active.fadeOut("normal", function(){
          lock = false;
        }).removeClass("active");
      }).addClass("active");
    }
  }

}

//var News = function(){
//  var object = this,
//    duration = 4000;
//
//  this.start = function(){
//    this.stop();
//    timer = setInterval(this.next, 10000);
//  };
//  this.stop = function(){
//    clearInterval(timer);
//  };
//  this.pause = function(){
//    // pause on hover
//  };
//
//  this.initialize = function(){
//
//  }
//}

