
var ProfileDetailRunner = Class.create({
	initialize : function(userId, deliveryTypeInstant)
	{
		this.deliveryTypeInstant = deliveryTypeInstant;
		this.client = new AjaxClient();
		$('AlertProfile_Name').observe('change', function(event){
			var element = event.element();
		});
		this.userId = userId;

		// fancy zoom stuff
		FancyZoomBox.init();
		$('addProfileLink').observe('click', this.onShowProfile.bind(this));
	},
	onWirePopupEvents : function()
	{
		$('AlertProfile_AllJurisdictions').observe('click', this.showJurisdictionList.bind(this,false));
		$('AlertProfile_SomeJurisdictions').observe('click', this.showJurisdictionList.bind(this,true));
		
		$('AlertProfile_Save').observe('click', this.onSaveProfile.bind(this));
		
		$('linkProfileOptions').observe('click', this.onTabChange.bind(this, true));
		$('linkAlertOptions').observe('click', this.onTabChange.bind(this, false));
		
		$('AlertProfile_Cancel').observe('click', function(event) { FancyZoomBox.hide(event); });
	},
	onTabChange : function(showProfile, event)
	{
		if(showProfile && !$('tableProfileOptions').visible())
			this.doTabSwitch('ProfileOptions', 'AlertOptions');
		else if(!showProfile && !$('tableAlertOptions').visible())
			this.doTabSwitch('AlertOptions', 'ProfileOptions');
	},
	doTabSwitch : function(turnOn, turnOff)
	{
		$('table'+turnOff).hide();
		$('link'+turnOff).removeClassName('selectedTab');
		$('table'+turnOn).show();
		$('link'+turnOn).addClassName('selectedTab');		
	},
	showJurisdictionList : function(show, event)
	{
		var d=document.getElementById("divJur"); 
		if (!show){d.style.display="none"}else{d.style.display="block"}
	},
	onShowProfile : function(event, profileId)
	{
		$('alertEmailSpinnerDiv').show();
		$('alertEmailContentDiv').hide();
		this.profileId = profileId;
		// get the data for this profile
		var editButton = event.element();
		// must show first
		this.showPopup(event, function()
		{
			this.getAsyncProfileDetails(profileId, function(alertProfile)
			{
				$('alertEmailContentDiv').show();
				$('alertEmailSpinnerDiv').hide();
				this.onWirePopupEvents();
				this.alertProfile = alertProfile;
				this.clearProfileDetails();
				this.loadProfileDetails((profileId != undefined)?true:false);				
			}.bind(this));
			
		}.bind(this));
	},
	onSaveProfile : function(event)
	{
		MM_validateForm('AlertProfile_Name','','R','AlertProfile_Email','','RisEmail');
		if (document.MM_returnValue)
		{
			// create a new object
			var profile = {};
			profile.ProfileId = (this.profileId == undefined)?"":this.profileId;
			profile.Name = $('AlertProfile_Name').value;
			profile.Email = $('AlertProfile_Email').value;
			
			if($('AlertProfile_DeliveryTypeInstant').checked)
				profile.Subscription_Delivery_Type_ID =  $('AlertProfile_DeliveryTypeInstant').value;
			else
				profile.Subscription_Delivery_Type_ID =  $('AlertProfile_DeliveryTypeDaily').value;
				
			if($('AlertProfile_AllJurisdictions').checked)
			{
				profile.All_Jurs_Selected =  1;
				profile.Jurisdiction_IDs = Array();
			}
			else
			{
				profile.All_Jurs_Selected =  0;
				// also compile list of jurisdictions
				var checkboxes = document.forms['alertDetailsForm']['Jurisdiction_IDs'];
				var len = checkboxes.length;
				var selectedJurs = Array();
				for(var counter = 0; counter < len; counter++)
				{
					if(checkboxes[counter].checked)
						selectedJurs.push(checkboxes[counter].value);
				}
				profile.Jurisdiction_IDs = selectedJurs;
			}			
			
			// save the profile
			this.client.RunCommand('Profile', 'SaveAlertProfile', {'profile' : $H(profile).toJSON(), 'userId' : this.userId},function(ob)
			{
				// all done successfully so close the
				//Lightview.hide();
				FancyZoomBox.hide();
				//$('formsubmitted').value = "0";
				//document.forms['form1'].submit();
				document.location = 'profile_details.php';
			});
		}
	},
	getAsyncProfileDetails : function(profileId, callback)
	{
		// get the profile details via ajax
		if(profileId == undefined)
			profileId = "";
		this.client.RunCommand('Profile', 'LoadAlertProfile', {'profileId': profileId, 'userId': this.userId }, function(ob){
			callback(ob.profile);
		});
	},
	loadProfileDetails : function(existing)
	{
		if(existing)
		{
			$('titleDetailsPopup').update('Edit Profile');
		}
		else
		{
			$('titleDetailsPopup').update('New Profile');
		}
		
		$('AlertProfile_Name').value = this.alertProfile.Profile_Name;
		$('AlertProfile_Email').value = this.alertProfile.Email;
		if(this.alertProfile.Subscription_Delivery_Type_ID == this.deliveryTypeInstant)
			$('AlertProfile_DeliveryTypeInstant').checked = true;
		else
			$('AlertProfile_DeliveryTypeDaily').checked = true;
		
		
		$('divJur').update(this.alertProfile.allJurisdictionHtml);
		if(this.alertProfile.All_Jurs_Selected)
		{
			$('divJur').hide();
			$('AlertProfile_AllJurisdictions').checked = true;
		}
		else
		{
			$('AlertProfile_SomeJurisdictions').checked = true;
			$('divJur').show();
		}
		
		if(!$('tableProfileOptions').visible())
			this.doTabSwitch('ProfileOptions', 'AlertOptions');
	},
	clearProfileDetails : function()
	{
		$('AlertProfile_Name').value = "";
		$('AlertProfile_Email').value = "";
		$('AlertProfile_DeliveryTypeInstant').checked = false;
		$('AlertProfile_DeliveryTypeDaily').checked = false;
		$('divJur').hide();
		$('AlertProfile_AllJurisdictions').checked = false;
		$('AlertProfile_SomeJurisdictions').checked = false;
	},
	showPopup : function(event, callback)
	{
		FancyZoomBox.showImmediate($('alertEmailDetails'),680,440,event.pointerX(), event.pointerY(), callback);
		// not using lightview anymore
		/*
		Lightview.show({
			overlay : 0,
			href: '#alertEmailDetails',
			options: {
				autosize : false,
				closeButton : 'large',
				topclose : true,
				height : 480,
				width : 680
			},
  		 	resizeDuration : .1
		});
		*/
	}
});
