// Shop frontend by Novicell - www.novicell.dk - KHA, APN - We love Umbraco + uCommerce

$(document).ready(function () {

    // wrap content boxes for adding shadow
    $('#content .box').each(function () {
        $(this).addClass('boxBody').before('<div class="boxHeader" />').after('<div class="boxFooter" />');
    });

    // detach small basket from footer and move to header
    var basketSmall = $('#basketSmall').detach();
    basketSmall.insertAfter('#logo').removeClass('hide');

    // ajust footer height on short pages
    /*
    $('#footer').ajustFooter();
    $('#footer').before('<div class="clearFooter"></div>');
    */

    // Image fading for all but IE
    if (!$.browser.msie) {
        $('.image img').css({ 'opacity': 0.05 });
        $('.image img').load(function () {
            $(this).fadeTo(250, 0.9);
            $(this).closest('div').css({ 'background': 'transparent' });
        });

        $('.image img').css({ 'opacity': 0.9 });
    }
    if ($.browser.msie) {
        $('.image').css({ 'background': 'transparent' });
    }

    // fade-in on produkct title in add to basket
    if (!$.browser.msie) {
        $('.basketAdd h2').css({ 'opacity': 0.3 });
        $(this).find('.basketAdd h2').fadeTo(1500, 1);
    }

    $('a.button').each(function () {
        $(this).wrapInner('<p />');
    });

    $('input.button').each(function () {
        $(this).wrap('<div class="' + $(this).attr('class') + '" />');
    });

    // add more acticive links on product items in lists
    $('li.productItem').each(function () {
        var url = $(this).find('.name a').attr('href');
        $(this).find('.name a').removeAttr('title');
        $(this).find('p.button').removeAttr('class').wrap('<a href="' + url + '" class="button" />');
        $(this).find('img').wrap('<a href="' + url + '" />');
    });



    // css for address table
    $('table.addressTable tr').each(function () {
        $(this).find('td:nth-child(1)').addClass('cellLabel');
        $(this).find('td:nth-child(2)').addClass('cellInput');
        $(this).find('td input[type="checkbox"]').addClass('inputCheckbox');
    });

    // patch button wrapper in checkout flow
    $('.checkoutButtons').after('<div class="clear" />');


    // simple transfer effect on basket add buttons
    $("#basketAdd div.button div.button").click(function (event) {
        event.preventDefault();
        $(this).effect("transfer", { to: "#basketSmall a", className: "ui-effects-transfer" }, 500, function () {
            $('#basketSmall a').html('<span class="updating">' + text_UpdatingBasket + '</span>');
            $(this).closest('form').submit();
        });
    });

    // transfer effect on basket add buttons on category/product list pages
    $("#basketListAdd div.button div.button").click(function (event) {
        event.preventDefault();
        $(this).effect("transfer", { to: "#basketSmall a", className: "ui-effects-transfer" }, 500, function () {
            $('#basketSmall a').html('<span class="updating">' + text_UpdatingBasket + '</span>');

            var product_item = $(this).parent().parent().parent();
            product_item.find("#buyProductSku").attr("value", $(this).parent().find(".productSku").attr("value"));
            product_item.find("#buyProductVariantSku").attr("value", $(this).parent().find(".productVariantSku").attr("value"));

            $(this).closest('form').submit();
        });
    });

    $(".deleteLineItem").click(function () {
        if (confirm(text_RemoveFromBasket)) {
            $(this).parent().find(".quantity_textfield").val(0);
            $(this).closest('form').submit();
        } else { }
    });

    //select variant switcher on category/product list page
    $(".productListVariantSelect").change(function () {
        /*
        //set product variant sku
        $(this).parent().find(".productVariantSku").attr("value", $(this).attr('value'));

        //alert("new hidden value:" + $(this).parent().find(".productVariantSku").attr("value"));
        var product_item = $(this).parent().parent().parent().parent();
        product_item.find(".product_item_image").attr('href', productVariant[$(this).attr('value')]['productUrl']);
        if (productVariant[$(this).attr('value')]['primaryImage'] != '0') {
        product_item.find(".product_item_image").html('<img src="' + productVariant[$(this).attr('value')]['primaryImage'] + '&amp;width=130&amp;height=172&amp;constrain=true" alt="" />');
        } else {
        product_item.find(".product_item_image").html("&nbsp;");
        }
        product_item.find(".product_item_text .product_item_name a").html(productVariant[$(this).attr('value')]['displayName']);
        product_item.find(".product_item_description").html(productVariant[$(this).attr('value')]['shortDescription']);
        product_item.find(".product_item_id span").html(productVariant[$(this).attr('value')]['variantSku']);
        product_item.find(".product_item_previousprice").html(productVariant[$(this).attr('value')]['previousprice']);
        product_item.find(".product_item_price").html(productVariant[$(this).attr('value')]['price']);
        if (productVariant[$(this).attr('value')]['stockAmount'] > 0) {
        product_item.find(".product_item_status .statusAvailable").attr('style', 'display:inline;');
        product_item.find(".product_item_status .statusNotAvailable").attr('style', 'display:none;');
        } else {
        product_item.find(".product_item_status .statusAvailable").attr('style', 'display:none;');
        product_item.find(".product_item_status .statusNotAvailable").attr('style', 'display:inline;');
        }
        product_item.find(".product_item_name a").attr('href', productVariant[$(this).attr('value')]['productUrl']);
        */
        window.location.href = productVariant[$(this).attr('value')]['productUrl'];
    });

    //select variant switcher on product page
    $('#productVariantSelect').change(function () {
        if (productVariant[$(this).attr('value')]['primaryImage'] != '0') {
            $('#gallery').html('<span id="zoom">' +
			   '<img src="/images/zoom_icon.png" alt="" />' +
			'</span>' +
            '<a href="' + productVariant[$(this).attr('value')]['primaryImage'] + '&amp;class=Variant" title="">' +
			'<img src="' + productVariant[$(this).attr('value')]['primaryImage'] + '&amp;class=Product" alt="' + productVariant[$(this).attr('value')]['displayName'] + '" />' +
			'</a>');
        } else {
            $('#gallery').html('&nbsp;');
        }
        $('#gallery img').attr('alt', productVariant[$(this).attr('value')]['displayName']);
        $('#displayName').html(productVariant[$(this).attr('value')]['displayName']);
        $('#shortDescription').html(productVariant[$(this).attr('value')]['shortDescription']);
        $('#sku').html(productVariant[$(this).attr('value')]['variantSku']);
        if (productVariant[$(this).attr('value')]['stockAmount'] > 0) {
            $('#statusAvailable').attr('style', 'display:inline;');
            $('#statusNotAvailable').attr('style', 'display:none;');
        } else {
            $('#statusAvailable').attr('style', 'display:none;');
            $('#statusNotAvailable').attr('style', 'display:inline;');
        }

        if (productVariant[$(this).attr('value')]['previousprice'] != '' && productVariant[$(this).attr('value')]['previousprice'] != 'NaN') {
            $('#previousprice').html('<span class="old_price" style="font-size: 12px;">' + productVariant[$(this).attr('value')]['currency'] + ' ' + productVariant[$(this).attr('value')]['previousprice'] + '</span>');
            $('#price').html('<span class="new_price">' + productVariant[$(this).attr('value')]['currency'] + ' ' + productVariant[$(this).attr('value')]['price'] + '</span>' + '<span class="inkl_moms">' + inkltax + '</span>');
        }
        else {
            $('#previousprice').html('&nbsp;');
            $('#price').html(productVariant[$(this).attr('value')]['currency'] + ' ' + productVariant[$(this).attr('value')]['price'] + '<span class="inkl_moms">' + inkltax + '</span>');
        }

        $('#longDescription').html(productVariant[$(this).attr('value')]['longDescription']);
        $('#deliveryTime').html(productVariant[$(this).attr('value')]['deliveryTime']);
        $('#product_header_h2').html(productVariant[$(this).attr('value')]['displayName']);

        $('#gallery a').lightBox();
    });

    // color hover effect on product titles
    $.getScript('/scripts/plugins/jquery.color.js');

    $('li.productItem').hover(
	  function () {
	      if (!$.browser.msie)
	          $(this).find('.image img').fadeTo(100, 1);
	      $(this).find(' .name a').stop().animate({ color: '#51B848' }, 250);
	  },
	  function () {
	      if (!$.browser.msie)
	          $(this).find('.image img').fadeTo(90, 0.9);
	      $(this).find(' .name a').stop().animate({ color: '#000' }, 400);
	  }
	);

    /* Making sure IE7 behaves */
    $('#logo').css('position', 'absolute');

});                                        // ready
/*
jQuery.fn.ajustFooter = function () {
	var offset = $(this).offset();
	if (offset.top <= 550)
		$(this).css({ 'margin-top': '450px' });
}
*/
// prevent console log from failing if not atcive
if (typeof console === "undefined") {
	console = { log: function () { } };
}
