/*
* Copyright (C) 2012 PrimeBox (info@primebox.co.uk)
*
* This is a free jQuery plugin. Use it, modify it, distribute it
* as you wish!
*
* Documentation available at:
* http://www.primebox.co.uk/projects/cookie-bar/
*
* When using this software you use it at your own risk. We hold
* no responsibility for any damage caused by using this plugin
* or the documentation provided.
*/
(function($){
$.cookieBar = function(options,val){
if(options=='cookies'){
var doReturn = 'cookies';
}else if(options=='set'){
var doReturn = 'set';
}else{
var doReturn = false;
}
var defaults = {
message: '
We use cookies on our website, if you continue to use the site without changing your settings, we´ll assume that you are happy to receive all cookies on the site.
', //Message displayed on bar
acceptButton: true, //Set to true to show accept/enable button
acceptText: 'Accept', //Text on accept/enable button
declineButton: true, //Set to true to show decline/disable button
declineText: 'Decline', //Text on decline/disable button
policyButton: true, //Set to true to show Privacy Policy button
policyText: 'Privacy Policy', //Text on Privacy Policy button
policyURL: '/privacy', //URL of Privacy Policy
autoEnable: false, //Set to true for cookies to be accepted automatically. Banner still shows
acceptOnContinue: false, //Set to true to silently accept cookies when visitor moves to another page
expireDays: 365, //Number of days for cookieBar cookie to be stored for
forceShow: false, //Force cookieBar to show regardless of user cookie preference
effect: 'fade', //Options: slide, fade, hide
element: 'body', //Element to append/prepend cookieBar to. Remember "." for class or "#" for id.
append: false, //Set to true for cookieBar HTML to be placed at base of website. Actual position may change according to CSS
fixed: true, //Set to true to add the class "fixed" to the cookie bar. Default CSS should fix the position
redirect: String(window.location.href), //Current location
domain: String(window.location.hostname), //Location of privacy policy
referrer: String(document.referrer) //Where visitor has come from
}
var options = $.extend(defaults,options);
//Sets expiration date for cookie
var expireDate = new Date();
expireDate.setTime(expireDate.getTime()+(options.expireDays*24*60*60*1000));
expireDate = expireDate.toGMTString();
var cookieEntry = 'cb-enabled={value}; expires='+expireDate+'; path=/'
//Retrieves current cookie preference
var i,cookieValue='',aCookie,aCookies=document.cookie.split('; ');
for (i=0;i0 && String(window.location.href).indexOf(options.policyURL)==-1 && doReturn!='cookies' && doReturn!='set' && cookieValue!='accepted' && cookieValue!='declined'){
doReturn = 'set';
val = 'accepted';
}
}
if(doReturn=='cookies'){
//Returns true if cookies are enabled, false otherwise
if(cookieValue=='enabled' || cookieValue=='accepted'){
return true;
}else{
return false;
}
}else if(doReturn=='set' && (val=='accepted' || val=='declined')){
//Sets value of cookie to 'accepted' or 'declined'
document.cookie = cookieEntry.replace('{value}',val);
if(val=='accepted'){
return true;
}else{
return false;
}
}else{
//Sets up enable/accept button if required
var message = options.message.replace('{policy_url}',options.policyURL);
if(options.acceptButton){
var acceptButton = ''+options.acceptText+'';
}else{
var acceptButton = '';
}
//Sets up disable/decline button if required
if(options.declineButton){
var declineButton = ''+options.declineText+'';
}else{
var declineButton = '';
}
//Sets up privacy policy button if required
if(options.policyButton){
var policyButton = ''+options.policyText+'';
}else{
var policyButton = '';
}
//Whether to add "fixed" class to cookie bar
if(options.fixed){
var fixed = ' class="fixed"';
}else{
var fixed = '';
}
//Displays the cookie bar if arguments met
if(options.forceShow || cookieValue=='enabled' || cookieValue==''){
if(options.append){
$(options.element).append(''+message+acceptButton+declineButton+policyButton+'
');
}else{
$(options.element).prepend('
'+message+acceptButton+declineButton+policyButton+'
');
}
}
//Sets the cookie preference to accepted if enable/accept button pressed
$('#cookie-bar .cb-enable').click(function(){
document.cookie = cookieEntry.replace('{value}','accepted');
if(cookieValue!='enabled' && cookieValue!='accepted'){
window.location = options.currentLocation;
}else{
if(options.effect=='slide'){
$('#cookie-bar').slideUp(300,function(){$('#cookie-bar').remove()});$("#cookie-bar-changesetting-on").hide();$("#cookie-bar-changesetting").show();
}else if(options.effect=='fade'){
$('#cookie-bar').fadeOut(300,function(){$('#cookie-bar').remove()});$("#cookie-bar-changesetting-on").hide();$("#cookie-bar-changesetting").show();
}else{
$('#cookie-bar').hide(0,function(){$('#cookie-bar').remove()});$("#cookie-bar-changesetting-on").hide();$("#cookie-bar-changesetting").show();
}
return false;
}
});
//Sets the cookie preference to declined if disable/decline button pressed
$('#cookie-bar .cb-disable').click(function(){
var deleteDate = new Date();
deleteDate.setTime(deleteDate.getTime()-(864000000));
deleteDate = deleteDate.toGMTString();
aCookies=document.cookie.split('; ');
for (i=0;i
=0){
document.cookie = aCookie[0]+'=0; expires='+deleteDate+'; domain='+options.domain.replace('www','')+'; path=/';
//document.cookie = aCookie[0]+'=0; expires='+deleteDate+'; domain='+options.domain+'; path=/';
}else{
document.cookie = aCookie[0]+'=0; expires='+deleteDate+'; path=/';
}
}
document.cookie = cookieEntry.replace('{value}','declined');
if(cookieValue!='disabled' && cookieValue!='declined'){
window.location = options.redirect;
}else{
if(options.effect=='slide'){
$('#cookie-bar').slideUp(300,function(){$('#cookie-bar').remove()});$("#cookie-bar-changesetting-on").hide();$("#cookie-bar-changesetting").show();
}else if(options.effect=='fade'){
$('#cookie-bar').fadeOut(300,function(){$('#cookie-bar').remove()});$("#cookie-bar-changesetting-on").hide();$("#cookie-bar-changesetting").show();
}else{
$('#cookie-bar').hide(0,function(){$('#cookie-bar').remove()});$("#cookie-bar-changesetting-on").hide();$("#cookie-bar-changesetting").show();
}
return false;
}
});
}
}
})(jQuery);