var editing = false;
var current_edit;

function editPage(){
	if(editing == false){
		$('.edit_page_button').css('display','none');
		$('.save_page_button').css('display','block');
		
		$('#help_text').slideDown('normal');
		
		editing = true;
	}
		else{
			savePage();
			
			editing = false;
		}
	
	if(editing == true){
		
		/**
		 * Make the header text editable
		 */
		$('.header_text').css('cursor','pointer').attr('title','Click to edit the header').click(function(){
			editTitle();
		});
		
		$('.textbox_one').css('cursor','pointer').attr('title','Click to edit').click(function(){
			$('#content_area').val($('.textbox_one').html());
			$('#content_area').wysiwyg();
			
			$('#content_editor').jqmShow();
			
			current_edit = 'textbox_one';
		});
		
		$('.caption.one').css('cursor','pointer').attr('title','Click to edit').click(function(){
			$('#content_area').val($('.caption.one').html());
			$('#content_area').wysiwyg();
			
			$('#content_editor').jqmShow();
			
			current_edit = 'caption.one';
		});
		
		$('.caption.two').css('cursor','pointer').attr('title','Click to edit').click(function(){
			$('#content_area').val($('.caption.two').html());
			$('#content_area').wysiwyg();
			
			$('#content_editor').jqmShow();
			
			current_edit = 'caption.two';
		});
		
		$('.sub_text').css('cursor','pointer').attr('title','Click to edit').click(function(){
			$('#content_area').val($('.sub_text').html());
			$('#content_area').wysiwyg();
			
			$('#content_editor').jqmShow();
			
			current_edit = 'sub_text';
		});
		
		$('.textbox_two').css('cursor','pointer').attr('title','Click to edit').click(function(){
			$('#content_area').val($('.textbox_two').html());
			$('#content_area').wysiwyg();
			
			$('#content_editor').jqmShow();
			
			current_edit = 'textbox_two';
		});
		
		$('.bottom_link').css('cursor','pointer').attr('title','Click to edit').click(function(){
			title = prompt('Please enter a new title', '');
			link = prompt('Please enter a URL', '');
			
			if(title !== '' && title !== null && link !== '' && link !== null){
				$('.bottom_link a').attr('href',link);
				$('.bottom_link a img').attr('src','/common/actions/image.php?text=' + title + '&color=FFF&bg_color=transparent&size=15').attr('alt',title);

			}
							
			return false;
		});
		
		$('.image_container.one img').css('cursor','pointer').attr('title','Click to edit').click(function(){			
			$('#photo_uploader').jqmShow();
			
			current_edit = 'image_container.one img';
		});
		
		$('.image_container.two img').css('cursor','pointer').attr('title','Click to edit').click(function(){			
			$('#photo_uploader').jqmShow();
			
			current_edit = 'image_container.two img';
		});
	}
}

var saving = false;

function savePage(){
			
	resetPage();
	
	data = {
		page_id: page_id,
		header: $('.header_text img').attr('alt'),
		content: $('.textbox_one').html(),
		image_header_1: $('.caption.one h2').text(),
		image_header_2: $('.caption.two h2').text(),
		image_caption_1: $('.caption.one').html(),
		image_caption_2: $('.caption.two').html(),
		image_1: $('.image_container.one img').attr('src'),
		image_2: $('.image_container.two img').attr('src'),
		content_2: $('.sub_text').html(),
		header_2: $('.textbox_two h2').text(),
		textbox_2: $('.textbox_two').html(),
		link_1: $('.bottom_link a').attr('href'),
		link_title: $('.bottom_link a img').attr('alt')
	}
		
	if(saving == false){
		saving = true;
		
		$('#help_text').slideUp('normal');
		
		$('.save_page_button span').text('Saving...');
		
		$.post('/common/actions/save/template_1.php', data, function(d){
			$('.save_page_button span').text('Saved!');
			
			saving = false;
			
			$('#debug').html(d);
			
			setTimeout(function(){
				$('.save_page_button').css('display','none');
				$('.edit_page_button').css('display','block');
				
				$('.save_page_button span').text('Save this page');
			}, 500);
		});

	}
}

function editTitle(){
	if(editing == true){
		title = prompt('Please enter a new title','');
		
		if(title !== '' && title !== null){
			$('.header_text img').attr('src','/common/actions/image.php?text=' + title).attr('alt',title);
		}
		
		data = {
			header: title
		}
	}
}

function resetPage(){
	$('.header_text').css('cursor','default').attr('title','').unbind('click');
	$('.textbox_one').css('cursor','default').attr('title','').unbind('click');
	$('.caption.one').css('cursor','default').attr('title','').unbind('click');
	$('.caption.two').css('cursor','default').attr('title','').unbind('click');
	$('.sub_text').css('cursor','default').attr('title','').unbind('click');
	$('.textbox_two').css('cursor','default').attr('title','').unbind('click');
	$('.bottom_link').css('cursor','default').attr('title','').unbind('click');
	$('.image_container.one img').css('cursor','default').attr('title','').unbind('click');
	$('.image_container.two img').css('cursor','default').attr('title','').unbind('click');
}

function saveContentChanges(){
	$('.' + current_edit).html($('#content_area').val());

	$('#content_editor').jqmHide();
	$('#content_editor div.wysiwyg').remove();
}

function discardContentChanges(){
	$('#content_editor').jqmHide();
	$('#content_editor div.wysiwyg').remove();
}