$(function(){
	//Get the current domain and assign to global variable
	var mainDomain = document.domain;
	myDomain = mainDomain.substr(mainDomain.indexOf(".")); 
	$('.changeCookies').show();

	//Check to see if any cookies have been set so we know if user has been to site before 
	if (typeof(cdReadCookie) != "undefined"){
		if(!cdReadCookie('acceptTarget') && !cdReadCookie('rejectTarget')){	
			//Call wrapper functions so enclosed cookie scripts fire
			functionalCookieScriptWrapper();
			targetingCookieScriptWrapper();
			
			//Set all cookies as notice is shown
			setTargetCookies();
			setFunctionalCookies();
						
			//Display initial cookie notice
			//$('#cookieNotice').fadeIn('slow');
			$('#cookieNotice').slideDown({
				duration:1000,
				queue:false
			});
			$('#cookieNotice').animate({
				opacity: "0.8"
			}, 
			{
				duration:1000,
				queue:false
			});
			//$('#cookieNotice').slideDown({queue:false}).fadeTo({queue:false});
			$('#outerContainer').css('marginTop','30px');
			$('#header').css('marginTop','30px');
			$('#utilityNav').css('marginTop','30px');
			$('#logo').attr('src','/drupal/sites/all/themes/cigna/images/CignaColourLogoCookies.gif');
			$('#productName').attr('src','/drupal/sites/all/themes/cigna/images/cigna-global-health-options-cookies.gif');
			//$('#cookieNotice').delay(15000).fadeOut(400);	
			
		}else{
			//Code to handle all conditions when user arrives at page
			if(cdReadCookie('acceptTarget') && cdReadCookie('acceptFunctional')) {
				//User has fully accepted so call wrapper functions
				functionalCookieScriptWrapper();
				targetingCookieScriptWrapper();			
			}else if(cdReadCookie('rejectTarget') && cdReadCookie('rejectFunctional')) {
				//Do not call any scripts as all cookies rejected
			}else if(cdReadCookie('acceptTarget') && cdReadCookie('rejectFunctional')) {
				//Call targeting
				targetingCookieScriptWrapper();			
			}else if(cdReadCookie('rejectTarget') && cdReadCookie('acceptFunctional')) {
				//Call functional
				functionalCookieScriptWrapper();
			}
		}
	}
	

	function functionalCookieScriptWrapper(){
		//functional cookie scripts to go here
	}
	
	function targetingCookieScriptWrapper(){
		//targeting cookie scripts to go here
	}
	
	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);
		}	
	}
	
	$('.changeCookies').click(function() {
		//Hide the cookie notice if visible
		if ($('#cookieNotice').is(":visible")){
			$('#cookieNotice').hide();
		}
		showMask();

		if(cdReadCookie('acceptTarget')){
			$('#enableTargetCookies').hide();
			$('#removeTargetCookies').show();
			$('#cookieList3').removeClass('disabledCookieList').addClass('enabledCookieList');
		}else{
			$('#enableTargetCookies').show();
			$('#removeTargetCookies').hide();
			$('#cookieList3').removeClass('enabledCookieList').addClass('disabledCookieList');
		}

		if(cdReadCookie('acceptFunctional')){
			$('#enableFunctionalCookies').hide();
			$('#removeFunctionalCookies').show();
			$('#cookieList2').removeClass('disabledCookieList').addClass('enabledCookieList');
		}else{
			$('#enableFunctionalCookies').show();
			$('#removeFunctionalCookies').hide();
			$('#cookieList2').removeClass('enabledCookieList').addClass('disabledCookieList');
		}

		//Get the window height and width
		var winH = jQuery(window).height();
		var winW = jQuery(window).width();
			//Set the popup window to center
			jQuery('#cookiesWindow.dialog2').css('top',  '20px');
			jQuery('#cookiesWindow.dialog2').css('left', winW/2-jQuery('#cookiesWindow.dialog2').width()/2);
			
			//transition effect
			jQuery('#cookiesWindow.dialog2').fadeIn(2000); 
	
	});

	$('.closeCookieNotice').click(function() {
		$('#cookieNotice').hide();
		setTargetCookies();
		setFunctionalCookies();
		location.reload(true);
	});
	
	//Click functions to show/hide crosses and buttons
	$('#enableFunctionalCookies').click(function() {
		$('#enableFunctionalCookies').hide();
		$('#removeFunctionalCookies').show();
		$('#cookieList2').removeClass('disabledCookieList').addClass('enabledCookieList');
		return false;
	});

	$('#removeFunctionalCookies').click(function() {
		$('#enableFunctionalCookies').show();
		$('#removeFunctionalCookies').hide();
		$('#cookieList2').removeClass('enabledCookieList').addClass('disabledCookieList');
		return false;
	});

	$('#enableTargetCookies').click(function() {
		$('#enableTargetCookies').hide();
		$('#removeTargetCookies').show();
		$('#cookieList3').removeClass('disabledCookieList').addClass('enabledCookieList');
		return false;
	});

	$('#removeTargetCookies').click(function() {
		$('#enableTargetCookies').show();
		$('#removeTargetCookies').hide();
		$('#cookieList3').removeClass('enabledCookieList').addClass('disabledCookieList');
		return false;
	});

	$('#savePreferences').click(function() {
		if ($('#enableFunctionalCookies').is(":visible")){
			deleteFunctionalCookies();
		}else if ($('#removeFunctionalCookies').is(":visible")){
			setFunctionalCookies();
		}
		
		if ($('#enableTargetCookies').is(":visible")){
			deleteTargetCookies();
		}else if ($('#removeTargetCookies').is(":visible")){
			setTargetCookies();
		}
		
		jQuery('.window').hide();
		jQuery('.mask').hide();
		$('.mask').stop(true,true);//clear queue of animations
	
		location.reload(true);
	});
	
	//Functions to delete cookies and set rejections
	function deleteFunctionalCookies(){
		cdEraseCookie('acceptFunctional',myDomain);	
	
		//Set a new cookie so that we remember the user's choice
		cdCreateCookie('rejectFunctional',0,730, myDomain);
	
	}

	function deleteTargetCookies(){
		cdEraseCookie('acceptTarget',myDomain);	
	
		//Set a new cookie so that we remember the user's choice
		cdCreateCookie('rejectTarget',0,730, myDomain);			
	}

	//Functions to set acceptance cookies
	function setFunctionalCookies(){
		//Create the acceptance cookie	
		cdCreateCookie('acceptFunctional',1,730, myDomain);	
	
		//Erase the reject cookie as user has accepted
		cdEraseCookie('rejectFunctional', myDomain);	
	}
	
	
	function setTargetCookies(){
		//Create the acceptance cookie	
		cdCreateCookie('acceptTarget',1,730, myDomain);	
	
		//Erase the reject cookie as user has accepted
		cdEraseCookie('rejectTarget', myDomain);	
	}
		
	//Show the mask overlay
	function showMask(){
		//Get the screen height and width
		var maskHeight = jQuery(document).height();
		var maskWidth = jQuery(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		jQuery('.mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		jQuery('.mask').fadeIn(1000);	
		jQuery('.mask').fadeTo("slow",0.5);	

	}

	//Code to redraw modals on browser resize
	$(window).resize(function() {
		var winH = jQuery(window).height();
		var winW = jQuery(window).width();
		
		if ($('#cookiesWindow').is(":visible")){
			jQuery('#cookiesWindow.dialog2').css('top',  '20px');
			jQuery('#cookiesWindow.dialog2').css('left', winW/2-jQuery('#cookiesWindow.dialog2').width()/2);
		}
		if ($('.mask').is(":visible")){
			showMask();
		}	
	});
		
	//if close button is clicked
	jQuery('.closeModal').unbind('click');
	jQuery('.closeModal').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
			
		jQuery('.window').hide();
		jQuery('.mask').hide();
		$('.mask').stop(true,true);//clear queue of animations
	});		
});
