﻿$(function() {

    // promo slides
    $("#wc_promoslides").each(function() {
        var promoCont = $(this);
        var list = promoCont.children("ul");
        var actPic = 0;
        var maxPic = $("li", list).length;

        function showSlide(slideID) {

            $("li:eq(" + actPic + ") > a", list).removeClass("active");
            $("li:eq(" + slideID + ") > a", list).addClass("active");
            $(".wc_promocontent:eq(" + (actPic - 1) + ")", promoCont).fadeOut(600);
            $(".wc_promocontent:eq(" + (slideID - 1) + ")", promoCont).fadeIn(600);

            actPic = slideID;

        }

        list.prepend("<li><a href=\"javascript:;\" class=\"prev\" title=\"előző\">&laquo;</a></li>");
        list.append("<li><a href=\"javascript:;\" class=\"next\" title=\"következő\">&raquo;</a></li>");

        $(".prev", this).click(function() {
            clearInterval(timer);
            var nextSlide = actPic == 1 ? maxPic : actPic - 1;
            showSlide(nextSlide);
        });
        $(".next", this).click(function() {
            if (timer != null) clearInterval(timer);
            var nextSlide = (actPic % maxPic) + 1;
            showSlide(nextSlide);
        });
        $("li:not(:last,:first) > a", list).click(function() {
            if (timer != null) clearInterval(timer);
            var index = $("li:not(:last,:first) > a", list).index(this) + 1;
            if (index != actPic) showSlide(index);
            return false;
        });
        showSlide(1);
        var timer = setInterval(function() { showSlide((actPic % maxPic) + 1); }, 3000);

    });

    // news slides
    $("#wc_newsslides").each(function() {
        var actNews = 1;
        var newslist = $("ul li", this);
        var newsNum = newslist.length;
        var newspix = $(".wc_newspic");

        newslist.hover(
		    function() {
		        if (newstimer != null) clearInterval(newstimer);
		        var idx = $("#wc_newsslides ul li").index(this);
		        newslist.removeClass("active");
		        $(this).addClass("active");
		        newspix.hide();
		        $("#news" + idx).show();
		    },
		    function() {
		    }
	    );
        $("#news0").show();
        var newstimer = setInterval(function() {
            newspix.hide();
            newslist.removeClass("active");
            $("#wc_newsslides ul li:eq(" + actNews + ")").addClass("active");
            $("#news" + actNews % newsNum).show();
            actNews = (actNews + 1) % newsNum;
        }, 2700);
    });

    var specialistrotator = function() {
        $('.wc_specrotator .wc_spec').hide();

        prevNo1 = randNo1;
        prevNo2 = randNo2;

        randNo1 = Math.floor(Math.random() * totalNo);
        while ((randNo1 == prevNo1) || (randNo1 == prevNo2)) {
            randNo1 = Math.floor(Math.random() * totalNo);
        }

        randNo2 = Math.floor(Math.random() * totalNo);
        while ((randNo1 == randNo2) || ((randNo2 == prevNo1) || (randNo2 == prevNo2))) {
            randNo2 = Math.floor(Math.random() * totalNo);
        }

        $('.wc_spec').eq(randNo1).fadeIn(600);
        $('.wc_spec').eq(randNo2).fadeIn(600);
    };

    var randNo1 = 0; var randNo2 = 0; var prevNo1 = 0; var prevNo2 = 0;
    var totalNo = $('.wc_specrotator .wc_spec').length;
    if (totalNo > 2) {
        specialistrotator();
        var specialisttimer = setInterval(specialistrotator, 5000);
    }
    else {
        $('.wc_specrotator .wc_spec').fadeIn(600);
    }

    /* annak vizsgálata, hogy szerkesztő módban vagyunk-e? */
    if ($('.snPortlet .snDrag').length == 0) {

        /* 'Olvass tovább' doboz elrejtése, ha a tartalma üres */
        var childNo = $('.wc_readmore .wc_col').children().length;
        if (childNo == 0) { $('.wc_readmore').parent().parent().parent().remove(); }

        /* Fórum doboz elrejtése, ha a tartalma üres */
        if ($('.snPtHeader:contains("Fórum")').parent().find('snPtBody div').children().length == 0) {
            $('.snPtHeader:contains("Fórum")').parent().remove();
        }

        /* Nyereményjáték doboz elrejtése, ha a tartalma üres */
        if ($('.wc_wide2 .GreenBrownSkin').find('.wc_offer').length == 0) {
            $('.wc_wide2 .GreenBrownSkin').remove();
        }
    }

    /* kommentek megjelenítése/elrejtése */
    if ($('.wc_comments').length > 0) {
        $('.wc_comments, #btn_comment').hide();
        //var state = 0;
        $('.related #btn_show').click(

        function(event) {
            event.preventDefault();
            /*  if (state == 0) {*/
            $('.wc_comments').slideDown();
            $('#btn_show').hide();
            $('#btn_comment').show();
            /*$(this).find('span').text('Hozzászólok').parent().attr({ 'title': 'Hozzászólok', 'id': 'btn_comment' });
            / state = 1;
            }
            else {
            $('.wc_comments').slideUp();
            $(this).find('span').text('Megnézem').parent().attr({ 'title': 'Megnézem' });
            state = 0;
            }*/
        });
        if (location.hash == '#comments') {
            $('.related #btn_show').trigger('click');
        }
    }

    if ($('.AddToFavorites').length > 0) {
        $('.AddToFavorites').jFav();
    }

    /* href kicserélése egyes action-ökön */
    var currentURL = $('a.SendArticle').attr('href');
    $('a.SendArticle').attr('href', 'javascript:popup("' + currentURL + '", 675, 600);').removeAttr('target');
    var currentURL = $('a.Comment').attr('href');
    $('a.Comment, #btn_comment').attr('href', 'javascript:popup("' + currentURL + '", 675, 465);').removeAttr('target');
    $('.wc_commentfoot a').each(function() {
        var thisURL = $(this).attr('href');
        $(this).attr('href', 'javascript:popup("' + thisURL + '", 675, 465);').removeAttr('target');
    });

    /* comment postolása utáni esemény */
    $('.btn_post_comment').click(function() {
        opener.location.search = Math.floor(Math.random() * 11000); opener.location.hash = 'comments'; opener.location.reload(true); opener.focus(); self.close();
    })



    /* run gallery */
    if ($('#galleria .wc_contentpic').length > 0) {
        /* initialize gallery theme */
        Galleria.loadTheme('/Root/System/Skins/WCafeSkin/js/themes/classic/galleria.classic.js');
        $('#galleria .wc_contentpic').galleria({
            height: 490
        });
    }

    /* katalógus eredményekre ugrás */

    if ($('#wc_katalogus').length > 0) {
        if (window.location.search != '') {
            window.location.hash = 'eredmenyek';
        }
    }
});
function SetStartPage() {
    alert('Ha a WellnessCafet szeretnéd kezdőlapként, húzd a fenti navigációs sávban lévő WellnessCafe szöveget a házacskára!');
}


var currentSize = 0;
function resize() {
    if (currentSize == 0) {
        $('#wc_cikk .abstract, #wc_cikk .article_body').css({ 'font-size': '130%' });
        currentSize = 1;
    }
    else {
        $('#wc_cikk .abstract, #wc_cikk .article_body').css({ 'font-size': '100%' });
        currentSize = 0;
    }
}

/* popup új ablakba */
function popup(myUrl, myWidth, myHeight) {
    newwindow = window.open(myUrl, 'WellnessCafe', 'scrollbars=no,menubar=no,height=' + myHeight + ',width=' + myWidth + ',resizable=yes,toolbar=no,location=no,status=no');
    if (window.focus) { newwindow.focus() }
}

var WCafe = {};
WCafe.Flora =
{
    Steps: ["FloraWelcome", "FloraEating", "FloraSports", "FloraStress", "FloraResults"],
    CurrentStep: 0,
    Results: { "1": 0, "2": 0, "3": 0 },
    Start: function() {
        this.CurrentStep = 0;
        this.Results = { "1": 0, "2": 0, "3": 0 };
        this.Next();

        for (var i = 1; i <= 4; i++)
            $(".Result" + i).hide();
    },
    SetResult: function(sender, result) {
        sender.className += "Checked";
        this.Results["" + result]++;
        if (this.CurrentStep == this.Steps.length - 2) {
            var results = this.Results;
            if (results["1"] == 3)
                $(".Result1").show();
            else if (results["2"] == 3)
                $(".Result2").show();
            else if (results["3"] == 3)
                $(".Result3").show();
            else
                $(".Result4").show();
        }
        window.setTimeout(function() { WCafe.Flora.Next(); }, 500);

    },
    Next: function() {
        this.CurrentStep++;
        this.ReDisplay();
    },
    Prev: function() {
        this.CurrentStep--;
        this.ReDisplay();
    },
    ReDisplay: function() {
        for (var i = 0; i < this.Steps.length; i++) {
            var item = $("." + this.Steps[i]);
            if (i != this.CurrentStep)
                item.hide();
            else
                item.show();
        }
    }

};

var ChooseLargest = function(num1, num2, num3) {
    if (num1 < num2) {
        if (num2 < num3) return 3;
        else return 2;
    }
    else {
        if (num1 < num3) return 3;
        else return 1;
    }
};