var orig;
var content;

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function activateEdits() {
$('#menu').onselectstart = function() { return false; };
$('#menu').unselectable = "on";

$('#editor').hide();

$('.editable').dblclick(function() {
	var editor = tinyMCE.getInstanceById('editorform');
	if (editor == undefined) {
		tinyMCE.execCommand('mceAddControl', true, 'editorform');
		editor = tinyMCE.getInstanceById('editorform');
		return;
	}
	$('#editor').css('margin-top', '-22px');
	$('#editor').css('top', $(this).offset().top);
	$('#editor').css('left', $(this).offset().left);
	$('#editor #resource').empty().prepend($(this).attr('path'));
	$('#mce_editor_0').height($(this).height());
	$('#mce_editor_0').width($(this).width());
	$('.mceEditor').height($(this).height());

	orig = $(this);
	content = $(this).clone();
	$(':hidden', content).show();
	$('*', content).removeAttr('style');
	$('*[.white-line]', content).each(function() {
		$(this).prepend($('.white-line', $(this)).html());
		$('.white-line', $(this)).remove();
	});
	$('.pagebullet', content).each(function() {
		$(this).remove();
	});

	$('#notch', content).each(function() {
		$(this).remove();
	});

	tinyMCE.execCommand('mceSetContent', true, $(content).html());

	$('#editor').fadeIn("normal");
	editor.resizeToContent();
});

$('.saveButton').click(function(){
	var editor = tinyMCE.getInstanceById('editorform');
	$('#editor').fadeOut("normal");

	atts = {};
	atts['path']  = content.attr('path');
	atts['save'] = 'Save';

	if (content.attr('attribute') == undefined) {
		atts['content'] = tinyMCE.getContent();
	} else {
		atts['attr.'+ content.attr('attribute')] = tinyMCE.getContent();
	}

	$.post('/Edit', atts,
		function (data) { 	
			window.location.reload();
		});
});

$('.cancelButton').click(function(){
	var editor = tinyMCE.getInstanceById('editorform');
	$('#editor').fadeOut("normal");
});
}

$(function(){
$('#menu ul ul').hide();

$('#menu > ul > li').mouseover(function(){
	$('#menu ul li ul').css('display', 'none');
	$('ul', this).css('display', 'inline');
});

$('#menu > ul > li').mouseout(function(){
	$('#menu ul li ul').css('display', 'none');
});

$('div.orange').each(function() {
	var content = $(this).clone();
	$(this).empty();
	$(this).append("<div class='white-line'>" + $(content).html() + "</div>");
});

$('//div#content/h1').each(function() {
	$(this).prepend('<img src="/Images/PageBullet.jpg" class="pagebullet" />');
});

$('div.blue').each(function() {
	var content = $(this).clone();
	$(this).empty();
	$(this).append("<div class='white-line'>" + $(content).html() + "</div>");
});

$('div.grey').each(function() {
	var content = $(this).clone();
	$(this).empty();
	$(this).append("<div class='white-line'>" + $(content).html() + "</div>");
});

$('#right-shadow').height($('#top').height());
$('#right-shadow-middle').height($('#top').height() + 3);

if (contentPath != '/') $('#menu a[@href*='+contentPath+']').css('color','white');

activateEdits();


});

