/*--------------------------------------------------------------
*   settings & intialization
--------------------------------------------------------------*/

//globals
var undefined;
var PAGE ={};
var MENUCONTROLLER = {};
var CONTENTPAGER = {};
var CONTENTROTATOR = {};
var CONTENTROTATORSMALL = {};
var FORMS = {};
var SHOPSEARCH = {};
var POINTTABLE = {};
var FAQ = {};
var POPUPS = {};
var TOOLTIP ={};
var RADIOHELPER ={};
var AGILLICUTILS ={};
var CYCLE = {};
var BABYCLUBCOMPONENT = {};
var FACEBOOK = {};
var PARTNERS = {};
var PARTNERS_CONTENTPAGER = {};

//DOM initialization
jQuery(document).ready(function() {

    // general initialization without vars
    MENUCONTROLLER.init();

    PAGE.vars = {
        httpDomain:   undefined,
        name:   undefined,
        path:   undefined
    };

    BABYCLUBCOMPONENT.classes = {
        comingchildren: 'wunBabyClubComingChildren',
        comingchildrennum: 'wunBabyClubComingChildrenNum',
        comingchilddate: 'wunBabyClubComingChildDate',
        childrenbirthdates: 'wunChildDate',
        childrennum : 'wunNumChildren'
    };

    BABYCLUBCOMPONENT.elems = {
        comingchildren: undefined,
        comingchildrennum: undefined,
        comingchilddate: undefined,
        childrenbirthdates: undefined,
        childrennum: undefined
    };

    //CONTENTROTATOR settings
    CONTENTROTATOR.classes = {
        container: 'wunRotatingContainer',
        steps:     'wunRotatingSteps',
        step:      'wunStep'
    };

    CONTENTROTATOR.elems = {
        container:  jQuery('.'+CONTENTROTATOR.classes.container),
        steps:      undefined,
        active:     undefined
    };

    CONTENTROTATOR.vars = {
        idleTime:   5000,
        timeout:    undefined,
        interval:   undefined
    };

    //CONTENTROTATORSMALL settings
    CONTENTROTATORSMALL.classes = {
        container: 'wunRotatingContainerSmall',
        steps:     'wunRotatingStepsCentered',
        step:      'wunStep'
    };

    CONTENTROTATORSMALL.elems = {
        container:  jQuery('.'+CONTENTROTATORSMALL.classes.container),
        steps:      undefined,
        active:     undefined
    };

    CONTENTROTATORSMALL.vars = {
        idleTime:   5000,
        timeout:    undefined,
        interval:   undefined
    };

    //FORMS settings
    FORMS.classes = {
        error:      'wunError',
        focus:      'wunFocus',
        disablefocus: 'wunDisableFocus',
        forms:      'wunForm',
        submits:    'wunSubmit',
        fields: {
            texts:      'wunTextBox',
            radios:     'wunRadioButton',
            dropdowns:  'wunDropDown',
            checkboxgroup: 'wunCheckBoxGroup',

            birthdate:  'wunDate',
            mobile:     'wunPhone',
            children:   'wunNumChildren',
            childdate:  'wunChildDate'
        }
    };

    FORMS.elems = {
        forms:      jQuery('.'+FORMS.classes.forms),
        submits:    undefined,
        fields: {
            texts:      undefined,
            radios:     undefined,
            dropdowns:  undefined,

            birthdate:  undefined,
            mobile:     undefined,
            children:   undefined,
            childdate:  undefined
        }
    };

    FORMS.vars = {
        submitted: false,
        allowed: {
            date:  /[\d]/,
            phone: /[\d]/
        },
        validation: {
            date: /^(0[1-9]|[1-2][0-9]|3[0-1])\.(0[1-9]|1[0-2])\.[1-2]{1}[0-9]{3}$/
        },
        length: {
            date:  10,
            phone: 8
        }
    };

    //SHOPSEARCH
    SHOPSEARCH.classes = {
        field:      'wunShopSearch',
        dropdown:   'wunShopSearchDropdown',
        wrap1:      'wunWrap1',
        results:    'wunResults',
        noresults:  'wunNoResults',
        street:     'wunAddress',
        fieldValue: 'wunShopSelected'
    };

    SHOPSEARCH.elems = {
        field:      jQuery('.'+SHOPSEARCH.classes.field),
        dropdown:   undefined,
        wrap1:      undefined,
        results:    undefined,
        noresults:  undefined,
        selected:   undefined,
        fieldValue: undefined
    };

    SHOPSEARCH.vars = {
        noresults:  'ingen match',
        url:        '/api/searchButiks.jsp',
        value:      undefined,
        active:     false,
        submitted:  false,
        searchresults: undefined
    };

    //POINTTABLE
    POINTTABLE.classes = {
        tables:     'wunExpandable'
    };

    POINTTABLE.selectors = {
        theads:     'thead',
        links:      'a'
    };

    POINTTABLE.elems = {
        tables:     jQuery('.'+POINTTABLE.classes.tables),
        theads:     undefined,
        links:      undefined
    };

    //POINTTABLE
    FAQ.classes = {
        items:  'wunFAQItem'
    };

    FAQ.selectors = {
        headlines:  'h4'
    };

    FAQ.elems = {
        items:      jQuery('.'+FAQ.classes.items),
        headlines:  undefined
    };

    //CONTENTPAGER settings
    CONTENTPAGER.classes = {
        container:  'wunContentPager',
        content:    'wunContent',
        panel:      'wunControlPanel',
        wrap:       'wunWrap1',
        marker:     'wunMarker',
        steps:      'wunStep',
        pause:      'wunPause',
        play:       'wunPlay'
    };

    CONTENTPAGER.elems = {
        container:  jQuery('.'+CONTENTPAGER.classes.container),
        content:    undefined,
        panel:      undefined,
        wrap:       undefined,
        marker:     undefined,
        steps:      undefined,
        pause:      undefined,
        play:       undefined,
        active:     undefined
    };

    CONTENTPAGER.vars = {
        idleTime:   10000,
        interval:   undefined,
        paused:     false,
        labels:       {
            play:    'Play',
            pause:   'Pause'
        },
        widths: {
            marker: undefined,
            steps: undefined
        }
    };

    FACEBOOK.vars = {
        fbAppID:            undefined,
        fbCaption:          'www.clubmatas.dk',
        fbDialog:           'http://www.facebook.com/dialog/feed?app_id=',
        fbRedirectNoDomain: '/api/facebook/popupclose.html',
        fbRedirect:          undefined,
        fbName:              undefined,
        fbPicture:           undefined,
        fbDecription:        undefined
    };

    FACEBOOK.classes = {
        mainClass:       'wunFacebookShared'
    };

    //PARTNERS_CONTENTPAGER settings
    PARTNERS_CONTENTPAGER.classes = {
        container: 'wunContentPager1',
        content: 'wunContent1',
        panel: 'wunControlPanel1',
        wrap: 'wunWrap11',
        marker: 'wunMarker1',
        steps: 'wunStep1',
        pause: 'wunPause1',
        play: 'wunPlay1'
    };
    PARTNERS_CONTENTPAGER.elems = {
        container: jQuery('.' + PARTNERS_CONTENTPAGER.classes.container),
        content: undefined,
        panel: undefined,
        wrap: undefined,
        marker: undefined,
        steps: undefined,
        pause: undefined,
        play: undefined,
        active: undefined,
        pathBullets: '/web/bcmresource/media/resources/clubmpartnereresources/Images/bullets_unselected.png'
    };
    PARTNERS_CONTENTPAGER.vars = {
        idleTime: 10000,
        interval: undefined,
        paused: false,
        labels: {
            play: 'Play',
            pause: 'Pause'
        },
        widths: {
            marker: undefined,
            steps: undefined
        }
    };

    // general initialization
    PAGE.init();
    CONTENTPAGER.init();
    CONTENTROTATOR.init();
    CONTENTROTATORSMALL.init();
    FORMS.init();
    SHOPSEARCH.init();
    POINTTABLE.init();
    FAQ.init();
    POPUPS.init();
    TOOLTIP.init();
    CYCLE.init();
    AGILLICUTILS.init();
    FACEBOOK.init();
    PARTNERS.init();
    PARTNERS_CONTENTPAGER.init();
});

/*--------------------------------------------------------------
*   Page settings
--------------------------------------------------------------*/
PAGE.init = function(){
    PAGE.vars.httpDomain = 'http://' + document.domain;
    PAGE.vars.path = window.location.pathname;
    PAGE.vars.name = PAGE.vars.path.substring(PAGE.vars.path.lastIndexOf('/') + 1);
};

/*--------------------------------------------------------------
*   Radio button helper (for poll/quiz)
--------------------------------------------------------------*/

RADIOHELPER.isValid = function(sender, content) {
    var _form = jQuery('form');
    var _radios = _form.find('input:radio');

    if(_radios.filter(':checked').length > 0) {
        return true;
    }
    else {
        POPUPS.showCommon(content);
        return false;
    }
};

BABYCLUBCOMPONENT.init = function(){
    BABYCLUBCOMPONENT.elems.comingchildren = jQuery('.' + BABYCLUBCOMPONENT.classes.comingchildren);
    BABYCLUBCOMPONENT.elems.comingchildrennum = jQuery('.' + BABYCLUBCOMPONENT.classes.comingchildrennum);
    BABYCLUBCOMPONENT.elems.comingchilddate = jQuery('.' + BABYCLUBCOMPONENT.classes.comingchilddate).next().next().next().find('select');
    BABYCLUBCOMPONENT.elems.childrenbirthdates = jQuery('.' + BABYCLUBCOMPONENT.classes.childrenbirthdates).next().next().next().find('select');
    BABYCLUBCOMPONENT.elems.childrennum = jQuery('.' + BABYCLUBCOMPONENT.classes.childrennum);

    BABYCLUBCOMPONENT.elems.comingchildren.click(BABYCLUBCOMPONENT.comingChildrenChanged);

    BABYCLUBCOMPONENT.elems.comingchildrennum.change(BABYCLUBCOMPONENT.comingChildrenNumChanged);
    BABYCLUBCOMPONENT.elems.childrennum.change(BABYCLUBCOMPONENT.babyClubMember);
    BABYCLUBCOMPONENT.elems.childrenbirthdates.bind('blur', BABYCLUBCOMPONENT.childrenBirthdateChanged);

    BABYCLUBCOMPONENT.elems.childrenbirthdates.each(function(i, elem){
        BABYCLUBCOMPONENT.threeYearsOldCheck(jQuery(elem));
    });

    BABYCLUBCOMPONENT.comingChildrenChanged();
};

BABYCLUBCOMPONENT.comingChildrenChanged = function(){
    var elem = jQuery('.' + BABYCLUBCOMPONENT.classes.comingchildren + ':checked');
    if(jQuery(elem[0]).val() == 'true'){
        jQuery(BABYCLUBCOMPONENT.elems.comingchildrennum.parents('div.wunFormSection')[0]).removeClass('wunHideMe');
        BABYCLUBCOMPONENT.elems.comingchildrennum.change();
    }else{
        BABYCLUBCOMPONENT.elems.comingchildrennum.val('0');
        BABYCLUBCOMPONENT.elems.comingchildrennum.change();
        jQuery(BABYCLUBCOMPONENT.elems.comingchildrennum.parents('div.wunFormSection')[0]).addClass('wunHideMe');
    }
};

BABYCLUBCOMPONENT.comingChildrenNumChanged = function(){
    var elem = jQuery(this);
    if(elem.context.selectedIndex > 0){
        jQuery(BABYCLUBCOMPONENT.elems.comingchilddate.parents('div.wunFormSection')[0]).removeClass('wunHideMe');
    }else{
        jQuery(BABYCLUBCOMPONENT.elems.comingchilddate.parents('div.wunFormSection')[0]).addClass('wunHideMe');
    }
    BABYCLUBCOMPONENT.babyClubMember();
};

BABYCLUBCOMPONENT.childrenBirthdateChanged = function() {
    BABYCLUBCOMPONENT.threeYearsOldCheck(jQuery(this));
    BABYCLUBCOMPONENT.babyClubMember();
};

BABYCLUBCOMPONENT.threeYearsOldCheck = function(elem){
    var component = elem.parent();
    var dateTime = component.find('select');
    component.removeClass('showBabyClub');
    var day = dateTime[0].value;
    var month = dateTime[1].value;
    var year = dateTime[2].value;
    if(day != '' && month != '' && year != ''){
        var birthDate = new Date(year, month, day);
        var tmp = jQuery('#threeYearsAgo').val();
        var threeYearsAgo = new Date(tmp.substr(6,4),tmp.substr(3,2),tmp.substr(0,2));
        if(threeYearsAgo.getTime() <= birthDate.getTime()){
            component.addClass('showBabyClub');
        }
    }
};

BABYCLUBCOMPONENT.babyClubMember = function(){
    if(jQuery('.showBabyClub:visible').length > 0 || BABYCLUBCOMPONENT.elems.comingchilddate.is(':visible')){
        jQuery('.wunBabyClub').removeClass('wunHideMe');
    }else{
        jQuery('.wunBabyClub').addClass('wunHideMe');
        jQuery('.wunBabyClub input[name="BABY_CLUB_PERMISSION"]').removeAttr("checked");
    }
};

/*--------------------------------------------------------------
*   Cycle
--------------------------------------------------------------*/
CYCLE.init = function(){
    var elem = $('.wunCycle');
    if(elem != null && elem.length != 0){
        if(elem.children().length == 1){
            jQuery(elem.children()[0]).show();
        }else{
            elem.cycle({
                fx: 'fade',
                speed: 1000,
                timeout: 5000
            });
        }
    }   
}

/*--------------------------------------------------------------
*   User navigation menu controller
--------------------------------------------------------------*/
MENUCONTROLLER.init = function() {
    $(".wunMenu ul li:first-child").addClass("first");

    var menuContainer = $(".wunMenu");
    var menu = $(".wunMenu ul");
    var menuItems = $(".wunMenu ul li").size() - 2;
    var excludeItemsW = ($(".wunPointbalance").outerWidth() + $(".wunWelcomeNote").outerWidth());

    var average = (menuContainer.width() - excludeItemsW) / menuItems;

    var menuItemsTotalWidth = 0;

    //Set width to its child width
    $(".wunMenu ul li").not("li.wunPointbalance, li.wunWelcomeNote").each(function() {

        //Set width to average width
        $("a", this).width(average);
        //Set width of link equal to its child
        var linkContainerW = $("a span", this).width();
        $("a", this).width(linkContainerW);

        menuItemsTotalWidth += $(this).width();

    });

    //If the menu isn't full width, fill the extra pixels onto each menuitem
    var menuWidth = menuItemsTotalWidth + excludeItemsW;

    if ($(menuContainer).width() >=! menuWidth) {

        var differenceAverage = ($(menuContainer).width() - menuWidth) / menuItems;

        $(".wunMenu ul li").not(".wunPointbalance, .wunWelcomeNote").each(function() {
            var menuItemNewWidth = $("a", this).parent().width() +1;
            //var menuItemNewWidth = $("a", this).width() + differenceAverage;
            $("a", this).width(menuItemNewWidth);

            //Add class that adds half padding, on double lines
            if ($("span", this).height() >= 20) {
                $(this).addClass("doublelines");
            } else {
                $(this).addClass("singlelines");
            }

        });
    }

    /*
         //If the menu is wider than full width, extract the extra pixels from each item
         var menuWidth = menuItemsTotalWidth + excludeItemsW;
         alert(menuWidth);
         alert($(menuContainer).width());

         if ($(menuContainer).width() < menuWidth) {
             alert("Den er for bred");
             var tooWidePx = (menuWidth - $(menuContainer).width());
             var tooWideAverage = tooWidePx / menuItems;

             $(".wunMenu ul li").not(".wunPointbalance, .wunWelcome").each(function() {
                 var menuItemNewWidth = $("a", this).width() - tooWideAverage;
                 $("a", this).width(menuItemNewWidth);
             });

         }*/

}

/*--------------------------------------------------------------
*   Content pager with automated rotation
--------------------------------------------------------------*/
CONTENTPAGER.init = function() {

    if(CONTENTPAGER.elems.container.length > 0) {

        CONTENTPAGER.elems.container    = CONTENTPAGER.elems.container.first();
        CONTENTPAGER.elems.content      = CONTENTPAGER.elems.container.children('.' + CONTENTPAGER.classes.content).first();
        CONTENTPAGER.elems.panel = jQuery('<div></div>').addClass(CONTENTPAGER.classes.panel);
        CONTENTPAGER.elems.wrap = jQuery('<div></div>').addClass(CONTENTPAGER.classes.wrap);
        CONTENTPAGER.elems.marker = jQuery('<div></div>').addClass(CONTENTPAGER.classes.marker);

        CONTENTPAGER.elems.pause = jQuery('<div></div>')
                .addClass(CONTENTPAGER.classes.pause)
                .click(CONTENTPAGER.pause);

        CONTENTPAGER.elems.play = jQuery('<div></div>')
                .addClass(CONTENTPAGER.classes.play)
                .click(CONTENTPAGER.play)
                .hide();

        CONTENTPAGER.elems.wrap.append(CONTENTPAGER.elems.marker);

        for(i=0; i < CONTENTPAGER.elems.content.children().length; i++) {

            CONTENTPAGER.elems.wrap.append(
                    jQuery('<div></div>')
                            .addClass(CONTENTPAGER.classes.steps)
                            .text(i+1)
                            .click(CONTENTPAGER.goToThis)
                    );
        }

        CONTENTPAGER.elems.steps = CONTENTPAGER.elems.wrap.children('.'+CONTENTPAGER.classes.steps);

        CONTENTPAGER.elems.wrap.append(CONTENTPAGER.elems.pause);
        CONTENTPAGER.elems.wrap.append(CONTENTPAGER.elems.play);

        CONTENTPAGER.elems.panel.append(CONTENTPAGER.elems.wrap);

        CONTENTPAGER.elems.container.append(CONTENTPAGER.elems.panel);

        CONTENTPAGER.vars.widths.marker = CONTENTPAGER.elems.marker.width();
        CONTENTPAGER.vars.widths.steps = CONTENTPAGER.elems.steps.width();

        CONTENTPAGER.goto(1);
    }
};

CONTENTPAGER.goto = function(num) {

    if(CONTENTPAGER.elems.active != undefined) {

        if(CONTENTPAGER.elems.active.index()+1 != num) {
            CONTENTPAGER.elems.active.fadeOut(1000);
            CONTENTPAGER.elems.steps.css('color', '');
        }
    }

    CONTENTPAGER.elems.active = CONTENTPAGER.elems.content.children(':nth-child(' + (num) + ')');
    CONTENTPAGER.elems.active.fadeIn(1000);

    CONTENTPAGER.elems.marker.animate({
        left: ( (num-1) * CONTENTPAGER.vars.widths.steps ) + ( (CONTENTPAGER.vars.widths.marker/2) - (CONTENTPAGER.vars.widths.steps/2) )
    }, 500, function() {
        jQuery(CONTENTPAGER.elems.steps.get(num-1)).css('color', '#ffffff');
    });


    if(!CONTENTPAGER.vars.paused) {
        CONTENTPAGER.play();
    }
};

CONTENTPAGER.goToThis = function(){
    CONTENTPAGER.goto(jQuery(this).index());
}

CONTENTPAGER.goToNext = function() {

    if (CONTENTPAGER.elems.active.next().html() == null) {
        CONTENTPAGER.goto(CONTENTPAGER.elems.steps.first().index());
    }
    else {
        CONTENTPAGER.goto(CONTENTPAGER.elems.active.next().index()+1);
    }
};


CONTENTPAGER.pause = function() {

    clearInterval(CONTENTPAGER.vars.interval);

    CONTENTPAGER.elems.play.show();
    CONTENTPAGER.elems.pause.hide();
};

CONTENTPAGER.play = function() {

    clearInterval(CONTENTPAGER.vars.interval);

    CONTENTPAGER.elems.play.hide();
    CONTENTPAGER.elems.pause.show();

    CONTENTPAGER.vars.interval = setInterval('CONTENTPAGER.goToNext()', CONTENTPAGER.vars.idleTime);
};

/*--------------------------------------------------------------
*   FAQ list
--------------------------------------------------------------*/

FAQ.init = function() {

    if(FAQ.elems.items.length > 0) {

        FAQ.elems.headlines =  FAQ.elems.items.find(FAQ.selectors.headlines);
        FAQ.elems.headlines.click(FAQ.toggle);

    }
};

FAQ.toggle = function(){

    var sender = jQuery(this);
    var target = sender.next();
    var newTargetDesign = sender.parents(".wunFLtop").length;
    var logoImage = undefined;
    var logoImageSrc = undefined;
    var logoImageExtension = undefined;
    if (newTargetDesign > 0){
       target = jQuery(sender.parents(".wunFLtop")).siblings(".wunRichText").first();
       logoImage = jQuery(jQuery(sender.parents(".wunFLtop")).siblings(".wun_partner_logo").first()).find("img");
       logoImageSrc = logoImage.attr("src").split(".")[0];
       logoImageExtension = logoImage.attr("src").split(".")[1];
    }

    if(target.height() > 0) {

        target.animate({
            height: 0
        }, function(){
            sender.parent().removeClass('wunFaqExpanded');
        });

        if (newTargetDesign > 0){
           target.hide('slow');
           logoImage.attr('src','' + logoImageSrc.replace('_mo','') +'.' + logoImageExtension);
        }
    }else {

        var height = target
                .css('overflow', 'visible')
                .css('height', 'auto')
                .height();

        target
                .css('overflow', 'hidden')
                .css('height', '0px')
                .animate({
            height: height
        },
                function() {
                    jQuery(this).css('height', 'auto');
                    sender.parent().addClass('wunFaqExpanded');
                });

        if (newTargetDesign > 0){
           logoImage.attr('src','' + logoImageSrc.replace('_mo','') +'_mo.' + logoImageExtension);
        }
    }
};


/*--------------------------------------------------------------
*   Point table
--------------------------------------------------------------*/

POINTTABLE.init = function() {

    if(POINTTABLE.elems.tables.length > 0) {

        POINTTABLE.elems.theads = POINTTABLE.elems.tables.find(POINTTABLE.selectors.theads);
        POINTTABLE.elems.links = POINTTABLE.elems.tables.find(POINTTABLE.selectors.links);

        POINTTABLE.elems.theads.click(POINTTABLE.expandMonth);
        POINTTABLE.elems.links.click(POINTTABLE.expandPurchase);
    }
};

POINTTABLE.expandMonth = function(){

    var sender = jQuery(this);
    var target = sender.parent().next();

    if(target.height() > 0) {

        target.animate({
            height: 0
        }, function(){
            sender.removeClass('wunExpanded');
        });
    }
    else {

        var height = target
                .css('overflow', 'visible')
                .css('height', 'auto')
                .height();

        target
                .css('overflow', 'hidden')
                .css('height', '0px')
                .animate({
            height: height
        },
                function() {
                    jQuery(this).css('height', 'auto');
                    sender.addClass('wunExpanded');
                });
    }
};

POINTTABLE.expandPurchase = function(){

    var sender = jQuery(this);
    var target = sender.next();

    if(target.height() > 0) {

        target.animate({
            height: 0
        }, function(){
            sender.parent().parent().removeClass('wunExpanded');
        });
    }
    else {

        var height = target
                .css('overflow', 'visible')
                .css('height', 'auto')
                .height();

        target
                .css('overflow', 'hidden')
                .css('height', '0px')
                .animate({
            height: height
        },
                function() {
                    jQuery(this).css('height', 'auto');
                    sender.parent().parent().addClass('wunExpanded');
                });
    }

    return false;
};


/*--------------------------------------------------------------
*   Forms interaction
--------------------------------------------------------------*/

FORMS.init = function() {

    if(FORMS.elems.forms.length > 0){

        FORMS.elems.submits =    FORMS.elems.forms.find('.'+FORMS.classes.submits);
        FORMS.elems.fields = {
            texts:      FORMS.elems.forms.find('.'+FORMS.classes.fields.texts),
            radios:     FORMS.elems.forms.find('.'+FORMS.classes.fields.radios),
            dropdowns:  FORMS.elems.forms.find('.'+FORMS.classes.fields.dropdowns),

            birthdate:  FORMS.elems.forms.find('.'+FORMS.classes.fields.birthdate),
            mobile:     FORMS.elems.forms.find('.'+FORMS.classes.fields.mobile),
            children:   FORMS.elems.forms.find('.'+FORMS.classes.fields.children),
            childdate:  FORMS.elems.forms.find('.'+FORMS.classes.fields.childdate)
        };

        FORMS.elems.submits.click(FORMS.click)
        FORMS.elems.forms.keyup(FORMS.keyup);

        FORMS.elems.fields.texts.focus(FORMS.focus);
        FORMS.elems.fields.texts.blur(FORMS.blur);

        FORMS.elems.fields.radios.focus(FORMS.focus);
        FORMS.elems.fields.radios.blur(FORMS.blur);

        FORMS.elems.fields.dropdowns.focus(FORMS.focus);
        FORMS.elems.fields.dropdowns.blur(FORMS.blur);

        //FORMS.elems.fields.birthdate.keypress(FORMS.checkKeyDate);
        //FORMS.elems.fields.birthdate.keyup(FORMS.formatDate);
        //FORMS.elems.fields.birthdate.blur(FORMS.blurOnDate);
        //FORMS.elems.fields.birthdate.focus(FORMS.focusOnDate);        

        FORMS.elems.fields.children.change(FORMS.numChildren);
        FORMS.elems.fields.children.change();

        FORMS.elems.fields.childdate.keypress(FORMS.checkKeyDate);
        FORMS.elems.fields.childdate.keyup(FORMS.formatDate);
        FORMS.elems.fields.childdate.blur(FORMS.blur);

        FORMS.elems.fields.mobile.keypress(FORMS.checkKeyPhone);
        FORMS.elems.fields.mobile.keyup(FORMS.formatPhone);
        FORMS.elems.fields.mobile.blur(FORMS.formatPhone);

        FORMS.validate(FORMS.elems.forms);
    }
};

FORMS.numChildren = function(){

    var dropdown = jQuery(this);
    var rows = jQuery('.wunChildren tr');

    rows.each(function(index){
        jQuery(this).toggleClass("wunActive", dropdown.context.selectedIndex > index+1);
    });
};

FORMS.click = function() {

    jQuery(this).parents('form').submit();
};

FORMS.checkKeyDate = function(event){

    //backspace in firefox
    if(event.which < 32) return true;

    var validKey = FORMS.vars.allowed.date.test( String.fromCharCode(event.which) );
    var validLength = jQuery(this).val().length < FORMS.vars.length.date;

    return validKey;
};

FORMS.formatDate = function(event){

    if(event.keyCode == 8) {
        return true;
    }

    jField = jQuery(this);
    var value = jField.val().split(' ').join('');

    value = FORMS.sanitize(value, FORMS.vars.allowed.phone);

    if(value.length > 2) {
        value = value.substr(0,2) + '.' + value.substr(2);
    }

    if(value.length > 5) {
        value = value.substr(0,5) + '.' + value.substr(5);
    }

    if(value.length > 10) {
        value = value.substr(0,10);
    }
    jField.val(value);
    return true;
};

FORMS.checkKeyPhone = function(event){

    //backspace in firefox
    if(event.which == 8) return true;

    var validKey = FORMS.vars.allowed.phone.test( String.fromCharCode(event.which) );
    var validLength = jQuery(this).val().length < FORMS.vars.length.phone;

    return validKey && validLength;
};

FORMS.formatPhone = function(event){

    //backspace
    if(event.keyCode == 8) {
        return true;
    }

    jField = jQuery(this);

    var value = jField.val().split(' ').join('');
    value = FORMS.sanitize(value, FORMS.vars.allowed.phone);

    if(value.indexOf('0045') == 0) {
        value = value.substr(4);
    }

    if(value.length > 8) {
        value = value.substr(0,8);
    }

    jField.val(value);

    return true;
};

FORMS.sanitize = function(value, regex) {

    var chars = value.split('');
    var sanitized = [];

    for(i=0; i < chars.length; i++) {

        if(regex.test(chars[i])) {
            sanitized[sanitized.length] = chars[i];
        }
    }

    return sanitized.join('');
};

FORMS.keyup = function(event) {

    switch (event.keyCode) {

        case 13: // Enter key
            jQuery(this).submit();
            break;
        case 27: // Esc key
        case 38: // Arrow up
        case 40: // Arrow down
        default:
            break;
    }
};

FORMS.validate = function(forms){

    jQuery.validator.addMethod("checkboxgroup", function(value, element) {
        return jQuery(element).parents('.' + FORMS.classes.fields.checkboxgroup).find('input:checkbox:checked').length > 0;
    }, "");

    jQuery.validator.addMethod("customfeaturedate", function(value, element) {
        var elem = jQuery(element);
        if(elem.is(':hidden')){
            return true;
        }
        try{
            var curTime = (new Date()).getTime();
            var birthTime = (new Date(elem.val().substr(6,4),elem.val().substr(3,2),elem.val().substr(0,2))).getTime();
            return birthTime > curTime;
        }catch(e){
            return false;
        }
    }, "");

    jQuery.validator.addMethod("customdate", function(value, element) {
        if(jQuery(element).is(':hidden')){
            return true;
        }
        return this.optional(element) || FORMS.vars.validation.date.test(value);
    }, "");

    jQuery.validator.addMethod("dropdown", function(value, element) {
        if(jQuery(element).is(':hidden')){
            return true;
        }
        return this.optional(element) || element.selectedIndex > 0;
    }, "");

    jQuery.validator.addMethod("dropbirthday", function(value, element) {
        var current = jQuery(element);
        if(current.is(':hidden')){
            return true;
        }
        var valid = true;
        current.parent().find('select').each(function(){
            if(this.selectedIndex == 0){
                valid = false;
                return;
            }
        });
        return valid;
    }, "");

    jQuery.validator.addMethod("checkbox", function(value, element) {
        return this.optional(element);
    }, "");

    jQuery.validator.addMethod("hiddenfield", function(value, element) {
        return jQuery(element).prev().prev().val().length != 0;
    }, "");

    jQuery.validator.addClassRules({
        wunBabyClubComingChildDate :{
            customfeaturedate: true,
            required: function(element) {
                return jQuery(element).is(':visible');
            }
        },

        wunRequired: {
            required: true
        },
        wunDropDown: {
            dropdown: true
        },
        wunDropDownBirthday:{
            dropbirthday: true
        },
        wunDate: {
            required: true,
            customdate: true
        },
        wunEmail: {
            required: true,
            email: true
        },
        wunZipCode: {
            required: true,
            range: [1000, 9999]
        },
        wunPhone: {
            required: true,
            range: [20000000, 99999999]
        },
        wunPassword: {
            required: true,
            minlength: 5
        },
        wunPasswordAgain: {
            required: true,
            equalTo: '.wunPassword'
        },
        wunCheckBoxRequired: {
            checkbox: true
        },
        wunChildDate: {
            required: function(element) {

                var row = jQuery(element).parent().parent();
                var value = parseInt(FORMS.elems.fields.children.val());

                return row.index() < value;
            },
            customdate: true
        },
        wunShopSearch: {
            required: true,
            hiddenfield: true
        },
        wunCheckBoxGroup: {
            checkboxgroup: true
        }
    });

    forms.each(function(){

        jQuery(this).validate({
            onfocusout: false,
            onkeyup: false,
            onclick: false,

            onsubmit: true,
            focusInvalid: true,

            errorPlacement: function(){},

            unhighlight: function(element) {
                var curr = jQuery(element);
                if(curr.is(':hidden')){
                    return;
                }
                curr.parents('.wunFormSection > table > tbody > tr').removeClass('wunError');
            },

            highlight: function(element) {
                jQuery(element).parents('.wunFormSection > table > tbody > tr').removeClass('wunFocus').addClass('wunError');
            },

            submitHandler: function(form) {

                if(SHOPSEARCH.vars.submitted) {
                    SHOPSEARCH.vars.submitted = false;
                }
                else if(!SHOPSEARCH.vars.active && !FORMS.vars.submitted) {
                    FORMS.vars.submitted = true;
                    form.submit();
                }
            }
        })
    });
};

FORMS.focus = function() {

    var row = jQuery(this).parents('.wunFormSection > table > tbody > tr');

    if(!row.hasClass(FORMS.classes.error) && !row.hasClass(FORMS.classes.disablefocus)) {

        row.addClass(FORMS.classes.focus);
    }
};

FORMS.blur = function() {
    var row = jQuery(this).parents('.wunFormSection > table > tbody > tr');
    row.removeClass(FORMS.classes.focus);
};

/*----------------------------------------
*  Butik search
----------------------------------------*/

SHOPSEARCH.init = function() {

    if(SHOPSEARCH.elems.field.html() != null) {

        SHOPSEARCH.elems.fieldValue = jQuery('.'+SHOPSEARCH.classes.fieldValue);

        SHOPSEARCH.elems.field.blur(SHOPSEARCH.hide);
        SHOPSEARCH.elems.field.keyup(SHOPSEARCH.keyup);

        SHOPSEARCH.create();
    }
};

SHOPSEARCH.create = function() {

    SHOPSEARCH.elems.dropdown = jQuery('<div></div>')
            .addClass(SHOPSEARCH.classes.dropdown);

    SHOPSEARCH.elems.wrap1 = jQuery('<div></div>')
            .addClass(SHOPSEARCH.classes.wrap1);

    SHOPSEARCH.elems.results = jQuery('<div></div>')
            .addClass(SHOPSEARCH.classes.results);

    SHOPSEARCH.elems.noresults = jQuery('<div></div>')
            .addClass(SHOPSEARCH.classes.noresults)
            .text(SHOPSEARCH.vars.noresults);

    SHOPSEARCH.elems.wrap1.append(SHOPSEARCH.elems.results);
    SHOPSEARCH.elems.wrap1.append(SHOPSEARCH.elems.noresults);

    SHOPSEARCH.elems.dropdown.append(SHOPSEARCH.elems.wrap1);

    SHOPSEARCH.elems.field.after(SHOPSEARCH.elems.dropdown);

    SHOPSEARCH.elems.results.children().click(SHOPSEARCH.click);
};

SHOPSEARCH.load = function() {

    if (SHOPSEARCH.elems.field.val() != SHOPSEARCH.vars.value && SHOPSEARCH.elems.field.val().length > 1) {
        SHOPSEARCH.show();
        if(SHOPSEARCH.vars.searchresults == undefined){
            jQuery.getJSON(SHOPSEARCH.vars.url, null, SHOPSEARCH.loaded);
        }else{
            SHOPSEARCH.fulfillresult(SHOPSEARCH.searchF(SHOPSEARCH.elems.field.val()));
        }
    }
};

SHOPSEARCH.searchF = function(searchQuery){
    var filteredData = [];
    filteredData["shops"] = [];
    var index = 0;
    jQuery.each(SHOPSEARCH.vars.searchresults.shops, function(i, shop) {
        if(!SHOPSEARCH.matchQuery(searchQuery.replace(/\s/g,''),
                shop.phone != null ? shop.phone.toString().replace(/\s/g,'') : shop.phone,
                shop, filteredData["shops"])){
            if(!SHOPSEARCH.matchQuery(searchQuery, shop.address, shop, filteredData["shops"])){
                if(!SHOPSEARCH.matchQuery(searchQuery, shop.name, shop, filteredData["shops"])){
                    if(!SHOPSEARCH.matchQuery(searchQuery, shop.zip, shop, filteredData["shops"])){
                        SHOPSEARCH.matchQuery(searchQuery, shop.city, shop, filteredData["shops"])
                    }
                }
            }
        }
        if(index == 2){
            return;
        }
    });
    return filteredData;
}

SHOPSEARCH.matchQuery = function(searchQuery, field, object, filteredData){
    if(field != null && field.toString().toLowerCase().indexOf(searchQuery.toLowerCase()) != -1){
        filteredData[filteredData.length] = object;
        return true;
    }
    return false;
}

SHOPSEARCH.loaded = function(data) {
    SHOPSEARCH.vars.searchresults = data;
    SHOPSEARCH.fulfillresult(SHOPSEARCH.searchF(SHOPSEARCH.elems.field.val()));
};

SHOPSEARCH.fulfillresult = function(data){
    SHOPSEARCH.elems.results.empty();

    if (data.shops.length > 0) {

        SHOPSEARCH.elems.noresults.hide();
        SHOPSEARCH.elems.selected = undefined;

        jQuery.each(data.shops, function(i, shop) {
            if (i < 6) {
                jQuery('<a></a>')
                        .mousedown(SHOPSEARCH.click)
                        .addClass(i % 2 == 0 ? 'wunOdd' : '')
                        .append(jQuery('<span></span>').html(shop.address != '' ? shop.address : '&nbsp;'))
                        .append(jQuery('<span></span>').html((shop.zip != '' ? shop.zip : '&nbsp;') + '&nbsp;' + (shop.city != '' ? shop.city + ',' : '&nbsp;')))
                        .append(jQuery('<span></span>').html(shop.phone != '' ? shop.phone : '&nbsp;').addClass('wunAlignRight'))
                        .append(jQuery('<span></span>').html((shop.address != '' ? shop.address : '&nbsp;') + ',&nbsp;' + (shop.zip != '' ? shop.zip : '&nbsp;') + '&nbsp;' + (shop.city != '' ? shop.city : '&nbsp;')).addClass('wunHidden'))
                        .append(jQuery('<span></span>').html(shop.uuid != '' ? shop.uuid : '&nbsp;').addClass('wunHidden'))
                        .appendTo(SHOPSEARCH.elems.results);
            }
        });

        if (data.shops.length == 1) {
            SHOPSEARCH.down();
        }
    }
    else {
        SHOPSEARCH.elems.noresults.show();
    }
}

SHOPSEARCH.click = function(){
    SHOPSEARCH.elems.selected = jQuery(this);
    SHOPSEARCH.submit();
    //fake an [enter] "form submit"
    SHOPSEARCH.vars.submitted = false;
    return false;
}

SHOPSEARCH.submit = function() {

    if (SHOPSEARCH.elems.selected == undefined) {
        SHOPSEARCH.elems.selected =  SHOPSEARCH.elems.results.children().first();
    }

    //SHOPSEARCH.elems.field.val(SHOPSEARCH.elems.selected.children()[3].text());
    //SHOPSEARCH.elems.field.val(SHOPSEARCH.elems.selected.children()[3].data);               length
    SHOPSEARCH.elems.field.val(SHOPSEARCH.elems.selected.children().eq(3).text());
    SHOPSEARCH.elems.fieldValue.val(SHOPSEARCH.elems.selected.children().last().text());

    SHOPSEARCH.vars.submitted = true;
    SHOPSEARCH.hide();

    return false;
};

SHOPSEARCH.hide = function() {

    SHOPSEARCH.vars.active = false;

    SHOPSEARCH.elems.dropdown.removeClass('wunActive');
    SHOPSEARCH.elems.selected = undefined;
};

SHOPSEARCH.show = function() {

    SHOPSEARCH.vars.active = true;

    if (!SHOPSEARCH.elems.dropdown.hasClass('wunActive')) {
        SHOPSEARCH.elems.dropdown.addClass('wunActive');
    }
};

SHOPSEARCH.up = function() {

    if (SHOPSEARCH.elems.selected != undefined) {
        if (SHOPSEARCH.elems.selected.prev().length > 0) {

            SHOPSEARCH.elems.selected.removeClass('wunActive');
            SHOPSEARCH.elems.selected = SHOPSEARCH.elems.selected.prev();
            SHOPSEARCH.elems.selected.addClass('wunActive');
        }
    }
    else {

        SHOPSEARCH.elems.selected = SHOPSEARCH.elems.results.children().last();
        SHOPSEARCH.elems.selected.addClass('wunActive');
    }
};

SHOPSEARCH.down = function() {

    if (SHOPSEARCH.elems.selected != undefined) {
        if (SHOPSEARCH.elems.selected.next().length > 0) {

            SHOPSEARCH.elems.selected.removeClass('wunActive');
            SHOPSEARCH.elems.selected = SHOPSEARCH.elems.selected.next();
            SHOPSEARCH.elems.selected.addClass('wunActive');
        }
    }
    else {

        SHOPSEARCH.elems.selected = SHOPSEARCH.elems.results.children().first();
        SHOPSEARCH.elems.selected.addClass('wunActive');
    }
};

SHOPSEARCH.keyup = function(event) {

    switch (event.keyCode) {

        case 13: // Enter key
            SHOPSEARCH.submit();
            break;

        case 27: // Esc key
            SHOPSEARCH.hide();
            break;

        case 38: // Arrow up
            SHOPSEARCH.up();
            break;

        case 40: // Arrow down
            SHOPSEARCH.down();
            break;

        default:
            SHOPSEARCH.load();
            break;
    }
};

/*--------------------------------------------------------------
*   Content rotator
--------------------------------------------------------------*/

CONTENTROTATOR.init = function() {

    if(CONTENTROTATOR.elems.container.length > 0) {

        CONTENTROTATOR.elems.container = CONTENTROTATOR.elems.container.first();

        CONTENTROTATOR.elems.steps = jQuery('<div></div>').addClass(CONTENTROTATOR.classes.steps);
        /*CONTENTROTATOR.elems.container.parent().prepend(CONTENTROTATOR.elems.steps);*/
        CONTENTROTATOR.elems.container.prev().prepend(CONTENTROTATOR.elems.steps);

        for(i=0; i < CONTENTROTATOR.elems.container.children().length; i++) {

            CONTENTROTATOR.elems.steps.append(
                    jQuery('<div></div>')
                            .addClass(CONTENTROTATOR.classes.step)
                    );
        }

        CONTENTROTATOR.elems.container.children().first().addClass('wunActive');
        CONTENTROTATOR.elems.steps.children().first().addClass('wunActive');
        CONTENTROTATOR.elems.steps.children().click(CONTENTROTATOR.click);

        CONTENTROTATOR.begin();
    }
};

CONTENTROTATOR.click = function(e) {

    if(!jQuery(this).hasClass('wunActive')) {
        CONTENTROTATOR.pause();
        CONTENTROTATOR.setActive(jQuery(this));
    }
};

CONTENTROTATOR.setActive = function(sender) {

    CONTENTROTATOR.elems.active = sender;
    CONTENTROTATOR.elems.active.siblings().removeClass('wunActive');
    CONTENTROTATOR.elems.active.addClass('wunActive');

    CONTENTROTATOR.elems.container.children().removeClass('wunActive');
    CONTENTROTATOR.elems.container.children(':nth-child(' + (sender.index() + 1) + ')').addClass('wunActive');
};

CONTENTROTATOR.showNext = function() {

    if (CONTENTROTATOR.elems.active.next().html() == null) {
        CONTENTROTATOR.begin();
    }
    else {
        CONTENTROTATOR.setActive(CONTENTROTATOR.elems.active.next());
    }
};

CONTENTROTATOR.begin = function() {

    CONTENTROTATOR.setActive(CONTENTROTATOR.elems.steps.children().first());
    CONTENTROTATOR.resume();
};

CONTENTROTATOR.stop = function() {

    clearInterval(CONTENTROTATOR.vars.interval);
    clearTimeout(CONTENTROTATOR.vars.timeout);
};

CONTENTROTATOR.pause = function() {

    CONTENTROTATOR.stop();
    CONTENTROTATOR.vars.timeout = setTimeout('CONTENTROTATOR.resume()', CONTENTROTATOR.vars.idleTime);
};

CONTENTROTATOR.resume = function() {

    CONTENTROTATOR.stop();
    CONTENTROTATOR.vars.interval = setInterval('CONTENTROTATOR.showNext()', CONTENTROTATOR.vars.idleTime);
};

/*--------------------------------------------------------------
*   Content rotator (small)
--------------------------------------------------------------*/

CONTENTROTATORSMALL.init = function() {

    if(CONTENTROTATORSMALL.elems.container.length > 0) {

        CONTENTROTATORSMALL.elems.container = CONTENTROTATORSMALL.elems.container.first();
        CONTENTROTATORSMALL.elems.steps = jQuery('<div></div>').addClass(CONTENTROTATORSMALL.classes.steps);
        CONTENTROTATORSMALL.elems.container.parent().append(CONTENTROTATORSMALL.elems.steps);

        for(i=0; i < CONTENTROTATORSMALL.elems.container.children().length; i++) {

            CONTENTROTATORSMALL.elems.steps.append(
                    jQuery('<div></div>')
                            .addClass(CONTENTROTATORSMALL.classes.step)
                    );
        }

        CONTENTROTATORSMALL.elems.container.children().first().addClass('wunActive');
        CONTENTROTATORSMALL.elems.steps.children().first().addClass('wunActive');
        CONTENTROTATORSMALL.elems.steps.children().click(CONTENTROTATORSMALL.click);

        CONTENTROTATORSMALL.begin();
    }
};

CONTENTROTATORSMALL.click = function(e) {

    if(!jQuery(this).hasClass('wunActive')) {
        CONTENTROTATORSMALL.pause();
        CONTENTROTATORSMALL.setActive(jQuery(this));
    }
};

CONTENTROTATORSMALL.setActive = function(sender) {

    CONTENTROTATORSMALL.elems.active = sender;
    CONTENTROTATORSMALL.elems.active.siblings().removeClass('wunActive');
    CONTENTROTATORSMALL.elems.active.addClass('wunActive');

    CONTENTROTATORSMALL.elems.container.children().removeClass('wunActive');
    CONTENTROTATORSMALL.elems.container.children(':nth-child(' + (sender.index() + 1) + ')').addClass('wunActive');
};

CONTENTROTATORSMALL.showNext = function() {

    if (CONTENTROTATORSMALL.elems.active.next().html() == null) {
        CONTENTROTATORSMALL.begin();
    }
    else {
        CONTENTROTATORSMALL.setActive(CONTENTROTATORSMALL.elems.active.next());
    }
};

CONTENTROTATORSMALL.begin = function() {

    CONTENTROTATORSMALL.setActive(CONTENTROTATORSMALL.elems.steps.children().first());
    CONTENTROTATORSMALL.resume();
};

CONTENTROTATORSMALL.stop = function() {

    clearInterval(CONTENTROTATORSMALL.vars.interval);
    clearTimeout(CONTENTROTATORSMALL.vars.timeout);
};

CONTENTROTATORSMALL.pause = function() {

    CONTENTROTATORSMALL.stop();
    CONTENTROTATORSMALL.vars.timeout = setTimeout('CONTENTROTATORSMALL.resume()', CONTENTROTATORSMALL.vars.idleTime);
};

CONTENTROTATORSMALL.resume = function() {

    CONTENTROTATORSMALL.stop();
    CONTENTROTATORSMALL.vars.interval = setInterval('CONTENTROTATORSMALL.showNext()', CONTENTROTATORSMALL.vars.idleTime);
};


/*--------------------------------------------------------------
*  wun.popup.js
--------------------------------------------------------------*/

var POPUPS_player_hide = function() {
    POPUPS.player.hide();
};

POPUPS.init = function() {
    POPUPS.player = new wun.popup({ cssClass: 'wunPlayerPopup' });
    POPUPS.window = new wun.popup({ cssClass: 'wunCommonPopup' });
};

POPUPS.showPlayer = function(flashVars) {
    flashVars.closeFunction = 'POPUPS_player_hide';
    POPUPS.player.setFlash(
            '/web/bcmresource/media/resources/flash/flashplayer/player/main.swf',
            '950',
            '650',
            '10.0.0',
            '/web/bcmresource/media/resources/flash/flashplayer/expressInstall.swf',
            flashVars,
    {
        wmode: 'transparent',
        scale: 'noscale',
        menu: 'false',
        allowscriptaccess: 'always',
        allowFullScreen: 'true'
    }
            );
    POPUPS.player.show();
};

/* forPageNumber is a first number from the convention of numbering the pages in Agillic, for example W_0.00_LOGIN the number will be 0 */
POPUPS.showMovies = function(forPageNumber) {
    var flashvarsMovies;

    switch(forPageNumber){
        case 0: // LOGIN AND FORSIDE PAGES

            flashvarsMovies = {
            //Here format is: title of the movie|path of the movie|thumbnail for the movie
                movie1: 'Introduktion|/web/bcmresource/media/resources/flash/flashplayer/player/introClubMatas.f4v|/web/bcmresource/media/resources/flash/flashplayer/player/introduktion.jpg',
                movie2: 'Mine aktiviteter|/web/bcmresource/media/resources/flash/flashplayer/player/mineAktiviteter.f4v|/web/bcmresource/media/resources/flash/flashplayer/player/mineAktiviteter.jpg',
                movie3: 'Mine tilbud|/web/bcmresource/media/resources/flash/flashplayer/player/mineTilbud.f4v|/web/bcmresource/media/resources/flash/flashplayer/player/mineTilbud.jpg',
                movie4: 'Mine tips og r%E5d|/web/bcmresource/media/resources/flash/flashplayer/player/tipsRaad.f4v|/web/bcmresource/media/resources/flash/flashplayer/player/tipsRaad.jpg',
                movie5: 'Produktnyheder|/web/bcmresource/media/resources/flash/flashplayer/player/produktnyheder.f4v|/web/bcmresource/media/resources/flash/flashplayer/player/produktnyheder.jpg',
                movie6: 'Point|/web/bcmresource/media/resources/flash/flashplayer/player/point.f4v|/web/bcmresource/media/resources/flash/flashplayer/player/point.jpg',
                movie7: 'Pointshoppen|/web/bcmresource/media/resources/flash/flashplayer/player/pointshoppen.f4v|/web/bcmresource/media/resources/flash/flashplayer/player/pointshoppen.jpg',
                movie8: 'Outtro|/web/bcmresource/media/resources/flash/flashplayer/player/outtro.f4v|/web/bcmresource/media/resources/flash/flashplayer/player/outtro.jpg',
                headerlabel:'Her kan du f%E5 alt at vide om Club Matas, du kan se filmen i sin fulde l%E6ngde eller selv v%E6lge kapitel i h%F8jremenuen'
            };
            break;
        case 1://Min Profil
            window.alert("My profile is not supported yet");
            break;
        case 2://Mine  aktiviteter
            flashvarsMovies = {
                movie1: 'Mine aktiviteter|/web/bcmresource/media/resources/flash/flashplayer/player/mineAktiviteter.f4v|/web/bcmresource/media/resources/flash/flashplayer/player/mineAktiviteter.jpg',
                headerlabel:'%20'
            };
            break;
        case 3://Mine  tilbud
            flashvarsMovies = {
                movie1: 'Mine tilbud|/web/bcmresource/media/resources/flash/flashplayer/player/mineTilbud.f4v|/web/bcmresource/media/resources/flash/flashplayer/player/mineTilbud.jpg',
                headerlabel:'%20'
            };
            break;
        case 4://Mine  tips og raad
            flashvarsMovies = {
                movie1: 'Mine tips og råd|/web/bcmresource/media/resources/flash/flashplayer/player/tipsRaad.f4v|/web/bcmresource/media/resources/flash/flashplayer/player/tipsRaad.jpg',
                headerlabel:'%20'
            };
            break;
        case 5://Pointshop
            flashvarsMovies = {
                movie1: 'Pointshoppen|/web/bcmresource/media/resources/flash/flashplayer/player/pointshoppen.f4v|/web/bcmresource/media/resources/flash/flashplayer/player/pointshoppen.jpg',
                headerlabel:'%20'
            };
            break;
        case 6://Produktnyheder
            flashvarsMovies = {
                movie1: 'Produktnyheder|/web/bcmresource/media/resources/flash/flashplayer/player/produktnyheder.f4v|/web/bcmresource/media/resources/flash/flashplayer/player/produktnyheder.jpg',
                headerlabel:'%20'
            };
            break;
        case 7://Pointbalance
            flashvarsMovies = {
                movie1: 'Point|/web/bcmresource/media/resources/flash/flashplayer/player/point.f4v|/web/bcmresource/media/resources/flash/flashplayer/player/point.jpg',
                headerlabel:'%20'
            };
            break;
        case 10://Club Matas Baby Introduction
            flashvarsMovies = {
                movie1: 'Introduktion|/web/bcmresource/media/resources/flash/flashplayer/player/introClubMatasBaby.f4v|/web/bcmresource/media/resources/flash/flashplayer/player/introduktion.jpg',
                headerlabel:'%20'
            };
            break;
        case 11://Club M Partners fordele
            flashvarsMovies = {
                movie1: 'Introduktion|/web/bcmresource/media/resources/flash/flashplayer/player/introClubMatasBaby.f4v|/web/bcmresource/media/resources/flash/flashplayer/player/introduktion.jpg',
                headerlabel:'%20'
            };
            break;
        default:
            window.alert("The page nr.:"+ forPageNumber + " is not supported yet");
    }
    POPUPS.showPlayer(flashvarsMovies);
    return false;
};


/*common pop up*/
POPUPS.showCommon = function(content) {
    POPUPS.window.setContent(content);
    POPUPS.window.show();
}

POPUPS.hideCommon = function() {
    POPUPS.window.hide();
}

/*--------------------------------------------------------------
*  wun.tooltip.js
--------------------------------------------------------------*/
TOOLTIP.init = function(){
    TOOLTIP.tooltip = new wun.tooltip({
        align: 'left',
        valign: 'top',
        offsetX: 110,
        offsetY: -10
    });
}

TOOLTIP.hide = function(){
    TOOLTIP.tooltip.hide();
}

TOOLTIP.position = function(event){
    TOOLTIP.tooltip.position(event);
}

TOOLTIP.setContent = function(content){
    TOOLTIP.tooltip.setContent(content);
}

/*--------------------------------------------------------------
*  Agillic utilities
--------------------------------------------------------------*/

AGILLICUTILS.init =function(){
    //increase facebook iframe height
    if (jQuery('.topPlus10pxMargin iframe').length){
        jQuery('.topPlus10pxMargin iframe').css({"height": "85px"});
    }

    // Override background color for IE on wunNotification
    if(jQuery('.wunCycle > div ').length){
        jQuery('.wunCycle > div ').each(function(){
            $(this).css({"background-color": "transparent"});
        });
    }
}

/*--------------------------------------------------------------
*  Facebook elements
--------------------------------------------------------------*/
FACEBOOK.init = function(){
    FACEBOOK.vars.fbAppID = PAGE.vars.httpDomain.indexOf('clubmatas')!=-1?'172225549532081':'227232014014515';
    FACEBOOK.vars.fbRedirect = PAGE.vars.httpDomain + FACEBOOK.vars.fbRedirectNoDomain;
    FACEBOOK.vars.fbName = jQuery('h4.'+FACEBOOK.classes.mainClass).text();
    FACEBOOK.vars.fbPicture = PAGE.vars.httpDomain + jQuery('.'+FACEBOOK.classes.mainClass + ' > img:first-child').attr('src');
    FACEBOOK.vars.fbDecription ='' + jQuery('.wunRichText.'+FACEBOOK.classes.mainClass).text() +  jQuery('.wunRichText.wunFacebookSharedBeside').text();
}

FACEBOOK.popupNewWindow = function(url){
    newwindow=window.open(url,'name','height=300,width=550');
    if (window.focus) {newwindow.focus()}
    return false;
}

FACEBOOK.clickShareButton = function(parameters){
    var fbLink = PAGE.vars.httpDomain + '/web/namedservice/' + PAGE.vars.name + '?page=' + PAGE.vars.name + parameters;
    var url =''+ FACEBOOK.vars.fbDialog + FACEBOOK.vars.fbAppID + '&link=' + fbLink + '&picture=' + FACEBOOK.vars.fbPicture +
             '&name=' + encodeURIComponent(FACEBOOK.vars.fbName) + '&caption=' + encodeURIComponent(FACEBOOK.vars.fbCaption)
            + '&description=' + encodeURIComponent(FACEBOOK.vars.fbDecription) +
             '&redirect_uri=' + FACEBOOK.vars.fbRedirect + '&display=popup'
    return FACEBOOK.popupNewWindow(url);
}

FACEBOOK.initOverride = function(htmlElement){
    var htmlElementParentNr2 = jQuery(htmlElement).parents()[1];
    FACEBOOK.vars.fbName = jQuery(htmlElementParentNr2).find('h4.'+FACEBOOK.classes.mainClass).text();
    FACEBOOK.vars.fbPicture = PAGE.vars.httpDomain + jQuery(htmlElementParentNr2).find('.' + FACEBOOK.classes.mainClass + ' > img:first-child').attr('src');
    FACEBOOK.vars.fbDecription ='' + jQuery(htmlElementParentNr2).find('.wunRichText.'+FACEBOOK.classes.mainClass).text() + jQuery('.wunRichText.wunFacebookSharedBeside').text();
}

FACEBOOK.clickShareButtonOverview = function(htmlElement){
    var htmlElementHref = jQuery(htmlElement).closest('div').prev('div').find('a').attr('href');
    var promotion = null; var proposition = null; var pageName = null;
    if(htmlElementHref.indexOf('&details=')!=-1){
        var splitByDetails = htmlElementHref.split('&details=');
        var details = splitByDetails[1].split('%3A');
        pageName = splitByDetails[0].split('?evt')[0].split('/web/namedservice/').join('');
        promotion = details[0];
        proposition = details[1];
    }
    if (promotion != null && proposition != null && pageName != null ){
        //override default values
        PAGE.vars.name = pageName;
        FACEBOOK.initOverride(htmlElement);
        jQuery.getJSON('api/generateOverwriteParameters.jsp?promotion='+promotion+'&proposition='+proposition, null, function(data){
            FACEBOOK.clickShareButton(data.parameters);
        });
    }
}


/*--------------------------------------------------------------
*  CLUB M PARTNERS
--------------------------------------------------------------*/
PARTNERS.init = function(){
    if (jQuery(".partner_container").length > 0 && jQuery(".partner_logo_container").length > 0 || jQuery(".wun_partner_logo").length > 0){
        jQuery(".partner_container").hover(
                function () {
                    var _logo = jQuery(this).find(".partner_logo_container img");
                    if (_logo.attr("src")) {
                        var logo = _logo.attr("src").split(".jpg")[0];
                        logo = logo + "_mo.jpg";
                        _logo.attr("src", logo);
                    }

                }, function () {
            var _logo = jQuery(this).find(".partner_logo_container img")
            if (_logo.attr("src")) {
                var logo = _logo.attr("src").split("_mo.jpg")[0];
                logo = logo + ".jpg";
                _logo.attr("src", logo);
            }
        }
                );
        jQuery(".wun_partner_logo").hover(
                function () {
                    var _logo = jQuery(this).find("img");
                    if (_logo.attr("src")) {
                        var logo = _logo.attr("src").split(".jpg")[0];
                        logo = logo + "_mo.jpg";
                        _logo.attr("src", logo);
                    }

                }, function () {
            var _logo = jQuery(this).find("img")
            if (_logo.attr("src")) {
                var logo = _logo.attr("src").split("_mo.jpg")[0];
                logo = logo + ".jpg";
                _logo.attr("src", logo);
            }
        }
        );
    }
}

PARTNERS_CONTENTPAGER.init = function () {
    if (PARTNERS_CONTENTPAGER.elems.container.length > 0) {
        PARTNERS_CONTENTPAGER.elems.container = PARTNERS_CONTENTPAGER.elems.container.first();
        PARTNERS_CONTENTPAGER.elems.content = PARTNERS_CONTENTPAGER.elems.container.children('.' + PARTNERS_CONTENTPAGER.classes.content).first();
        PARTNERS_CONTENTPAGER.elems.panel = jQuery('<div></div>').addClass(PARTNERS_CONTENTPAGER.classes.panel);
        PARTNERS_CONTENTPAGER.elems.wrap = jQuery('<div></div>').addClass(PARTNERS_CONTENTPAGER.classes.wrap);
        PARTNERS_CONTENTPAGER.elems.marker = jQuery('<div></div>').addClass(PARTNERS_CONTENTPAGER.classes.marker);
        /*PARTNERS_CONTENTPAGER.elems.pause = jQuery('<div></div>')
            .addClass(PARTNERS_CONTENTPAGER.classes.pause)
            .click(PARTNERS_CONTENTPAGER.pause);
        PARTNERS_CONTENTPAGER.elems.play = jQuery('<div></div>')
            .addClass(PARTNERS_CONTENTPAGER.classes.play)
            .click(PARTNERS_CONTENTPAGER.play)
            .hide();*/
        PARTNERS_CONTENTPAGER.elems.wrap.append(PARTNERS_CONTENTPAGER.elems.marker);
        for (i = 0; i < PARTNERS_CONTENTPAGER.elems.content.children().length; i++) {
            PARTNERS_CONTENTPAGER.elems.wrap.append(
            jQuery('<div></div>')
            .addClass(PARTNERS_CONTENTPAGER.classes.steps)
            .html("<img src='"+ PARTNERS_CONTENTPAGER.elems.pathBullets +"' />")
            .click(PARTNERS_CONTENTPAGER.goToThis)
            );
        }
        PARTNERS_CONTENTPAGER.elems.steps = PARTNERS_CONTENTPAGER.elems.wrap.children('.' + PARTNERS_CONTENTPAGER.classes.steps);
        PARTNERS_CONTENTPAGER.elems.wrap.append(PARTNERS_CONTENTPAGER.elems.pause);
        PARTNERS_CONTENTPAGER.elems.wrap.append(PARTNERS_CONTENTPAGER.elems.play);
        PARTNERS_CONTENTPAGER.elems.panel.append(PARTNERS_CONTENTPAGER.elems.wrap);
        PARTNERS_CONTENTPAGER.elems.container.append(PARTNERS_CONTENTPAGER.elems.panel);
        PARTNERS_CONTENTPAGER.vars.widths.marker = PARTNERS_CONTENTPAGER.elems.marker.width();
        PARTNERS_CONTENTPAGER.vars.widths.steps = PARTNERS_CONTENTPAGER.elems.steps.width();
        PARTNERS_CONTENTPAGER.goto(1);
    }
};
PARTNERS_CONTENTPAGER.goto = function (num) {
    if (PARTNERS_CONTENTPAGER.elems.active != undefined) {
        if (PARTNERS_CONTENTPAGER.elems.active.index() + 1 != num) {
            PARTNERS_CONTENTPAGER.elems.active.fadeOut(1000);
            PARTNERS_CONTENTPAGER.elems.steps.css('color', '');
        }
    }
    PARTNERS_CONTENTPAGER.elems.active = PARTNERS_CONTENTPAGER.elems.content.children(':nth-child(' + (num) + ')');
    PARTNERS_CONTENTPAGER.elems.active.fadeIn(1000);
    PARTNERS_CONTENTPAGER.elems.marker.animate({
        left: ((num - 1) * PARTNERS_CONTENTPAGER.vars.widths.steps) + ((PARTNERS_CONTENTPAGER.vars.widths.marker / 2) - (PARTNERS_CONTENTPAGER.vars.widths.steps / 2))
    }, 500, function () {
        jQuery(PARTNERS_CONTENTPAGER.elems.steps.get(num - 1)).css('color', '#ffffff');
    });

    if ((PARTNERS_CONTENTPAGER.elems.active).hasClass("wunTimer")) {
        $(".wunTextFader").css("opacity", "0");
        $(".float1").css("opacity", "0");
        $(".float2").css("opacity", "0");
        $(".float3").css("opacity", "0");
        $(".float4").css("opacity", "0");
        $('.wunBrownOrb').css("top", "20px").css("left", "260px").css("opacity", "1");
        setTimeout(function () {
            $('.wunTextFader').animate({
                opacity: 1
            }, 5000, function () {

            });
        }, 0);
        setTimeout(function () {
            $('.float2').animate({
                opacity: 1
            }, 1000, function () {
                $('.float4').animate({
                    opacity: 1
                }, 1000, function () {
                    $('.float3').animate({
                        opacity: 1
                    }, 1000, function () {
                        $('.float1').animate({
                            opacity: 1
                        }, 1000, function () {
                            // Animation complete.
                        });
                    });
                });
            });
        }, 3000);
        $('.wunBrownOrb').animate({
            top: -60,
            left: 500,
            opacity: 0
        }, 10000, function () {
            // Animation complete.
        });
    }
   
    if (!PARTNERS_CONTENTPAGER.vars.paused) {
        PARTNERS_CONTENTPAGER.play();
    }
};
PARTNERS_CONTENTPAGER.goToThis = function () {
    PARTNERS_CONTENTPAGER.goto(jQuery(this).index());
}
PARTNERS_CONTENTPAGER.goToNext = function () {
    if (PARTNERS_CONTENTPAGER.elems.active.next().html() == null) {
        PARTNERS_CONTENTPAGER.goto(PARTNERS_CONTENTPAGER.elems.steps.first().index());
    }
    else {
        PARTNERS_CONTENTPAGER.goto(PARTNERS_CONTENTPAGER.elems.active.next().index() + 1);
    }
};
PARTNERS_CONTENTPAGER.pause = function () {
    clearInterval(PARTNERS_CONTENTPAGER.vars.interval);
    PARTNERS_CONTENTPAGER.elems.play.show();
    PARTNERS_CONTENTPAGER.elems.pause.hide();
};
PARTNERS_CONTENTPAGER.play = function () {
    clearInterval(PARTNERS_CONTENTPAGER.vars.interval);
    /*PARTNERS_CONTENTPAGER.elems.play.hide();
    PARTNERS_CONTENTPAGER.elems.pause.show();*/
    PARTNERS_CONTENTPAGER.vars.interval = setInterval('PARTNERS_CONTENTPAGER.goToNext()', PARTNERS_CONTENTPAGER.vars.idleTime);
};
