// Cookies Directive Disclosure Script Version 1.0 by Ollie Phillips
function cookiesDirective(privacyPolicyUri,cookieScripts){
	
	// Have we already been granted permission, if so lets not keep doing this
	if(!cdReadCookie('cookiesDirective')){
		// Going to use JQuery, if we've not got it we load it
		if(!(window.jQuery)){
			var s = document.createElement('script');
			s.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js');
			s.setAttribute('type', 'text/javascript');
			document.getElementsByTagName('head')[0].appendChild(s);
		}
		
		// Give the HTTP request time to complete
		if(cookieScripts){
			setTimeout("cdHandler('"+ privacyPolicyUri +"','" + cookieScripts + "')",1000);
		}else{
			setTimeout("cdHandler('"+ privacyPolicyUri +"')",1000);
		}
	
	}else{
		if(!(window.jQuery)){
			var s = document.createElement('script');
			s.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js');
			s.setAttribute('type', 'text/javascript');
			document.getElementsByTagName('head')[0].appendChild(s);
		}
		// Cookies have been accepted, call our wrapper function here so that enclosed cookie setting scripts fire 
		setTimeout("cookiesDirectiveScriptWrapper()",1000);
	}
	
}

function cdHandler(privacyPolicyUri,cookieScripts){
	
	// Our main disclosure script
	$(document).ready(function(){

		var epdApps;
		var epdAppsCount;
		var epdAppsDisclosure;
		var epdPrivacyPolicyUri;
		
		epdPrivacyPolicyUri = privacyPolicyUri;
		
		// What scripts must be declared, user passes these as comma delimited string
		if (cookieScripts){
			epdApps = cookieScripts.split(',');
			epdAppsCount = epdApps.length;
			var epdAppsDisclosureText='';
			if(epdAppsCount>1){
				for(var t=0; t < epdAppsCount - 1; t++){
					epdAppsDisclosureText += epdApps[t] + ', ';				
				}	
				epdAppsDisclosure = ' Vi anv&auml;nder ocks&aring; ' + epdAppsDisclosureText.substring(0, epdAppsDisclosureText.length - 2) + ' och ' + epdApps[epdAppsCount - 1] + ' script, wilka alla anv&auml;nder kakor. ';
			}else{
				epdAppsDisclosure = ' Vi anv&auml;nder ocks&aring; ' + epdApps[0] + ' vilket i sin tur anv&auml;nder sig av kakor.';		
			}	
		}else{
			epdAppsDisclosure = '';
		}
		
		// Create our overlay with message
		var divNode = document.createElement('div');
		divNode.setAttribute('id','epd');
		document.body.appendChild(divNode);
		
		// The disclosure narrative pretty much follows that on the Information Commissioners Office website		
		var disclosure = '<div id="cookiesdirective" style="position:absolute;top:-300px;left:0px;width:100%;height:auto;background:#000000;opacity:.80; -ms-filter: “alpha(opacity=80)”; filter: alpha(opacity=80);-khtml-opacity: .80; -moz-opacity: .80; color:#FFFFFF;font-family:verdana;font-size:14px;text-align:center;z-index:1000;">';
		
		disclosure +='<div style="position:relative;height:auto;width:90%;padding:15px;margin-left:auto;margin-right:auto;">';	
		disclosure += 'Den 1:a juli 2011 f&ouml;r&auml;ndrades lagen om elektronisk kommunikation (2003:389). Det inneb&auml;r att du som bes&ouml;ker en webbplats aktivt m&aring;ste samtycka till att webbplatsen anv&auml;nder s&aring; kallade kakor (eng. cookies).&nbsp; ';
		disclosure += 'P&aring; www.aba.nu anv&auml;nds sessionskakor, vilka kr&auml;vs f&ouml;r att v&aring;r tekniska plattform skall fungera. En s&aring;dan sessionskaka har redan skapats. '+ epdAppsDisclosure +' Det g&aring;r bra att radera alla ';
		disclosure += 'kakor fr&aring;n denna webbplats, men delar av webbplatsen kommer inte fungera.&nbsp;';
		disclosure += 'Vill du veta mer kan du l&auml;sa mer  <a style="color:#ca0000;font-weight:bold;font-family:arial;font-size:14px;" href="'+ epdPrivacyPolicyUri + '">h&auml;r</a>.<br/><br/>';
		disclosure += '<div id="epdnotick" style="color:#ca0000;display:none;margin:2px;"><span style="background:#cecece;padding:2px;">You must tick the "I accept cookies from this site" box to accept</span></div>'
		disclosure += 'I accept cookies from this site <input type="checkbox" name="epdagree" id="epdagree" />&nbsp;';
		disclosure += '<input type="submit" name="epdsubmit" id="epdsubmit" value="Continue"/><br/><br/></div></div>';
		document.getElementById("epd").innerHTML= disclosure;
	  	
		// Bring our overlay in
		$('#cookiesdirective').animate({
		    top: '0'
		 }, 1000, function() {
			// Overlay is displayed, set a listener on the button
			$('#epdsubmit').click(function() {
			  
				if(document.getElementById('epdagree').checked){
					// Set a cookie to prevent this being displayed again
					cdCreateCookie('cookiesDirective',1,365);	
					// Close the overlay
					$('#cookiesdirective').animate({
						top:'-300'
					},1000,function(){
						// Remove the elements from the DOM and reload page, which should now
						// fire our the scripts enclosed by our wrapper function
						$('#cookiesdirective').remove();
						location.reload(true);
					});
				}else{
					// We need the box checked we want "explicit consent", display message
					document.getElementById('epdnotick').style.display = 'block';	
				}
			});
		});
	});
		
}

function cdScriptAppend(scriptURI,mylocation){
	// location is 'head', 'body' or specific or after a specific html element identified by its id attribute
	var elementId = String(mylocation);
	var s = document.createElement('script');
	s.setAttribute('src',scriptURI);
	s.setAttribute('type', 'text/javascript');
	
	switch(mylocation){
	case 'head':
		document.getElementsByTagName('head')[0].appendChild(s);
	  	break;
	case 'body':
		document.getElementsByTagName('body')[0].appendChild(s);
	  	break;
	default: 
		document.getElementById(elementId).appendChild(s);
	}	
}


// Simple Cookie functions from http://www.quirksmode.org/js/cookies.html - thanks!
function cdReadCookie(name) {
	
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
	
}

function cdCreateCookie(name,value,days) {
	
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	
}

