﻿
var dateToShow = "." + getQueryParam("dateToShow");

var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;


function getQueryParam(query) {
    var regexp = new RegExp(query + "=([^&]+)");
    var match = (window.location + "").match(regexp);
    return (match != null) ? match[1] : null;
}

$(document).ready(function () {

    //CALENDAR

    var group = $('<dl class="happenings"></dl>');
    $('#calendar').append(group);
    while ($('#calendar dl:first').children().length > 6) {
        $('#calendar dl:first dt:eq(3)').appendTo(group);
        $('#calendar dl:first dd:eq(3)').appendTo(group);
        if (group.children().length == 6) {
            group = $('<dl class="happenings"></dl>');
            $('#calendar').append(group);
        }
    }

    $('#calendar').removeClass('nojs');

    headline_count = $("dl.happenings").size();
    $("dl.happenings:eq(" + current_headline + ")").css('top', '4em');

    headline_interval = setInterval(headline_rotate, 6000); //time in milliseconds
    $('#calendar').hover(function () {
        clearInterval(headline_interval);
    }, function () {
        headline_interval = setInterval(headline_rotate, 6000); //time in milliseconds
        headline_rotate();
    });


    function headline_rotate() {
        current_headline = (old_headline + 1) % headline_count; //remainder will always equal old_headline until it reaches headline_count - at which point it becomes zero. clock arithmetic
        $("dl.happenings:eq(" + old_headline + ")").stop(true, true).animate({ top: '-24em' }, "slow", function () {
            $(this).css('top', '24em');
        });
        $("dl.happenings:eq(" + current_headline + ")").stop(true, true).animate({ top: '4em' }, "slow");
        old_headline = current_headline;
    }

    //TEASERS ENTIRE BLOCK CLICKABLE

    $('#teasers div, div#message').click(function () {
        window.location = $(this).find('h2 a, h1 a').attr('href');
    });

    $("#teasers p, div#message").hover(
		function () {
		    $(this).css("cursor", "pointer");
		},
		function () {
		    $(this).css("cursor", "default");
		});

    //ADD SPAN TO THE END OF EACH PARAGRAPH IN TEASER and MESSAGE BLOCKS

    $('<span class="linkColor">&raquo;</span>').appendTo('#teasers p, #message p');

    //FOCUS EFFECT ON SEARCHFIELD FIELDSET

    $('.courseSearchFreeText').focus(function () {
        $('.searchField').addClass('focus');
    });
    $('.courseSearchFreeText').blur(function () {
        $('.searchField').removeClass('focus');
    });

    //SHOW / HIDE ADVANCED SEARCH IN COURSE SEARCH 

    $('.moreLink a').click(function () {
        $('.more div').slideToggle("fast");
        $(this).toggleClass("active");
        return false
    });

    //SHOW / HIDE COURSES

    $(".accordion h3").hover(
		function () {
		    $(this).css("cursor", "pointer");
		},
		function () {
		    $(this).css("cursor", "default");
		});

    $(".accordion span").hover(
		function () {
		    $(this).css("cursor", "pointer");
		},
		function () {
		    $(this).css("cursor", "default");
		});

    var originalTitle = "Visa mer information"
    var newTitle = "Dölj information"
    var orgText = "Tider, kurslokal, etc. "
    var newText = "Dölj information "


    $(".accordion h3").attr("title", originalTitle);
    $(".accordion").find(dateToShow).addClass("active").attr("title", newTitle);
    $(".accordion .courseitem-info").hide();
    $(dateToShow).next("div").show();


    $(".accordion h3").click(function () {
        toggleDivsAndTexts(this, $(this).siblings("span"), $(this).next("div"));
        /*        var thisDiv = $(this).next("div");
        var otherDivs = $(this).parent().parent().find(".courseitem-info").not(thisDiv);
        $(thisDiv).slideToggle("fast");
        $(otherDivs).slideUp("fast");
        $(this).parent().parent().find("h3").removeClass("active").attr("title", originalTitle);
        $(this).toggleClass("active"); 


        if ($(this).hasClass("active"))
        $(this).attr("title", newTitle);
        else
        $(this).attr("title", originalTitle);
        */
    });

    $(".accordion span").click(function () {
        toggleDivsAndTexts($(this).siblings("h3"), this, $(this).prev("div"));
        /*var thisDiv = $(this).prev("div");
        var otherDivs = $(this).parent().parent().find(".courseitem-info").not(thisDiv);
        $(thisDiv).slideToggle("fast");
        $(otherDivs).slideUp("fast");
        //$(this).prev("div").slideToggle("fast").siblings("div:visible").slideUp("fast");
        $(this).parent().parent().find("span").removeClass("active").attr("title", originalTitle).text(orgText);
        $(this).toggleClass("active"); 

        if ($(this).hasClass("active"))
        $(this).text(newText);
        else
        $(this).text(orgText);
        */
    });

    function toggleDivsAndTexts(h3Object, spanObject, assDiv) {
        var otherDivs = $(h3Object).parent().find(".courseitem-info").not(assDiv);
        $(assDiv).slideUp("fast");
        $(otherDivs).slideToggle("fast");
        $(h3Object).parent().parent().find("span").not(spanObject).removeClass("active").attr("title", originalTitle).text(orgText);
        // $(h3Object).parent().parent().find("h3").not(h3Object).removeClass("active").attr("title", originalTitle);
        // $(h3Object).toggleClass("active"); 
        $(spanObject).toggleClass("active");

        if ($(spanObject).hasClass("active")) {
            $(spanObject).text(newText);
            $(h3Object).attr("title", newTitle);
        } else {
            $(spanObject).text(orgText);
            $(h3Object).attr("title", originalTitle);
        }

    }

    // SEARCH INPUT SHOW HIDE TEXT


    /*$('input[type="text"]').addClass("idleField");
    $('input[type="text"]').focus(function() {
    $(this).removeClass("idleField").addClass("focusField");
    if(this.value != this.defaultValue){
    this.select();
    }
    });
		
    $('input[type="text"]').blur(function() {
    $(this).removeClass("focusField").addClass("idleField");
    if ($.trim(this.value) == ''){
    this.value = (this.defaultValue ? this.defaultValue : '');
    }
    });*/

    // STYLING 

    $(".accordion h3:first-child").css("border", "none");

    // Show or hide 'Lägg till kurs' area
    $('a.collapsibleHeadingAddCourses').click(function () {
        $('div.quickCoursePanel').toggle();
        //$('.qp_holder').toggle();
    });

    if ($('#noCoursesSelected').text() != '') {
        $('div.quickCoursePanel').show();
    };

    // Expand or collapse Handläggare and Faktura fields
    $('a.collapsibleHeadingAdmin').click(function () {
        $('fieldset.collapsibleFieldAdmin').toggle();
    });

    $('a.collapsibleHeadingInvoice').click(function () {
        $('fieldset.collapsibleFieldInvoice').toggle();
    });

    //Shopping cart.

    $(function () {
        $("#sc_showbasket").click(function () {
            $("#sc_cartlist").slideToggle("slow");
        });
    });

    if ($('#shoppingbasket').length) {
        var top = $('#shoppingbasket').offset().top - parseFloat($('#shoppingbasket').css('marginTop').replace(/auto/, 0));
        $(window).scroll(function (event) {

            // what the y position of the scroll is
            var y = $(this).scrollTop();

            // whether that's below the form
            if (y >= top) {
                // if so, ad the fixed class
                $('#shoppingbasket').addClass('fixed');
            } else {
                // otherwise remove it
                $('#shoppingbasket').removeClass('fixed');
            }
        });
    }

    /**
    * Make the alphabetic navigation float while scrolling
    */
    if ($('#alphabetic_navigation').length) {
        //var nav_top = $('#alphabetic_navigation').offset().top - parseFloat($('#alphabetic_navigation').css('marginTop').replace(/auto/, 0));
        var nav_top = $('#courses').offset().top;
        $(window).scroll(function (event) {
            // what the y position of the scroll is
            var y = $(this).scrollTop();

            // whether that's below the form
            if (y >= nav_top) {
                // if so, ad the fixed class
                $('#alphabetic_navigation').addClass('fixed');
            } else {
                // otherwise remove it
                $('#alphabetic_navigation').removeClass('fixed');
            }
        });
    }

    /**
    * Tab navigation on search results page
    */
    if ($('ul.tabNavigation').length) {

        //console.log("hej");

        var tabContainers = $('#courses > div');

        $('#tabs ul.tabNavigation a').click(function () {

            tabContainers.hide().filter(this.hash).show();

            $('#tabs ul.tabNavigation li').removeClass('active');
            $(this).parent().addClass('active');

            return false;
        }).filter(':first').click();
    };

    /**
     * Toggle "show more info" on search result page
     */
    if ($('div.searchResults').length) {
        $('.show-more-holder a').click(function () {
            $(this).parent().parent().find('.more-info').slideToggle('slow');
            return false;
        });
    }

    /**
     * Toggle "same address" on order page
     * Clear values if you select "same address"
     * Select "Other address" if you have other address data in your cookies
     */
    if ($('div.content_level3').length) {

        $('input[type="radio"]').each(function (index) {
            $(this).parent().parent().parent().parent().find('.content_level3').hide();
            $(this).parent().parent().parent().find('input[type="radio"]').first().attr('checked', true);
            $(this).parent().parent().parent().parent().find('input[type="text"]').each(function (index) {
                if ($(this).val() != "") {
                    $(this).parent().parent().show();
                    $(this).parent().parent().parent().find('input[type="radio"]').first().next().next().next().attr('checked', true);
                }
            });
        });

        $('input[type="radio"]').click(function () {
            $(this).parent().parent().parent().parent().find('input').each(function (index) {
                $(this).val("");
            });
            $(this).parent().parent().parent().parent().find('.content_level3').slideToggle('slow');
        });
    }
});
