﻿$(function() {

    // GOOGLE ADWORDS UI CHECK
    ////////////////////////////////////////////////////////////////////

    // list of adword detection URLs
    var fullUrl = window.location.href,
        urlsToCheck = new Array();

    /* live adwords urls */
    urlsToCheck[0] = "http://www.alliprogramm.de/?";
    urlsToCheck[1] = "http://www.alliprogramm.de/Forum/?";
    urlsToCheck[2] = "http://www.alliprogramm.de/PreparingForAlli/HealthyEating/TastyRecipes/?";
    urlsToCheck[3] = "http://www.alliprogramm.de/ConsideringAlli/VisceralFat/?";

    /* staging adword urls for testing */
    urlsToCheck[4] = "http://stagingiis6.alliprogramm.de/?";
    urlsToCheck[5] = "http://stagingiis6.alliprogramm.de/Forum/?";
    urlsToCheck[6] = "http://stagingiis6.alliprogramm.de/PreparingForAlli/HealthyEating/TastyRecipes/?";
    urlsToCheck[7] = "http://stagingiis6.alliprogramm.de/ConsideringAlli/VisceralFat/?";


    if (adwordsCheck(urlsToCheck, fullUrl)) {
        adwordsChangeUI(fullUrl);
    }

    // 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;
    });

    //add thickbox class to tell a friend spotlight

    $("#sidelinks li a:contains('Freunden')").addClass("thickbox");


    // 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();
    }

    // Remove first spotlight in HCP secion until content ready

    if (bodyClass.substring(0, 3) == "hcp") {
        $('#sidelinks li:eq(0)').hide();
    }


    // add "Back to top" links on pages taller than 850px
    if (($("#content_main").height() > 850) || bodyClass == "faq") {

        var colClass = $("#column1").attr("class");
        var colClass2 = $("#column2").attr("class");

        if (($("#column1").length > 0) && (colClass != "faqhome")) {
            $("<p><a id='backTopLink' href=\"#content\">Zur&#252;ck nach oben</a></p>").appendTo("#column1");
        } else if (($("#column2").length > 0) && (colClass2 != "updates")) {
            $("<p><a id='backTopLink' href=\"#content\">Zur&#252;ck nach oben</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 "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");
            });
            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();
            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");
			});
		}
	});
};

// check if we're on one of the specific adwords locations then check the querystring for adwords parameters
// and amend the UI as needed
function adwordsCheck(urlsToCheck, fullUrl) {
    for (i = 0; i < urlsToCheck.length; i++) {
        if (fullUrl.indexOf(urlsToCheck[i]) != -1) {
            adwordsChangeUI();
            return true;
            break;
        }
    }
    return false;
}

// check that the URL has the right google adwords parameters present and then
// remove certain UI elements and insert a legal blurb if so
function adwordsChangeUI(fullUrl) {
    var fullUrl = window.location.href, 
        qs = fullUrl.substr(fullUrl.indexOf("?") + 1, fullUrl.length),
        qsArray = qs.split("&");

    // check for google adwords QS stuff
    if ((qsArray[0] == "src=ppc") && qsArray[1] == "site=google") {
    
        // remove tools and 3 header links
        $("#notcontent #myTools").remove();
        $("ul#shortcuts li a:contains('Was gibt’s Neues?')").parent("li").remove();
        $("ul#shortcuts li a:contains('Downloads')").parent("li").remove();
        $("ul#shortcuts li a:contains('Pressebereich')").parent("li").remove();
        
        // insert legal blurb paragraph if it's not there already
        if ( $(".adwordPara").length == 0 ) {
            $("#notcontent #region").after("<p class='adwordPara'>alli Wirkstoff: Orlistat. Anwendungsgebiete: alli dient der Gewichtsreduktion und wird bei übergewichtigen Erwachsenen ab 18 Jahren mit einem Body-Mass-Index (BMI) von 28 oder darüber angewendet. alli sollte in Verbindung mit einer fett- und kalorienreduzierten Ernährung angewendet werden. Zu Risiken und Nebenwirkungen lesen Sie die Packungsbeilage und fragen Sie Ihren Arzt oder Apotheker. GlaxoSmithKline Consumer Healthcare GmbH & Co. KG - 77815 Bühl. </p>");
        }
    }
}