// JavaScript Document
$(document).ready(function () {

   if ($('#productList').html() != undefined) {
      $('h1', this).after($('.eComText').html());
   }

   if ($('#topVideoContainer').html() != undefined) {
      if ($.browser.msie) {
         var flashvars = {};
         flashvars.flvUrl = "/files/System/Invita/flash/videoPlayer2011/INVITA_01_Nyhed_og_intro_pris_web.mp4";
         flashvars.imageUrl = "/files/System/Invita/flash/videoPlayer2011/videoBanner2011.jpg";
         var params = {
            scale: "noScale",
            allowScriptAccess: "always",
            wmode: 'transparent'
         };
         var attributes = {};
         attributes.id = "myVideo";
         swfobject.embedSWF("/files/system/invita/flash/videoplayer/Organiskvideo.swf", "video", "728", "500", "9.0.0", "/files/system/invita/flash/resource/expressInstall.swf", flashvars, params, attributes);

         function expandVideo(h) {
            w = 975;
            h = 546;
            $('#myVideo').attr({ 'height': h, 'width': w });
         };

         function closeVideo() {
            h = 500;
            w = 728;
            $('#myVideo').attr({ 'height': h, 'width': w });
         };
      }
      else {
         $('#video').append('<img src="/files/System/Invita/flash/videoPlayer2011/videoBanner2011.jpg" alt="" />');
         $('#video').click(function () {
            play();
         });

         $('.btnClose').click(function () {
            close();
         });
         $("video").bind("ended", function () {
            close();
         });

         function close() {
            $('video')[0].pause();
            $('#HTMLvideo').fadeOut('300');
            $('.btnClose').hide();
         }
         function play() {
            $('#HTMLvideo').fadeIn('300');
            $('#HTMLvideo').get(0).play();
            $('.btnClose').fadeIn('300');
         }

      }
   }

   if (navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/BlackBerry/)) {
      $("#mainNav").children(".hasMegaSub").click(function () {

         if ($(this).hasClass("active") == true) {
            megaClose();
         } else {

            megaClose();

            $(this).children(".megaSub").stop(true, true).slideDown(200, 'easeOutBack');
            $("#whiteOverlay").stop().fadeTo(200, .8);

            var o = 400;
            var d = 200;

            if ($.browser.msie) {
               $(this).find(".megaSubItem, .megaTeaserPlain, .megaSubSub").show();
            } else {
               $(this).find(".megaSubItem, .megaTeaserPlain, .megaSubSub").each(function () {
                  $(this).stop(true, true).delay(d).fadeIn(o);
                  o = o + 10;
                  d = d + 100;
               });
            }
            $(this).addClass("active");
         }
      });
   }
   else {

      $(".hasMegaSub").hover(function () {
         // BEWARE: DRAGONS AHEAD!!!
         // Vær forsigtig med at "optimere" på følgende metode, da der skal tages højde for to milliarder edge cases. Nu virker det nemlig. | NIS

         //megaCloseFix();
         //$(this).addClass("adding");
         //$(this).addClass("active");

         var o = 600;
         var d = 400;

         $(".megaSub", this).stop(true, true).delay(250).fadeIn(400);
         $("#whiteOverlay").stop().fadeTo(200, .8);

         if ($.browser.msie) {
            $(this).find(".megaSubItem, .megaTeaserPlain, .megaSubSub").stop(true, true).delay(400).fadeIn(400);
         }
         else {
            $(this).find(".megaSubItem, .megaTeaserPlain, .megaSubSub").each(function () {
               $(this).stop(true, true).delay(d).fadeIn(o);
               o = o + 10;
               d = d + 100;
            });
         }

         setTimeout(function () {
            $(".hasMegaSub.adding").addClass("active").removeClass("adding");
         }, 650);


      }, function () {
         //$(this).addClass("removing");
         $(this).removeClass("active");
         //         $(".megaSub, .megaSubItem, .megaTeaserPlain", this).slideUp(175);
         $(this).find(".megaSubItem, .megaTeaserPlain, .megaSubSub").delay(350).fadeOut(10);
         $(this).children(".megaSub").stop(true, true).delay(300).fadeOut(175);

         setTimeout(function () {
            $(".hasMegaSub.removing").removeClass("active removing");
         }, 125);

         $("#whiteOverlay").stop().fadeTo(200, 0);
      });
   }

   $("#whiteOverlay").click(function () {
      megaClose();
   });

   /*$(document).keyup(function (e) {
   if (e.keyCode == 27) { megaClose(); }   // esc
   });*/

   function megaClose() {
      $(".hasMegaSub.active").addClass("temp");
      $(".megaSub").stop(true, false).delay(400).fadeOut(405);
      $(".megaSubItem, .megaTeaserPlain, .megaSubSub").fadeOut(125);
      $("#whiteOverlay").fadeOut(200);
      setTimeout(function () {
         $(".hasMegaSub.active.temp").removeClass("active temp");
      }, 125);
      $("#mainNavigationCon").removeClass("zIndexReset");
   }

   function megaCloseFix() {
      $(".hasMegaSub.active").find(".megaSub").children().fadeOut(125);
      //$(".megaSub, .megaSubItem, .megaTeaserPlain, .megaSubSub").stop(true, false).hide();
      //$(".megaSubItem, .megaTeaserPlain, .megaSubSub").hide();
      //$("#whiteOverlay").fadeOut(200);
      setTimeout(function () {
         $(".hasMegaSub.active.temp").removeClass("active temp");
      }, 125);
      //$("#mainNavigationCon").removeClass("zIndexReset");
   }

   $("#bookMeetingTrigger").fancybox({
      overlayColor: "#fff",
      overlayOpacity: 0.9
   });

   $("#bookMeetingTrigger").click(function () {
      $("#fancybox-close").addClass("fancyCloseNew");
      $("#mainNavigationCon").addClass("zIndexReset");
   });

   $("#callMeBtn").fancybox({
      overlayColor: "#fff",
      overlayOpacity: 0.9
   });

   $("#callMeBtn").click(function () {
      $("#fancybox-close").addClass("fancyCloseNew");
      $("#mainNavigationCon").addClass("zIndexReset");
   });

   $("#fancybox-close").click(function () {
      $(this).removeClass("fancyCloseNew");
      $("#mainNavigationCon").removeClass("zIndexReset");
   });


   /* Funktion til, at sørge for at mailadressen fra dropdownfeltet, bliver ført over i modtagerfeltet */
   $("#callMeForm, #bookMeetingForm").live("change", "select", function () {
      var v = $(this).find("select").val();
      $("#callMeForm").find("input[name='Recipient']").val("nis@nozebra.dk");
      //$(this).siblings("#dwControls").children("input[name='AccessUserName']").val(v);
   });


   /*$("#mainNav").children("li").hover(function () {
   $(this).children("ul, div").stop(true, false).slideDown(200, 'easeOutBack');
   var o = 400;
   var d = 10;
   $(this).find("ul:not(li > .M0), li").each(function () {
   $(this).stop(true, true).delay(d).fadeIn(o);
   o = o + 10;
   d = d + 10;
   });
   }, function () {
   $(this).children("ul, div").slideUp(175);
   $(this).find("ul:not(li > .M0), li").stop(true, true).fadeOut(125);
   });*/


   /***  SLIDER  ***/
   // Hvis #sliderTemp ikke er tom...
   if ($('#sliderTemp').children().size() > 0) {
      // Find første billede i teksten, og wrap dem i en container
      $('.newsArticle').find('img:first').wrap("<div class='newsSlider'></div>");
      // Hent bilederne fra deres midlertidige container, og smid dem det rigtige sted hen. Fjern dernæst containeren.
      $('#sliderTemp').children().appendTo(".newsSlider");
      $('#sliderTemp').remove();
   }

   // Der er nogle gange grimme <p>-tags omkring vores billeder...
   $('.nivoSlider img').unwrap();

   $('.newsSlider, .nivoSlider').nivoSlider({
      effect: 'fade',
      animSpeed: 350,
      pauseTime: 4000,
      controlNav: true,
      directionNavHide: false,
      prevText: '',
      nextText: '',
      afterLoad: function () {
         // Midterstil prikkerne
         var nivoPosAdj = 268 - ($('.nivo-controlNav').width() / 2);
         $('.nivo-controlNav').css({ 'left': nivoPosAdj });
      }
   });
   /***  END SLIDER  ***/

   // Invita vil gerne have smidt et billede ind midt i et eCom modul. Da man ikke kan bruge global paragraphs her, så skal vi have flyttet vores indhold fra andetsteds. | 1. sep 2011, NIS
   $("#moveTeaser").appendTo("#typeMenu");
   $("#productGallery").each(function () {
      $("#moveTeaser").hide();
   });

   // Teksten  skal altid holde højrekant med billedet, så vi sætter størrelsen dynamisk
   var newsW = $('.newsArticle').find('img:first').width();
   //$('.newsArticle').width(newsW);


   $('.marketItem:nth-child(3n)').addClass('noPadding');
   $('.photo:nth-child(3n)').addClass('noPadding');
   $('.stepContainer:nth-child(5n)').addClass('left');
   //	$('.quarterBox:nth-child(4n)').after('<div class="hr"></div>'); // Hvad laver den her her? ~ NIS
   if (area == 'dk') {
      $('#searchRedirect').val('12120');
      $('#searchBox').val('Indtast søgeord');
   }
   $(".box").each(function (n) {
      $(this).click(function () {
         obj = this.getElementsByTagName("a");
         /*if(obj[0].href.substring(0,7)=='http://'){
         window.open(obj[0].href);
         }
         else{*/
         location.href = obj[0].href
         /*}*/
      });
   });
   $(".widebox").each(function (n) {
      $(this).click(function () {
         obj = this.getElementsByTagName("a");
         location.href = obj[0].href
      });
   });
   $(".videositeTeaserSmall").each(function (n) {
      $(this).click(function () {
         obj = this.getElementsByTagName("a");
         location.href = obj[0].href
      });
   });
   if ($.browser.msie == true && parseInt($.browser.version) < 8) {
      var zIndexNumber = 1000;
      $('div').each(function () {
         $(this).css('zIndex', zIndexNumber);
         zIndexNumber -= 10;
      });
   }

   $("a[rel=img-a], a[rel=img-b], a[rel=img-c], a[rel=img-d], a[rel=img-e]").fancybox({
      'transitionIn': 'none',
      'transitionOut': 'none',
      'titlePosition': 'over',
      'showNavArrows': false,
      'centerOnScroll': true,
      'overlayShow': false,
      'titleFormat': function (title, currentArray, currentIndex, currentOpts) {
         return '<div id="fancybox-title-over"><div id="fancybox-image-title"><p>' + title + '</p></div><div id="fancy-navigation"><img onclick="javascript:$.fancybox.prev();" src="/files/system/Invita/gfx/fancybox/prev.gif" id="fancy-prev" alt="Forrige billede" /><span id="fancy-img-count">Billede ' + (currentIndex + 1) + ' / ' + currentArray.length + '</span><img onclick="javascript:$.fancybox.next();" src="/files/system/Invita/gfx/fancybox/next.gif" id="fancy-next" alt="Næste billede" /></div></div>';
         //return '<span id="fancybox-title-over">Billede ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
      },
      'onStart': function () { $('.fancybox-ie').attr('style', 'z-index:2000;') }
   });

   $('#fancybox-outer').hover(function () {
      $('#fancybox-title').fadeIn(300);
   }, function () {
      $('#fancybox-title').fadeOut(500);
   });

   var hash = getUrlVars();
   var newsId = hash['NewsId'];
   $('.relatedCase').each(function () {
      if ($(this).attr('id') == newsId) {
         $(this).hide();
      }
   });
   /*
   $.ajax({
   type: "POST",
   url: "https://invita-koekkener.clients.ubivox.com/handlers/post/",
   data: "name=John&location=Boston",
   success: function(msg){
   alert( "Data Saved: " + msg );
   }
   });*/
});

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
 
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
 
    return vars;
}
function swapImage(image) {
	$('#postedImage img').fadeOut(function(){
		$('#postedImage img').replaceWith('<img style="display:none" src="/files/billeder/'+ image +'" height="387" width="730" />');
		$('#postedImage img').fadeIn();
	});
}

/*function openVideo1() {
	$("#homeBanner #image").css("display","none");
	$("#videoContainer").animate({ 
		height: '498px'
		}, 1000,'swing',function () { runVideo1() } );
}

function closeVideo(){
	$("#video").remove();
	$("#videoContainer").append('<div id="video"></div>');
	$("#videoContainer").animate({ 
		height: '266px'
		}, 1000,'swing',function () { $("#homeBanner #image").css("display","block"); } );
}
function runVideo1(){
	var flashvars = {};
	flashvars.flvUrl = "/files/System/Invita/flash/videoPlayer/video/Final_Master_ovenskabe_vers2_small.flv";
	
	var params = {}
	params.scale = "noScale";
	params.wmode = "opaque";
	align:"topLeft";
	params.allowfullscreen = "true";
	swfobject.embedSWF("/files/system/invita/flash/videoplayer/videoplayer.swf", "video", "972", "547", "9.0.0", "/files/system/invita/flash/resource/expressInstall.swf", flashvars, params);
}
*/

/* Minisite 70'er køkken */
$('.txtInner:first').addClass('txtBorderRight');
$('.productImg img:first').css('left', 0);
$('.overlay:first').css('left', 0);
$('.imgTooltip').css('zIndex', 104);
$('.imgTooltip p').css('zIndex', 105);
$('.btnImgCtrl, .btnImgCtrlActive').css('zIndex', 110);
$('.overlay').css('zIndex', 1000);
$('.btnImgCtrl:first').css('left', 448);
$('.imgTooltip:first').css('left', 333);



if($.browser.msie == true && parseInt($.browser.version) < 8) {
	$('.greyWrapper_70er_subpage').css('height', 'auto');
	$('.greyWrapper_70er_default').css('height', 'auto');
	$('.wrapper.visible').css('height', 'auto');
	$('.products').css('height', 'auto');
	$('.productImg img, .overlay').css('top', 0);
	$('.btnImgCtrl, .imgTooltip').css('top', 10);
	$('.btnImgCtrlActive').css('top', -310);
	var topPosStart = 0;
	var topPosEnd = -320;
} else {
	 var topPosStart = 394
	 var topPosEnd = 74
}


$('.productImg img:first, .btnImgCtrl:first, .imgTooltip:first, .imgTooltip:first p, .overlay:first').hover(function() {
	$('.imgTooltip:first').stop(true, true).fadeIn(150);
}, function() {
	$('.imgTooltip:first').stop(true, true).delay(150).fadeOut(150);
});

$('.productImg img:last, .btnImgCtrl:last, .imgTooltip:last, .imgTooltip:last p, .overlay:last').hover(function() {
	$('.imgTooltip:last').stop(true, true).fadeIn(150);
}, function() {
	$('.imgTooltip:last').stop(true, true).delay(150).fadeOut(150);
});

// Zoom-funktion på deres billeder. De to billeder skal behandles hver for sig, da deres position er anderledes.
$('.overlay:first').toggle(function() {
	$(this).animate({width:972, height:641, top:topPosEnd}, 15).css('zIndex', 1001);
	$('.productImg img:first').animate({width:972, height:641, top:topPosEnd}, 150).css('zIndex','101').addClass('imgLAnimated');
	$('.imgTooltip').css('zIndex','98');
	$('.btnImgCtrl:first').fadeOut(150);
	$('.btnImgCtrlActive:first').delay(150).fadeIn(225);
	$('.productImg img:last').delay(150).fadeOut(1);
	$('.btnImgCtrl:last').fadeOut(75);
}, function() {
	$(this).animate({width:486, height:321, top:topPosStart}, 150).css('zIndex', 1000);
	$('.productImg img:first').animate({width:486, height:321, top:topPosStart}, 150).css('zIndex','97').removeClass('imgLAnimated');
	$('.imgTooltip').css('zIndex','104');
	$('.productImg img:last').fadeIn(350);
	$('.btnImgCtrlActive').fadeOut(225);
	$('.btnImgCtrl').fadeIn(350);
});

$('.overlay:last').toggle(function() {
	$(this).animate({width:972, height:641, left:0, top:topPosEnd}, 15).css('zIndex', 1001);
	$('.productImg img:last').animate({width:972, height:641, top:topPosEnd, left:0}, 150).css('zIndex','101');
	$('.imgTooltip').css('zIndex','98');
	$('.btnImgCtrl:last').fadeOut(150);
	$('.btnImgCtrlActive:last').delay(150).fadeIn(225);
	$('.productImg img:first').delay(150).fadeOut(1);
	$('.btnImgCtrl:first').fadeOut(75);	
}, function() {
	$(this).animate({width:486, height:321, left:486, top:topPosStart}, 150).css('zIndex', 1000);
	$('.productImg img:last').animate({width:486, height:321, top:topPosStart, left:486}, 150).css('zIndex','99');
	$('.imgTooltip').css('zIndex','104');
	$('.productImg img:first').fadeIn(350);
	$('.btnImgCtrlActive').fadeOut(225);
	$('.btnImgCtrl').fadeIn(350);
});

/*$('.imgLNotAnimated').click(function() {
	$('.productImg img:first').animate({width:972, height:641, top:topPosEnd}, 150).css('zIndex','101').toggleClass('imgLAnimated').toggleClass('imgLNotAnimated');
	$('.imgTooltip').css('zIndex','98');
	$('.btnImgCtrl:first').fadeOut(150);
	$('.btnImgCtrlActive:first').delay(150).fadeIn(225);
	$('.productImg img:last').delay(150).fadeOut(1);
	$('.btnImgCtrl:last').fadeOut(75);
});

('.imgLAnimated').click(function() {
	$('.productImg img:first').animate({width:486, height:321, top:topPosStart}, 150).css('zIndex','97').toggleClass('imgLAnimated').toggleClass('imgLNotAnimated');;
	$('.imgTooltip').css('zIndex','104');
	$('.productImg img:last').fadeIn(350);
	$('.btnImgCtrlActive').fadeOut(225);
	$('.btnImgCtrl').fadeIn(350);
});*/
/*
$('.productImg:first img').click(function() {
	$('overlay:first').animate({width:972, height:641, left:0, top:topPosEnd}, 15);
	$('.productImg img:first').animate({width:486, height:321, top:topPosStart}, 150).css('zIndex','97');
	$('.imgTooltip').css('zIndex','104');
	$('.productImg img:last').fadeIn(350);
	$('.btnImgCtrlActive').fadeOut(225);
	$('.btnImgCtrl').fadeIn(350);
});*/

/*$('.productImg:first img').toggle(function() {
	$('.productImg img:first').animate({width:972, height:641, top:topPosEnd}, 150).css('zIndex','101');
	$('.imgTooltip').css('zIndex','98');
	$('.btnImgCtrl:first').fadeOut(150);
	$('.btnImgCtrlActive:first').delay(150).fadeIn(225);
	$('.productImg img:last').delay(150).fadeOut(1);
	$('.btnImgCtrl:last').fadeOut(75);
}, function() {
	$('.productImg img:first').animate({width:486, height:321, top:topPosStart}, 150).css('zIndex','97');
	$('.imgTooltip').css('zIndex','104');
	$('.productImg img:last').fadeIn(350);
	$('.btnImgCtrlActive').fadeOut(225);
	$('.btnImgCtrl').fadeIn(350);
});

$('.productImg:last img').toggle(function() {
	$('.productImg img:last').animate({width:972, height:641, top:topPosEnd, left:0}, 150).css('zIndex','101');
	$('.imgTooltip').css('zIndex','98');
	$('.btnImgCtrl:last').fadeOut(150);
	$('.btnImgCtrlActive:last').delay(150).fadeIn(225);
	$('.productImg img:first').delay(150).fadeOut(1);
	$('.btnImgCtrl:first').fadeOut(75);
}, function() {
	$('.productImg img:last').animate({width:486, height:321, top:topPosStart, left:486}, 150).css('zIndex','99');
	$('.imgTooltip').css('zIndex','104');
	$('.productImg img:first').fadeIn(350);
	$('.btnImgCtrlActive').fadeOut(225);
	$('.btnImgCtrl').fadeIn(350);
});


/*$('.overlay:last').toggle(function() {
	$(this).animate({width:972, height:641, top:74, left:0}, 150);
	$('.productImg img:last').animate({width:486, height:321, top:394, left:486}, 150).css('zIndex','99');
	$('.btnImgCtrl:last').fadeOut(150);
	$('.btnImgCtrlActive').delay(150).fadeIn(225);
	$('.productImg img:first').delay(150).fadeOut(1);
	$('.btnImgCtrl:first').delay(150).fadeOut(1);
}, function() {
	$(this).animate({width:486, height:321, top:394, left:486}, 150);
	$('.productImg img:last').animate({width:486, height:321, top:394, left:486}, 150).css('zIndex','99');
	$('.btnImgCtrl:last').animate({top:404, left:937}, 150).css('zIndex','100');
	$('.productImg img:first').fadeIn(350);
	$('.btnImgCtrlActive').fadeOut(225);
	$('.btnImgCtrl').fadeIn(350);
});*/

$("#pageName:contains('Den enkle løsning')").each(function() {
	$("#navEnkle").addClass("active");
});
$("#pageName:contains('Lys og luft')").each(function() {
	$("#navLys").addClass("active");
});
$("#pageName:contains('Vend køkkenet om')").each(function() {
	$("#navVend").addClass("active");
});
$("#pageName:contains('Luk op til stuen')").each(function() {
	$("#navLukop").addClass("active");
});

// Når man læser aktuelle nyheder fra badesiden, skal det vises som en almindelig artikel, uden dato
$('#removeDate').each(function(){
	$('.newsArticle span.red:first').hide();
});

Cufon.replace('#product a.h2', { fontFamily: 'vida_reg' });
Cufon.replace('#callMeBox h2', { fontFamily: 'vida_reg' });
Cufon.replace('#callMeBtn', { fontFamily: 'vida_reg' });
Cufon.replace('#bookMeetingCon h2', { fontFamily: 'vida_reg' });
Cufon.replace('#mainNav > li > a', { fontFamily: 'vida_reg', hover: true });
Cufon.replace('.megaSubSub a', { fontFamily: 'vida_reg', hover: true });
Cufon.replace('.megaSub h3', { fontFamily: 'vida_reg' });
Cufon.replace('.megaSub h2', { fontFamily: 'vida_reg' });
Cufon.replace('.rente1, .rente3', { fontFamily: 'vida_reg' });
Cufon.replace('.hudeRedText', { fontFamily: 'vida' })
Cufon.replace('.renteBig, .renteSemi', { fontFamily: 'vida' });
Cufon.replace('.bannerH1', { fontFamily: 'vida_reg' });
Cufon.replace('.bannerH1_Green', { fontFamily: 'vida_reg' });
Cufon.replace('.cirkel span', { fontFamily: 'vida' });
Cufon.replace('.whiteBoxHeaderText', { fontFamily: 'vida_reg' });
