﻿$(document).ready(function () {

    $(".openquerywindow").click(function () {
        $(".showquerywindow").click();
        return false;
    });

    // Advertisement
    if ($('#AdvertisementFrontpage > div').length > 1) {
        setInterval('switchAdvertisementFrontpage()', 4000);
    }

    /* Work-a-round for the missing of :first-child CSS in IE6 */
    $('.frontpage_row1 .item:first').addClass('first-child');
    $('.frontpage_row2 .item:first').addClass('first-child');
    $('ul.AspNet-Menu li:first').addClass('first-child');

    $('.readmore').click(function() { $('.event_text .textpreview').hide(); $('.event_text .textfull').show(); });

    $('.contentoffer .offers > div a').click(function(event) { event.preventDefault(); });

    $('.contentoffer .offers > div').click(function() {

        var offerLink = $(this).find('a');

        if (offerLink.attr('target') == '') { document.location = offerLink.attr('href'); } else { window.open(offerLink.attr('href'), offerLink.attr('target')); }

    });

    var divCount = 0;

    $('#top5events > div').each(function() { divCount++; });

    if (divCount > 1) { setInterval('eventSwitch()', 5000); }


    divCount = 0;

    $('.offers > div').each(function() { divCount++; });

    if (divCount > 1) { setInterval('currentofferSwitch()', 5000); }

    // -- START PÅ OVERLAY --
    $("a#doi_openerlink").fancybox({
        "width": "800",
        "height": "500",
        "showCloseButton": false,
        "padding": 0,
        "scrolling": false,
        "enableEscapeButton": false,
        "overlayColor": "#000000"
    });

    $('#doi_openerlink').trigger('click');
    // -- SLUT PÅ OVERLAY --
});

// -- START PÅ OVERLAY --
function doi_close() {
    $.fancybox.close();
}
// -- SLUT PÅ OVERLAY --

function validateUserTerms(source, clientside_arguments) { clientside_arguments.IsValid = ($("input[id$='cbox868']").is(':checked') || $("input[id$='cbox867']").is(':checked') || $("input[id$='cbox866']").is(':checked') || $("input[id$='cbox865']").is(':checked')); }

function switchAdvertisementFrontpage() {

    var $tohide = $("#AdvertisementFrontpage > div:not(.Hidden)");
    var $toshow = $tohide.next();

    if ($toshow.length == 0) { $toshow = $('#AdvertisementFrontpage > div:first'); }

    $toshow.fadeIn(1500, function () { $toshow.removeClass('Hidden'); });
    $tohide.fadeOut(1500, function () { $tohide.addClass('Hidden'); });
    //$tohide.addClass('Hidden');
    //$toshow.removeClass('Hidden');
}

function eventSwitch() {

    var $old = null;
    var $next = null;

    $('#top5events > div').each(function() {

        if ($old != null && $next == null) {

            $next = $(this);
        }

        if ($(this).attr('class') == 'top5image') {

            $old = $(this);
        }
    });

    if ($next == null) {

        $next = $('#top5events > div:first');
    }

    if ($old == null || $next == null) {
        return;
    }

    $next.fadeIn(1500, function() { $next.removeClass('top5image_in').addClass('top5image'); });
    $old.fadeOut(1500, function() { $old.removeClass('top5image').addClass('top5image_in'); });
}

function currentofferSwitch() {

    var $old = null;
    var $next = null;

    $('.offers > div').each(function() {

        if ($old != null && $next == null) {

            $next = $(this);
        }

        if ($(this).attr('class') == 'active') {

            $old = $(this);
        }
    });

    if ($next == null) {

        $next = $('.offers > div:first');
    }

    if ($old == null || $next == null) {
        return;
    }

    $next.fadeIn(1500, function() { $next.removeClass('inactive').addClass('active'); });
    $old.fadeOut(1500, function() { $old.removeClass('active').addClass('inactive'); });
}
