$(document).ready(function() {


    //disable auto postback on enter-key
    $('body').keydown(function() {
        return (event.keyCode != 13);
    });


    var SearchBox = function(div) {
        this.div = $(div);
        this.defaultVal = this.div.find('input[type=text]').val();
        this.txtField = this.div.find('input[type=text]');
        this.searchBtn = this.div.find('.hidSearchBtn');
        this.init();
    }

    SearchBox.prototype = {
        div: null,
        defaultVal: null,
        txtField: null,
        searchBtn: null,
        init: function() {
            var self = this;
            self.txtField.focus(function() {
                if ($(this).val() == self.defaultVal) {
                    $(this).val('');
                }

                $(self.txtField).keydown(function(event) {
                    var key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
                    if (key === 13) {
                        self.searchBtn.click();
                    }
                });
            });

            self.txtField.blur(function() {
                if ($(this).val() == '') {
                    $(this).val(self.defaultVal);
                }
            });
        }

    }

    new SearchBox($('.hToolbar'));

    $('.spotItem img').hover(function() {
        $(this).fadeTo(100, 0.85);
    }, function() {
        $(this).fadeTo(100, 1);
        //$(this).siblings().toggleClass("hovered");
    });

    if ($('.searchPanel').length > 0) {
        // init searchbox on search-page
        new SearchBox($('.searchPanel'));

    }


    $(".pressH ul li:even").addClass("even");
    $(".pressH ul li:odd").addClass("odd");

    $(".subsidiariesH ul li:even").addClass("even");
    $(".subsidiariesH ul li:odd").addClass("odd");

    $(".magmentListH ul li:even").addClass("even");
    $(".magmentListH ul li:odd").addClass("odd");



    $('.subsidiariesH li a').toggle(function() {
        var tclass = $(this).next("div").attr('class');

        if (tclass == 'showElement') {
            $(this).removeClass('addH');
            $(this).next('div').removeClass('showElement contentH').addClass('hideElement');
            return;
        }
        $(this).addClass('addH');
        $(this).next('div').removeClass('hideElement contentH').addClass('showElement');
    }, function() {
        var tclass = $(this).next("div").attr('class');
        if (tclass == 'hideElement') {
            $(this).addClass('addH');
            $(this).next('div').removeClass('hideElement contentH').addClass('showElement');
            return;
        }
        $(this).removeClass('addH');
        $(this).next('div').removeClass('showElement contentH').addClass('hideElement');
    });


    $("a.showAll").click(function() {
        $('.subsidiariesH li a').addClass('addH');
        $('.subsidiariesH li a').next().removeClass('contentH hideElement').addClass('showElement');
    });


    $("a.closeAll").click(function() {
        $('.subsidiariesH li a').removeClass('addH');
        $('.subsidiariesH li a').next().removeClass('contentH showElement').addClass('hideElement');
    });

    // startpagerotator
    var StartRotator = function(div) {
        this.div = $(div);
        this.lang = this.div.find('input[name="lang"]').val();
        this.init();
    }

    StartRotator.prototype = {
        div: null,
        lang: null,
        init: function() {
            var self = this.div;
            var population = new Array();
            //populate with first item (langauge control made server-side
            var obj = new Object();
            obj.src = self.find('.container img').attr('src');
            var name = self.find('.abcap span').html();
            obj.quot = self.find('.caption').html() + "|" + self.find('.abcap .name').html(); +"|" + self.find('.abcap .title').html();
            

            if (this.lang == "EN") {
                population.push({
                    src: '/Templates/QmedComse/Public/Images/rotator/maria-mohlen.jpg',
                    quot: " Restylane is not only about beauty. I can see that our products bring in so many other values in people's lives as well.|Maria Mol&eacute;n|Training & Account Manager Nordic countries"
                });
                population.push({
                    src: '/Templates/QmedComse/Public/Images/rotator/christer-atuna.jpg',
                    quot: "FDA's approval of Solesta was extremely instructive and the teamwork was crucial for our success|Christer Atuma|Head of Safety and Risk Management"
                });
                population.push({
                    src: '/Templates/QmedComse/Public/Images/rotator/cindy-wong.jpg',
                    quot: "The ability to change, develop and cooperate as team players is something I experience daily at Q-Med|Cindy Wong|Head of Medical Affairs and Chief Medical Officer"
                });
            }

            if (this.lang == "SV") {
                population.push({
                    src: '/Templates/QmedComse/Public/Images/rotator/maria-mohlen.jpg',
                    quot: "Restylane handlar inte bara om sk&ouml;nhet. Det &auml;r en livsstil. V&aring;ra produkter f&ouml;r med sig m&aring;nga olika v&auml;rden i m&auml;nniskors liv..|Maria Mol&eacute;n|Training & Account Manager Nordic countries"
                });
                population.push({
                    src: '/Templates/QmedComse/Public/Images/rotator/christer-atuna.jpg',
                    quot: "Godk&auml;nnandet av Solesta hos FDA var oerh&oumlrt l&auml;rorikt. D&auml;r var teamarbetet avg&ouml;rande f&ouml;r framg&aring;ngen|Christer Atuma|Head of Safety and Risk Management"
                });
                population.push({
                    src: '/Templates/QmedComse/Public/Images/rotator/cindy-wong.jpg',
                    quot: "F&ouml;rm&aring;ga till f&ouml;r&auml;ndring, utveckling och ett prestigel&ouml;st samarbete &auml;r n&aring;got jag upplever dagligen p&aring; Q-Med|Cindy Wong|Head of Medical Affairs and Chief Medical Officer"
                });


            }

            if ($('.container').size() === 1 && population.length === 3) {
                $('.container').crossSlide({
                    sleep: 10,
                    fade: 2
                }, [{
                    src: population[0].src,
                    alt: population[0].quot
                }, {
                    src: population[1].src,
                    alt: population[1].quot
                }, {
                    src: population[2].src,
                    alt: population[2].quot

}], function(idx, img, idxOut, imgOut) {
    if (idxOut == undefined) {
        // starting single image phase, put up caption
        var txt = img.alt.split("|");
        $('.caption').html(txt[0]);
        $('.abcap .name').html(txt[1]);
        $('.abcap .title').html(txt[2]);
        $('.caption').delay(2000).fadeIn('slow');
        $('.hline').delay(5000).fadeIn('slow');
        $('.abcap').delay(6000).fadeIn('slow');
       
    } else {
        // starting cross-fade phase, take out caption
        $('.caption').fadeOut();
        $('.abcap').fadeOut();
        $('.hline').fadeOut();
        //console.log('fadeOut');
    }
});

                } else {
                    
                }
            }


        };


        new StartRotator($('.imageRotator'));

        //$('.spotItem').last().addClass('omega');


        // SHARE THIS //
        stLight.options({
            publisher: 'c9445fdc-da44-41d1-b05f-b67dd2a85280'
        });

        $('.country-select').click(function() {
            $('.langmenu').toggleClass('hidden');
        });

    });

    $('#SiteMap').find('li:nth-child(even)').addClass('alternate');
    $('#SiteMap').children('ul').toggleClass('hidden').removeClass('pl20');
    $('#SiteMap').find('.parent').toggle(function() {
        $(this).siblings('ul').removeClass('hidden');
        $(this).addClass('close');
    }, function() {
    $(this).siblings('ul').addClass('hidden');
    $(this).removeClass('close');
    });
