﻿function accessoryPopup(productId) {
    var regex = /\d{7}/;
    if (!regex.test(productId))
        return false;

    var tmp = $('body').append('<div id="popupbg"></div><div id="popup"></div>');
    $('#popup').load('/Catalog/AccessoryPopup/' + productId);
    if ($.fn.bgiframe)
        $('#popup').bgiframe();
    return false;
}

function closePopup() {
    $('#popup').remove();
    $('#popupbg').remove();
}

function toggleMenu(id) {
    $('#productdetailsmenu a').removeClass('selected');
    $('#' + id).addClass('selected');
}

$(document).ready(function() {
    $('#dpForm').submit();
    $('#wh').next().hide();
    $('#wh').change(function() {
        $(this).parents('form').submit();
    });
    $('a.external').live("click", function(event) {
        window.open(this.href);
        event.preventDefault();
    });
    $('div.range-slider').each(function() {
        $('#' + this.id + '_min').attr('readonly', true);
        $('#' + this.id + '_max').attr('readonly', true);
        $(this).slider({
            animate: true,
            range: true,
            min: parseInt($('#' + this.id + '_minval').val()),
            max: parseInt($('#' + this.id + '_maxval').val()),
            values: [parseInt($('#' + this.id + '_min').val()), parseInt($('#' + this.id + '_max').val())],
            slide: function() {
                $('#' + $(this).attr('id') + '_min').val($(this).slider('values', 0));
                $('#' + $(this).attr('id') + '_max').val($(this).slider('values', 1));
            }
        });
    });
    $('#filter-reset').click(function(event) {
        $('#filter select').each(function() {
            $(this).children("option[value='']").attr('selected', true);
            this.selectedIndex = 0;
        });
        $('div.range-slider').each(function() {
            $('#' + this.id + '_min').val($(this).slider('option', 'min'));
            $('#' + this.id + '_max').val($(this).slider('option', 'max'));
            $(this).slider('option', 'values', [$(this).slider('option', 'min'), $(this).slider('option', 'max')]).slider('disable').slider('enable');
        });
    });
    $('input[name=PaymentMethod]').click(function() {
        var val = $('input[name=PaymentMethod]:checked').val();
        if (val == 19) {
            $('#payCreditCardForm').slideDown('fast');
            $('#payKlarnaForm').slideUp('fast');
        }
        else if (val == 35) {
            $('#payCreditCardForm').slideUp('fast');
            $('#payKlarnaForm').slideDown('fast');
        } else {
            $('#payCreditCardForm').slideUp('fast');
            $('#payKlarnaForm').slideUp('fast');
        }
    });
    if ($('input[name=PaymentMethod]:checked').val() != 19)
        $('#payCreditCardForm').hide();
    if ($('input[name=PaymentMethod]:checked').val() != 35)
        $('#payKlarnaForm').hide();
    $('#chkShippingAddress').click(function() {
        if ($('#chkShippingAddress').is(':checked')) {
            $('#CustomerName').removeAttr('disabled').removeClass('grey');
            $('#Street1').removeAttr('disabled').removeClass('grey');
            $('#Street2').removeAttr('disabled').removeClass('grey');
            $('#ZipCode').removeAttr('disabled').removeClass('grey');
            $('#City').removeAttr('disabled').removeClass('grey');
        } else {
            $('#CustomerName').attr('disabled', 'disabled').addClass('grey');
            $('#Street1').attr('disabled', 'disabled').addClass('grey');
            $('#Street2').attr('disabled', 'disabled').addClass('grey');
            $('#ZipCode').attr('disabled', 'disabled').addClass('grey');
            $('#City').attr('disabled', 'disabled').addClass('grey');
        }
    });
    $('#NoCellPhone').click(function() {
        if ($('#NoCellPhone').is(':checked')) {
            $('#CellPhone').attr('disabled', 'disabled').addClass('grey').val('');
            $('#CellPhone').next('.field-validation-error').remove();
        }
        else
            $('#CellPhone').removeAttr('disabled').removeClass('grey');
    });
    $('#chkOrderMark').click(function() {
        if ($('#chkOrderMark').is(':checked'))
            $('#OrderMark').removeAttr('disabled').removeClass('grey');
        else
            $('#OrderMark').attr('disabled', 'disabled').addClass('grey');
    });
    $('#search').val('Sök produkt').addClass('grey');
    $('#search').focus(function() {
        if ($('#search').val() == 'Sök produkt')
            $('#search').val('').removeClass('grey');
    });
    $('#search').blur(function() {
        if ($('#search').val() == '')
            $('#search').val('Sök produkt').addClass('grey');
    });
    $('input[name=shippingmethod]').click(function() {
        if (this.id == "shipPostpaket") {
            $('#info18').hide();
            $('#info10').show();
        }
        else {
            $('#info10').hide();
            $('#info18').show();
        }
    });
    $('input[name=compare]').live('click', function(event) {
        var chks = $('input[name=compare]:checked');
        if (chks.length > 4) {
            alert('Du kan jämföra max 4 produkter!');
            event.preventDefault();
        }
    });
    $(':submit.compare').live('click', function(event) {
        var chks = $('input[name=compare]:checked');
        if (chks.length < 2) {
            alert('Markera 2 till 4 produkter för att jämföra dessa.');
            event.preventDefault();
        }
    });
    $('.tooltip').tooltip();
});


(function($) {
    $.fn.tooltip = function(options) {
        var opts = $.extend({}, $.fn.tooltip.defaults, options);

        return this.each(function(i) {
            $(this).hover(function(e) {
                this.tooltiptext = this.title;
                this.title = '';
                $('body').append('<div id="tooltip">' + this.tooltiptext + '</div>');
                $('#tooltip')
                    .css('top', (e.pageY + opts.yOffset) + 'px')
                    .css('left', (e.pageX + opts.xOffset) + 'px')
                    .fadeIn(opts.speed);
            },
            function(e) {
                this.title = this.tooltiptext;
                $('#tooltip').remove();
            });
        });
    };

    $.fn.tooltip.defaults = {
        xOffset: 10,
        yOffset: 20,
        speed: 'fast'
    }
})(jQuery);