
(function ($) {
    $(function () {
        // Fix prices 
        $('.price, #basketPrice, .SpecialOfferPrice_ProductInfo, .basketPrice').html(function (i, txt) {
            return txt.replace(/,00\s?/i, '');
        });
        /*
        $('.price').each(function(){
        $(this).html(function(i, html){
        return html.replace(/,00?DKK/i, ' DKK');
        });
        });*/

        /*
        // Fix "Nyheder" menu
        if (window.location.href.match(/news-all-1/i)) {
        $('.netbutik,.nyheder,.vejledninger,.om-butikken,.kontakt,.netbutik-2,.nyheder-2,.vejledninger-2,.om-butikken-2,.kontakt-2').each(function(){
        var $this = $(this);  
        var img = $this.css('background-image');
        var newImg = img.replace(/images\/[^\/]+\//, 'images/nyheder/');     
        $this.css('background-image', newImg);
        });
        }*/

        // Fix "Tilbud" menu
        if (window.location.href.match(/specialoffer-all-1/i)) {
            $('.netbutik,.nyheder,.vejledninger,.om-butikken,.kontakt,.netbutik-2,.nyheder-2,.vejledninger-2,.om-butikken-2,.kontakt-2').each(function () {
                var $this = $(this);
                var img = $this.css('background-image');
                var newImg = img.replace(/images\/[^\/]+\//, 'images/nyheder/');
                $this.css('background-image', newImg);
            });
        }
        /*
        // Fix unclickable images
        $('.produktbillede').each(function(){
        var specialPic = $('.tilbud-box', this);
        $('a', this).prepend( specialPic );
        }); 
        */
        $('.produktbillede').each(function () {
            var anchor = $('a', this);
            var img = $('.tilbud-box img', this);
            var newAnchor = $('<a />', { "class": "special-pic", "href": anchor.attr('href'), "title": anchor.attr('title') }).append(img);
            //var specialPic = $('.tilbud-box', this).append(newAnchor);
            anchor.before(newAnchor);
        });

        $('.SearchField_SearchPage1, #newsletterMail').clearField();

        // Newsletter validation
        $('#newsletterForm input[type=submit]').click(function () {
            $('#newsletterForm').data('type', $(this).attr('name'));
        });

        $('#newsletterForm').submit(function () {
            var isEmail = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test($('#newsletterMail').val())
            if (!isEmail) {
                window.alert('Din indtastede email lader til at være forkert. Du har muligvis lavet en tastefejl.');
                $('#newsletterMail').focus();
                return false;
            }
            // Track as virtual pageview
            var type = $('#newsletterForm').data('type');
            _gaq.push(['_trackPageview', "/newsletter/" + type]);
        });

        // Replace 0,00 shipping with "Gratis"
        $('#productShipping').text(function (i, text) {
            if (text.match(/^0\sDKK/i)) {
                return "Gratis";
            } else {
                return text;
            }
        });

        /* Gift wrapping */

        // Control graphics of gift wrap button
        if ($('#giftwrap').length > 0 && $.cookie('indpakning') === 'true') {
            $('#giftwrap').addClass('on');
        }

        // Change gift wrapping cookie
        $('#giftwrap').click(function () {
            if ($.cookie('indpakning') != 'true') {
                var indpakning = 'true';
                $(this).addClass('on');
            } else {
                var indpakning = 'false';
                $(this).removeClass('on');
            }

            $.cookie('indpakning', indpakning, { expires: 2, path: '/' });
            return false;
        }).show();

        //Frifragt i indkøbskurv

        if ($('.finalPrice').length) {
            var finalPrice = "0";
            finalPrice = $('.finalPrice').html().replace(/[.]/g, '');

            if (finalPrice != "0") {

                if (finalPrice < 1100) {
                    $('#friFragt').css("display", "block");
                    $('#friFragt p').html("Køb for <b>" + (1100 - finalPrice) + " kr.</b> mere og få <b>gratis fragt!</b>");

                }
            }
        }
		
        /* Address page */
		var $addrForm = $('form[name*="Orderstep1Form"]');
		
		if ($addrForm.length) {
			
			// Remove gift wrapping order field and set default value 
			$('input[name=res3]').parentsUntil('tr').parent().remove(); // Remove old input
			
			// Add new input
			var wrap = ($.cookie('indpakning') === 'true') ? 'Ja' : 'Nej';
			$addrForm.append('<input type="hidden" name="res3" value="' + wrap + '" />');
			
			// Simplify form fields
			$('#country, #lcountry').replaceWith('Danmark');
			$addrForm.append('<input type="hidden" name="country" value="2397" /><input type="hidden" name="lcountry" value="2397" />');
			
			// Track form abandonment / GA 
			//$addrForm.find('input[type!=hidden]').change( function(){ _gaq.push(['_trackEvent', 'forms', 'addressform', this.name]); });
			
		} else {
		
			// Stupid IE quirksmode fix
			$('input[name=res3]').each(function () {
				$this = $(this);
				if ($.cookie('indpakning') === 'true') {
					$this.val('Ja');
					$this.attr('Value', 'Ja');
				} else {
					$this.val('Nej');
					$this.attr('Value', 'Nej');
				}
			});
		
		}
		
		/*
        // Remove gift wrapping order field and set default value
        $('input[name=res3]').each(function () {
            $this = $(this);
            if ($.cookie('indpakning') === 'true') {
                $this.val('Ja');
                $this.attr('Value', 'Ja');
            } else {
                $this.val('Nej');
                $this.attr('Value', 'Nej');
            }
			// Change type
			
            $this.attr('type', 'hidden');
            $(this).parent().parent().hide();
        });*/

        /*
        $('.CustomerData_Orderstep1_TBL table td:contains(Gaveindpakning)').each(function(){
        var $this = $(this)
        , $input = $this.siblings().find('input').hide().filter('.TextInputField_Orderstep1');
        $input.val('Nej');
			
        if ($.cookie('indpakning') === 'true') { 
        $input.val('Ja');
        }
			
        }); */

        // $('.CustomerData_Orderstep1_TBL table td:contains(Gaveindpakning)').text('').siblings().find('input').hide().filter('.TextInputField_Orderstep1').val('Nej');
		
		
    });
})(jQuery);

/* Google Website Optimizer Control Script */
function utmx_section(){}function utmx(){}
(function(){var k='4280365584',d=document,l=d.location,c=d.cookie;function f(n){
if(c){var i=c.indexOf(n+'=');if(i>-1){var j=c.indexOf(';',i);return escape(c.substring(i+n.
length+1,j<0?c.length:j))}}}var x=f('__utmx'),xx=f('__utmxx'),h=l.hash;
d.write('<sc'+'ript src="'+
'http'+(l.protocol=='https:'?'s://ssl':'://www')+'.google-analytics.com'
+'/siteopt.js?v=1&utmxkey='+k+'&utmx='+(x?x:'')+'&utmxx='+(xx?xx:'')+'&utmxtime='
+new Date().valueOf()+(h?'&utmxhash='+escape(h.substr(1)):'')+
'" type="text/javascript" charset="utf-8"></sc'+'ript>')})();
/* End of Google Website Optimizer Control Script */
/*Google Website Optimizer Tracking Script */
  var _gaq = _gaq || [];
  _gaq.push(['gwo._setAccount', 'UA-12708691-2']);
  _gaq.push(['gwo._trackPageview', '/4280365584/test']);
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
/* End of Google Website Optimizer Tracking Script */



