﻿// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function () { log.history = log.history || []; log.history.push(arguments); if (this.console) { console.log(Array.prototype.slice.call(arguments)) } };

var currentPhotoPos = 0;
var realPhotoWidth = 0;

$(function () {
    // set placeholder text in older browsers
    $('input, textarea').placeholder();

    // slimbox
    if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {
        $("a[rel^='lightbox']").slimbox({ imageFadeDuration: 0, counterText: "Bild {x} av {y}", loop: true }, null, function (el) {
            return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
        });
    }

    $('.quantityup').click(function () {
        var value = $(this).parent().parent().find('input').val();
        if (!parseInt(value)) {
            value = 1;
        } else {
            value = parseInt(value);
        }
        value += 1;
        $(this).parent().parent().find('input').val(value)
    });

    $('.quantitydown').click(function () {
        var value = $(this).parent().parent().find('input').val();
        if (!parseInt(value)) {
            value = 1;
        } else {
            value = parseInt(value);
        }
        if (value < 2) {
            value = 2;
        }
        value -= 1;
        $(this).parent().parent().find('input').val(value)
    });

    // resize input elements to fit in field
    $('.field > input').each(function (index) {
        var w = $(this).width();
        $(this).width(w - 22);
    });

    // photolist
    var realWidth = 0;
    $.each($('#images .thumbcontainer img'), function () {
        realWidth += $(this).width();
        $(this).show();
    });

    // If Internet Explorer
    if (navigator.appVersion.indexOf('MSIE') != -1) {
        if (realWidth > 600) {
            realPhotoWidth = realWidth;
            $('#images a.next').addClass('enabled');
            $("#images a.next.enabled").effect("bounce", { times: 3, distance: 20 }, 300);
        }
    }
    else {
        window.setTimeout(CountImageWidth, 500);
    }

    $('#images a.next').click(function () {
        if ($('#images a.next').hasClass('enabled')) {
            currentPhotoPos += 600;
            var $container = $('#images .thumbcontainer div');
            $container.animate({ marginLeft: '-' + currentPhotoPos + 'px' }, 1000);

            if ((currentPhotoPos + 600) > realPhotoWidth) {
                $('#images a.next').removeClass('enabled');
            }
            $('#images a.prev').addClass('enabled');
        }
    });

    $('#images a.prev').click(function () {
        if ($('#images a.prev').hasClass('enabled')) {
            currentPhotoPos -= 600;
            var $container = $('#images .thumbcontainer div');
            $container.animate({ marginLeft: '-' + currentPhotoPos + 'px' }, 1000);
            $('#images a.next').addClass('enabled');
            if (currentPhotoPos == 0) {
                $('#images a.prev').removeClass('enabled');
            }
        }
    });

    $('#images a.next').css('top', '30px');
    $('#images a.next').css('right', '15px');
});

function CountImageWidth() {
    $.each($('#images .thumbcontainer img'), function () {
        realPhotoWidth += this.width;
        $(this).show();
        if ($("#images .thumbcontainer img:last").attr('id') == $(this).attr('id')) {
            window.setTimeout(BounceArrow, 500);
        }
    });
    $("#images .thumbcontainer img").load(function () {
        realPhotoWidth += this.width;
        if ($("#images .thumbcontainer img:last").attr('id') == $(this).attr('id')) {
            window.setTimeout(BounceArrow, 500);
        }
    });
}

function BounceArrow() {
    if (realPhotoWidth > 600) {
        // If NOT Internet Explorer
        if (navigator.appVersion.indexOf('MSIE') == -1) {
            $('#images a.next').addClass('enabled');
            //$("#images a.next.enabled").effect("bounce", { times: 3, distance: 20 }, 300);
            $('#images a.next').css('top', '30px');
            $('#images a.next').css('right', '15px');
        }
    }
}

function ShowMessage(message, type, delay) {
    $('#message').remove();
    var container = $('<div />').attr('id', 'message').css('top', $(window).scrollTop());
    container.append($('<span></span>').addClass('icon-48 ' + type));
    container.append($('<p />').html(message));
    container.append($('<a />').attr('class', 'messagelink').attr('href', 'javascript:void(0);').text('Stäng').click(function () {
        HideMessage();
    }));

    $('body').append(container);
    container.slideDown(300);

    container.attr('class', type);

    if (delay != 0) {
        window.setTimeout(HideMessage, delay);
    }
}

function HideMessage() {
    var container = $('#message');
    if (container.length > 0) {
        container.slideUp("400", function () { $(this).remove(); });
    }
}

function DisplayBox(title, message) {
    if (title != '') {
        title = '<h1>' + title + '</h1>';
    }

    try {
        $.fancybox('<div class="popup">' + title + '<div>' + message + '</div><div class="buttons"><input type="button" onclick="$.fancybox.close();" value="OK!" class="actionbutton" /></div></div>',
        {
            'autoDimensions': false,
            'width': '500',
            'height': 'auto'
        });

    } catch (e) {
    }
}

