﻿$(function() {

    // adding the "openwindow" class to an anchor link will open it in a new window or tab
    $("a.openwindow").click(function() {
        window.open(this.href);
        return false;
    });


    // adding the "printThisPage" class to an anchor link will open the print dialog window
    $("a.printThisPage").click(function() {
        window.print();
        return false;
    });

    // Get the body class    
    var bodyClassArray = $("body").attr("class").split(" ");
    var bodyClass = null;

    $.each(bodyClassArray, function(i, n) {
        if (n != "sIFR-active") {
            bodyClass = n;
        }
    });

    // Trigger the share widget if this is NOT the home page or an HCP page

    if (bodyClass != "home" && bodyClass.substring(0, 3) != "hcp") {
        $("#share").sharePage();
    }


    // add "Back to top" links on pages taller than 850px
    if (($("#content_main").height() > 850) || bodyClass == "faq") {

        if ($("#column1").length > 0) {
            $("<p><a id='backTopLink' href=\"#content\">Takaisin alkuun</a></p>").appendTo("#column1");

        } else if ($("#column2").length > 0) {

            $("<p><a id='backTopLink' href=\"#content\">Takaisin alkuun</a></p>").appendTo("#column2");

        }

    }

    switch (bodyClass) {
        case "home":
            $("#whatsnewcarousel").jCarouselLite({
                btnNext: ".jcarousel-next",
                btnPrev: ".jcarousel-prev",
                visible: 3,
                circular: true
            });
            break;

        case "alliinaction":
            $("#hiddenFoodTypes").hide();
            $("#hiddenFoodProtein").hide();
            $("#hiddenFoodCarbs").hide();
            $("#hiddenFoodFats").hide();
            break;

        case "whyabsorblessfat":
            $("#hiddenFoodTypes").hide();
            $("#hiddenFoodProtein").hide();
            $("#hiddenFoodCarbs").hide();
            $("#hiddenFoodFats").hide();
            break;

        case "dietrelatedtreatmenteffects":
            $("ul.contentlist li ul.contentlist li").parent().closest("li").addClass("nobullet");
            break;

        case "visceralfat":
            $("#content h1").addClass("doubleheight");
            break;

        case "realallistories":
            var showClass = null;
            $("#storiescarousel ul").jcarousel({ scroll: 1 });
            $("#realstories div.story").hide();
            $("#lorna").show().addClass("in");
            $("#storiescarousel li a").click(function() {
                showClass = $(this).attr("class");
                $("#realstories div.in").removeClass("in").hide();
                $("#realstories div").each(function() {
                    if ($(this).attr("id") == showClass) {
                        $(this).addClass("in").fadeIn();
                    }
                });
            });
            break;

        case "setyourtargets":
            $("#hiddentables").hide();
            $("#column2 span.reveal").click(function() {
                if ($(this).hasClass("in")) {
                    $("#hiddentables").fadeOut("fast");
                } else {
                    $("#hiddentables").fadeIn("fast");
                }
                $(this).toggleClass("in");
            });
            //$('div.error').parent().addClass("error");
            //$('#contactform #ctl00_masterPageContent_genderRequiredFieldValidator,#contactform #ctl00_masterPageContent_weightInput_weightRequiredFieldValidator').parent().addClass("error");
            break;

        case "healthyeating":
            $("#faqs").faqShowHide();
            break;

        case "tastyrecipes":
            $("div.tabbed > ul").tabs();
            $("ul.recipelist").recipeShowHide();
            break;

        case "eatingandrecipes":
            $("div.tabbed > ul").tabs();
            $("#faqs").faqShowHide();
            $("ul.contentlist li ul.contentlist li").parent().closest("li").addClass("nobullet");
            break;

        case "gettingmoreactive":
            $("div.tabbed > ul").tabs();
            $("#hiddentables").hide();
            $("#column2 span.reveal").click(function() {
                if ($(this).hasClass("in")) {
                    $("#hiddentables").fadeOut("fast");
                } else {
                    $("#hiddentables").fadeIn("fast");
                }
                $(this).toggleClass("in");
            });
            break;

        case "registerforpressalerts":
            //$('div.error').parent().addClass("error");
            //$('#pressform #RequiredFieldValidator1,#pressform #RequiredFieldValidator2,#pressform #RequiredFieldValidator3,#pressform #RequiredFieldValidator4,#pressform #RequiredFieldValidator5,#pressform #RegularExpressionValidator1,#pressform #CompareValidator1').parent().addClass("error");    		
            break;

        case "myalliplan":
            $("div.fakeTabs a:not(.selected):first-child").hover(
				function() {
				    $(this).css("border-bottom", "0");
				    $(this).css("border-right", "0");
				    $(this).css("border-left", "1px solid #ccc");
				},
				function() {
				    $(this).css("border-left", "1px solid #ECF3EB");
				}
			);

            $("div.fakeTabs a:.selected):first-child").hover(
		    function() {
		        $(this).css("border-bottom", "0");
		    },
		    function() {
		        $(this).css("border-bottom", "0");
		    }
		  );
            $("#boxesContainer div.miniToolNav ul li:last-child").addClass("last");
            $("input[type=text], input[type=password]").addClass("text");
            $("select").addClass("narrow");
            $("#search input[type=text]").removeClass("text");
            $("ul.actionsRow li a").addClass("linkbutton");
            $("div.calendarBoxContent td a").removeClass("linkbutton");
            break;

        case "signin":
            $("input[type=text], input[type=password]").addClass("text");
            $("select").addClass("narrow");
            $("#backTopLink").hide();
            $("li.error span.info").hide();
            break;

        case "contactusregister":
            //$('div.error').parent().addClass("error");			
            //$('#contactform #ctl00_masterPageContent_RequiredFieldValidator1,#contactform #ctl00_masterPageContent_RequiredFieldValidator2,#contactform #ctl00_masterPageContent_RequiredFieldValidator3,#contactform #ctl00_masterPageContent_RequiredFieldValidator4,#contactform #ctl00_masterPageContent_RequiredFieldValidator5,#contactform #ctl00_masterPageContent_RequiredFieldValidator6,#contactform #ctl00_masterPageContent_RequiredFieldValidator7').parent().addClass("error");									
            break;

        case "faq":
            $("#faqs").faqShowHide();
            break;

        case "whatsyourbmi":
            $("#helpbmi").hide();
            $("#helpbmilink").mouseover(function() {
                $("#helpbmi").slideToggle();
                return false;
            });
            break;

        case "hcpfaqs":
            $("#faqs").faqShowHide();
            $("div.answer a[href^='#hcpfaq']").click(function() {
                $("#ref").show().prev().toggleClass("in").addClass("xref");
            });
            break;

        case "hcpregister":
            //$('div.error').parent().addClass("error");
            //$('#hcpregisterform #ctl00_masterCPageContent_RequiredFieldValidator1,#hcpregisterform #ctl00_masterCPageContent_RequiredFieldValidator2,#hcpregisterform #ctl00_masterCPageContent_RequiredFieldValidator3,#hcpregisterform #ctl00_masterCPageContent_RequiredFieldValidator4,#hcpregisterform #ctl00_masterCPageContent_RequiredFieldValidator5,#hcpregisterform #ctl00_masterCPageContent_RequiredFieldValidator6,#hcpregisterform #ctl00_masterCPageContent_RequiredFieldValidator7,#hcpregisterform #ctl00_masterCPageContent_RequiredFieldValidator8,#hcpregisterform #ctl00_masterCPageContent_RequiredFieldValidator9,#hcpregisterform #ctl00_masterCPageContent_RequiredFieldValidator10,#hcpregisterform #ctl00_masterCPageContent_RequiredFieldValidator11,#hcpregisterform #ctl00_masterCPageContent_RequiredFieldValidator12').parent().addClass("error");
            break;

        case "hcpforgottenpassword":
            //$('div.error').parent().addClass("error");
            //$('#hcpforgotpassword #ctl00_masterCPageContent_RequiredFieldValidator1,#hcpforgotpassword #ctl00_masterCPageContent_RequiredFieldValidator2').parent().addClass("error");
            break;

        case "hcpforgottenusername":
            //$('div.error').parent().addClass("error");
            //$('#hcpforgotpassword #ctl00_masterCPageContent_RequiredFieldValidator1,#hcpforgotpassword #ctl00_masterCPageContent_RequiredFieldValidator2').parent().addClass("error");
            break;

        case "hcpchangepassword":
            //$('div.error').parent().addClass("error");
            //$('#hcpforgotpassword #ctl00_masterCPageContent_RequiredFieldValidator1,#hcpforgotpassword #ctl00_masterCPageContent_RequiredFieldValidator2').parent().addClass("error");
            break;
    }
}); 

// ---------------------------------------
// FUNCTIONS
//----------------------------------------

// Function: FAQ show/hide

$.fn.faqShowHide = function() {
    $(this).children("div.answer").hide();
    $(this).children("div.question").removeClass("in").click(function(){
        if ( $(this).hasClass("in") ) {
            // then answer visible
            $(this).toggleClass("in").next().slideUp("fast");
            if ($("#ref").length > 0 &&  $("#ref").prev().hasClass("xref")){
                $("#ref").hide().prev().toggleClass("xref");
                if ($("#ref").prev().hasClass("in")){
                    $("#ref").prev().toggleClass("in");
                }
            }
        } else {
            // then answer hidden
            $(this).toggleClass("in").next().slideDown("fast");
        }
        
    });
    
    $("img.close").click(function(){
        $(this).closest("div.answer").slideUp("fast").prev().toggleClass("in");
        if ($("#ref").length > 0 &&  $("#ref").prev().hasClass("xref")){
            $("#ref").hide().prev().toggleClass("xref");
            if ($("#ref").prev().hasClass("in")){
                $("#ref").prev().toggleClass("in");
            }
        }
    });
};

// Function: Creates hide and show actions for recipe section
$.fn.recipeShowHide = function() {
	$(this).find("div.recipedetail").hide();
	$(this).find("li:first-child div.recipesummary").css("border","none");
	$(this).find("div.recipesummary p.recipename").click(function(){
		if ( $(this).closest("div").hasClass("in") ) { 
			// div.recipedetail visible
			$(this).closest("div").toggleClass("in").next().fadeOut("fast",function(){
			  $(this).css("filter","");
				$(this).closest("div").prev().find("img.recipethumb").fadeIn("fast");
			});
		} else {
			// div.recipedetail hidden
			$(this).closest("div").toggleClass("in").find("img.recipethumb").fadeOut("fast",function(){
			  $(this).css("filter","");
				$(this).closest("div").next().fadeIn("normal");
			});
		}
	});
};