$(document).ready(function(){ var bannerCookieValue = readCookie("bannerFlag"); var elem = $(".fullBanner p.textToReplace"); var showHideElem = $("#showHideButton p"); var bammerElem = $(".fullBanner"); messageToBeDisplayed(elem); displayHideBanner(bammerElem, showHideElem, bannerCookieValue); attachEventOnClick(bammerElem, showHideElem, bannerCookieValue); }) function messageToBeDisplayed(elem){ var today, switchDay; today = new Date(); switchDay = new Date(); switchDay.setFullYear(2016, 06, 01); if (switchDay > today){ elem.html("From the 1st July 2016 it will not be possible to renew your policy as echoice is closing."); } else { elem.html("Echoice is closing so it’s no longer possible to renew your policy."); } } function attachEventOnClick(bammerElem, showHideElem, bannerCookieValue){ var cookieFlag; showHideElem.parent().unbind("click").bind("click",function(){ if($(this).hasClass("show")){ cookieFlag = "Show"; } if($(this).hasClass("hide")){ cookieFlag = "Hide"; } displayHideBanner(bammerElem,showHideElem, cookieFlag) document.cookie = "bannerFlag="+cookieFlag+";path=/"; }) } function readCookie(name) { name += '='; for (var ca = document.cookie.split(/;\s*/), i = ca.length - 1; i >= 0; i--) if (!ca[i].indexOf(name)) return ca[i].replace(name, ''); } function displayHideBanner(bammerElem, showHideElem, bannerCookieValue) { if (bannerCookieValue != undefined){ if (bannerCookieValue == "Show"){ showHideElem.text(bannerCookieValue +" +"); showHideElem.parent().removeClass("show").addClass("hide"); bammerElem.css("display","none"); } else { showHideElem.text(bannerCookieValue +" -"); showHideElem.parent().removeClass("hide").addClass("show"); bammerElem.css("display","block"); } } }