$(document).ready(init);

function init() {
    resized();
    
    $("#open-custom").click( function() { customise(); return false; }).show();

    // Reset button click event handler
    $("#resetbtn").click( function() {
        resetSettings();
        submit();
    });

    $('article').each(
        function() {
            if ($(this).find('h2 a').length)
                $(this).click(function() {window.location.href = ($(this).find('h2 a').attr('href'));})
        }
    );

    backgroundSelect();
    setTimeout("initMasonry()", 1000);
}
function initMasonry() {
$wall = $('.masonry');
    if ($wall.length) {
        $wall.masonry({
            columnWidth: 245,
            itemSelector: 'article',
            animate: true,
            animationOptions: {
                duration: 300,
                easing: 'linear',
                queue: true
            }
        });
    }
    $(".masonry article").css({visibility: "visible"});
}

function resized() {
    var windowWidth = $(window).width();
    $('body').removeClass('w990');
    $('body').removeClass('w1240');
    $('body').removeClass('w1490');
    if (windowWidth >= 1000 && windowWidth < 1265) {
        $('body').addClass('w990');
    } else if (windowWidth >= 1265 && windowWidth < 1515) {
        $('body').addClass('w1240');
    } else if (windowWidth >= 1515) {
        $('body').addClass('w1490');
    } else {

    }
}

window.onresize = resized;

/**
 * Restores default settings for customisation form.
 * Note: Default settings are not stored anywhere. Will select all checkboxes and set theme to theme1
 */
function resetSettings() {
    // Find the form and loop through each input inside the form
    $('#customisation').find('input').each(function() {
        if ($(this).is(':checkbox')) {
            // Select all checkboxes
            $(this).attr('checked', true);
        } else if ($(this).is(':hidden')) {
            // Select theme1
            $(this).attr('value', 'theme1');

            $(this).parent().find('img').each(function() {
                if ($(this).attr('alt') == 'theme1') {
                    // Mark theme1 thumbnail as selected
                    $(this).addClass('selected');
                } else {
                    // Remove classes from other thumbnail images
                    $(this).removeClass('selected');
                }
            });
        }
    });
}

function customise() {
    if (!$("#customise").hasClass('open')) {
        $("#customise-choises").slideDown(200);
        $("#customise").addClass('open');
    } else {
        $("#customise-choises").slideUp(200);
        $("#customise").removeClass('open');
    }
}

function backgroundSelect() {
    $('#custom-backgrounds img').click(
        function() {
            $("#custom-backgrounds img.selected").removeClass('selected');
            $(this).addClass('selected');
			$("#theme").val($(this).attr('alt'));
			//$("body").attr('class', '');
			//$("body").addClass($(this).attr('alt'));
        }
    )
}
