var current_parent_id;
var template = '';

function addNavItem(parent_id){
	/*
nav_name = prompt('Navigation Bar Name');
	link = prompt('Link');
	
	if(nav_name !== '' || nav_name !== false){
		$('#sub_' + parent_id + ' ul li.add').before('<li><a href="' + link + '">' + nav_name + '</a><div class="remove_link"><a href="#" onclick="removeNavItem($(this), true); "><img src="/common/images/cross.png" alt="Delete" /></a></li>');
	}
*/
	current_parent_id = parent_id;
	
	$('#nav_bar_add_item').jqmShow();
}

function updateNavItem(nav_id, caption, target){
	$('#sub_item_' + nav_id).html('');
	
	$('#sub_item_' + nav_id).append('<a href="' + target + '">' + caption + '</a><div class="edit_link"><a href="#" onclick="editNavItem(\'' + nav_id + '\'); return false"><img src="/common/images/pencil.png" alt="Edit" /></a></div><div class="remove_link"><a href="#" onclick="removeNavItem(\'' + nav_id + '\'); return false"><img src="/common/images/cross.png" alt="Delete" /></a></div>');
}

function removeNavItem(elem, full_elem){
	if(typeof(full_elem) == 'undefined'){
		full_elem = false;
	}
	
	if(confirm('Are you sure you want to delete this item?') == true){
		if(full_elem == true){
			elem.parent().parent().remove();
		}
			else{			
				$.post('/common/actions/delete_nav_item.php', {
					nav_id: elem
				}, function(d){
						$('#sub_item_' + elem).remove();
						
						$('#debug').html(d);
				});
			}
	}
}

function editNavItem(nav_id){
	$('#nav_bar_add_item').html('');
	
	$('#nav_bar_add_item').load('/common/actions/show_edit_nav_form.php',{nav_id: nav_id}, function(){
		$('#nav_bar_add_item').jqmShow();
	});
}

function pushNewItem(nav_id, nav_name, link){
	$('#sub_' + current_parent_id + ' ul li.add').before('<li id="sub_item_' + nav_id + '"><a href="' + link + '">' + nav_name + '</a><div class="edit_link"><a href="#" onclick="editNavItem(\'' + nav_id + '\'); return false"><img src="/common/images/pencil.png" alt="Edit" /></a></div><div class="remove_link"><a href="#" onclick="removeNavItem(\'' + nav_id + '\'); "><img src="/common/images/cross.png" alt="Delete" /></a></li>');
}

function showOptions(elem, type){
	$('#nav_bar_add_item span').removeClass('selected');
	
	if(type == 'page'){		
		if($('#nav_bar_add_item #link_options').css('display') !== 'none'){
			$('#nav_bar_add_item #link_options').slideUp('normal');
			$('#nav_bar_add_item #link_options input').val('');
		}
		
		if($('#nav_bar_add_item #page_options').css('display') == 'none'){
			$('#nav_bar_add_item #page_options').slideDown('normal');
		}
		
		$('#page_type').val('page');
	}
		else{
			if($('#nav_bar_add_item #page_options').css('display') !== 'none'){
				$('#nav_bar_add_item #page_options').slideUp('normal');
				
				$('#nav_bar_add_item #page_options select').val('');
			}
			
			if($('#nav_bar_add_item #link_options').css('display') == 'none'){
				$('#nav_bar_add_item #link_options').slideDown('normal');
			}
			
			$('#page_type').val('link');
		}
				
	$(elem).addClass('selected');
}

function discardAddChanges(){			
	$('#nav_bar_add_item').jqmHide();
	
	$('#nav_bar_add_item').html(template);
}

var nav_id = '';

function saveAddChanges(edit){
	if(typeof(edit) == 'undefined'){
		edit = false;
	}
	
	if(edit == false){
		nav_target = 'add_nav_item.php';
	}
		else{
			nav_id = edit;
			nav_target = 'edit_nav_item.php';
		}
		
	caption = $('#nav_bar_name').val();
	type = $('#page_type').val();
	
	if(type == 'page'){
		target = $('#nav_bar_page').val();
	}
		else{
			target = $('#add_bar_link').val();
		}
	
	//$('#nav_bar_add_item p.error').slideUp('normal');
	
	
	if($('#nav_bar_name').val() == '' || $('#page_type').val() == ''){
		error_msg = 'You must: ';
		error = 0;
		
		if($('#nav_bar_name').val() == ''){
			error_msg = error_msg + ' enter a name';
			error = 1;
		}
		
		if($('#page_type').val() == ''){
			if(error == 1){
				error_msg = error_msg + ', choose a page type';
			}
				else{
					error_msg = error_msg + ' choose a page type';
				}
		}
		
		
		
		if($('#nav_bar_add_item p.error').css('display') !== 'none'){
			$('#nav_bar_add_item p.error').slideUp('normal', function(){
				$('#nav_bar_add_item p.error').html(error_msg);
				$('#nav_bar_add_item p.error').slideDown('normal');
			});
		}
			else{
				$('#nav_bar_add_item p.error').html(error_msg);
				$('#nav_bar_add_item p.error').slideDown('normal');
			}
		
	}
		else{
			if(target == '' || target == 'http://'){
				if(type == 'page'){
					if(target == ''){
						$('#nav_bar_add_item p.error').html('You must choose a page');
			
						$('#nav_bar_add_item p.error').slideDown('normal');
					}
				}
					else{
						if(target == '' || target == 'http://'){
							$('#nav_bar_add_item p.error').html('You must enter a URL to link to');
			
							$('#nav_bar_add_item p.error').slideDown('normal');
						}
					}
			}
				else{
					//Post the vars
					$('#nav_bar_add_item p.error').html('Saving...');
			
					$('#nav_bar_add_item p.error').slideDown('normal');
					
					if(edit !== false){
						data = {
							caption: caption,
							type: type,
							target: target,
							parent_id: '0',
							nav_id: edit
							}
					}
						else{
							data = {
									caption: caption,
									type: type,
									target: target,
									parent_id: current_parent_id
									}
						}
					$.post('/common/actions/' + nav_target, data, function(d){
						$('#debug').html($(d).find('response').text());
						
						nav_id = $(d).find('nav_id').text();			
									
						if($(d).find('message').text() !== 'Okay'){
							$('#nav_bar_add_item p.error').html('An error occurred, please try again');
					
							$('#nav_bar_add_item p.error').slideDown('normal')
						}
							else{
								if($('#nav_bar_add_item p.error').css('display') !== 'none'){
									$('#nav_bar_add_item p.error').slideUp('normal');
								}
								
								if(edit == false){
									$('#nav_bar_add_item p.error').html('Added');
								}
									else{
										$('#nav_bar_add_item p.error').html('Edited');
									}
									
								//$('#nav_bar_add_item p.error').slideDown('normal');
								
								
								//Close the window.. and reset the form...
								setTimeout(function(){
									discardAddChanges();
								}, 1000);
							}
											
					if(edit == false){
						pushNewItem(nav_id, caption, target);
					}
						else{
							updateNavItem(edit, caption, target);
						}
					}, 'xml');

				}
		}
}

function showReorderList(parent_id){
	$('#nav_bar_add_item').html('');
	
	$('#nav_bar_add_item').load('/common/actions/show_reorder.php', {parent_id: parent_id});
	
	$('#nav_bar_add_item').jqmShow();
}

function saveOrder(parent_id){
	sort = $.SortSerialize('reorder_list');
	
	$.post('/common/actions/update_nav_order.php?parent_id=' + parent_id, sort.hash, function(d){
		if(d == 'Okay'){
			$('#nav_bar_add_item p.error').html('Updated! You must refresh to view the changes');
			$('#nav_bar_add_item p.error').slideDown('normal');
			
			setTimeout(function(){
				$('#nav_bar_add_item').jqmHide();
				
				$('#nav_bar_add_item').html(template);
			}, 1000);
		}
	});
}

function showAddPage(){
	$('#add_page').jqmShow();
}

function hideAddPage(){
	$('#add_page').jqmHide();
}

var template_id = '';
var choose_temp = '';

function choosePageTemplate(template){
	template_id = template;
	
	choose_temp = $('#add_page').html();
	
	$('#add_page').slideUp('normal', function(){
		$('#add_page').html('<h2>Please enter the name of this page</h2><p class="error" style="display: none"></p><p><label>Page Name</label> <input id="page_name" name="page_name" type="text"/> <span class="example">Ex: Dance Classes, About Gotta Dance, Etc..</span><p>');
		$('#add_page').append('<p class="right"><input type="button" onclick="cancelPageAdd()" value="Cancel" /><input type="button" onclick="pageExists()" value="Finish" /></p>');
		$('#add_page').slideDown('normal');	
	});
}

function pageExists(){
	name = $('#page_name').val();
	
	if($('#add_page p.error').css('display') !== 'none'){
		//$('#add_page p.error').slideUp('normal');
	}
	
	$('#add_page p.error').slideUp('normal', function(){
		if($('#page_name').val() == ''){
			$('#add_page p.error').html('Uh oh! Looks like your forgot to enter in a page name.');
			$('#add_page p.error').slideDown('normal');
		}
			else{
				$('#add_page p.error').html('Please wait while I check if this page already exists..');
				
				$('#add_page p.error').slideDown('normal', function(){
					$.post('/common/actions/page_exists.php',{name: name}, function(d){
						$('#debug').html(d);
						
						if(d == 'true'){
							$('#add_page p.error').slideUp('normal', function(){	
								$('#add_page p.error').html('I\'m sorry but this page name already exists, please try another one?');
								
								$('#add_page p.error').slideDown('normal');
							});
						}
							else{
								finishPageAdd();
							}
					});
				});
		}
	});
}

function cancelPageAdd(){
	$('#add_page').jqmHide();
	$('#add_page').html(choose_temp);
}

function finishPageAdd(){
	$('#add_page').slideUp('normal', function(){
		name = $('#page_name').val();

		$('#add_page').html('<h2>Please wait while we create the page</h2><p class="progress error" style="text-align: center"><img src="/common/images/progress.gif" alt="Progress" /></p>');
		$('#add_page').slideDown('normal');	
			
		$.post('/common/actions/create_page.php',{template_id: template_id, name: name}, function(d){
			$('#debug').append("<br/>" + d);
			
			if(d !== 'error' && d !== '' && d !== 'page exists'){
				location.href = '/' + d;
			}
				else{
					if(d == 'error'){
						$('#add_page p.error').html('An error occurred while adding this page, please try again');
					}
						else if(d == 'page exists'){
							$('#add_page p.error').html('This page already exists, please choose a different name');
						}
							else{
								$('#add_page p.error').html('An error occurred while adding this page, please try again');
							}
					
					$('#add_page p.error').slideDown('normal');
				}
		});	
	});
}

function removePage(){
	if(typeof(page_id) !== 'undefined'){	
		response = confirm('Are you sure you want to delete this page?\n\nThis action CAN NOT be undone.');
		
		if(response == true){
			$('#schedule_loading h2').html('Please wait while we delete this page');
			$('#schedule_loading').jqmShow();
			
			$.post('/common/actions/remove_page.php', {page_id: page_id}, function(){
				location.href = '/';
			});
		}
	}
}

$(document).ready(function(){
	if(typeof(page_id) == 'undefined'){
		$('.remove_page_button_c').remove();
		$('.edit_page_button_c').remove();
	}
		else{
			
		}	
});
function changeSchedule(elem, location_id){
	$('#schedule_loading').jqmShow();
	
	$('p.choose_location a').removeClass('selected');
	
	$(elem).addClass('selected');
	
	$('#calendar_holder').load('/common/actions/return_calendar.php',{location: location_id}, function(){
		$('#schedule_loading').jqmHide();
	});

}

function insertHTML(){
	html = prompt('Enter in the html code here');
	
	$('#content_areaIFrame').html(html);
}

/**
 * Overwrite Document.Write for the schedule.
 */
document.write = (function () {
    for (var i = 0; i < arguments.length; i++) {
        $('#schedule').append(arguments[i]);
    }
}); 