var editing = false;
var current_edit;

/**
 * Add the add a row button
 */
$(document).ready(function(){
	$('#page_options').append('<li onclick="addRow();"><span class="add_row_button"><img src="/common/images/tab_add.png" alt="Add Row" /> Add a new row</span></li>');
	$('#page_options').append('<li onclick="toggleReorder();"><img src="/common/images/arrow_switch.png" alt="Reorder" /><span class="reorder_row_button"> Reorder Rows</span></li>');
	$('#page_options').append('<li onclick="removeRow();"><img src="/common/images/tab_delete.png" alt="Remove Row" />  <span class="remove_row_button">Remove a row</span></li>');
});

function editPage(){
	resetPage();
	removeReorder();
	
	if(editing == false){
		$('.edit_page_button').css('display','none');
		$('.save_page_button').css('display','block');
		
		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();
		});
		
		$('.content').css('cursor','pointer').attr('title','Click to edit').click(function(){
			$('#content_area').val($('.content').html());
			$('#content_area').wysiwyg();
			
			$('#content_editor').jqmShow();
			
			current_edit = 'content';
		});
		
		$('.row .text').css('cursor','pointer').attr('title','Click to edit').click(function(){
			$('#content_area').val($(this).html());
			$('#content_area').wysiwyg();
			
			$('#content_editor').jqmShow();
			
			current_edit = $(this).parent().attr('id') + ' .text';
		});
		
		$('.row .thumb_container').css('cursor','pointer').attr('title','Click to edit').click(function(){			
			$('#photo_uploader').jqmShow();			
			
			current_edit = $(this).parent().parent().attr('id') + ' img';
		});
		
		$('.bottom_text').css('cursor','pointer').attr('title','Click to edit').click(function(){
			$('#content_area').val($('.bottom_text').html());
			$('#content_area').wysiwyg();
			
			$('#content_editor').jqmShow();
			
			current_edit = 'bottom_text';
		});
	}
}

var saving = false;

function savePage(){
	resetPage();
	
	var row_array = new Array();
	
	var rows = '<rows>'; 
	
	$('.row').each(function(){
		rows = rows + '<row><row_id>' + this.id + '</row_id><html><![CDATA[' + $(this).html() + ']]></html></row>';
	});
	
	rows = rows + '</rows>';
		
	data = {
		page_id: page_id,
		header: $('.header_text img').attr('alt'),
		bottom_text: $('.bottom_text').html(),
		row_xml: 'true',
		row: rows
	}
		
	if(saving == false){
		saving = true;
		
		$('.save_page_button span').text('Saving...');
		
		$.post('/common/actions/save/template_3.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');
	$('.row').css('cursor','default').attr('title','').unbind('click');
	$('.row .text').css('cursor','default').attr('title','').unbind('click');
	$('.row .thumb_container').css('cursor','default').attr('title','').unbind('click');
	$('.bottom_text').css('cursor','default').attr('title','').unbind('click');
	
	if($('.remove_row_button').text() !== 'Remove a row'){
		removing = false;
		
		$('.remove_row_button').text('Remove a Row');
	}
}

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();
}

function addRow(){
	row = $('.rows .row:last-child').html();
	total = parseInt(total_rows);

	
	row_id = total+1;
	
	total_rows = row_id;
	
	$('.rows').append('<div class="row row_' + row_id + '" id="row_' + row_id + '"><div title="" style="cursor: default;" class="text"> <h2>Title</h2> <p>Description</p> </div> <div class="image"> <div title="" style="cursor: default;" class="thumb_container"><img title="" style="cursor: default;" src="template_4/pic-classpreschool1.jpg"></div> </div> <div class="clear"></div> <div class="clear"></div> <div class="clear"></div> <div class="clear"></div> <div class="clear"></div> <div class="clear"></div> </div>');
	
}

var removing = false;

function removeRow(){	
	if(removing == true){
		$('.remove_row_button').text('Remove a row');
		removing = false;
	}
		else{
			resetPage();
			$('.remove_row_button').text('Turn off row removal');
			removing = true;
		}
		
	$('.row').unbind('click').css('cursor','pointer').click(function(){
		if(removing == true){			
			check = confirm('Are you sure you want to remove this row?\n\nThere is no undo after you save the page');
			
			if(check == true){
				$(this).remove();
			}
		}
			else{			
				$('.row').unbind('click').css('cursor','default');
			}
	});
}

var reorder = false;

function toggleReorder(){
	resetPage();
	
	if(reorder == false){
		reorder = true;
		$('.rows').Sortable(
			{
				accept : 		'row',
				helperclass : 	'helper',
				opacity: 		0.8,
				revert:			true
						
			}
		);
		
		$('.row').css('cursor','pointer');
		$('.reorder_row_button').text('Stop Reordering');
	}
		else{
			removeReorder();
		}
}

function removeReorder(){
	if(reorder == true){
		reorder = false;
		$('.reorder_row_button').text('Reorder Rows');
		$('.rows').SortableDestroy();
	}
}