$(document).ready(function() {
	$("a.sg_show_product").click(function(event){
		var href = this.href;
		$.dimScreen(500, 0.6, function() {
		    $('#sg_content').fadeIn("fast", function(){
					$('#sg_content').load(href + " #sg_product", function(){
						$('#sg_close').fadeIn("fast");
					});
				});
		});
		event.preventDefault();
	});
	
	$("a.sg_close").click(function(event){
	  $('#sg_close').fadeOut("fast", function(){
		  $('#sg_content').fadeOut("fast", function(){
			$.dimScreenStop(sg_empty);
			});
		});
		event.preventDefault();
	});
	
	$(window).scroll(function() {
	  $('div#sg_content').animate({
			marginTop: ($(window).scrollTop() - 200) + 'px'
		},{queue: false, duration: 200});
	  $('div#sg_close').animate({
			marginTop: ($(window).scrollTop() - 195) + 'px'
		},{queue: false, duration: 200});
	  $('div#__dimScreen').animate({
			top: $(window).scrollTop() + 'px'
		},{queue: false, duration: 200});
	});

	$("a.fv_show_map").click(function(event){
		var href = this.href;
		$.dimScreen(500, 0.6, function() {
		    $('#fv_map_content').fadeIn("fast", function(){
					$('#fv_map_content').load(href + " #fv_map_container", function(){
						$('#fv_map_close').fadeIn("fast");
					});
				});
		});
		event.preventDefault();
	});
	
	$("a.fv_map_close").click(function(event){
	  $('#fv_map_close').fadeOut("fast", function(){
		  $('#fv_map_content').fadeOut("fast", function(){
			$.dimScreenStop(fv_map_empty);
			});
		});
		event.preventDefault();
	});
	
	$(window).scroll(function() {
	  $('div#fv_map_content').animate({
			marginTop: ($(window).scrollTop() - 200) + 'px'
		},{queue: false, duration: 200});
	  $('div#fv_map_close').animate({
			marginTop: ($(window).scrollTop() - 195) + 'px'
		},{queue: false, duration: 200});
	  $('div#__dimScreen').animate({
			top: $(window).scrollTop() + 'px'
		},{queue: false, duration: 200});
	});

  $('a[rel="external"]').click(function (e) {
    e.preventDefault();
    window.open($(this).attr('href'));
  });

	function sg_empty() {
		document.getElementById("sg_content").innerHTML = "";
	}

	function fv_map_empty() {
		document.getElementById("fv_map_content").innerHTML = "";
	}
	
	$('.gallery-fancybox').each(function () {
	  var g = $(this);
	  var a = g.find('.images a.image');
	  var rand = Math.floor(Math.random()*101);
	  a.attr('rel', 'gallery-' + rand);
	  a.fancybox({
	    transitionIn: 'elastic',
      transitionOut: 'elastic'
	  });
	});
	
	$('.gallery').each(function () {
	  var g = $(this);
	  var container = g.find('.thumbs-container:first');
	  var thumbs = container.find('.thumbs:first');
	  var thumbs_a = thumbs.find('a.thumb');
	  
	  thumbs.css('width', (80 * thumbs_a.length) + 'px')
	  
	  if (thumbs_a.length > 0) {
	    var image = g.find('.image');
	    var first = thumbs.find('a.thumb').get(0);
	    first = $(first);
	    first.addClass('active');
	    
	    var w = image.width();
	    var h = Math.ceil(w/1.5);
	    image.css('height', h + 'px');
	    var src = '/admin/public/getimage.aspx?Height=' + h + '&Resolution=72&Compression=90&Image=' + first.attr('href');
	    
      $.get(src, function () {
        image.append('<img class="large-image" src="' + src + '"/>');
        image.removeClass('hide');
        g.find('.loader').hide();
      });
	    
	    if (thumbs.width() > container.width()) {
	      g.find('a.next').removeClass('hide');
	    }
   }
	});
	
	$('.gallery a.thumb').click(function (e) {
	  e.preventDefault();
	  var a = $(this);
	  var g = a.parents('.gallery:first');
	  
    g.find('a.thumb').removeClass('active');
    a.addClass('active');
    
	  var loader = g.find('.loader');
	  var image = g.find('.image');
	  var img = image.find('img.large-image:first');
    var w = image.width();
    var h = Math.ceil(w/1.5);
    var src = '/admin/public/getimage.aspx?Height=' + h + '&Resolution=72&Compression=90&Image=' + a.attr('href');
	  
	  if (img.length > 0) {
      loader.show();
	    $.get(src, function () {
	      img.attr('src', src);
	      loader.hide();
	    });
    }
    
	});
	
	$('.gallery a.next, .gallery a.prev').not('.disabled').click(function (e) {
	  e.preventDefault();
	  
    var a = $(this);
    a.addClass('disabled');
    var g = a.parents('.gallery:first');
    
    var container = g.find('.thumbs-container:first');
    var thumbs = g.find('.thumbs:first');
    
    if (a.hasClass('next')) {
      var other = g.find('a.prev');
      var left = '-=' + container.width();
      var curr_left = parseInt(thumbs.css('left'), 10);
      if ( ((curr_left * -1) + (container.width() * 2)) >= thumbs.width() ) {
        a.addClass('hide');
      }
    } else {
      var other = g.find('a.next');
      var left = '+=' + container.width();
      var curr_left = parseInt(thumbs.css('left'), 10);
      if (curr_left + container.width() >= 0) {
        a.addClass('hide');
      }
    }
    
    thumbs.animate({
      left: left
    }, 600, function () {
      a.removeClass('disabled');
      other.removeClass('hide');
    });
	});
	
	
	if ($('#home-top-image').length > 0) {
    $('#home-top-image').crossSlide({
      sleep: 2,
      fade: 0.7
    }, [
      { src: '/Files/Billeder/Groenttorvet/slideshow/slide_1.jpg' },
      { src: '/Files/Billeder/Groenttorvet/slideshow/slide_2.jpg' },
      { src: '/Files/Billeder/Groenttorvet/slideshow/slide_3.jpg' },
      { src: '/Files/Billeder/Groenttorvet/slideshow/slide_4.jpg' },
      { src: '/Files/Billeder/Groenttorvet/slideshow/slide_5.jpg' }
    ]);
  } 
});

function ipaper_height() {
	var iw = $("div.ipaper").width();
	var ih = $("div.ipaper").height();
	var nh = (Math.ceil($("div.ipaper").width()/1.41)) + 60;
	$("div.ipaper").css("height", nh + "px");
}

