<?php
if ($_SESSION['FirstName'] == "") {
	echo 'You are not authorized to view this page. <br><br>You may <a href="index.php">log in here.</a>';
	exit;
	}

echo "These are the buttons displayed on the touch slider across the bottom of your mobile website. Simply drag and drop them into the order you want them to show up on your mobile website.<br><br>";

//require_once ('mysql_connect.php');
$action          = $_GET["a"];
$del_item			= $_GET["item"];
$builtin			= $_GET["builtin"];
$filename			= $_GET["filename"];
$pageid			= $_GET["pageid"];

if(($action=="addfile")&&(!empty($filename))){ //add pdf doc to nav menu
	$button_text = $filename;
	$button_text = str_ireplace('.pdf', '', $button_text);
	$button_text = str_replace('_', ' ', $button_text);
	$button_text = strtoupper($button_text);	
	$sql="Insert into nav_slider_mobile (button_text, button_url, nav_order, navdoc, active, custom) values ('$button_text','$filename','0','1','1', '0')";
	//echo "sql=" . $sql . "<br>";
	if(mysql_query($sql)){
		echo "<center><font color='red'>Document link button added successfully to menu</font></center>";
	} else {
		echo "<center><font color='red'>There was a problem saving the item to the menu.</font></center>";
	}
}

if(($action=="addpage")&&($pageid !="")){
	$sql="SELECT * from nav_slider_mobile where pageid='$pageid'";
	$rt=mysql_query($sql); 
	if(mysql_num_rows($rt)==0){
		//add pdf doc to nav menu
		$sql="INSERT INTO nav_slider_mobile ( button_text, button_url, nav_order, pageid) SELECT pageTitle, permalink, pageOrder, pageID from content where pageID ='$pageid'";
		
	} else {
		$sql="UPDATE nav_slider_mobile set active='1' where pageid='$pageid'"; 
	}
	//echo "sql=" . $sql . "<br>";
	if(mysql_query($sql)){
		echo "<center><font color='red'>Page added successfully to menu</font></center>";
	} else {
		echo "<center><font color='red'>There was a problem saving the item to the menu.</font></center>";
	}
	$sql="SELECT * from nav_slider_mobile where pageid='$pageid'";
	$rt=mysql_query($sql); 
	while($nt=mysql_fetch_array($rt)){
		if(empty($nt[button_url])){
			$sql="update nav_slider_mobile set button_url='core.php&pageid=$pageid' where pageid='$pageid'";
			mysql_query($sql);
		}
	}
}

if($_POST[add_new]){
	$button_text = mysql_real_escape_string(strtoupper($_POST[button_text]));
	$button_url = mysql_real_escape_string($_POST[button_url]);
	if((!empty($button_text))&&(!empty($button_url))){
		// Get default color theme
		$sql = "SELECT *
				FROM color_themes
				ORDER BY default_option DESC 
				LIMIT 1"; // Just in case default isn't set, it'll pull the first row
		$result = mysql_query($sql) or die(mysql_error());
		$colorTheme = mysql_fetch_assoc($result);

		$sql = "INSERT INTO nav_slider_mobile (
					button_text, button_url, nav_order, navdoc, active, custom, icon_text, background_color, text_color
				) VALUES (
					'$button_text',
					'$button_url',
					'0',
					'0',
					'1', 
					'1', 
					'".mysql_real_escape_string($button_text)."', 
					'".mysql_real_escape_string($colorTheme['background_color'])."', 
					'".mysql_real_escape_string($colorTheme['text_color'])."'
				)";
		if(mysql_query($sql)){
			echo "<center><font color='red'>Button added successfully to menu</font></center>";
		} else {
			echo "<center><font color='red'>There was a problem saving the button to the menu.</font></center>";
		}
	} else {
		echo "<center><font color='red'>There was a problem saving the button to the menu. Please make sure you fill in the Button text AND the button URL.</font></center>";
	}
}

if(($action=="delete")&&(!empty($del_item))){
	if($builtin=='0'){
		$sql="delete from nav_slider_mobile where recordkey='$del_item'";
	} else {
		$sql="update nav_slider_mobile set active='0' where recordkey='$del_item'";
	}
		//echo "qry=". $sql;
	if(mysql_query($sql)){
		echo "<center><font color='red'>Button removed successfully from menu</font></center>";
	}
}

	//echo 'Action = ' . $action; (for testing)

//display current menu order	
$docqry="Select * from nav_slider_mobile where active='1'  and parent_page_id='0' order by nav_order";
$rt=mysql_query($docqry); 
echo '<div id="mobile-icon-wrapper"><div id="menusort" class="dd current">';
echo '<h2>Current Pages Displayed</h2>';
$curPageNum = 1;
if (mysql_num_rows($rt) < 1) {
	echo '<div class="dd-empty"></div>';
} else {
	echo"<ol class='dd-list'>";
	$x=0;
	while($nt=mysql_fetch_array($rt)){
		$id="page-".$nt[recordkey];
		//$id=$nt[recordkey];
		if($nt[button_url]=="orderredirect.php"){$builtin=1;} else {$builtin=0;}
		echo '<li class="dd-item cur-page-recordkey-'.$nt['recordkey'].'" id="'.$id.'" name="'.$id.'" data-cur-page-num="'.$curPageNum.'" data-recordkey="'.$nt['recordkey'].'" data-pageid="'.$nt['pageid'].'" data-socialid="'.$nt['socialnetworkid'].'" data-type="';
		if($nt['custom'] == 1) {
			echo 'custom';
		}elseif($nt['navdoc'] == 1) {
			echo 'doc';
		}elseif($nt['pageid'] != 0) {
			echo 'page';
		}elseif($nt['socialnetworkid'] != 0) {
			echo 'social';
		}
		echo '">';
		echo '<span class="dd-handle">';
		echo $nt[button_text];
		echo '</span>';

		// Icon
		if (!empty($nt['icon_image'])) {
			echo '<span class="mobile-slider-icon popup image" data-popup-id="edit_icon"><img src="../custom/images/icons/'.$nt['icon_image'].'" /></span>';
		} else {
			echo '<span class="mobile-slider-icon popup" data-popup-id="edit_icon" style="background-color: #'.$nt['background_color'].'; color: #'.$nt['text_color'].';">';
			echo '<span class="icon-text">'.$nt['icon_text'].'</span>';
			echo '</span>';
		}

		echo ' <span class="remove-icon"><a class="remove-nav-item" href="#"><img src="../images/remove-icon.png" title="Remove from menu"></a></span>';

		$docqry2="Select * from nav_slider_mobile where active='1' and parent_page_id<>'0' and parent_page_id='".$nt[pageid]."' order by nav_order ";
		$rt2=mysql_query($docqry2); 
		if(mysql_num_rows($rt2) > 0) {	
			echo'<ol class="dd-list">';
			while($nt2=mysql_fetch_array($rt2)){
				$id="subpage-".$nt2[recordkey];
				if($nt2[button_url]=="orderredirect.php"){$builtin=1;} else {$builtin=0;}
				echo'<li id="'.$id.'" class="dd-item" name="'.$id.'" data-recordkey="'.$nt2['recordkey'].'" data-pageid="'.$nt2['pageid'].'" data-type="';
				if($nt2['custom'] == 1) {
					echo 'custom';
				}elseif($nt2['navdoc'] == 1) {
					echo 'doc';
				}elseif($nt2['pageid'] != 0) {
					echo 'page';
				}
				echo '">';
				echo '<span class="dd-handle">';
				echo $nt2[button_text];
				echo '</span>';
				echo '<span class="remove-icon"><a class="remove-nav-item" href=""><img src="../images/remove-icon.png" title="Remove from menu"></a></span>';
				echo '</li>';
				
			}
			echo'</ol>';
		}
		echo '</li>';
		$x++;
		$curPageNum++;
	}	
	echo"</ol>";
}
echo '</div></div>';

//show active pages that are not in the menu
echo '<div id="available-pages">';
echo '<h2>Available Pages</h2>';
echo '<div class="dd available pages">';

$items = '';
$pageqry="Select * from content where active='1' and availability <> 'desktop'";
$rt=mysql_query($pageqry); 
while($nt=mysql_fetch_array($rt)){
	$pageqry2="Select * from nav_slider_mobile where pageid = '" . $nt[pageID] ."'";
	$rt2=mysql_query($pageqry2); 
	if (mysql_num_rows($rt2) == 0) {  //if not listed, add now
		$items .= "<li class='dd-item' data-type='page' data-new='true' data-pageid='".$nt['pageID']."'>";
		$items .= '<span class="dd-handle">';
		$items .= $nt[pageTitle];
		// echo "<div class='add-to-menu'><a href='?a=addpage&pageid=".$nt[pageID]."'>Add to menu</a></div>";
		$items .= '</span>';
		$items .= "</li>";
	}	
}
//check if ordering button is active
$docqry="Select * from nav_slider_mobile where button_url='orderredirect.php'";
$rt=mysql_query($docqry); 
while($nt=mysql_fetch_array($rt)){
	if($nt[active]=='0'){
		$items .= "<li class='dd-item' data-type='doc' data-new='true'>";
		$items .= '<span class="dd-handle">';
		$items .= $nt[button_text];
		// echo "<div class='add-to-menu'><a href='?a=addpage&pageid=".$nt[pageid]."'>Add to menu</a></div>";
		$items .= '</span>';
		$items .= "</li>";
	}
}

if (!empty($items)) {
	echo "<ol id='ol-available-pages' class='dd-list'>";
	echo $items;
	echo '</ol>';
} else {
	echo '<div class="dd-empty"></div>';
}
echo '</div>';

echo "<h2>Available PDF Documents</h2>";
echo '<div class="dd available docs">';
//show docs in the library that are not in the menu
$items = '';
if ($handle = opendir('../docs')) {
    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
		if ($file != "." && $file != "..") {				
			//check if file listed in nav_slider_mobile
			$pageqry="Select * from nav_slider_mobile where button_url = '" . $file ."'";
			$rt=mysql_query($pageqry); 
			if (mysql_num_rows($rt) == 0) {  //if not listed, add now
				$items .= "<li class='dd-item' data-new='true' data-type='doc' data-filename='".$file."'>";
				$items .= '<span class="dd-handle">';
				$items .= $file;
				// echo "<div class='add-to-menu'><a href='?a=addfile&filename=".$file."'> Add to menu</a></div>";
				$items .= '</span>';
				$items .= "</li>";
			} else {	//if listed, delete now
				//echo '<font color="green">Yes</font>';
			}	
			mysql_close;	
		}
    }
    closedir($handle);
}

if (!empty($items)) {
	echo '<ol id="ol-available-docs" class="dd-list">';
	echo $items;
	echo "</ol>";
} else {
	echo '<div class="dd-empty"></div>';
}

echo '</div> <!--end dd class -->';

echo '<h2>Available Social Networks</h2>';
echo '<div class="dd available social-networks">';

// Get active social networks
$activeSocialNetworks = array();
$sql = "SELECT * 
		FROM nav_slider_mobile
		WHERE socialnetworkid != 0";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
	$activeSocialNetworks[] = $row['socialnetworkid'];
}

$items = '';
$sql = "SELECT *
		FROM social_networks
		WHERE page_url != ''
		ORDER BY order_sequence";
$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {
	if (!in_array($row['recordkey'], $activeSocialNetworks)) {
		$items .= "<li class='dd-item' data-type='social' data-new='true' data-socialid='".$row['recordkey']."'>";
		$items .= '<span class="dd-handle">';
		$items .= $row['network_name'];
		$items .= '</span>';
		$items .= "</li>";
	}
}

if (!empty($items)) {
	echo "<ol id='ol-available-social-networks' class='dd-list'>";
	echo $items;
	echo '</ol>';
} else {
	echo '<div class="dd-empty"></div>';
}
echo '</div>';

echo '</div>';

// Get all color themes
$colorThemes = array();

$sql = "SELECT *
		FROM color_themes
		ORDER BY themekey ASC";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
	$colorThemes[$row['themekey']] = $row;
}

?>

	<div class="toPopDiv" id="popup_edit_icon"> 
		<div class="close"></div>
		<span class="ecs_tooltip">Esc to close <span class="arrow"></span></span>
		<div id="popup_content">
			<h2>Edit Icon</h2>

			<form id="form-edit-icon" action="">
				<input type="hidden" name="action" value="save" />
				<input id="edit-icon-recordkey" type="hidden" name="recordkey" value="" />
				<input id="edit-icon-cur-page-num" type="hidden" name="curPageNum" value="" />
				<table id="edit-icon">
					<tr>
						<td width="80">Icon Type: </td>
						<td>
							<label>
								<input type="radio" name="icon_type" value="pre_made" />
								Pre-Made Icon &nbsp; &nbsp;
							</label>
							<label>
								<input type="radio" name="icon_type" value="custom" />
								Custom
							</label>
						</td>
					</tr>
					<tr class="tr-custom-icon">
						<td>Icon Text: </td>
						<td><input id="edit-icon-text" type="text" name="icon_text" value="" /></td>
					</tr>
					<tr class="tr-custom-icon">
						<td valign="top">Color<br />Theme: </td>
						<td>
							<div id="color-themes">
								<?php foreach ($colorThemes as $colorTheme) : ?>
									<label class="label-color-theme">
										<div class="color-theme" style="background-color: #<?php echo $colorTheme['background_color']; ?>; color: #<?php echo $colorTheme['text_color']; ?>;">
											<span class="icon-text">Theme</span><br /><?php echo $colorTheme['themekey']; ?>
										</div>
										<br/ >
										<input id="edit-color-theme-<?php echo $colorTheme['themekey']; ?>" type="radio" name="color_theme" value="<?php echo $colorTheme['themekey']; ?>" />
									</label>
								<?php endforeach; ?>
							</div>
						</td>
					</tr>
					<tr class="tr-pre-made-icon">
						<td valign="top">Icon: </td>
						<td>
                        <div id="color-themes">
							<?php 
								$dir = realpath('../').'/custom/images/icons/';

								$icons = scandir($dir);
							?>
							<?php foreach ($icons as $icon) : ?>
								<?php if ($icon == '.' || $icon == '..') continue; ?>
								<label class="label-color-theme">
									<img src="../custom/images/icons/<?php echo $icon; ?>" width="60" />
									<br />
									<input type="radio" name="icon" value="<?php echo $icon; ?>" />
								</label>
							<?php endforeach; ?>
                            </div>
						</td>
					</tr>
				</table>

				<button id="save-icon" type="button">Save Icon</button>
			</form>
		</div>
	</div> <!--toPopup end-->
	<div class='loader'></div>
	<div id='backgroundPopup'></div>


<script>
var curPageNum = <?php echo $curPageNum; ?>;

function updateOrder() {
	// add X button if not there
	$('.dd li').each(function() {
		if($(this).children('span.remove-icon').length == 0) {
			$(this).append('<span class="remove-icon"><a class="remove-nav-item" href="#"><img src="../images/remove-icon.png" title="Remove from menu"></a></span>');
		}
	});

	// remove X button for available pages, docs
	$('.dd.available li').each(function() {
		if($(this).children('span.remove-icon').length != 0) {
			$(this).children('span.remove-icon').remove();
		}
	});

	if ($('.dd.current li').length < 1) {
		// add dd-empty div
		$('.dd.current ol').remove();
		$('.dd.current').append('<div class="dd-empty"></div>');
	}

	// set a cur-page-num data for any new pages
	$('.dd.current .dd-item').each(function() {
		if ($(this).data('new') == true) {
			$(this).data('cur-page-num', curPageNum);
			curPageNum++;
		}
	});

	order = window.JSON.stringify($('.dd.current').nestable('serialize'));

	$.ajax({
		url: 'save_mob_slider_order.php',
		type: 'post',
		data: {menuOrder: order}
	}).done(function(response) {
		// update recordkeys
		var recordKeys = $.parseJSON(response);
		$('.dd.current .dd-item').each(function() {
			var thisCurPageNum = $(this).data('cur-page-num');
			if (recordKeys[thisCurPageNum] !== undefined) {
				$(this).data('recordkey', recordKeys[thisCurPageNum]);
				$(this).addClass('cur-page-recordkey-'+recordKeys[thisCurPageNum]);
			}
		
		});

		$('.dd.current .dd-item').each(function() {
			// remove data "new" so it won't re-add it on sorting and create icon
			if ($(this).data('new') == true) {
				$(this).removeData('new');
				$(this).removeAttr('data-new');

				addNewIcon($(this));
			}
		});
	});

	// check list items of available pages and docs
	checkAvailablePagesLocation();

	if($('.dd.docs .dd-list li').length == 0 && $('.dd.docs .dd-list').length > 0) {
		$('.dd.docs .dd-list').remove();
		$('.dd.docs').append('<div class="dd-empty"></div>');
	}

	if($('.dd.pages .dd-list li').length == 0 && $('.dd.pages .dd-list').length > 0) {
		$('.dd.pages .dd-list').remove();
		$('.dd.pages').append('<div class="dd-empty"></div>');
	}
}

function resetAvailablePage(inObj) {
	if (inObj.children('.mobile-slider-icon').length > 0) {
		inObj.children('.mobile-slider-icon').remove();
	}
	inObj.removeData('cur-page-num');
	inObj.removeAttr('data-cur-page-num');
	inObj.removeData('recordkey');
	inObj.removeAttr('data-recordkey');
	inObj.attr('class', 'dd-item');
	inObj.data('new', true);
}

function addNewIcon(inObj) {
	var recordkey = inObj.data('recordkey');

	$.ajax({
		url: 'editmobilesliderajax.php',
		type: 'POST',
		data: {action: 'get_info', recordkey: recordkey}
	}).done(function(response) {
		if (response != '0') {
			var menuItem = $.parseJSON(response);
			if (menuItem.icon_image.length > 0) {
				var newIcon = '<span class="mobile-slider-icon popup image" data-popup-id="edit_icon"><img src="/custom/images/icons/'+menuItem.icon_image+'" width="60"></span>';
			} else {
				var newIcon = '<span class="mobile-slider-icon popup" data-popup-id="edit_icon" style="color:#'+menuItem.text_color+'; background-color:#'+menuItem.background_color+';"><span class="icon-text">'+menuItem.icon_text+'</span></span>';
			}
			inObj.children('.dd-handle').after(newIcon);
		}
	});


}

function checkAvailablePagesLocation() {
	$('.dd.pages li').each(function() {
		var type = $(this).data('type');

		if (type != 'page') {
			moveAvailablePageToCorrectSpot($(this));
		}

		resetAvailablePage($(this));
	});

	$('.dd.docs li').each(function() {
		var type = $(this).data('type');

		if (type != 'doc') {
			moveAvailablePageToCorrectSpot($(this));
		}

		resetAvailablePage($(this));
	});

	$('.dd.social-networks li').each(function() {
		var type = $(this).data('type');

		if (type != 'social') {
			moveAvailablePageToCorrectSpot($(this));
		}

		resetAvailablePage($(this));
	});
}

function moveAvailablePageToCorrectSpot(inObj) {
	var type = inObj.data('type');
	switch (type) {
		case 'page':
			if($('.dd.pages .dd-list').length == 0) {
				$('.dd.pages .dd-empty').remove();
				$('.dd.pages').append('<ol class="dd-list"></ol>');
			}

			$('.dd.pages .dd-list').append(inObj);
			break;
		case 'doc':
			if($('.dd.docs .dd-list').length == 0) {
				$('.dd.docs .dd-empty').remove();
				$('.dd.docs').append('<ol class="dd-list"></ol>');
			}

			$('.dd.docs .dd-list').append(inObj);		
			break;
		case 'social':
			if($('.dd.social-networks .dd-list').length == 0) {
				$('.dd.social-networks .dd-empty').remove();
				$('.dd.social-networks').append('<ol class="dd-list"></ol>');
			}

			$('.dd.social-networks .dd-list').append(inObj);
			break;
	}
}

$(document).ready(function() {
	$('.dd.current').nestable({
		maxDepth: 1,
		group: 1
	}).on('change', function() {
		updateOrder();
	});

	$('.dd.available').nestable({
		maxDepth: 1,
		group: 1
	})

	$(document).on('click', 'a.remove-nav-item', function(e) {
		liObj = $(this).closest('li');

		type = liObj.data('type');

		liObj.children('span.remove-icon').remove();

		if(type == 'doc') {
			$('#ol-available-docs').append(liObj);
		}
		if(type == 'page') {
			$('#ol-available-pages').append(liObj);
		}
		if(type == 'social') {
			$('#ol-available-social-networks').append(liObj);
		}
		if(type == 'custom') {
			liObj.remove();
		}

		updateOrder();

		e.preventDefault();
	});

	// Edit icon
	$(document).on('click', '.mobile-slider-icon', function() {
		var recordkey = $(this).parent().data('recordkey');
		$('#edit-icon-recordkey').val(recordkey);

		$.ajax({
			url: 'editmobilesliderajax.php',
			type: 'POST',
			data: {action: 'get_info', recordkey: recordkey}
		}).done(function(response) {
			if (response != '0') {
				var menuItem = $.parseJSON(response);
				if (menuItem.icon_image.length > 0) {
					$('input[name="icon_type"]:eq(0)').prop('checked', true);
					toggleIconType();

					// uncheck all first
					$('input[name="icon"]').prop('checked', false);

					// iterate though them and see if any have the same value
					$('input[name="icon"]').each(function() {
						if ($(this).val() == menuItem.icon_image) {
							$(this).prop('checked', true);
							return false;
						}
					});
				} else {
					$('input[name="icon_type"]:eq(1)').prop('checked', true);
					toggleIconType();
				}
				if (menuItem.hasOwnProperty('icon_text')) {
					$('#edit-icon-text').val(menuItem.icon_text);
				}
				if (menuItem.hasOwnProperty('themekey') && menuItem.themekey != 0) {
					$('#edit-color-theme-'+menuItem.themekey).attr('checked', 'checked');
				}
			}
		});
	});

	// Save icon
	$('#save-icon').click(function() {
		var formData = $('#form-edit-icon').serialize();
		$.ajax({
			url: 'editmobilesliderajax.php',
			type: 'POST',
			data: formData
		}).done(function(response) {
			if (response != '0') {
				var menuItem = $.parseJSON(response);
				var recordkey = menuItem.recordkey;
				// $('.cur-page-recordkey-'+recordkey+' .mobile-slider-icon').css('background-color', '#'+menuItem.background_color);
				// $('.cur-page-recordkey-'+recordkey+' .mobile-slider-icon').css('color', '#'+menuItem.text_color);
				// $('.cur-page-recordkey-'+recordkey+' .mobile-slider-icon').children('span.icon-text').text(menuItem.icon_text);

				if (menuItem.icon_image.length > 0) {
					var newIcon = '<span class="mobile-slider-icon popup image" data-popup-id="edit_icon"><img src="/custom/images/icons/'+menuItem.icon_image+'" width="60"></span>';
				} else {
					var newIcon = '<span class="mobile-slider-icon popup" data-popup-id="edit_icon" style="color:#'+menuItem.text_color+'; background-color:#'+menuItem.background_color+';"><span class="icon-text">'+menuItem.icon_text+'</span></span>';
				}
				$('.cur-page-recordkey-'+recordkey+' .mobile-slider-icon').remove();
				$('.cur-page-recordkey-'+recordkey+' .dd-handle').after(newIcon);

				disablePopup();
			}
		});
	});

	$(document).on('scroll', function() {
		scrollToView();
	});

	$('input[name="icon_type"]').change(function() {
		toggleIconType();
	});
});

function toggleIconType() {
	if ($('input[name="icon_type"]:checked').length == 0) {
		$('input[name="icon_type"]:eq(0)').prop('checked', true);
	}

	if ($('input[name="icon_type"]:checked').val() == 'pre_made') {
		$('.tr-pre-made-icon').show();
		$('.tr-custom-icon').hide();
	} else {
		$('.tr-pre-made-icon').hide();
		$('.tr-custom-icon').show();
	}
}

function disablePopup() {
	if(popupStatus == 1) {
		var popDivs = $(".toPopDiv");
		
		$.each(popDivs,function(){
			$(this).fadeOut("normal");
		});
		
		//$("#toPopup_"+idNum[0]).fadeOut("normal");  
		$("#backgroundPopup").fadeOut("normal");
		popupStatus = 0;  // and set value to 0
	}
}

var totalHeight = $('#main-content').height();
function scrollToView() {
	var scrollingDiv = $("#menusort");
	var margin = $(window).scrollTop();
	var iframeTop = $("#menusort").position().top - 10;

	if(margin - iframeTop < 0) {
		margin = 0;
	} else {
		margin = margin - iframeTop;
	}
	if (margin < 15) {
		margin = 15;
	}

	if ((margin+scrollingDiv.height()) > totalHeight) {
		margin = totalHeight - scrollingDiv.height() + 55;
	}
	scrollingDiv
		.stop()
		.animate({"marginTop": margin + "px"}, 200 );
}

</script>
<?php /*
	$j=0;
	while ($j<=$x){
?>
		<script>$("#menusort<?php echo $j;?>").sortable({
    axis: 'y',
    stop: function (event, ui) {
        var data = $(this).sortable("serialize").toString();
		var idsInOrder = $(this).sortable("toArray").toString();
		//alert(idsInOrder); //popup alert to show data for testing		
		//var elem = document.getElementById("results").value = idsInOrder;	//update results input field for testing
        $.ajax({
            data: data,
            type: 'POST',
            url: 'save_submenu_order.php'
        });
    }
});
</script>
<?php
$j++;
	} */
?>
<div id="add-custom-link">
<form action="" method="POST">
<h2>Add Custom Navigation Link</h2>
<p>Link navigation to external web links</p>
Button text: <input type="text" name="button_text" size="30"><br />
<p>The text you want to display as a navigation link</p>
URL: <input type="text" name="button_url" size="36">
<br /><p>Paste link to page you want button to link to here</p>
<input id="save-button" type="submit" name="add_new" value="Save to Navigation Menu">

</form>
</div>
<!--<p><input type="text" name="results" id="results" value=""></p>-->