/*jslint browser: true, undef: true, nomen: true, eqeqeq: true, bitwise: true, regexp: true, newcap: true, immed: true */
/*global $ */
var disableSlideshow = false;
$(function() {

  $("#intro").each(function(){
    $("body, html").css({
      width:"100%",
      height:"100%"
    });
    $(this).css({width:"100%", height:"100%"}).html("").flash({
      src: "/Content/intro.swf",
      width: "100%",
      height: "100%",
      wmode: "transparent"
    });
  });

  $("#columnTitle").each(function(){
    var $this = $(this);
    $this.flash({
      src: "/Content/title.swf",
      width: $this.width(),
      height: $this.height(),
      wmode: "transparent",
      flashvars: {
        text: $this.text()
      }
    });
  });

  $("#imageList img").click(function(){
    if($(this).hasClass("selected")) return;
    $("#imageList img.selected").removeClass("selected");
    var $this = $(this).addClass("selected");
    var $image = $("#image");
    var duration = $(".bigimage").is(":visible") ? "0" : "fast";
    $image.stop().fadeTo(duration, 0.1, function(){
      var f = function(){
        $image.fadeTo(duration, 1);
        f = function(){};
      };
      if($this.attr("data-mediumsrc").indexOf(".flv") > 0){
        disableSlideshow = true;
        video = $this.attr("data-mediumsrc");
        video = video.replace("/402x230/", "/mellan/");
        video = video.replace("/480x276/", "/mellan/");
        $("#imageoverlay").html('<div id="video"></div>');
        window.videocompleted = function(){
          disableSlideshow = false;
        };
        $("#video").flash({
          src: "/Content/video.swf",
          width: 410,
          height: 233,
          wmode: "transparent",
          flashvars: {
            videosrc: video
          }
        });
      }else{
        $("#imageoverlay").html("");
        var $img = $image.html('<img src="' + $this.attr("data-mediumsrc") + '" alt="">').find("img").attr("data-largesrc", $this.attr("data-largesrc")).load(f);
        setTimeout(function(){
          if($img.height() > 2) { f(); }
        }, 100);
      }
      var newPosition = 0 - $this.position().left + $("#images").width() / 2;
      var $last = $("#imageList img:last");
      var limit = $("#images").width() - $last.position().left - $last.width() - 2;
      if(newPosition < limit) newPosition = limit;
      if(newPosition > 0) newPosition = 0;
      $("#imagesinner").stop().animate({left: newPosition + "px"}, {duration: "fast"});
    });
  });


  setInterval(function(){
    if(disableSlideshow) return;
    var $current = $("#images img.selected");
    if($current.length > 0) {
      if($current.attr("data-mediumsrc").indexOf(".flv") > 0) return;
    }
    $("#nextImage").click();
  }, 2500);

  $("#columnImages").hover(function(){
    disableSlideshow = true;
  }, function(){
    disableSlideshow = false;
  });

  
  (function(){
    var f = function(i) {
      if(document.selection && document.selection.empty){
        document.selection.empty() ;
      } else if(window.getSelection) {
        var sel = window.getSelection();
        if(sel && sel.removeAllRanges) sel.removeAllRanges() ;
      }

      var $img = $("#imageList img.selected");
      if(i > 0) {
        $img = $img.nextAll("img:first");
        if($img.length === 0) $img = $("#imageList img:first");
      }
      if(i < 0) {
        $img = $img.prevAll("img:first");
        if($img.length === 0) $img = $("#imageList img:last");
      }
      $img.click();
    };
    $("#nextImage").click(function() {
      f(1);
    });
    $("#prevImage").click(function() {
      f(-1);
    });
  })();

  $("#imagesinner img:first").click();
  
  $("#menucontainer li a").hover(function(){
    $("#pageName").text($(this).text());
  }, function(){
    $("#pageName").text("");
  });

  $("#submenuinnerinnercontainer .submenuarchivegroup:first").hide().nextAll(".submenugroup").hide();

  $(".submenuarchive:last").each(function(){
    if($(this).closest(".submenugroup").nextAll(".submenuarchivegroup:first").length === 0) $(this).hide();
  });

  $(".submenuarchive").click(function(){
    $(".submenugroup").hide();
    $(this).closest(".submenugroup").nextAll(".submenuarchivegroup:first").show().nextUntil(".submenuarchivegroup").show();
  });

  $("#submenuinnerinnercontainer a.selected").each(function(){
    $(".submenugroup").hide();
    var $ul = $(this).closest("ul");
    if(!$ul.is(".submenuarchivegroup, .submenugroupfirst")) $ul = $ul.prevAll(".submenuarchivegroup, .submenugroupfirst").slice(0,1);
    $ul.show().nextUntil(".submenuarchivegroup").show();
  });

  $("#imageoverlay").click(function(){
    $(".bigimage img").attr("src", $("#imagesinner img.selected").attr("data-largesrc"));
    $(".page").fadeOut("normal", function(){
      pause = true;
      disableSlideshow = false;
      $(".bigimage").fadeIn();
    });
  });


  $(".bigimage").click(function(){
    $(".bigimage").fadeOut("normal", function(){
      pause = false;
      disableSlideshow = true;
      $(".page").fadeIn("normal");
    });
  });


  $("#blogpostsinnerinner").each(function(){
    var x = 0;
    var origx = $("#blogpostsinnerinner span:first").offset().left;
    var $repeater = $("#blogpostsinnerinner span.repeater");
    setInterval(function(){
      x -= 1;
      $("#blogpostsinnerinner").css("left", x);
      if ($repeater.offset().left < origx){
        x = 0;
      };
    }, 25);
  });

  $("#bigimageprev").click(function() {
    $("#prevImage").click();
    $(".bigimage img").attr("src", $("#imagesinner img.selected").attr("data-largesrc"));
    return false;
  });

  $("#bigimagenext").click(function() {
    $("#nextImage").click();
    $(".bigimage img").attr("src", $("#imagesinner img.selected").attr("data-largesrc"));
    return false;
  });

});