(function($) {

$(function() { // on DOM ready

// Initialize editor
if(livewhale.has_pages_editor) {
	var login = livewhale.getCookie('lw_pages_editor'), // get the login cookie
		initPagesEditor = function() {
			var head = $('head'),
				body = $('body');
			body.find('script').remove(); // remove scripts so they don't execute when we
			body.wrapInner('<div id="lw_page"/>'); // wrap the document in #lw_page
			$.each(livewhale.pages_editor.styles,function() {
				head.append('<link rel="stylesheet" type="text/css" href="'+this+'"/>');
			});
			body.prepend(livewhale.pages_editor.html);
			if (livewhale.edit_item) $('#lw_menu_pages').append('<li id="lw_pages_edit_item"><a href="'+livewhale.edit_item+'">Edit this item</a></li>'); // add edit this item link if available
			var loaded = 0, // how many scripts have loaded?
				loadlast = livewhale.pages_editor.scripts.pop();
			$.each(livewhale.pages_editor.scripts,function(index,path) { // for each script
				$.getScript(path,function() { // load&execute it, then
					loaded++; // increment the counter
					if(loaded==livewhale.pages_editor.scripts.length) { // if they've all loaded
						livewhale.jQuery.fn.tinymce = jQuery.fn.tinymce; // copy over tinymce, when added
						livewhale.jQuery.throttle = jQuery.throttle; // and throttle
						livewhale.jQuery.debounce = jQuery.debounce; // and debounce
						$.getScript(loadlast); // load that one last one
					}
				});
			});
		};
	if(login) { // if the login cookie exists
		if(livewhale.pages_editor) {
			initPagesEditor();
		} else {
			$.ajax({
				url:livewhale.page+'?livewhale=ajax&function=getPagesEditorToolbar'+((livewhale.draft && livewhale.draft.is_loaded) ? '&lw_draft=1' : ''),
				success:function(data) {
					if(data&&data.html) { // (data has no properties if the login is invalid)
						livewhale.pages_editor = data;
				   		initPagesEditor();
				   	}
				},
				error:function(XMLHttpRequest, textStatus, errorThrown) {
					if((textStatus=='parsererror'&&!$.trim(XMLHttpRequest.response))||(textStatus=='error'&&XMLHttpRequest.status==404)) { // if empty (expired cookie), or the page is a 404
						return; // just ignore the request and don't show an error
					} else {
						livewhale.ajaxError.apply(this,[XMLHttpRequest, textStatus, errorThrown]); // otherwise, pass on to the generic error handler
					}
				},
				dataType:'json'
			});
		}				
	}
}

if(livewhale.is_details_preview) {
	$('<div id="lw_preview_bar">Here’s the preview you requested. <a href="#" class="lw_close_window">Click to close window</a></div>').prependTo($('body'));
	$('.lw_close_window').click(function() { window.close(); return false; });

}

// Common behaviors
$('.lw_widget:has(.lw_paginate)').paginate(); // paginate
$('.lw_item_images').slideshow(); // quick gallery for detail pages
$('.lw_datepicker').datepicker(); // add datepicker for forms
if($.fn.timePicker) $('.lw_timepicker').timePicker({show24Hours:false}); // activate the timepicker	for times

});
})(livewhale.jQuery);