// ###############################
// ######### BEGIN CONFIG SECTION# 
// ###############################
// ######### EDIT TO CUSTOMISE ###
// ###############################

var config = {
	//this is the text above
	popupContent: "<div id='cookieDialogue' title='To use site with only essential cookies close box >>'><p><b>Cookies and Your Privacy </b></p><p>On 26 May 2011, the rules about <b>cookies</b> on websites changed.</p> <p>This site uses cookies. One or more of the cookies we use are essential for parts of this website to operate and has already been set. </p><p>You may delete and block cookies from this site, but parts of the site may not work. To find out more about cookies used on this website and how to delete cookies, see our <a href='/page.asp?id=privacy'><b>privacy policy</b></a> cookie section.</p><p>This web site sometimes displays adverts that incorporate Online Behavioural Advertising. Visit <a href='http://www.YourOnlineChoices.com' target='_blank'>www.YourOnlineChoices.com</a> for more information.<p></div>",
	width:"500px",
	autoOpen: false, 
	//image for floating cookie clickable
	frontBoxImage: "/images/yourcookies-tab.png",
	frontBoxPosition: "leftTop",//leftMiddle,leftTop,leftBottom,rightMiddle,rightTop,rightBottom
	frontBoxClass: "cookieFloatingBox",
	//text for dontshow button--prevents showing the floating box again
	okayButtonText: "I understand and accept this site uses cookies. Do not show this message again", 
	//cookie name to be used to specify the floating box is not to be shown
	disabledFloaterCookieName: "wise_cookie_accept",
	okayFunc:" " 
};

// ###############################
// ############ END CONFIG SECTION
// ###############################
// ############ DO NOT EDIT ######
// ###############################

(function() {

	function scriptLoad(url, callback) {
		var ieLoaded  = function(script) { 
			if(script.readyState=='loaded' || script.readyState=='complete') {
				script.onreadystatechange=null;
				return true;
			} else return false;
		};
		var addOnloadCallback = function(script, callback) {
			if (window.attachEvent) {
				script.onreadystatechange=function(){ if(ieLoaded(script)) { callback(); } }; 
			} else
				script.onload = callback; 
		};
		var insertIntoScript = function(scriptElement, script) {
			if(window.attachEvent) {
				scriptElement.text = script;
			} else {
				scriptElement.innerHTML = script
			}
		};
		var insertScript = function(url, callback) { // if no url, just insert callback into the script
			var ga1 = document.createElement('script'); 
			ga1.type = 'text/javascript'; ga1.async = 'true'; 
			if(url==null) { // just insert the callback function in the <script> and finish
				insertIntoScript(ga1, callback);
				var s = document.getElementsByTagName('script')[0];
				s.parentNode.insertBefore(ga1, s);
				return null;
			} else { // add a url only, call back added later
				ga1.src=url[0];
				var s = document.getElementsByTagName('script')[0];
				s.parentNode.insertBefore(ga1, s);
				return ga1;
			}
		}
		var script;
		if(!(script = insertScript(url, callback))) return; //if the callback is to be inserted in a new <script> (via entering null as the url) exit
		if(url.length>1) { // call this function again, with the other url, if there's more than one
			addOnloadCallback(script, function() { url.shift(); scriptLoad(url, callback); });
		} else { // if there's only one script left, load it then call the callback
			addOnloadCallback(script, callback);
		}
	}

	scriptLoad(['https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.js', 
		   'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js'], 
		   function() { load(); start(); });

	//load the functionality for displaying the popup
	function load() {
		//define the actual floating popup
		floatingPopupDialogue = {
			box: undefined, 
			disabledFloaterCookieName: undefined,
			init: function(optionsHash) {
				var that = this;
				this.disabledFloaterCookieName=optionsHash['disabledFloaterCookieName'];
				if(this.insertIfAlreadyAllowed(optionsHash['okayFunc'])) return;
				var popup = PopupDialogue.init(optionsHash['popupContent'], optionsHash['height'], optionsHash['width'], optionsHash['autoOpen'], optionsHash['okayButtonText'], 
							   function() { that.disableFloatingBox(); 
									scriptLoad( null, optionsHash['okayFunc']); 
									} );
				this.box = this.makeFrontBox(optionsHash['frontBoxClass'], optionsHash['frontBoxImage']);
				this.setLocation(optionsHash['frontBoxPosition']);
				this.box = this.frontClickable(this.box,popup);
				$(document.body).append(this.box);
			},
			makeFrontBox: function(boxClass, boxImg) {
				var box = $('<div>').attr('id', boxClass);
				box.append($('<img>').attr('src', boxImg).css('cursor', 'pointer')); 
				return box;
			},
			setLocation: function(locationName) {
				this.box.css('position', 'absolute');
				if(locationName=='leftMiddle') this.box.css('top', '50%').css('left', '0px');
				else if(locationName=='leftTop') this.box.css('top', '0%').css('left', '0px');
				else if(locationName=='leftBottom') this.box.css('bottom', '0px').css('left', '0px');
				else if(locationName=='rightMiddle') this.box.css('top', '50%').css('right', '0px');
				else if(locationName=='rightTop') this.box.css('top', '0px').css('right', '0px');
				else if(locationName=='rightBottom') this.box.css('bottom', '0px').css('right', '0px');
				else this.box.css('top', '50%').css('left', '0px'); //default
			},
			disableFloatingBox: function() {
				var exdate=new Date();
				exdate.setFullYear(exdate.getFullYear() + 2);
				var c_value=escape('yes') + "; expires="+exdate.toGMTString() + '; path=/';
				document.cookie=this.disabledFloaterCookieName + "=" + 'yes;' + c_value;
				this.box.hide();
			},
			isDisabled: function() {
				var cookies = document.cookie.split(';');
				for(var a in cookies) {
					if(jQuery.trim(cookies[a])===this.disabledFloaterCookieName+'=yes') return true;
				}
				return false;
			},
			insertIfAlreadyAllowed: function(jsmethod) {
				if(this.isDisabled()) { 
					scriptLoad( null, jsmethod);
					return true;
				}
				return false;
			},
			frontClickable: function(box, popup) {
				box.click(function() {
					popup.open();
				});
				return box;
			}
		};

		//define the dialogue box that appears on clicking the floating part
		var PopupDialogue = {
			popup: undefined,
			init: function(textToInsert, height, width, autoOpen,
					      okayText, dontshowFunc) {
				//var okay = this.makeOkayButton(okayText, okayButtonClass);
				//var dontshow = this.makeDontshowButton(dontshowText, dontshowClass, dontshowFunc);
				//var buttonBar = this.makeButtonBar().append(okay, dontshow);
				//this.popup.append(buttonBar);
				//return this.popup;
				this.popup = $(textToInsert).dialog({modal: true, autoOpen: autoOpen, show: 'drop', height: 'auto', width: width});
				this.popup.dialog("option", "buttons", [ 
							    	{ 
									text:okayText, 
									click: function() { dontshowFunc();$(this).dialog('close'); } 
								}
								]);
				$('button').blur();
				return this;
			},
			open: function() {
				this.popup.dialog('open');
			},
			makeOkayButton: function(okayText, okayButtonClass) {
				var that = this;
				return $('<span id='+okayButtonClass+'>'+okayText+'</span>')
					.click(function() { that.popup.hide(); });
			},
			makeDontshowButton: function(dontshowText, dontshowClass, dontshowFunc) {
				var that = this;
				return $('<span id='+dontshowClass+'>'+dontshowText+'</span>')
					.click(function() { 
							that.popup.hide();
							dontshowFunc(); 
							});
		       },
		       makeButtonBar: function() {
			      return $('<div id="wise_buttonbar">').css('text-align', 'center');
		       }
		};
	}
	// load the floating popup dialogue box. config defined at the start of the file.
	function start() {
			floatingPopupDialogue.init(config)
	}
})();
