// Forces a confirmation before allowing any link to take you outside of the current site
function blockExternalLinks(){
	var anchors = $$('a');
	for(i=0;i<anchors.length;i++){
		anchors[i].addEvent('click', function(){
			if(!this.href.match(document.domain) && this.href.match('http')){
				var ext_bg = new Element('div', {'id' : 'extNotify'});
				ext_bg.set('opacity', 0);
				ext_bg.addEvent('click', function(){
					fader = new Fx.Tween(ext_bg);
					fader.addEvent('onComplete', function(){ext_bg.dispose();});
					fader.start('opacity', 0);
				});
				ext_bg.innerHTML = 
				'<table id="ext_table" cellpadding="0" cellspacing="0" height="100%" width="100%"><tr>'+
				'<td id="ext_container" align="center" valign="middle"></td></tr></table>';
				var ext_confirm = new Element('div');
				ext_confirm.innerHTML = 
				'<table id="ext_content" cellpadding="20" cellspacing="0"><tr>'+
				'<td  valign="middle">'+
				'<h1>Notice:</h1>'+
				'You have clicked on a link that will take you outside of this site.<br>'+
				'Here is the URL:<br>'+
				'<a onClick="location.href=this.href;" href="'+this.href+'">'+this.href.replace('http://','').slice(0, -1)+'</a><br>'+
				'If you would like to continue, please click the URL above.<br>'+
				'Click outside this box to cancel.'+
				'</td></tr></table>';
				ext_bg.inject(document.body);
				ext_confirm.injectInside($('ext_container'));
				$('ext_content').addEvent('click', function(){return false});
				ext_bg.fade(0, 0.9);
				return false;
			}
		});
	}
}

function control_switch(url, control){
	login_box.slideOut().chain(function(){
		$('control_content').innerHTML = ajax('load_control', 'url='+location.href+(control?'&control='+control:''));
		login_box.slideIn();
	});
}

function ajax_login(){
	var login = ajax('login', 'username='+$('username').value+'&password='+$('password').value);
	if(login == 'success'){
		control_switch(location.href);
		$('control_btn').innerHTML = 'Control Panel';
	}
	else {
		$('login_err').innerHTML = 'Your login information was invalid';
	}
	return false;
}

function ajax(action, data){
	var request = new Request({
		method : 'post',
		url : '/ajax_control.php',
		async : false,
		onSuccess : function(responseTree){
			result = responseTree;
		}
	});
	request.send('action='+action+(data?'&'+data:''));
	return(result);
}

function load_object(target, object){
	holder_html = $(target).innerHTML;
	holder_height = $(target).scrollHeight;
	$(target).fade('out');
	var new_html = ajax('test', "wtf=hello&omg[eep]=yhelothar&omg[oop]=ohai");
			$(target).style.height = holder_height+'px';
			var tmp = new Element('div');
			var object = new Element('div', {'style' : 'padding:10px 0;'});
			var object_return = new Element('div', {'style' : 'text-align:right;padding:5px;'});
			object_return.innerHTML = '<a href="javascript:void(0);" style="font-size:16px; text-decoration:none; font-weight:bold; color:#333333;">Return</a>';
			object_return.addEvent('click', function(){
				fx = new Fx.Morph($(target), {onComplete : function(){$(target).innerHTML = holder_html;$(target).fade('in');}});
				fx.start({'height' : holder_height, 'opacity' : 0});
			});
			object_return.injectInside(tmp);
			object.innerHTML = new_html;
			object.injectInside(tmp);
			$(target).innerHTML = '';
			tmp.injectInside($(target));
			new_height = tmp.scrollHeight;
			var fx = new Fx.Tween($(target), {onComplete : function(){$(target).fade('in');}});
			fx.start('height', new_height);
	return false;
}

function edit_blogs(){
	var blog_boxes = $$('.data');
	for(x=0;x<blog_boxes.length;x++){
		blog_boxes[x].addEvent('click', function(){
			if(this.className == 'data'){
				var edit_box = new Element('textarea', {
					'id' : this.id,
					'style' : 'width:100%; margin:0; padding:0; background:#fffd7d; border:none; font-size:12px; height:' + this.scrollHeight + 'px'
				});
				edit_box.value = ajax('blog_html_get', 'id=' + this.id);
				edit_box.addEvent('blur', function(){
					$$('#item-' + this.id + ' .data')[0].innerHTML = ajax('update_blog', 'id=' + this.id + '&data=' + this.value.replace(/&/g, 'amp;'));
					$$('#item-' + this.id + ' .data')[0].className = 'data';
				});
				this.innerHTML = '';
				this.className = 'data disable';
				edit_box.inject(this);
				edit_box.focus();
			}
		});
	}
}

function edit_blog_titles(){
	var blog_titles = $$('.title');
	for(x=0;x<blog_titles.length;x++){
		blog_titles[x].addEvent('click', function(){
			if(this.className == 'title'){
				var edit_title = new Element('input', {
					'id' : this.id,
					'type' : 'text',
					'style' : 'width:100%; font-size:15px; font-weight:bold; margin:0; padding:0; background:#fffd7d; border:none;'
				});
				edit_title.value = ajax('blog_title_get', 'id=' + this.id);
				edit_title.addEvent('blur', function(){
					$$('#item-' + this.id + ' .title')[0].innerHTML = ajax('update_blog_title', 'id=' + this.id + '&data=' + this.value.replace(/&/g, 'amp;'));
					$$('#item-' + this.id + ' .title')[0].className = 'title';
				});
				this.innerHTML = '';
				this.className = 'title disable';
				edit_title.inject(this);
				edit_title.focus();
			}
		});
	}
}

function edit_notes(){
	var note_boxes = $$('.data');
	for(x=0;x<note_boxes.length;x++){
		note_boxes[x].addEvent('click', function(){
			if(this.className == 'data'){
				var edit_box = new Element('textarea', {
					'id' : this.id,
					'style' : 'width:100%; margin:0; padding:0; background:#fffd7d; border:none; font-size:12px; height:' + this.scrollHeight + 'px'
				});
				edit_box.value = ajax('note_html_get', 'id=' + this.id);
				edit_box.addEvent('blur', function(){
					$$('#item-' + this.id + ' .data')[0].innerHTML = ajax('update_note', 'id=' + this.id + '&data=' + this.value.replace(/&/g, 'amp;'));
					$$('#item-' + this.id + ' .data')[0].className = 'data';
				});
				this.innerHTML = '';
				this.className = 'data disable';
				edit_box.inject(this);
				edit_box.focus();
			}
		});
	}
}

function edit_note_titles(){
	var note_titles = $$('.title');
	for(x=0;x<note_titles.length;x++){
		note_titles[x].addEvent('click', function(){
			if(this.className == 'title'){
				var edit_title = new Element('input', {
					'id' : this.id,
					'type' : 'text',
					'style' : 'width:100%; font-size:15px; font-weight:bold; margin:0; padding:0; background:#fffd7d; border:none;'
				});
				edit_title.value = ajax('note_title_get', 'id=' + this.id);
				edit_title.addEvent('blur', function(){
					$$('#item-' + this.id + ' .title')[0].innerHTML = ajax('update_note_title', 'id=' + this.id + '&data=' + this.value.replace(/&/g, 'amp;'));
					$$('#item-' + this.id + ' .title')[0].className = 'title';
				});
				this.innerHTML = '';
				this.className = 'title disable';
				edit_title.inject(this);
				edit_title.focus();
			}
		});
	}
}

function add_menu_tips(){
	items = $$('.nav_item');
	for(x=0;x<items.length;x++){
		items[x].addEvent('mouseenter', function(){
			if(this.className != 'disable'){
				var tip = new Element('div', {
					'style' : 'top:-30px; left:25px;',
					'class' : 'dialogbox'
				});
				var tip_fx = new Fx.Tween(tip, {'duration' : 'short'});
				tip_fx.set('opacity', '0');
				tip.innerHTML = this.rel;
				this.className = 'disable';
				tip.inject(this);
				tip.addEvent('mouseover', function(){return false;});
				tip_fx.start('opacity', '1');
				this.addEvent('mouseleave', function(){
					tip_fx.start('opacity', 0).chain(function(){tip.dispose();});
					this.className = '';
				});
			}
		});
	}
}
