CH.ns(function() {with(CH) {
	/****************************************
	PORTAL CODE
	****************************************/
	Portal = Class.create({
		initialize: function (options) {
	    	this.setOptions(options);
		    var sortables = $A(document.getElementsByClassName(
		      this.options.column, this.options.portal
		    ));
		    
			sortables.each(function (sortable) {
				Sortable.create(sortable, { 
		        	containment: sortables,
			        constraint: false,
			        tag: 'div',
			        only: this.options.module,
			        dropOnEmpty: true,
			        handle: this.options.handle,
			        scroll: window,
			        hoverclass: this.options.hoverclass,
		        	onUpdate: function (container) {
		          		if (!this.options.saveurl) {
		            		return;
		          		}
		          		
		          		if (container.id == this.options.modulelist) {
		           	 		return;
		          		}
		          		
						var url = this.options.saveurl;
						var postBody = container.id + ':';
						var modules = $A(document.getElementsByClassName(
							this.options.module, container
						));
	
						postBody += modules.pluck('id').join(',');
						postBody = 'value=' + escape(postBody);
			          	postBody += '&type=position';
						
						new Ajax.Request(url, {
							method: 'post',
							postBody: postBody
						});
					}.bind(this)
				});
			}.bind(this));
	    
			var modules = $A(document.getElementsByClassName(
				this.options.module, this.options.portal
		    ));
		    
			modules.each(
				function (module) {
		
			      	var content = $A(module.getElementsByClassName(this.options.content))[0];
			        var toggle = $A(module.getElementsByClassName(this.options.toggle))[0];
			
			        if(content.style.display == 'none') {
						toggle.className = this.options.toggle_selected;
					} else {
						toggle.className = this.options.toggle;
					}
							
					Event.observe(
						toggle, 'click', 
						function (e) { 
							Effect.toggle(content, 'Blind');
							
							if(content.style.display == 'none') {
								show = 1;
								toggle.className = this.options.toggle;
							} else {
								show = 0;
								toggle.className = this.options.toggle_selected;
							}
							
							new Ajax.Request('/ajax/linkswap/save_module', {
								method: 'post',
								postBody: 'type=state&id=' + content.ancestors()[0].id + '&show=' + show
							});
	
						}.bind(this),
						false
					);
		        
				}.bind(this)
		    );
		
			new Draggable(this.options.modulelist, {
				handle: this.options.modulelisthandle
			});
	    
		},
	
		displaymoduleList: function (e) {
			Effect.toggle(this.options.modulelist);
			Event.stop(e);
		},
	
		setOptions: function (options) {
			this.options = {
				portal: 'portal',
				column: 'portal_column',
				module: 'module',
				content: 'content',
				handle: 'handle',
				hoverclass: 'module_hover',
				toggle: 'module_toggle',
				toggle_selected: 'module_toggle_selected',
				modulelist: 'portal_column_module_list',
				modulelistlink: 'portal_module_list_link',
				modulelisthandle: 'module_list_handle',
				saveurl: '/ajax/linkswap/save_module'
	    	};
	    	Object.extend(this.options, options || {});
		},
	
		applySettings: function (settings) {
			for (var container in settings) {
				settings[container].each(function (module) {
					$(container).appendChild($(module));
				});
			}
		}
	});
}});

/****************************************/

function delete_domain(id) {
	
	if(confirm('Are you sure you want to remove tracking for this domain?')) {
		var pars = 'id='+id;
		
		new Req.ajax({
			url: '/ajax/linkswap/delete_domain',
			options: {
				parameters: pars,
				onFailure: function() {
					alert('An error occurred. Please try again.');
				}
			}
		});
	}
}


function delete_link(id) {
	
	if(confirm('Are you sure you want to remove tracking for this domain?')) {
		var pars = 'id='+id;
		
		new Req.ajax({
			url: '/ajax/linkswap/delete_link',
			options: {
				parameters: pars,
				onFailure: function() {
					alert('An error occurred. Please try again.');
				}
			}
		});
	}
}


function change_weight(id, direction) {
	
	prev_weight = parseInt($('weight_'+id).innerHTML);
	if(direction == 'inc') {
		weight = prev_weight + 1;
		
		if(weight >= 10)
			weight = 9;
	} else {
		weight = prev_weight - 1;
		
		if(weight <= 0)
			weight = 1;
	}
	
	$('weight_'+id).innerHTML = weight;
	
	if(prev_weight != weight) {
		var pars = 'id='+id+'&weight='+weight;
		
		new Req.ajax({
			url: '/ajax/linkswap/change_weight',
			options: {
				parameters: pars,
				onFailure: function() {
					alert('An error occurred. Please try again.');
				}
			}
		});
	}
}


function toggle_status(id, status) {

	var pars = 'id='+id+'&status='+status;
	
	new Req.ajax({
		url: '/ajax/linkswap/toggle_status',
		options: {
			parameters: pars,
			onFailure: function() {
				alert('An error occurred. Please try again.');
			}
		}
	});
	
};


function reset_portal() {
	if(confirm('Are you sure you want to reset your module positions?')) {
		window.location = '/linkswap2/reset_portal';
	}
}


function route_hash_request(callback) {
	new Ajax.Request('/ajax/user/request_hash?' + Math.round(9999999*Math.random()), {
		onComplete: function(transport) {
			hash = transport.responseText;

				if(hash.length == 32) {
  					callback(hash);
				} else {
					$('signin_error').innerHTML = 'There was an error requesting a secure login. Please refresh the page and try again.';
				}
		},
		onFailure: function() {
			$('signin_error').innerHTML = 'An unkown error occurred while trying to obtain a secure login session. Please refresh the page and try again.';
		}
	});
}


function linkswap_signin(hash) {
	
	var pars = '&linkswap_password='+MD5.hex(MD5.hex($('linkswap_password').value)+hash);
	pars += '&hash='+hash;
	
	new Req.submitForm('/ajax/linkswap/signin', 'frm_linkswap_signin', $('btn_linkswap_signin'), pars);
}


function linkswap_change_password() {
	
}
