$(document).ready(function () {

    if ($("#gallery").length) {
        slideShow();
    }


    $("#Table41").parent().addClass("rightmenu");
    $("#Table41").removeAttr("style");
    $("#Table41").css({ 'width': '166px' });
    $("#ShopLayOut_Basket_TD").removeAttr("style");
    $('#productsLogo').insertBefore($('#Table39'));

    $("#vareinfo .itemcontainer").each(
  function (intIndex) {

      if ((intIndex + 1) % 3 == 0) {
          $(this).css("margin-right", "0px");
      }
      var save = $(this).find(".hiddenInfo").find(".active").text();
      var hor;
      var ver;

      //alert(imgH + ' ' + imgW);

      if (save != '') {
          hor = $(this).find(".hiddenInfo").find(".hor").text();
          ver = $(this).find(".hiddenInfo").find(".ver").text();

          var imgH = 168;
          var imgW = 168;

          if (imgH == '' || imgW == '') {
              hor = '';
              ver = '';
          }

          $(this).find("div:first").addClass("spar" + save);

          if (hor != '')
              $(this).find("div:first").css({ 'left': imgW + 10 - 96 });

          if (ver != '')
              $(this).find("div:first").css({ 'top': imgH + 10 - 96 });


          $(this).find("div:first").css({ 'visibility': 'visible' });
      }
  }
);

    $(".price").each(
    function (intIndex) {
        if ($(this).html() == 'Tilbudspris&nbsp;&nbsp;DKK')
            $(this).css("display", "none");


    });

    //Knapper på Orderstep1
    var isOnOrder1 = $("#ShopLayOut_Content_TD .SubmitButton_Orderstep1");
    if (isOnOrder1.length == 2) {
        $(isOnOrder1[0]).addClass("hentButton");
        $(isOnOrder1[1]).addClass("videreButton");
    }
    if (isOnOrder1.length == 1) {
        $(isOnOrder1[0]).addClass("videreButton");
    }


    $("#ProductMenu_Table").find(".RootCategory_Productmenu:first").css("background-image", "none");

    $(".SubCategory1_Productmenu:first").css("padding-top", "6px");
    $(".SubCategory1_Productmenu:last").css("padding-bottom", "6px");

    $(".SubCategory1_Productmenu a").each(
    function (intIndex) {
        var link = $(this).text();
        if (link.length > 0) {
            if (link.substring(0, 1) == '-') {
                $(this).text(link.substring(1, link.length));
            }
        }

    });

    $(".price").find("strike").each(
    function (intIndex) {
        $(this).parent().addClass("before");
    });

    if ($(".VariantGroup").length == 0)
        $("#customVariant").css("display", "none");

});

$(document).ready(function(){

   
	$.ajax({
		type: "GET",
		url: "frontpage.html?rawxml=1",
		dataType: "xml",
		success: function(xml) {
		    $("#RecentWrap div").remove();
		    $("#MostSoldWrap div").remove();
			$(xml).find('ProductLastAdded').each(function(){
				var number = $(this).find('NUMBER').text();
				var url = $(this).find('ID').text();
				var name = $(this).find('PRODNAME').text();
				var urlArr = url.split("/");
				url = urlArr[urlArr.length-1];
				if(number < 6) {				
				    $('<div><a href="'+url+'" class="link">'+name+'</a></div><div style="height:6px;overflow:hidden;"></div>').appendTo('#RecentWrap');
				}
			});

			
			$(xml).find('ProductMostSold').each(function(){
				var number = $(this).find('NUMBER').text();
				var url = $(this).find('ID').text();
				var name = $(this).find('PRODNAME').text();
				var urlArr = url.split("/");
				url = urlArr[urlArr.length-1];
				if(number < 6) {				
				    $('<div><a href="'+url+'" class="link">'+name+'</a></div><div style="height:6px;overflow:hidden;"></div>').appendTo('#MostSoldWrap');
				}
			});			
		}
	});
}); 


function slideShow() {

	//Set the opacity of all images to 0
	$('#gallery a').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('#gallery a:first').css({opacity: 1.0});
	
	//Set the caption background to semi-transparent
	$('#gallery .caption').css({opacity: 0.7});

	//Resize the width of the caption according to the image width
	//$('#gallery .caption').css({width: $('#gallery a').find('img').css('width')});
	$('#gallery .caption').css("width","220px");
	
	//Get the caption of the first image from REL attribute and display it
	$('#gallery .content').html($('#gallery a:first').find('img').attr('rel'))
	.animate({opacity: 1.0}, 400);
	
	//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('gallery()',6000);
	
}

function gallery() {
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#gallery a.show')?  $('#gallery a.show') : $('#gallery a:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#gallery a:first') :current.next()) : $('#gallery a:first'));	
	
	//Get next image caption
	var caption = next.find('img').attr('rel');	
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	$('#gallery .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:400 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
	$('#gallery .caption').animate({opacity: 0.7},100 ).animate({height: '90px'},550 );
	
	//Display the content
	$('#gallery .content').html(caption);
	
	
}

