$().ready(function(){
  
  re_position_box();
  
  $is_expanded = false;
  
  $("#maps").scrollview();
  
  $("li.map_link").hover(function(){
    $(this).animate({
      scrollLeft : 125
    }, 200).addClass('no_ani');
  }, function(){
    $(this).animate({
      scrollLeft : 0
    }, 200).removeClass('no_ani');
  });
  
  $(".stop_op").click(function(){
    close_expandables();
    $("#stop_op").slideDown(250);
    $("#stop_op .close").unbind();
    $("#stop_op .close").click(function(){
      $("#stop_op").slideUp(250);
    });
    return false;
  });
  
  $(".map_link").click(function(){
    close_expandables();
    var map = $(this).find('a').attr('map');
    $("#maps").slideDown(250);
    setTimeout(function(){
      $("#map_"+map).fadeIn(500);
      setTimeout(function(){
        $(".controls").show();
        $(".change_view").unbind();
        $(".close").unbind();
        $(".change_view").click(function(){
          if(map == "11a") {
            if($(this).css('background-image').match('zoom_in.png')) {
              $(this).css('background', 'url(assets/images/zoom_out.png)');
              $("#map_"+map).animate({width : 2000, height : 1416}, 250);
              $("#map_"+map+" div").animate({width : 500, height : 354}, 250);
            } else {
              $(this).css('background', 'url(assets/images/zoom_in.png)');
              $("#map_"+map).animate({width : 980, height : 692}, 250);
              $("#map_"+map+" div").animate({width : 245, height : 180}, 250);
            }
          } else {
            if($(this).css('background-image').match('zoom_in.png')) {
              $(this).css('background', 'url(assets/images/zoom_out.png)');
              $("#map_"+map).animate({width : 2000, height : 2000}, 250);
              $("#map_"+map+" div").animate({width : 500, height : 500}, 250);
            } else {
              $(this).css('background', 'url(assets/images/zoom_in.png)');
              $("#map_"+map).animate({width : 980, height : 980}, 250);
              $("#map_"+map+" div").animate({width : 245, height : 245}, 250);
            }
          }
        });
        $(".controls .close").click(function(){
          $(".controls").fadeOut(250);
          setTimeout(function(){
            $("#map_"+map).fadeOut(250);
            $(".change_view").css('background', 'url(assets/images/zoom_in.png)');
            setTimeout(function(){
              $("#map_"+map+" img").animate({width : "100%"}, 0);
              $("#maps").slideUp(250);
            }, 250);
          });
        });
      }, 500);
    }, 250);
    return false;
  });
  
  $(".rejseplanen").click(function(){
    close_expandables();
    $("#the_rejseplanen").show(250);
    $("#the_rejseplanen .close").unbind().click(function(){
      $("#the_rejseplanen").hide(250);
    });
  });
    
  $(".expandable").click(function(){
    close_expandables();
    var href = $(this).find('a').attr('href'),
    ww = $(this).find('a').attr('w'),
    hh = $(this).find('a').attr('h');
    
    var offset = $(this).offset();
    
    var offset_left = offset.left-$("#container").offset().left;
    var offset_top = offset.top-$("#container").offset().top-119;
    
    $.get(href, function(data) {
      data = data+"<p class=\"close\" onclick=\"javascript: close_expandables();\">Close</p>";
      if(offset_left >= 500) {
        var expandable_element = $("<div />").css({
          'top' : offset_top+119,
          'left' : offset_left+125,
          'background' : "#eee",
          'width' : 1,
          'height' : 1,
          'position' : 'absolute',
          'z-index' : 99
        }).attr({
          'id' : 'expandable_element'
        }).html(data);
        $("#content").append(expandable_element);
        expandable_element.animate({
          width : ww-30,
          height : hh-30,
          left : offset_left-125,
          padding : 15
        }, 250);
      } else {
        var expandable_element = $("<div />").css({
          'top' : offset_top+119,
          'left' : offset_left,
          'background' : "#eee",
          'width' : 1,
          'height' : 1,
          'position' : 'absolute',
          'z-index' : 99
        }).attr({
          'id' : 'expandable_element'
        }).html(data);
        $("#content").append(expandable_element);
        expandable_element.animate({
          width : ww-30,
          height : hh-30,
          left : offset_left,
          padding : 15
        }, 250);
      }
    });
        
    return false;
  });
  
});

$(window).load(function(){
  
  re_position_box();
  
  if(!$.browser.msie) {
    $("#objects .image").css({
      opacity: 0.5
    }).hover(function(){
      $(this).fadeTo(100, 1);
    }, function(){
      $(this).fadeTo(100, 0.5);
    });
  }

  $("#loading").fadeOut(500);
  
  setTimeout(function(){
    var ii = 0;
    $(".object").each(function(){
      ii+=1;
      var el = $(this);
      setTimeout(function(){
        el.fadeIn(150);
      },ii*40);
    });
  }, 500);
  
  setInterval(function(){
    slide_lines($(".line_info"));
  }, 3000);
  
  //setTimeout(function(){
  //  $("li.map_link").each(function(){
  //    var el = $(this);
  //    setInterval(function(){
  //      ani_maplink(el);
  //    }, rand(2500, 5000));
  //  });
  //}, 2100);
  
});

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

function close_expandables() {
  if($("#expandable_element").length != 0) {
    var el = $("#expandable_element");
    if(el.offset().left > 500) {
      el.animate({
        width : 0,
        height : 0,
        padding : 0,
        left : "+=250"
      }, 250);
    } else {
      el.animate({
        width : 0,
        height : 0,
        padding: 0
      }, 250);
    }
    setTimeout(function(){
      el.remove();
    }, 250);
  };
  $("#the_rejseplanen").hide(250);
}

function re_position_box() {
  
  var ww = $(window).width();
  var hh = $(window).height();
  
  if(hh < 649 || ww < 1024) {
    $("#container").css({
      'width' : '1000px',
      'height' : '625px',
      'margin' : '12px auto',
      'position' : 'relative',
      'left' : 'auto',
      'top' : 'auto'
    });
  } else {
    $("#container").css({
      'position' : 'absolute',
      'top' : '50%',
      'left' : '50%',
      'margin' : '-312px 0 0 -500px'
    });
  } 
}

function ani_maplink(el) {
  if(!el.hasClass('no_ani')) {
    if(el.scrollLeft() == 125) {
      var ss = 0;      
    } else {
      var ss = 125;
    }
    el.animate({
      scrollLeft : ss
    }, 150);
  }
}

function rand (min, max) {
  var argc = arguments.length;
  if (argc === 0) {
    min = 0;
    max = 2147483647;
  } else if (argc === 1) {
    throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
  }
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function slide_lines(el) {
  if(el.find('div').offset().left > 5) {
    el.animate({
      scrollLeft : "+=125"
    }, 150);
  } else {
    el.animate({
      scrollLeft : 0
    }, 150);
  }
}
