/*  Media Converter - Premium Accounts
 *  (c) 2009 Pascal Beyeler
 *--------------------------------------------------------------------------*/

var premiumAccounts = Class.create({

	initialize: function() {
		this.pingInterval = null;

		if(!$('wizard_ad')) {
			//set the ping interval to keep the session during long conversions
			this.pingInterval = window.setInterval(this.ping,60000);
		};
	},

	login: function() {

		var postBody = 'username=' + $F('login_username') + '&password=' + $F('login_password') + '&autologin=' + $('login_autologin').checked + '&submitted=true';
		new Ajax.Request('/sidebar/login/' + Math.random(), {
			method: 'post',
			postBody: postBody,
			contentType: 'application/x-www-form-urlencoded' ,
			onSuccess: function(transport) {

				var response = transport.responseXML;
				var root_node = response.getElementsByTagName('root')[0];
				var status = root_node.getElementsByTagName("status")[0].firstChild.nodeValue;
				var html = root_node.getElementsByTagName("html")[0].firstChild.nodeValue;

				if(status == '1') {
					$('sidebar_top').update(html);
				} else {
					//reload the window
					var curr_location = window.location.href;
					window.location.href = curr_location.slice(0,curr_location.lastIndexOf('/'));
				}

			}
		});

	},

	logout: function() {

		new Ajax.Request('/sidebar/logout/' + Math.random(), {
			method: 'get',
			onSuccess: function(transport) {

				//remove the ping interval
				window.clearInterval(this.pingInterval);

				//reload the window
				var curr_location = window.location.href;
				window.location.href = curr_location.slice(0,curr_location.lastIndexOf('/'));

			}.bind(this)
		});

	},

	ping: function() {

		new Ajax.Request('/ping/index/' + Math.random(), {
			method: 'get',
			onSuccess: function(transport) {
			}
		});

	},

	switchForm: function(id) {

		switch(id) {

			case 'password':

				$('sidebar_changepassword_form').toggle();
				$('sidebar_changeemail_form').hide();

				break;

			case 'email':

				$('sidebar_changeemail_form').toggle();
				$('sidebar_changepassword_form').hide();

				break;

			case 'login':

				new Ajax.Request('/sidebar/login/' + Math.random(), {
					method: 'get',
					onSuccess: function(transport) {

						var response = transport.responseXML;
						var root_node = response.getElementsByTagName('root')[0];
						var html = root_node.getElementsByTagName("html")[0].firstChild.nodeValue;

						$('sidebar_top').update(html);

					}
				});

				break;

			case 'forgotpassword':

				new Ajax.Request('/sidebar/forgotpassword/' + Math.random(), {
					method: 'get',
					onSuccess: function(transport) {

						var response = transport.responseXML;
						var root_node = response.getElementsByTagName('root')[0];
						var html = root_node.getElementsByTagName("html")[0].firstChild.nodeValue;

						$('sidebar_top').update(html);

					}
				});

				break;

		}

	},

	changePassword: function() {

		var postBody = 'password1=' + $F('sidebar_newpassword1') + '&password2=' + $F('sidebar_newpassword2');
		new Ajax.Request('/sidebar/changepassword/' + Math.random(), {
			method: 'post',
			postBody: postBody,
			contentType: 'application/x-www-form-urlencoded' ,
			onSuccess: function(transport) {

				var response = transport.responseXML;
				var root_node = response.getElementsByTagName('root')[0];
				var status = root_node.getElementsByTagName("status")[0].firstChild.nodeValue;
				var html = root_node.getElementsByTagName("html")[0].firstChild.nodeValue;

				$('sidebar_changepassword_form').update(html);

			}
		});

	},

	changeEmail: function() {

		var postBody = 'email=' + $F('sidebar_newemail');
		new Ajax.Request('/sidebar/changeemail/' + Math.random(), {
			method: 'post',
			postBody: postBody,
			contentType: 'application/x-www-form-urlencoded' ,
			onSuccess: function(transport) {

				var response = transport.responseXML;
				var root_node = response.getElementsByTagName('root')[0];
				var status = root_node.getElementsByTagName("status")[0].firstChild.nodeValue;
				var html = root_node.getElementsByTagName("html")[0].firstChild.nodeValue;

				$('sidebar_changeemail_form').update(html);

			}
		});

	},

	forgotPassword: function() {

		var postBody = 'email=' + $F('forgotpassword_email') + '&submitted=true';
		new Ajax.Request('/sidebar/forgotpassword/' + Math.random(), {
			method: 'post',
			postBody: postBody,
			contentType: 'application/x-www-form-urlencoded' ,
			onSuccess: function(transport) {

				var response = transport.responseXML;
				var root_node = response.getElementsByTagName('root')[0];
				var status = root_node.getElementsByTagName("status")[0].firstChild.nodeValue;
				var html = root_node.getElementsByTagName("html")[0].firstChild.nodeValue;

				$('sidebar_top').update(html);

			}
		});

	}

});

var premium = new premiumAccounts();