var WP_CMS = {};

WP_CMS.options = {
	'protocol' : 'http',
	'host' : window.location.hostname,
	'port' : window.location.port,
	'base_path' : '',
	'hide_header' : true
};

WP_CMS.getUrl = function() {
	return WP_CMS.options['protocol'] + '://' + WP_CMS.options['host'] + (WP_CMS.options['port'] ? ':' + WP_CMS.options['port'] : '') + WP_CMS.options['base_path'];
}
WP_CMS.getAdminUrl = function() {
	return WP_CMS.getUrl() + '/pages';
}

$(document).ready(function() {
	if (!WP_CMS.isAuthenticated()) return;
    $('.editable_content').each(function(el) {
        var id = this.id;
        $(this).
        prepend(
            '<a href="' + WP_CMS.getUrl() + '/pages/edit/'+encodeURIComponent(id)+'?close_after_save=true' + (WP_CMS.options['hide_header'] ? '&hide_header=true' : '') + '" target="'+id+'_window" onclick="return WP_CMS.windowOpenerFor(this);">'+
	        '<img style="position:absolute; top:3px; right:3px;" src="/images/edit_on.png" class="edit_button" title="Edit '+id+'" />'+
	        '</a>').
	    addClass('hoverable');
	});
});

WP_CMS.cms_behaviour = {
	 // body gets a navigation menu
	'body' : function(element) {
		if (!WP_CMS.isAuthenticated()) return;
		new Insertion.Top(element,
			'<div id="navigation_bar_wrapper">' +
			'<div id="navigation_bar" style="display:none;">'+
				'<a href="' + WP_CMS.getUrl() + '/pages" target="webpages_admin">webpages admin</a> | ' +
				'<a href="' + WP_CMS.getUrl() + '/login/exit">EXIT</a> '+
				'<div id="navigation_bar_hide_button">hide</div></div>' + 
			'</div>');
		new Insertion.Bottom(element, '<div id="js_debug"></div>');
	},
	'#navigation_bar_wrapper' : function(element) {
		element.onmouseover = function(e) {
			Effect.Appear('navigation_bar', { 'duration' : 0.5, 'to' : 0.8 });
		}
	},
	'#navigation_bar' : function(element) {
		element.onmouseover = element.onmouseout = function(anEvent) {
			if (!anEvent) anEvent = event;
			Event.stop(anEvent);
		}
	},
	'#navigation_bar_hide_button' : function(element) {
		element.onclick = function(anEvent) {
			Effect.Fade('navigation_bar', { 'duration' : 0.5 });
		}
	},

	// all elements with class editable_content get an edit button
	'.editable_content' : function(element) {
		if (!WP_CMS.isAuthenticated()) return;
		var id = element.id;
		new Insertion.Top(element, 
			'<a href="' + WP_CMS.getUrl() + '/pages/edit/'+encodeURIComponent(id)+'?close_after_save=true' + (WP_CMS.options['hide_header'] ? '&hide_header=true' : '') + '" target="'+id+'_window" onclick="return WP_CMS.windowOpenerFor(this);">'+
			  '<img style="position:absolute; top:3px; right:3px;" src="/images/edit_on.png" class="edit_button" title="Edit '+id+'" />'+
			'</a>');
		element.className = element.className + ' hoverable';
	} 
}

WP_CMS.isAuthenticated = function () {
	return document.cookie.match(/authenticated=true/);
}

WP_CMS.windowOpenerFor = function(a_element) {
	var a = $(a_element).first();
	window.open(a.href, a.target, 'width=800,height=600,toolbar=false,resizable=yes,menubar=yes,location=yes,scrollbars=yes');
	return false;
}

//Event.observe(window, 'load', function () {});
