(function(){
	var loader = new YAHOO.util.YUILoader();
	JSModules.registerWithYUILoader(loader);
	loader.loadOptional = true;
	loader.combine = true;
	loader.require([ "formvalidation","stwprogressdialog","stwalertdialog","stwyuidialog","button"]);
	loader.insert();

	loader.onSuccess = function()
	{
		YAHOO.util.Dom.addClass(document.body, "yui-skin-sam");
		Privacy.initButtons(optIn);
	};
})();

var DEBUG = false;
var dialog = null;
optIn = false;
var Privacy = {
	optOutButton: null,
	optOutConfirm: function(){
		var html = "Are you sure you wish to opt out of Lat 49's optimization of advertising?";

		dialog = new STWYUIDialog("Please confirm opt out choice",html,{
			width: 300,
			draggable: true,
			effect: {effect:YAHOO.widget.ContainerEffect.FADE, duration: 0.25},
			buttons: STWYUIDialog.YES_BUTTON|STWYUIDialog.CANCEL_BUTTON
		});
		dialog.setCancelButtonCallback(cancelDialog);
		dialog.setOkButtonCallback(okDialog);

		dialog.show();

	},
	optIntoLat49: function(){
		setCookie("Thu, 01-Jan-1970 00:00:00 GMT");
		location=Session.docroot+"/docs/privacy.php?optout=0";
	},

	initButtons: function(optInValue){
		Privacy.optButton = new YAHOO.widget.Button("optButton", {disabled:false});
		// the button will be used to either allow
		// someone to opt out or, if the cookie already exists
		// to opt back in
		var cookie = cookieExists();
		if (cookieExists())
		{
			Privacy.optButton.on("click",this.optIntoLat49);
		}
		else
		{
			Privacy.optButton.on("click", this.optOutConfirm);
		}

	}
}

function cancelDialog(){
	dialog.hide();
}
function okDialog(){
	setCookie("12/12/2100 00:00:00");
	dialog.hide();
	location=Session.docroot+"/docs/privacy.php?optout=1";
}
function setCookie(date){
	document.cookie = "lat49OptOut=true; path=/; domain=.lat49.com; expires="+date+";";
}

function cookieExists() {

var name = 'lat49OptOut';
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
// NO COOKIE FOUND
if ((!start) && (name != document.cookie.substring( 0, name.length ) ) )
{
	return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}
