/******************************************************************************
@owner: SVSi
@author: Arthur Johnson
@function: User Interface Initialization
@setupUI
*******************************************************************************/

function openMenu(name){
	$("#"+name+"menu-menu").css({
		display:'block',
		height:'auto',
		padding:'5px 0px 0px 0px',
		margin:'0 auto',
		border:'0'
	});
}

function closeMenu(name){
	$("#"+name+"menu-menu").css({
		display:'none',
		height:'auto',
		padding:'5px 0px 0px 0px',
		margin:'0 auto',
		border:'0'
	});
}

var keys = {};
var keyReady = {};

function setupUI(){

	addEventHandler($('#selectorbox')[0], 'dragstart', cancelEvent);

	$("#thebody").keyup(function(event){
	
		var keycode = (event.keyCode ? event.keyCode : event.which);
		var key = String.fromCharCode(keycode);
		
		keys[keycode] = false;
		
		if (keycode == 46 && !itemEditor.open && !textEditor.open){
			var outlined = $('.outlined' ).toArray();
			var naction = new Action({type: 'delete'});
			naction.items = [];
			for (var i=0;i<outlined.length;i++){
				naction.items.push(pb.getItemById(outlined[i].id));
				pb.getItemById(outlined[i].id).remove();
			}
			$('.outlined').removeClass('outlined first');
			pb.saveaction(naction);
		}
		
	});
		
	$("#thebody").keydown(function(event){
	
		var keycode = (event.keyCode ? event.keyCode : event.which);
		var key = String.fromCharCode(keycode);
		
		keys[keycode] = true;
		
		if (textEditor.open || !$('.outlined').exists()) return;
		
		if (keys['37']) {	
			$('.outlined').css({left: '-=1px', right: '+=1px'});
		}
		if (keys['38']) {
			$('.outlined').css({top: '-=1px', bottom: '+=1px'});
		}
		if (keys['39']) {
			$('.outlined').css({right: '-=1px', left: '+=1px'});
		}
		if (keys['40']) {
			$('.outlined').css({bottom: '-=1px', top: '+=1px'});
		}
		$('.outlined').each(function(index, value){ 
			var button = pb.getItemById(this.id);
			button.constrain();
		});
		
	});
	
	if (!standalone) window.onbeforeunload = function (e) {
		e = e || window.event;

		// For IE and Firefox prior to version 4
		if (e) {
			e.returnValue = 'Exit without saving?';
		}

		// For Safari
		return 'Exit without saving?';
	};
	
	document.ondragover = function() {
		pb.mouse.mousemove(window.event);
	}

	$("#filemenuitem1").click(function(e){
		closeMenu('file');
		closeMenu('recent'); 
		pb.openDialog({
			title: 'Are you sure?', 
			content: '<div style="margin: 15px">Your current progress won\'t be saved.</div>',
			style: {
				height: '130px',
				'border-radius': '15px'
			},
			onConfirm: function(){
				pb.closeDialog();
				pb.newprojecttimer = setInterval(function(){
					if (pb.dialogopen !== true){
						pb.loadNewProject();
						clearInterval(pb.newprojecttimer);
					}
				}, 100);
			}
		});
		$('#project-name-display').css('display', 'none');
	});
	$("#filemenuitem2").click(function(e){
		closeMenu('file');
		closeMenu('recent');
		document.getElementById('file-open').accept = '.zip';
		pb.openFileBrowser(function(){
			if (standalone){
				console.log('sio-fio file upload');
				siofio.uploadFile($("#file-open")[0].files[0], {command: 'zip'}, function(data){
					// data.file, data.response, data.success
					console.log('sio-fio client return', data);
				});
			}
			else {
				var file = $("#file-open")[0].files[0];
				var fd = new FormData();
				fd.append("zip", file);
				fd.append("opts", JSON.stringify({command: 'import_project', name: file.name.replace('.zip', '')}));
			
				server({formdata: fd, callback: function(data){
						pb.loadProjectByName({puid: data, fromDB: false});
					}
				});
			}
		});
	});
	$("#filemenuitem3").click(function(e){
		pb.unselectAll();
		closeMenu('file');
		closeMenu('recent'); 	
		if (undef(pb.project.name) || pb.project.name == 'newproject'){
			pb.openDialog({
				content: 'Enter a name for your project:<br><br>'+
					'<input id="project_name" size="3" style="width: 145px;" value="" type="text"/>',
				onConfirm: function(){
					pb.project.name = $('#project_name').val();
					$('#project-name-display').css('display', 'block');
					$('#project-name-display')[0].innerHTML = 'Project: ' + pb.project.name;
					pb.saveProject();
					pb.closeDialog();
				}
			});
		}
		else{
			pb.saveProject();
		}	
	});
	$("#filemenuitem4").click(function(e){
		closeMenu('file');
		closeMenu('recent'); 
		pb.saveProject(true, function(){
			if (standalone) window.websocket.emit('message', {command: 'export_project', id: pb.project.puid});
			else $('#fileopener').attr('src', 'server.php?export='+pb.project.puid+'&name='+pb.project.name);
		});
	});
	$("#filemenuitem5").mouseover(function(e){
		openMenu('recent');	
		updateRecent();
	});
	$("#filemenuitem6").click(function(e){
		closeMenu('file');
		closeMenu('recent');
		pb.openSelectProjectList();
	});
	$("#filemenuitem7").click(function(e){
		closeMenu('file');
		closeMenu('recent');
		pb.openDialog({
			content: '<div style="margin: 15px;">Enter a new name for your project:<br><br>'+
			'<input id="project_name" size="3" style="width: 145px;" value="" type="text"/></div>',
			style: {
				height: '170px',
				'border-radius': '15px'
			},
			onConfirm: function(){
				if (0 || useDB_read)
					queryDB({opts: {query: 'rowcount'}, callback: function(id){
						pb.project.id = parseInt(id)+1;
						pb.project.name = $('#project_name').val();
						$('#project-name-display').css('display', 'block');
						$('#project-name-display')[0].innerHTML = 'Project: ' + pb.project.name;
						pb.saveProject();
						pb.closeDialog();
					}});
				else {
					pb.project.name = $('#project_name').val();
					$('#project-name-display').css('display', 'block');
					$('#project-name-display')[0].innerHTML = 'Project: ' + pb.project.name;
					pb.project.puid = false;
					pb.saveProject();
					pb.closeDialog();
				}
			}
		});
	});
	$("#filemenuitem1").hover(function(){
		closeMenu('recent');
	});
	$("#filemenuitem2").hover(function(){
		closeMenu('recent');
	});
	$("#filemenuitem3").hover(function(){
		closeMenu('recent');
	});
	$("#filemenuitem4").hover(function(){
		closeMenu('recent');
	});
	$("#filemenuitem6").hover(function(){
		closeMenu('recent');
	});
	$("#filemenuitem7").hover(function(){
		closeMenu('recent');
	});
	$("#filemenuitem9").hover(function(){
		closeMenu('recent');
	});
	$("#filemenuitem9").click(function(e){
		closeMenu('file');
		closeMenu('recent');
		//pb.openAddModule();
	});
	$("#filemenuitem8").click(function(e){
		closeMenu('file');
		closeMenu('recent');
		pb.openDeleteProjectList();
	});
	$("#recentmenuitem1").click(function(e){
		closeMenu('recent');	
		if (1||useDB) pb.loadProjectByName(pb.projectnamelist[0]);
		else pb.loadProjectByName(pb.projectnamelist[0].puid);
	});
	$("#recentmenuitem2").click(function(e){
		closeMenu('recent');	
		if (1||useDB) pb.loadProjectByName(pb.projectnamelist[1]);
		else pb.loadProjectByName(pb.projectnamelist[1].puid);
	});
	$("#recentmenuitem3").click(function(e){
		closeMenu('recent');	
		if (1||useDB) pb.loadProjectByName(pb.projectnamelist[2]);
		else pb.loadProjectByName(pb.projectnamelist[2].puid);
	});
	$("#recentmenuitem4").click(function(e){
		closeMenu('recent');	
		if (1||useDB) pb.loadProjectByName(pb.projectnamelist[3]);
		else pb.loadProjectByName(pb.projectnamelist[3].puid);
	});
	$("#recentmenuitem5").click(function(e){
		closeMenu('recent');	
		if (1||useDB) pb.loadProjectByName(pb.projectnamelist[4]);
		else pb.loadProjectByName(pb.projectnamelist[4].puid);
	});
	$("#recentmenuitem6").click(function(e){
		closeMenu('recent');	
		if (1||useDB) pb.loadProjectByName(pb.projectnamelist[5]);
		else pb.loadProjectByName(pb.projectnamelist[5].puid);
	});
	$("#recentmenuitem7").click(function(e){
		closeMenu('recent');	
		if (1||useDB) pb.loadProjectByName(pb.projectnamelist[6]);
		else pb.loadProjectByName(pb.projectnamelist[6].puid);
	});
	$("#recentmenuitem8").click(function(e){
		closeMenu('recent');	
		if (1||useDB) pb.loadProjectByName(pb.projectnamelist[7]);
		else pb.loadProjectByName(pb.projectnamelist[7].puid);
	});
	
	$("#editmenuitem2").click(function(e){
		closeMenu('edit');
		if ($('.outlined').exists()){
			if (pb.getItemById($('.outlined')[0].id) instanceof Button ||
				pb.getItemById($('.outlined')[0].id) instanceof Panel) 
				openItemEditor(pb.getItemById($('.outlined')[0].id), true, true);
			if (pb.getItemById($('.outlined')[0].id) instanceof Widget) 
				openWidgetEditor(pb.getItemById($('.outlined')[0].id), true, true);
		}
	});
	
	$("#addmenuitem1").click(function(e){
		closeMenu('add');
		addPanel(e);
	});
	$("#addmenuitem2").click(function(e){
		closeMenu('add');
		var nbutton = new Button({});
		nbutton.element.style.width = '200px';
		nbutton.element.style.height = '200px';
		openItemEditor(nbutton, false, true);
		pb.openToolsPane();
		pb.openResourcesPane();
		$('#button-library-select').val('User Buttons');
		pb.setButtonLibrary();
		var thisid = "button_library_label";
		$(".tools_item").removeClass('layerD').addClass('layerC');
		$(".tools_item_label").css({'background-color': '#caccca'});
		$('#'+thisid.substring(0,thisid.length-6)).removeClass('layerC').addClass('layerD');
		$('#'+thisid).css({'background-color': '#eeeeee'});
	});
	$("#addmenuitem3").click(function(e){
		closeMenu('add'); 
		var thisid = "widget_library_label";
		$(".tools_item").removeClass('layerD').addClass('layerC');
		$(".tools_item_label").css({'background-color': '#caccca'});
		$('#'+thisid.substring(0,thisid.length-6)).removeClass('layerC').addClass('layerD');
		$('#'+thisid).css({'background-color': '#eeeeee'});
		return false;
	});
	
	$("#viewmenuitem2").click(function(e){
		closeMenu('view');
		setLocalDBObject('projectpreview', pb.projectToJSON(pb.project));
		if (standalone) gui.Window.open('../panels/index.php?panel='+(pb.project.currentpanel.core.id.replace('Panel-',''))+'&preview=true');
		else window.open('../panels/?panel='+(pb.project.currentpanel.core.id.replace('Panel-',''))+'&preview=true','_blank');
	}); 
	
	$("#viewmenuitem3").click(function(e){
		closeMenu('view');
		if (!pb.project.core){
			pb.project.core = {};
			pb.project.core.scripts = ['',''];
			pb.project.core.conditional = '';
		}
		pb.project.isProject = true;
		openTextEditor({item: pb.project});
	}); 
	
	$("#windowmenuitem2").click(function(e){
		closeMenu('window'); 
		$('#toolsidebar img').addClass('fliph');
		$('.tools-pane').stop().animate({left: '-290px'}, 240).animate({left: '0px'}, 240);
		toolpaneopen = true;
		toolopener = false;
	});
	$("#windowmenuitem7").click(function(e){
		closeMenu('window'); 
		$('#resourcesidebar img').removeClass('fliph');
		$('.resources-pane').stop().animate({right: '-290px'}, 240).animate({right: '0px'}, 240);
		resourcepaneopen = true;
		resourceopener = false;

	});
	
	$("#windowmenuitem1").click(function(e){
		closeMenu('window'); 
		
		pb.openDialog({
			content: 'Choose your window skin colors:<br><br>'+
			'<div>Interface Color 1:  \
			<div id="uiColorSelector1" class="uiColorSelector colorSelector" title="Interface Color 1" style="margin-left: 10px; float: none; display: inline-block;"><div style="background-color: #caccca"></div></div>\
			&nbsp;&nbsp;Interface Color 2:  \
			<div id="uiColorSelector2" class="uiColorSelector colorSelector" title="Interface Color 1" style="margin-left: 10px; float: none; display: inline-block;"><div style="background-color: #caccca"></div></div>\</div>\
			<div>Font Color 1:  \
			<div id="uiColorSelector3" class="uiColorSelector colorSelector" title="Interface Color 1" style="margin-left: 10px; float: none; display: inline-block;"><div style="background-color: #caccca"></div></div>\
			&nbsp;&nbsp;Font Color 2:  \
			<div id="uiColorSelector4" class="uiColorSelector colorSelector" title="Interface Color 1" style="margin-left: 10px; float: none; display: inline-block;"><div style="background-color: #caccca"></div></div>\
			<br /><div>Dark Icons: <input id="isDarkIcons" type="checkbox" \></div></div>',
			onConfirm: function(){
				pb.ui.color1 = rgb2hex($('#uiColorSelector1 div').css('backgroundColor'));
				pb.ui.color1a = addHexColor(pb.ui.color1, '#222222');
				pb.ui.color1b = subHexColor(pb.ui.color1, '#111111'); 
				pb.ui.color2 = rgb2hex($('#uiColorSelector2 div').css('backgroundColor'));
				pb.ui.color2a = addHexColor(pb.ui.color1, '#020202');
				pb.ui.fontcolor1 = rgb2hex($('#uiColorSelector3 div').css('backgroundColor'));
				pb.ui.fontcolor2 = rgb2hex($('#uiColorSelector4 div').css('backgroundColor'));
				if ($('#isDarkIcons').prop('checked')) pb.ui.darkicons = '100%';
				else pb.ui.darkicons = '0%';
				pb.closeConfirmDialog();
				addStyleSheet('\
					.backgroundgradient{\
						background: '+pb.ui.color1+';\
						background: -moz-linear-gradient(top,  '+pb.ui.color1a+' 0%, '+pb.ui.color1b+' 50%, '+pb.ui.color2+' 51%, '+pb.ui.color2a+' 100%);\
						background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,'+pb.ui.color1a+'), color-stop(50%,'+pb.ui.color1b+'), color-stop(51%,'+pb.ui.color2+'), color-stop(100%,'+pb.ui.color2a+'));\
						background: -webkit-linear-gradient(top,  '+pb.ui.color1a+' 0%,'+pb.ui.color1b+' 50%,'+pb.ui.color2+' 51%,'+pb.ui.color2a+' 100%);\
						background: -o-linear-gradient(top,  '+pb.ui.color1a+' 0%,'+pb.ui.color1b+' 50%,'+pb.ui.color2+' 51%,'+pb.ui.color2a+' 100%);\
						background: -ms-linear-gradient(top,  '+pb.ui.color1a+' 0%,'+pb.ui.color1b+' 50%,'+pb.ui.color2+' 51%,'+pb.ui.color2a+' 100%);\
						background: linear-gradient(to bottom,  '+pb.ui.color1a+' 0%,'+pb.ui.color1b+' 50%,'+pb.ui.color2+' 51%,'+pb.ui.color2a+' 100%);\
						filter: progid:DXImageTransform.Microsoft.gradient( startColorstr=\''+pb.ui.color1+'\', endColorstr=\''+pb.ui.color2+'\',GradientType=0 );\
					}\
					.menu-button{\
						background: '+pb.ui.color1+';\
						background: -moz-linear-gradient(top,  '+pb.ui.color1a+' 0%, '+pb.ui.color1b+' 50%, '+pb.ui.color2+' 51%, '+pb.ui.color2a+' 100%);\
						background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,'+pb.ui.color1a+'), color-stop(50%,'+pb.ui.color1b+'), color-stop(51%,'+pb.ui.color2+'), color-stop(100%,'+pb.ui.color2a+'));\
						background: -webkit-linear-gradient(top,  '+pb.ui.color1a+' 0%,'+pb.ui.color1b+' 50%,'+pb.ui.color2+' 51%,'+pb.ui.color2a+' 100%);\
						background: -o-linear-gradient(top,  '+pb.ui.color1a+' 0%,'+pb.ui.color1b+' 50%,'+pb.ui.color2+' 51%,'+pb.ui.color2a+' 100%);\
						background: -ms-linear-gradient(top,  '+pb.ui.color1a+' 0%,'+pb.ui.color1b+' 50%,'+pb.ui.color2+' 51%,'+pb.ui.color2a+' 100%);\
						background: linear-gradient(to bottom,  '+pb.ui.color1a+' 0%,'+pb.ui.color1b+' 50%,'+pb.ui.color2+' 51%,'+pb.ui.color2a+' 100%);\
						filter: progid:DXImageTransform.Microsoft.gradient( startColorstr=\''+pb.ui.color1+'\', endColorstr=\''+pb.ui.color2+'\',GradientType=0 );\
						color: '+pb.ui.fontcolor1+';\
					}\
					.title_bar{\
						height: 15px;\
						background: #aebcbf; \
						background: -moz-linear-gradient(top,  '+pb.ui.color1a+' 0%, '+pb.ui.color1b+' 50%, '+pb.ui.color2+' 51%, '+pb.ui.color2a+' 100%);\
						background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,'+pb.ui.color1a+'), color-stop(50%,'+pb.ui.color1b+'), color-stop(51%,'+pb.ui.color2+'), color-stop(100%,'+pb.ui.color2a+'));\
						background: -webkit-linear-gradient(top,  '+pb.ui.color1a+' 0%,'+pb.ui.color1b+' 50%,'+pb.ui.color2+' 51%,'+pb.ui.color2a+' 100%);\
						background: -o-linear-gradient(top,  '+pb.ui.color1a+' 0%,'+pb.ui.color1b+' 50%,'+pb.ui.color2+' 51%,'+pb.ui.color2a+' 100%);\
						background: -ms-linear-gradient(top,  '+pb.ui.color1a+' 0%,'+pb.ui.color1b+' 50%,'+pb.ui.color2+' 51%,'+pb.ui.color2a+' 100%);\
						background: linear-gradient(to bottom,  '+pb.ui.color1a+' 0%,'+pb.ui.color1b+' 50%,'+pb.ui.color2+' 51%,'+pb.ui.color2a+' 100%);\
						filter: progid:DXImageTransform.Microsoft.gradient( startColorstr=\''+pb.ui.color1+'\', endColorstr=\''+pb.ui.color2+'\',GradientType=0 );\
						border-top-left-radius: 4px;\
						border-bottom: 1px solid #464745;\
						border-top-right-radius: 4px;\
						text-align: center;\
						font-family: Lucida Grande;\
						font-size: 14px;\
						color: '+pb.ui.fontcolor1+';\
						font-weight: bold;\
						text-shadow: 0 1px 1px #466278;\
						padding: 5px;\
					}\
					.menu-button:hover {\
						background: '+pb.ui.color2+';\
						background: -moz-linear-gradient(top, '+pb.ui.color2a+' 0%, '+pb.ui.color2+' 50%, '+pb.ui.color1+' 79%, '+pb.ui.color1+' 100%);\
						background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,'+pb.ui.color2+'), color-stop(50%,'+pb.ui.color2+'), color-stop(79%,'+pb.ui.color1+'), color-stop(100%,'+pb.ui.color1+'));\
						background: -webkit-linear-gradient(top, '+pb.ui.color2a+' 0%,'+pb.ui.color2+' 50%,'+pb.ui.color1+' 79%,'+pb.ui.color1+' 100%);\
						background: -o-linear-gradient(top, '+pb.ui.color2a+' 0%,'+pb.ui.color2+' 50%,'+pb.ui.color1+' 79%,'+pb.ui.color1+' 100%);\
						background: -ms-linear-gradient(top, '+pb.ui.color2a+' 0%,'+pb.ui.color2+' 50%,'+pb.ui.color1+' 79%,'+pb.ui.color1+' 100%);\
						background: linear-gradient(to bottom, '+pb.ui.color2a+' 0%,'+pb.ui.color2+' 50%,'+pb.ui.color1+' 79%,'+pb.ui.color1+' 100%);\
						filter: progid:DXImageTransform.Microsoft.gradient( startColorstr=\''+pb.ui.color2+'\', endColorstr=\''+pb.ui.color1+'\',GradientType=0 );\
						\
						color: white;\
					}\
					.main_bg_color{\
						background-color: #000000;\
					}\
					.second_bg_color{\
						background-color: #caccca;\
					}\
					.main_border_color{\
						border-color: '+pb.ui.color1+';\
					}\
					.second_border_color{\
						border-color: lightgrey;\
					}\
					.main_font_color{\
						border-color: #eeeeee;\
					}\
					.second_font_color{\
						border-color: lightgrey;\
					}\
					.icon img{\
						-webkit-filter: invert('+pb.ui.darkicons+');\
						filter: invert('+pb.ui.darkicons+');\
					}\
				');
				pb.closeDialog();
			},
			init: function(){
				$('#uiColorSelector1').ColorPicker({
					color: '#ffffff',
					onShow: function (colpkr) {
						$(colpkr).fadeIn(300);
						return false;
					},
					onHide: function (colpkr) {
						$(colpkr).fadeOut(300);
						return false;
					},
					onChange: function (hsb, hex, rgb) {
						$('#uiColorSelector1 div').css('backgroundColor', '#' + hex);
					}
				});
				$('#uiColorSelector2').ColorPicker({
					color: '#ffffff',
					onShow: function (colpkr) {
						$(colpkr).fadeIn(300);
						return false;
					},
					onHide: function (colpkr) {
						$(colpkr).fadeOut(300);
						return false;
					},
					onChange: function (hsb, hex, rgb) {
						$('#uiColorSelector2 div').css('backgroundColor', '#' + hex);
					}
				});
				$('#uiColorSelector3').ColorPicker({
					color: '#ffffff',
					onShow: function (colpkr) {
						$(colpkr).fadeIn(300);
						return false;
					},
					onHide: function (colpkr) {
						$(colpkr).fadeOut(300);
						return false;
					},
					onChange: function (hsb, hex, rgb) {
						$('#uiColorSelector3 div').css('backgroundColor', '#' + hex);
					}
				});
				$('#uiColorSelector4').ColorPicker({
					color: '#ffffff',
					onShow: function (colpkr) {
						$(colpkr).fadeIn(300);
						return false;
					},
					onHide: function (colpkr) {
						$(colpkr).fadeOut(300);
						return false;
					},
					onChange: function (hsb, hex, rgb) {
						$('#uiColorSelector4 div').css('backgroundColor', '#' + hex);
					}
				});
			}
		});
	});
	
	$("#windowmenuitem3").click(function(e){
		closeMenu('window'); 
		var thisid = "button_library_label";
		$(".tools_item").removeClass('layerD').addClass('layerC');
		$(".tools_item_label").css({'background-color': '#caccca'});
		$('#'+thisid.substring(0,thisid.length-6)).removeClass('layerC').addClass('layerD');
		$('#'+thisid).css({'background-color': '#eeeeee'});
		$('.libraryselected').removeClass('libraryselected');
		return false;
	});
	$("#windowmenuitem4").click(function(e){
		closeMenu('window'); 
		var thisid = "widget_library_label";
		$(".tools_item").removeClass('layerD').addClass('layerC');
		$(".tools_item_label").css({'background-color': '#caccca'});
		$('#'+thisid.substring(0,thisid.length-6)).removeClass('layerC').addClass('layerD');
		$('#'+thisid).css({'background-color': '#eeeeee'});
		$('.libraryselected').removeClass('libraryselected');
		return false;
	});
	$("#windowmenuitem5").click(function(e){
		closeMenu('window'); 
		var thisid = "template_library_label";
		$(".tools_item").removeClass('layerD').addClass('layerC');
		$(".tools_item_label").css({'background-color': '#caccca'});
		$('#'+thisid.substring(0,thisid.length-6)).removeClass('layerC').addClass('layerD');
		$('#'+thisid).css({'background-color': '#eeeeee'});
		$('.libraryselected').removeClass('libraryselected');
		return false;
	});
	$("#windowmenuitem6").click(function(e){
		closeMenu('window'); 
		var thisid = "panel_props_label";
		$(".tools_item").removeClass('layerD').addClass('layerC');
		$(".tools_item_label").css({'background-color': '#caccca'});
		$('#'+thisid.substring(0,thisid.length-6)).removeClass('layerC').addClass('layerD');
		$('#'+thisid).css({'background-color': '#eeeeee'});
		$('.libraryselected').removeClass('libraryselected');
		return false;
	});
	$("#helpmenuitem1, #helpmenuitem2, #helpmenuitem4").click(function(e){
		closeMenu('help');
	});
	$("#helpmenuitem3").click(function(e){
		closeMenu('help');
	});
	$("#helpmenuitem5").click(function(e){
		var type = ' N-Command';
		if (standalone) type = ' Standalone';
		if (wallcon) type = ' Wall Controller';
		if (modero) type = ' Wall Controller';
		pb.openDialog({
			content: '<div style="margin: 15px; text-align: center;"><span style="font-size: 18px;">Panel Builder'+type+'</span><br><br>&copy;SVSi 2015<br><br>version '+version+'</div>',
			confirm: 'ok',
			cancel: false,
			style: {
				height: '150px',
				'border-radius': '15px'
			},
			onConfirm: function(){pb.closeDialog();}
		});
		closeMenu('help');
	});
	
	$("#left-arrow" ).click(rotatePanelsLeft);
	$("#right-arrow" ).click(rotatePanelsRight);
	
	$("#windowmenuitem2").click();
	$("#windowmenuitem7").click();
	setTimeout(function(){
		$('#resourcesidebar img').removeClass('fliph');
		$('#toolsidebar img').addClass('fliph');
	}, 1000);
	
	//*************************** BATCH EDIT ICONS ***************************//
	$('#border-button').click(function(){
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		pb.openDialog({
			content: '<span style="font-size: 20px;">Enter border details for these items:</span><br><br>'+
			'Size: <input id="group_border" size="3" style="width: 45px;" value="0" type="text"/> px<br clear="all"><br>'+
			'Color: <input id="border-bgColorSelector" class="color {pickerFaceColor:\'#111\'}"></input>',
			style: {height: '250px'},
			onConfirm: function(){
			
				var tempoutlined = $('.outlined' ).toArray();
				var outlined = [];
				var outlineditems = [];
				var outlinedwhs = [];
				var outlinedwhsb = [];
				for (var i=0; i<tempoutlined.length; i++) {
					if (pb.getItemById(tempoutlined[i].id).panel){
						outlined.push(tempoutlined[i]);
						outlineditems.push(pb.getItemById(tempoutlined[i].id));
						outlinedwhs.push({style: tempoutlined[i].style.cssText});
					}
				}
				
				var taction = new Action({
					type: 'style',
					items: outlineditems,
					oldvalues: outlinedwhs
				});
				
				var tcolor = '#' + $('#border-bgColorSelector')[0].color;
				if (tcolor == 'rgba(0, 0, 0, 0)') tcolor = 'rgba(0, 0, 0, 1)';
				$('.outlined').css({'border-width': $('#group_border').val() + 'px', 'border-color': tcolor, 'border-style': 'solid'});
			
				for (var i=0;i<outlined.length;i++){
					outlinedwhsb.push({style: outlined[i].style.cssText});		
				}
				
				taction.newvalues = outlinedwhsb;
				pb.saveaction(taction);
				
				pb.closeDialog();
			
			},
			init: function(){
				jscolor.init();
				var tn = parseInt($('.outlined')[0].style.borderWidth);
				if (isNaN(tn)) tn = 0;
				$('#group_border').val(tn);
			}
		});
	});
	$('#bteditor-button').click(function(){
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		pb.openDialog({
			content: '<span style="font-size: 20px;">Enter item font options:</span><br><br>'+
				'font:&nbsp;&nbsp;<select id="group_font" name="font" >\
				  <option value="Arial">Arial</option>\
				  <option value="Impact">Impact</option>\
				  <option value="Georgia">Georgia</option>\
				  <option value="Times New Roman">Times New Roman</option>\
				</select><br><br>\
				color:&nbsp;&nbsp;<select id="group_fontcolor" name="fontcolor" >\
				  <option value="Black">Black</option>\
				  <option value="Red">Red</option>\
				  <option value="Green">Green</option>\
				  <option value="Blue">Blue</option>\
				  <option value="White">White</option>\
				</select><br><br>\
				size:&nbsp;&nbsp;<select id="group_fontsize" name="fontsize" >\
				  <option value="8">8</option>\
				  <option value="12">12</option>\
				  <option value="18">18</option>\
				  <option value="24">24</option>\
				  <option value="36">36</option>\
				  <option value="48">48</option>\
				  <option value="72">72</option>\
				</select>',
			onConfirm: function(){
				/*$('.outlined').css({'font-family': $('#group_font').val(),
					'color': $('#group_fontcolor').val(),
					'font-size': $('#group_fontsize').val() + 'px'});*/
				$('.outlined').each(function(index){
					var self = pb.getItemById($(this)[0].id);
					
					$(self.textnode).css({'font-family': $('#group_font').val(),
						'color': $('#group_fontcolor').val(),
						'font-size': $('#group_fontsize').val() + 'px'});
					$(self.textnode).find('*').css({'font-family': $('#group_font').val(),
						'color': $('#group_fontcolor').val(),
						'font-size': $('#group_fontsize').val() + 'px'});
						
					var theight = ($(self.element).outerHeight()/2)
						- ($(self.textnode).outerHeight()/2);
					$(self.textnode).css('top', theight + 'px');
				});
				pb.closeDialog();
			},
			init: function(){
				switch ($('.outlined')[0].style.fontFamily) {
					case 'Arial':
						$('#group_font')[0].selectedIndex = 0;
						break;
					case 'Impact':
						$('#group_font')[0].selectedIndex = 1;
						break;
					case 'Georgia':
						$('#group_font')[0].selectedIndex = 2;
						break;
					case '\'Times New Roman\'':
						$('#group_font')[0].selectedIndex = 3;
						break;
					default:
						break;
				}
				switch ($('.outlined')[0].style.fontSize) {
					case '8px':
						$('#group_fontsize')[0].selectedIndex = 0;
						break;
					case '12px':
						$('#group_fontsize')[0].selectedIndex = 1;
						break;
					case '18px':
						$('#group_fontsize')[0].selectedIndex = 2;
						break;
					case '24px':
						$('#group_fontsize')[0].selectedIndex = 3;
						break;
					case '36px':
						$('#group_fontsize')[0].selectedIndex = 4;
						break;
					case '48px':
						$('#group_fontsize')[0].selectedIndex = 5;
						break;
					case '72px':
						$('#group_fontsize')[0].selectedIndex = 6;
						break;
					default:
						break;
				}
				switch ($('.outlined')[0].style.color) {
					case 'rgb(0, 0, 0)':
						$('#group_fontcolor')[0].selectedIndex = 0;
						break;
					case 'rgb(255, 0, 0)':
						$('#group_fontcolor')[0].selectedIndex = 1;
						break;
					case 'rgb(0, 128, 0)':
						$('#group_fontcolor')[0].selectedIndex = 2;
						break;
					case 'rgb(0, 0, 128)':
						$('#group_fontcolor')[0].selectedIndex = 3;
						break;
					case 'rgb(255, 255, 255)':
						$('#group_fontcolor')[0].selectedIndex = 4;
						break;
					default:
						break;
				}
			}
		});
	});
	$('#shape-button').click(function(){
	
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		
		pb.openDialog({
			content: '<span style="font-size: 20px;">Enter border radii for items:</span><br><br>'+
				'<div style="width: 55%; position: relative; left: 80px;">Top-left: <span style="float: right;"> px &nbsp;&nbsp;</span>&nbsp;<input id="group_tl" size="3" style="width: 45px; float: right;" value="0" type="text"/><br clear="all"><br>\
				Top-right: <span style="float: right;"> px &nbsp;&nbsp;</span>&nbsp;<input id="group_tr" size="3" style="width: 45px; float: right;" value="0" type="text"/><br clear="all"><br>\
				Bottom-left: <span style="float: right;"> px &nbsp;&nbsp;</span>&nbsp;<input id="group_bl" size="3" style="width: 45px; float: right;" value="0" type="text"/><br clear="all"><br>\
				Bottom-right: <span style="float: right;"> px &nbsp;&nbsp;</span>&nbsp;<input id="group_br" size="3" style="width: 45px; float: right;" value="0" type="text"/></div>',
			style: {height: '270px'},
			onConfirm: function(){
				$('.outlined').css({'border-radius': 
					parseInt($('#group_tl').val()) + 'px '
					+ parseInt($('#group_tr').val()) + 'px '
					+ parseInt($('#group_br').val()) + 'px '
					+ parseInt($('#group_bl').val()) + 'px '});
					
				pb.closeDialog();
			},
			init: function(){
				var ttl = parseInt($('.outlined')[0].style.borderTopLeftRadius);
				if (isNaN(ttl)) ttl = 0;
				var ttr = parseInt($('.outlined')[0].style.borderTopRightRadius);
				if (isNaN(ttr)) ttr = 0;
				var tbl = parseInt($('.outlined')[0].style.borderBottomLeftRadius);
				if (isNaN(tbl)) tbl = 0;
				var tbr = parseInt($('.outlined')[0].style.borderBottomRightRadius);
				if (isNaN(tbr)) tbr = 0;
				
				$('#group_tl').val(ttl);
				$('#group_tr').val(ttr);
				$('#group_bl').val(tbl);
				$('#group_br').val(tbr);
			}
		});
	});
	$('#alignleft-button').click(function(){
	
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		
		var tempoutlined = $('.outlined' ).toArray();
		var outlined = [];
		var outlineditems = [];
		var outlinedwhs = [];
		var outlinedwhsb = [];
		for (var i=0; i<tempoutlined.length; i++) {
			if (pb.getItemById(tempoutlined[i].id).panel){
				outlined.push(tempoutlined[i]);
				outlineditems.push(pb.getItemById(tempoutlined[i].id));
				outlinedwhs.push({top: tempoutlined[i].style.top, left: tempoutlined[i].style.left});
			}
		}
		
		var lmost = parseInt($('.first')[0].style.left);

		var taction = new Action({
			type: 'move',
			items: outlineditems,
			oldvalues: outlinedwhs
		});
		
		$(".outlined" ).css({left: lmost + 'px'});
		
		for (var i=0;i<outlined.length;i++){
			outlinedwhsb.push({top: outlined[i].style.top, left: outlined[i].style.left});		
		}
		
		taction.newvalues = outlinedwhsb;
		pb.saveaction(taction);
		
	});
	$('#alignright-button').click(function(){
	
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		
		var tempoutlined = $('.outlined' ).toArray();
		var outlined = [];
		var outlineditems = [];
		var outlinedwhs = [];
		var outlinedwhsb = [];
		for (var i=0; i<tempoutlined.length; i++) {
			if (pb.getItemById(tempoutlined[i].id).panel){
				outlined.push(tempoutlined[i]);
				outlineditems.push(pb.getItemById(tempoutlined[i].id));
				outlinedwhs.push({top: tempoutlined[i].style.top, left: tempoutlined[i].style.left});
			}
		}
		
		var rmost = parseInt($('.first')[0].style.left) + parseInt($('.first')[0].style.width);
		
		var taction = new Action({
			type: 'move',
			items: outlineditems,
			oldvalues: outlinedwhs
		});
		for (var i=0; i<outlined.length; i++){
			outlined[i].style.left = (rmost - parseInt(outlined[i].style.width)) + 'px';
		}
		for (var i=0;i<outlined.length;i++){
			outlinedwhsb.push({top: outlined[i].style.top, left: outlined[i].style.left});		
		}
		
		taction.newvalues = outlinedwhsb;
		pb.saveaction(taction);
		
	});
	$('#aligntop-button').click(function(){
	
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		
		var tempoutlined = $('.outlined' ).toArray();
		var outlined = [];
		var outlineditems = [];
		var outlinedwhs = [];
		var outlinedwhsb = [];
		for (var i=0; i<tempoutlined.length; i++) {
			if (pb.getItemById(tempoutlined[i].id).panel){
				outlined.push(tempoutlined[i]);
				outlineditems.push(pb.getItemById(tempoutlined[i].id));
				outlinedwhs.push({top: tempoutlined[i].style.top, left: tempoutlined[i].style.left});
			}
		}
		
		var tmost = parseInt($('.first')[0].style.top);
		
		var taction = new Action({
			type: 'move',
			items: outlineditems,
			oldvalues: outlinedwhs
		});
		$(".outlined" ).css({top: tmost + 'px'});
		for (var i=0;i<outlined.length;i++){
			outlinedwhsb.push({top: outlined[i].style.top, left: outlined[i].style.left});		
		}
		
		taction.newvalues = outlinedwhsb;
		pb.saveaction(taction);
		
	});
	$('#alignbottom-button').click(function(){
	
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		
		var tempoutlined = $('.outlined' ).toArray();
		var outlined = [];
		var outlineditems = [];
		var outlinedwhs = [];
		var outlinedwhsb = [];
		for (var i=0; i<tempoutlined.length; i++) {
			if (pb.getItemById(tempoutlined[i].id).panel){
				outlined.push(tempoutlined[i]);
				outlineditems.push(pb.getItemById(tempoutlined[i].id));
				outlinedwhs.push({top: tempoutlined[i].style.top, left: tempoutlined[i].style.left})
			}
		}
		
		var bmost = parseInt($('.first')[0].style.top) + parseInt($('.first')[0].style.height);
		
		var taction = new Action({
			type: 'move',
			items: outlineditems,
			oldvalues: outlinedwhs
		});
		for (var i=0; i<outlined.length; i++){
			outlined[i].style.top = (bmost - parseInt(outlined[i].style.height)) + 'px';
		}
		for (var i=0;i<outlined.length;i++){
			outlinedwhsb.push({top: outlined[i].style.top, left: outlined[i].style.left});		
		}
		
		taction.newvalues = outlinedwhsb;
		pb.saveaction(taction);
		
	});
	$('#alignh-button').click(function(){
	
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		
		var tempoutlined = $('.outlined' ).toArray();
		var outlined = [];
		var outlineditems = [];
		var outlinedwhs = [];
		var outlinedwhsb = [];
		for (var i=0; i<tempoutlined.length; i++) {
			if (pb.getItemById(tempoutlined[i].id).panel){
				outlined.push(tempoutlined[i]);
				outlineditems.push(pb.getItemById(tempoutlined[i].id));
				outlinedwhs.push({top: tempoutlined[i].style.top, left: tempoutlined[i].style.left});
			}
		}
		
		var taction = new Action({
			type: 'move',
			items: outlineditems,
			oldvalues: outlinedwhs
		});
		var average = 0;
		for (var i=0; i<outlined.length; i++){
			average += parseInt(outlined[i].style.top) + (parseInt(outlined[i].style.height)/2);
		}
		average /= outlined.length;
		for (var i=0; i<outlined.length; i++){
			outlined[i].style.top = average - (parseInt(outlined[i].style.height)/2) + 'px';
		}
		
		for (var i=0;i<outlined.length;i++){
			outlinedwhsb.push({top: outlined[i].style.top, left: outlined[i].style.left});		
		}
		
		var taction = new Action({
			type: 'move',
			items: outlineditems,
			oldvalues: outlinedwhs
		});
		
		var panelwidth = parseInt(pb.project.currentpanel.element.style.width);
		var spacing = parseInt(panelwidth/(outlined.length+1));
		
		var swapped;
		do{
			swapped = false;
			for (var i=0;i<outlined.length-1;i++){
				if (parseInt(outlined[i].style.left) > parseInt(outlined[i+1].style.left)) {
					var tempsort = outlined[i];
					outlined[i] = outlined[i+1];
					outlined[i+1] = tempsort;
					swapped = true;
				}
			}
		}while(swapped);
		
		for (var i=0;i<outlined.length;i++){
			$(outlined[i]).css({left: (((i+1)*spacing) - ($(outlined[i]).outerWidth()/2)) + 'px'});
			outlinedwhsb.push({top: outlined[i].style.top, left: outlined[i].style.left});		
		}
		
		taction.newvalues = outlinedwhsb;
		pb.saveaction(taction);
		
	});
	$('#alignv-button').click(function(){
	
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		
		var tempoutlined = $('.outlined' ).toArray();
		var outlined = [];
		var outlineditems = [];
		var outlinedwhs = [];
		var outlinedwhsb = [];
		for (var i=0; i<tempoutlined.length; i++) {
			if (pb.getItemById(tempoutlined[i].id).panel){
				outlined.push(tempoutlined[i]);
				outlineditems.push(pb.getItemById(tempoutlined[i].id));
				outlinedwhs.push({top: tempoutlined[i].style.top, left: tempoutlined[i].style.left});
			}
		}
		
		var taction = new Action({
			type: 'move',
			items: outlineditems,
			oldvalues: outlinedwhs
		});
		
		var panelheight = parseInt(pb.project.currentpanel.element.style.height);
		var spacing = parseInt(panelheight/(outlined.length+1));
		
		var outlined2 = [];

		var swapped;
		do{
			swapped = false;
			for (var i=0;i<outlined.length-1;i++){
				if (parseInt(outlined[i].style.top) > parseInt(outlined[i+1].style.top)) {
					var tempsort = outlined[i];
					outlined[i] = outlined[i+1];
					outlined[i+1] = tempsort;
					swapped = true;
				}
			}
		}while(swapped);
			
		for (var i=0;i<outlined.length;i++){
			$(outlined[i]).css({top: (((i+1)*spacing) - ($(outlined[i]).outerHeight()/2)) + 'px'});
			outlinedwhsb.push({top: outlined[i].style.top, left: outlined[i].style.left});		
		}
		
		var taction = new Action({
			type: 'move',
			items: outlineditems,
			oldvalues: outlinedwhs
		});
		var average = 0;
		for (var i=0; i<outlined.length; i++){
			average += parseInt(outlined[i].style.left) + (parseInt(outlined[i].style.width)/2);
		}
		average /= outlined.length;
		for (var i=0; i<outlined.length; i++){
			outlined[i].style.left = average - (parseInt(outlined[i].style.width)/2) + 'px';
		}
		
		for (var i=0;i<outlined.length;i++){
			outlinedwhsb.push({top: outlined[i].style.top, left: outlined[i].style.left});		
		}
		
		taction.newvalues = outlinedwhsb;
		pb.saveaction(taction);
		
	});
	$('#aligng-button').click(function(){
	
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
	
		pb.openDialog({
			content: '<span style="font-size: 20px;">Enter the number of rows/columns for the grid:</span><br><br>'+
				'<div style="width: 280px; position: relative; left: 76px; top: 15px;">\
					rows: <input id="group_rows" size="3" style="width: 45px; float: right;" value="0" type="text"/><br clear="all"><br>\
					columns: <input id="group_columns" size="3" style="width: 45px; float: right;" value="0" type="text"/>\
				</div>',
			style: {height: '210px', width: '480px'},
			onConfirm: function(){
				var tempoutlined = $('.outlined' ).toArray();
				var outlined = [];
				var outlineditems = [];
				var outlinedwhs = [];
				var outlinedwhsb = [];
				for (var i=0; i<tempoutlined.length; i++) {
					if (pb.getItemById(tempoutlined[i].id).panel){
						outlined.push(tempoutlined[i]);
						outlineditems.push(pb.getItemById(tempoutlined[i].id));
						outlinedwhs.push({top: tempoutlined[i].style.top, left: tempoutlined[i].style.left});
					}
				}
				
				var taction = new Action({
					type: 'move',
					items: outlineditems,
					oldvalues: outlinedwhs
				});
						
				var rows = parseInt($('#group_columns').val());
				var cols = parseInt($('#group_rows').val());
				
				var rowoffset = parseInt(pb.project.currentpanel.element.style.width)/(rows+1);
				var coloffset = parseInt(pb.project.currentpanel.element.style.height)/(cols+1);
				
				var k = 0;
				for (var i=0; i<cols; i++){
					if (undef(outlined[k])) break;
					for (var j=0; j<rows; j++){
						if (undef(outlined[k])) break;
						outlined[k].style.top = (coloffset*(i+1))-(parseInt(outlined[k].style.height)/2)+'px';
						outlined[k].style.left = (rowoffset*(j+1))-(parseInt(outlined[k].style.width)/2)+'px';
						
						k++;
					}
				
				}
				
				for (var i=0;i<outlined.length;i++){
					outlinedwhsb.push({top: outlined[i].style.top, left: outlined[i].style.left});		
				}
				
				taction.newvalues = outlinedwhsb;
				pb.saveaction(taction);
				
				pb.closeDialog();
				
			}
		});

	});
	$('#spacex-button').click(function(){
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		var tempoutlined = $('.outlined' ).toArray();
		var outlined = [];
		var outlineditems = [];
		var outlinedwhs = [];
		var outlinedwhsb = [];
		for (var i=0; i<tempoutlined.length; i++) {
			if (pb.getItemById(tempoutlined[i].id).panel){
				outlined.push(tempoutlined[i]);
				outlineditems.push(pb.getItemById(tempoutlined[i].id));
				outlinedwhs.push({top: tempoutlined[i].style.top, left: tempoutlined[i].style.left});
			}
		}
		
		var taction = new Action({
			type: 'move',
			items: outlineditems,
			oldvalues: outlinedwhs
		});
		
		var panelwidth = parseInt(pb.project.currentpanel.element.style.width);
		var spacing = parseInt(panelwidth/(outlined.length+1));
		
		var swapped;
		do{
			swapped = false;
			for (var i=0;i<outlined.length-1;i++){
				if (parseInt(outlined[i].style.left) > parseInt(outlined[i+1].style.left)) {
					var tempsort = outlined[i];
					outlined[i] = outlined[i+1];
					outlined[i+1] = tempsort;
					swapped = true;
				}
			}
		}while(swapped);
		
		for (var i=0;i<outlined.length;i++){
			$(outlined[i]).css({left: (((i+1)*spacing) - ($(outlined[i]).outerWidth()/2)) + 'px'});
			outlinedwhsb.push({top: outlined[i].style.top, left: outlined[i].style.left});		
		}
		
		taction.newvalues = outlinedwhsb;
		pb.saveaction(taction);
		
	});
	$('#spacey-button').click(function(){
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		var tempoutlined = $('.outlined' ).toArray();
		var outlined = [];
		var outlineditems = [];
		var outlinedwhs = [];
		var outlinedwhsb = [];
		for (var i=0; i<tempoutlined.length; i++) {
			if (pb.getItemById(tempoutlined[i].id).panel){
				outlined.push(tempoutlined[i]);
				outlineditems.push(pb.getItemById(tempoutlined[i].id));
				outlinedwhs.push({top: tempoutlined[i].style.top, left: tempoutlined[i].style.left});
			}
		}
		
		var taction = new Action({
			type: 'move',
			items: outlineditems,
			oldvalues: outlinedwhs
		});
		
		var panelheight = parseInt(pb.project.currentpanel.element.style.height);
		var spacing = parseInt(panelheight/(outlined.length+1));
		
		var outlined2 = [];

		var swapped;
		do{
			swapped = false;
			for (var i=0;i<outlined.length-1;i++){
				if (parseInt(outlined[i].style.top) > parseInt(outlined[i+1].style.top)) {
					var tempsort = outlined[i];
					outlined[i] = outlined[i+1];
					outlined[i+1] = tempsort;
					swapped = true;
				}
			}
		}while(swapped);
			
		for (var i=0;i<outlined.length;i++){
			$(outlined[i]).css({top: (((i+1)*spacing) - ($(outlined[i]).outerHeight()/2)) + 'px'});
			outlinedwhsb.push({top: outlined[i].style.top, left: outlined[i].style.left});		
		}
		
		taction.newvalues = outlinedwhsb;
		pb.saveaction(taction);
	});
	
	$('#aligntext-button').click(function(){
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		pb.openDialog({
			content: '<span style="font-size: 20px;">Enter the text offset for these items:</span><br><br>'+
			'<div style="width: 280px; position: relative; left: 36px; top: 15px;">\
				x: <input id="group_textoffsetx" size="3" style="width: 45px; float: right;" value="0" type="text"/> px<br clear="all"><br>\
				y: <input id="group_textoffsety" size="3" style="width: 45px; float: right;" value="0" type="text"/> px\
			</div>',
			style: {height: '200px'},
			onConfirm: function(){
				var tempoutlined = $('.outlined' ).toArray();
				var outlined = [];
				for (var i=0; i<tempoutlined.length; i++) {
					if (pb.getItemById(tempoutlined[i].id).panel) outlined.push(tempoutlined[i]);
				}
				for (var i=0; i<outlined.length; i++){
					pb.getItemById(outlined[i].id).textnode.style.marginLeft = $('#group_textoffsetx').val() + 'px';
					pb.getItemById(outlined[i].id).textoffsetx = $('#group_textoffsetx').val();
					pb.getItemById(outlined[i].id).textnode.style.marginTop = -$('#group_textoffsety').val() + 'px';
					pb.getItemById(outlined[i].id).textoffsety = $('#group_textoffsety').val();
				}
				pb.closeDialog();
			},
			init: function(){
				if (!isNaN(parseInt(pb.getItemById($('.outlined')[0].id).textnode.style.marginLeft)))
					$("#group_textoffsetx" ).val(parseInt(pb.getItemById($('.outlined')[0].id).textnode.style.marginLeft));
						else $("#group_textoffsetx" ).val('0');
				if (!isNaN(parseInt(pb.getItemById($('.outlined')[0].id).textnode.style.marginTop)))
					$("#group_textoffsety" ).val(-parseInt(pb.getItemById($('.outlined')[0].id).textnode.style.marginTop));
						else $("#group_textoffsety" ).val('0');
			}
		});
	});
	$('#setwidth-button').click(function(){
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		pb.openDialog({
			content: '<span style="font-size: 20px;">Enter new width for these items:</span><br><br>'+
			'<div style="position: relative; width: 100%; text-align: center;">\
				<input id="group_width" size="3" style="width: 45px;" value="0" type="text"/> px\
			</div>',
			style: {height: '150px'},
			onConfirm: function(){
				
				var tempoutlined = $('.outlined' ).toArray();
				var outlined = [];
				var outlineditems = [];
				var outlinedwhs = [];
				var outlinedwhsb = [];
				for (var i=0; i<tempoutlined.length; i++) {
					if (pb.getItemById(tempoutlined[i].id).panel){
						outlined.push(tempoutlined[i]);
						outlineditems.push(pb.getItemById(tempoutlined[i].id));
						outlinedwhs.push({width: tempoutlined[i].style.width,
							height: tempoutlined[i].style.height});
					}
				}
				
				$(".outlined" ).css({width: $('#group_width').val() + 'px'});
				
				var taction = new Action({
					type: 'size',
					items: outlineditems,
					oldvalues: outlinedwhs
				});
				
				for (var i=0; i<outlined.length; i++){
					var panelbb = pb.project.currentpanel.element.getBoundingClientRect();
					var buttonbb = outlined[i].getBoundingClientRect();
					if (parseInt(outlined[i].style.width) > parseInt(pb.project.currentpanel.element.style.width)){
						outlined[i].style.width = pb.project.currentpanel.element.style.width;
					}
					if (parseInt(outlined[i].style.height) > parseInt(pb.project.currentpanel.element.style.height)){
						outlined[i].style.height = pb.project.currentpanel.element.style.height;
					}
					
					if (buttonbb.right > panelbb.right){
						outlined[i].style.left = parseInt(pb.project.currentpanel.element.style.width) - parseInt(outlined[i].style.width) + 'px';
					}
					if (buttonbb.bottom > panelbb.bottom){
						outlined[i].style.top = parseInt(pb.project.currentpanel.element.style.height) - parseInt(outlined[i].style.height) + 'px';
					}
					
					outlinedwhsb.push({width: outlined[i].style.width, height: outlined[i].style.height});
					
				}
				
				
				taction.newvalues = outlinedwhsb;
				pb.saveaction(taction);
				
				pb.closeDialog();
				
			},
			init: function(){
				$("#group_width" ).val(parseInt($('.outlined')[0].style.width));
			}
		});
	});
	$('#setheight-button').click(function(){
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		pb.openDialog({
			content: '<span style="font-size: 20px;">Enter new height for these items:</span><br><br>'+
			'<div style="position: relative; width: 100%; text-align: center;">\
				<input id="group_height" size="3" style="width: 45px;" value="0" type="text"/> px\
			</div>',
			style: {height: '150px'},
			onConfirm: function(){
				
				var tempoutlined = $('.outlined' ).toArray();
				
				var outlined = [];
				var outlineditems = [];
				var outlinedwhs = [];
				var outlinedwhsb = [];
				
				for (var i=0; i<tempoutlined.length; i++) {
					if (pb.getItemById(tempoutlined[i].id).panel){
						outlined.push(tempoutlined[i]);
						outlineditems.push(pb.getItemById(tempoutlined[i].id));
						outlinedwhs.push({width: tempoutlined[i].style.width, height: tempoutlined[i].style.height});
						//console.log(outlinedwhs[i]);
					}
				}
				
				$(".outlined" ).css({height: $('#group_height').val() + 'px'});
				
				var taction = new Action({
					type: 'size',
					items: outlineditems,
					oldvalues: outlinedwhs
				});
				
				for (var i=0; i<outlined.length; i++){
					var panelbb = pb.project.currentpanel.element.getBoundingClientRect();
					var buttonbb = outlined[i].getBoundingClientRect();
					if (parseInt(outlined[i].style.width) > parseInt(pb.project.currentpanel.element.style.width)){
						outlined[i].style.width = pb.project.currentpanel.element.style.width;
					}
					if (parseInt(outlined[i].style.height) > parseInt(pb.project.currentpanel.element.style.height)){
						outlined[i].style.height = pb.project.currentpanel.element.style.height;
					}
					if (buttonbb.right > panelbb.right){
						outlined[i].style.left = parseInt(pb.project.currentpanel.element.style.width) - parseInt(outlined[i].style.width) + 'px';
					}
					if (buttonbb.bottom > panelbb.bottom){
						outlined[i].style.top = parseInt(pb.project.currentpanel.element.style.height) - parseInt(outlined[i].style.height) + 'px';
					}
					outlinedwhsb.push({width: outlined[i].style.width, height: outlined[i].style.height});
					
				}
				
				
				taction.newvalues = outlinedwhsb;
				pb.saveaction(taction);
				
				pb.closeDialog();
				
			}
		});
	});
	$('#centerh-button').click(function(){
	
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		
		var tempoutlined = $('.outlined').toArray();
		var outlined = [];
		var outlineditems = [];
		var outlinedwhs = [];
		var outlinedwhsb = [];
		
		for (var i=0; i<tempoutlined.length; i++) {
			if (pb.getItemById(tempoutlined[i].id).panel){
				outlined.push(tempoutlined[i]);
				outlineditems.push(pb.getItemById(tempoutlined[i].id));
				outlinedwhs.push({top: tempoutlined[i].style.top,
					left: tempoutlined[i].style.left});
			}
		}
		
		var taction = new Action({
			type: 'move',
			items: outlineditems,
			oldvalues: outlinedwhs
		});

		for (var i=0;i<outlined.length;i++){
			$(outlined[i]).css({left: ((parseInt(pb.project.currentpanel.element.style.width)/2) - parseInt(outlined[i].style.width)/2) + 'px'});
			
			outlinedwhsb.push({top: outlined[i].style.top, left: outlined[i].style.left});
					
		}
		
		taction.newvalues = outlinedwhsb;
		pb.saveaction(taction);
		
	});
	$('#centerv-button').click(function(){
	
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		
		var tempoutlined = $('.outlined' ).toArray();
		var outlined = [];
		var outlineditems = [];
		var outlinedwhs = [];
		var outlinedwhsb = [];
		for (var i=0; i<tempoutlined.length; i++) {
			if (pb.getItemById(tempoutlined[i].id).panel){
				outlined.push(tempoutlined[i]);
				outlineditems.push(pb.getItemById(tempoutlined[i].id));
				outlinedwhs.push({top: tempoutlined[i].style.top, left: tempoutlined[i].style.left});
			}
		}
		
		var taction = new Action({
			type: 'move',
			items: outlineditems,
			oldvalues: outlinedwhs
		});
		for (var i=0;i<outlined.length;i++){
			$(outlined[i]).css({top: ((parseInt(pb.project.currentpanel.element.style.height)/2) - parseInt(outlined[i].style.height)/2) + 'px'});
			outlinedwhsb.push({top: outlined[i].style.top, left: outlined[i].style.left});
					
		}
		
		taction.newvalues = outlinedwhsb;
		pb.saveaction(taction);
		
	});
	$('#centerb-button').click(function(){
	
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		
		var tempoutlined = $('.outlined' ).toArray();
		var outlined = [];
		var outlineditems = [];
		var outlinedwhs = [];
		var outlinedwhsb = [];
		
		for (var i=0; i<tempoutlined.length; i++) {
			if (pb.getItemById(tempoutlined[i].id).panel){
				outlined.push(tempoutlined[i]);
				outlineditems.push(pb.getItemById(tempoutlined[i].id));
				outlinedwhs.push({top: tempoutlined[i].style.top, left: tempoutlined[i].style.left});
			}
		}
		
		var taction = new Action({
			type: 'move',
			items: outlineditems,
			oldvalues: outlinedwhs
		});
		
		$(".outlined" ).css({left: (((parseInt(pb.project.currentpanel.element.style.width)/2) - parseInt(outlined[0].style.width)/2 - 0) + 0) + 'px'});
		$(".outlined" ).css({top: (((parseInt(pb.project.currentpanel.element.style.height)/2) - parseInt(outlined[0].style.height)/2 - 0) + 0) + 'px'});
		
		for (var i=0;i<outlined.length;i++){
		
			outlinedwhsb.push({top: outlined[i].style.top, left: outlined[i].style.left});
					
		}
		
		taction.newvalues = outlinedwhsb;
		pb.saveaction(taction);
	
	});
	$('#text-button').click(function(){
	
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		
		pb.openDialog({
			content: 'Enter the new text for these items:<br><br>'+
				'<input id="group_text" size="3" style="width: 135px;" value="" type="text"/>',
			onConfirm: function(){
				var outlined = $('.outlined' ).toArray();
				for (var i=0; i<outlined.length; i++){
					pb.getItemById(outlined[i].id).textnode.lastChild.innerHTML = $('#group_text').val();
				}
				pb.closeDialog();
			},
			init: function(){
				var outlined = $('.outlined' ).toArray();
				$('#group_text').val(pb.getItemById(outlined[0].id).textnode.lastChild.innerHTML);
			}
		});
			
	});
	$('#copysize-button').click(function(){
	
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		
		var tempoutlined = $('.outlined' ).toArray();
		var outlined = [];
		
		for (var i=0; i<tempoutlined.length; i++) {
			if (pb.getItemById(tempoutlined[i].id).panel) outlined.push(tempoutlined[i]);
		}
		for (var i=0; i<outlined.length; i++){
			outlined[i].style.width = $('.first')[0].style.width;
			outlined[i].style.height = $('.first')[0].style.height;
		}
		
		var outlined = $('.outlined' ).toArray();
		
		for (var i=0; i<outlined.length; i++){
			var panelbb = pb.project.currentpanel.element.getBoundingClientRect();
			var buttonbb = outlined[i].getBoundingClientRect();
			if (parseInt(outlined[i].style.width) > parseInt(pb.project.currentpanel.element.style.width)){
				outlined[i].style.width = pb.project.currentpanel.element.style.width;
			}
			if (parseInt(outlined[i].style.height) > parseInt(pb.project.currentpanel.element.style.height)){
				outlined[i].style.height = pb.project.currentpanel.element.style.height;
			}
			if (buttonbb.right > panelbb.right){
				outlined[i].style.left = parseInt(pb.project.currentpanel.element.style.width) - parseInt(outlined[i].style.width) + 'px';
			}
			if (buttonbb.bottom > panelbb.bottom){
				outlined[i].style.top = parseInt(pb.project.currentpanel.element.style.height) - parseInt(outlined[i].style.height) + 'px';
			}
			pb.getItemById(outlined[i].id).centerText();
		}
		
		pb.project.currentpanel.resize();
		
	});
	$('#clone-button').click(function(){
	
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		
		var tempoutlined = $('.outlined' ).toArray();
		var outlined = [];
		var noutlined = [];
		
		for (var i=0; i<tempoutlined.length; i++) {
			if (pb.getItemById(tempoutlined[i].id).panel) outlined.push(tempoutlined[i]);
		}
		
		for (var i=0; i<outlined.length; i++){
		
			var clone = pb.getItemById(outlined[i].id).clone({
				extrastyle: 'left: '+(parseFloat(pb.getItemById(outlined[i].id).element.style.left)+20)
				+'px; top: '+(parseFloat(pb.getItemById(outlined[i].id).element.style.top)+20)+'px;'});
			pb.project.currentpanel.addChild(clone);
			var o = 0;
			var bbox = pb.project.currentpanel.element.getBoundingClientRect();
			
			clone.element.style.zIndex = 2000;
			clone.element.className = clone.element.className.replace('cloneable','');
			clone.container = pb.project.currentpanel.element;
			clone.texteditable = true;
			clone.element.className += ' draggable ';
			
			var elebox = clone.element.getBoundingClientRect();
			if (elebox.left < bbox.left) clone.element.style.left = '0px';
			if (elebox.top < bbox.top) clone.element.style.top = pb.project.currentpanel.element.style.top;
			if (elebox.right > bbox.right) clone.element.style.left =
				$(pb.project.currentpanel.element).outerWidth() - $(clone.element).outerWidth() + o + 'px';
			if (elebox.bottom > bbox.bottom) clone.element.style.top =
				$(pb.project.currentpanel.element).outerHeight() - $(clone.element).outerHeight() + o + 'px';
			clone.responsive = true;
			noutlined.push(clone);
			
		}
		
		$('.outlined').removeClass('outlined').removeClass('first');
		for (var i=0; i<noutlined.length; i++){
		
			$(noutlined[i].element).addClass('outlined');
			
		}
		
		var taction = new Action({
			type: 'clone',
			items: noutlined,
		});

		pb.saveaction(taction);
		
	});
	$('#fit-button').click(function(){
		if (pb.project.currentpanel){
			var tpanelwidth = parseInt(pb.project.currentpanel.element.style.width);
			var tpanelheight = parseInt(pb.project.currentpanel.element.style.height);
			var twindowwidth = window.innerWidth;
			var twindowheight = window.innerHeight-150;
			
			var s = (twindowwidth/tpanelwidth);
			var ts = (twindowheight/tpanelheight);
			
			s = ts;
			
			pb.project.currentpanel.core.scale = s;
			$(pb.project.currentpanel.element).css({
				'transform': 'scale('+pb.project.currentpanel.core.scale+
					','+pb.project.currentpanel.core.scale+')',
				'-webkit-transform': 'scale('+pb.project.currentpanel.core.scale+
					','+pb.project.currentpanel.core.scale+')',
				'-moz-transform': 'scale('+pb.project.currentpanel.core.scale+
					','+pb.project.currentpanel.core.scale+')',
				'-ms-transform': 'scale('+pb.project.currentpanel.core.scale+
					','+pb.project.currentpanel.core.scale+')',
			});
				
			var tml = (twindowwidth/2) - (tpanelwidth/2);
			pb.project.currentpanel.element.style.marginTop = (-tpanelheight*(1-s)/2) + 25 + 'px';		
			pb.project.currentpanel.element.style.marginLeft = tml + 'px';
			$('.panel-space-center').css({left: '0px'});
			
		}
	});
//*************************** END OF BATCH EDIT ICONS ***************************//
	
	$(".icon").not("#beditor-button").not("#peditor-button").hover(function(){
		$(this).css("opacity","1.0");
	},function(){
		$(this).css("opacity","0.9");
	});
	
	$(".arrowicon").hover(function(){
		$(this).css("opacity","1.0");
	},function(){
		$(this).css("opacity","0.9");
	});
	
	$("#beditor-button").hover(function(){
		if ($(".outlined").length == 1 && pb.getItemById($('.outlined')[0].id) instanceof Button)
			$(this).css("opacity","1.0");
	},function(){
		$(this).css("opacity","0.9");
	});
	
	$("#beditor-button").click(function(){
		if ($(".outlined").length == 1 && pb.getItemById($('.outlined')[0].id) instanceof Button)
			openItemEditor({item: pb.getItemById($('.outlined')[0].id)});
		else if ($(".outlined").length == 0){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		else if ($(".outlined").length > 1){
			pb.openDialog({
				content: '<center>Too many items selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
	});
	
	$("#weditor-button").hover(function(){
		if ($(".outlined").length == 1 && pb.getItemById($('.outlined')[0].id) instanceof Widget)
			$(this).css("opacity","1.0");
	},function(){
		$(this).css("opacity","0.9");
	});
	
	$("#weditor-button").click(function(){
		if ($(".outlined").length == 1 && pb.getItemById($('.outlined')[0].id) instanceof Widget)
			openWidgetEditor(pb.getItemById($('.outlined')[0].id));
	});
	
	$("#peditor-button").hover(function(){
		if (pb.project.currentpanel)
			$(this).css("opacity","1.0");
	},function(){
		$(this).css("opacity","0.9");
	});
	
	$("#peditor-button").click(function(){
		if (pb.project.currentpanel)
			openItemEditor(pb.project.currentpanel, true, true);
	});
	
	$("#seditor-button").hover(function(){
		if (pb.project.currentpanel)
			$(this).css("opacity","1.0");
	},function(){
		$(this).css("opacity","0.9");
	});
	
	$("#seditor-button").click(function(){
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		if ($(".outlined").length > 1){
			pb.openDialog({
				content: '<center>Too many items selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		var titem = pb.getItemById($('.outlined')[0].id);
		if (titem instanceof Button || titem instanceof Widget){
			openTextEditor({item: pb.getItemById($('.outlined')[0].id), mode: 'full'});
		}
		else if (titem instanceof Widget){
			switch (titem.type){
				case 'hslider': case 'vslider': case 'numpad':
				case 'dial': case 'dial2':
					pb.openDialog({
						content: 'Edit Script/Conditional:<br><br>'+
						'Change this item\'s script: <input id="item_script" style="" value="View" type="button"/><br />\
						Change this item\'s conditional: <input id="item_conditional" style="" value="View" type="button"/>',
						confirm: false,
						init: function(){
							$('#item_script').click(function(){
								openTextEditor('script',pb.getItemById($('.outlined')[0].id), 0);
							});
							$('#item_conditional').click(function(){
								openTextEditor('conditional',pb.getItemById($('.outlined')[0].id), 0);
							});
						}
					});
					break;
				case 'dpad':
					pb.openDialog({
						content: 'Edit Script/Conditional:<br><br>'+
						'Change this item\'s left button script: <input id="item_script" style="" value="View" type="button"/><br />\
						Change this item\'s left button conditional: <input id="item_conditional" style="" value="View" type="button"/>',
						confirm: false,
						init: function(){
							$('#item_script').click(function(){
								openTextEditor('script',pb.getItemById($('.outlined')[0].id).parts[0], 0);
							});
							$('#item_conditional').click(function(){
								openTextEditor('conditional',pb.getItemById($('.outlined')[0].id).parts[0], 0);
							});
						}
					});
				default:
					break;
			}
		}
	});
	
	$(document).delegate('#maintextarea', 'keydown', function(e) {
		var keyCode = e.keyCode || e.which;

		if (keyCode == 9){ //tab support
			e.preventDefault();
			var start = $(this).get(0).selectionStart;
			var end = $(this).get(0).selectionEnd;

			$(this).val($(this).val().substring(0, start) + "\t"
				+ $(this).val().substring(end));

			$(this).get(0).selectionStart =
			$(this).get(0).selectionEnd = start + 1;
		}
	});
	
	$("#delete-button").click(function(){
		if (!$('.outlined').exists()){
			pb.openDialog({
				content: '<center>No item selected to edit.</center>',
				style: {height: '100px'},
				cancel: false,
				confirm: 'ok',
				onConfirm: function(){pb.closeDialog();}
			});
			return;
		}
		var outlined = $('.outlined' ).toArray();
		var naction = new Action({type: 'delete'});
		naction.items = [];
		for (var i=0;i<outlined.length;i++){
			naction.items.push(pb.getItemById(outlined[i].id));
			pb.getItemById(outlined[i].id).remove();
		}
		$('.outlined').removeClass('outlined first');
		pb.saveaction(naction);
	});
	
	$("#undo-button").click(function(){
		pb.undo();
	});
	
	$("#redo-button").click(function(){
		pb.redo();
	});

	$('body').keydown(function(e){
        if(e.keyCode == 16){
            pb.shiftdown = true;
        }
    });
	
    $('body').keyup(function(e){
        if(e.keyCode == 16){
            pb.shiftdown = false;
        }
    });
	
	$("#filemenu-button").click(function(){
		if (document.getElementById('filemenu-menu').style.display == 'none') openMenu('file');
		else closeMenu('file');
	});
	$("#editmenu-button").click(function(){
		if (document.getElementById('editmenu-menu').style.display == 'none') openMenu('edit');
		else closeMenu('edit');
	});
	$("#addmenu-button").click(function(){
		if (document.getElementById('addmenu-menu').style.display == 'none') openMenu('add');
		else closeMenu('add');
	});
	$("#viewmenu-button").click(function(){
		if (document.getElementById('viewmenu-menu').style.display == 'none') openMenu('view');
		else closeMenu('view');
	});
	$("#windowmenu-button").click(function(){
		if (document.getElementById('windowmenu-menu').style.display == 'none') openMenu('window');
		else closeMenu('window');
	});
	$("#helpmenu-button").click(function(){
		if (document.getElementById('helpmenu-menu').style.display == 'none') openMenu('help');
		else closeMenu('help');
	});
	
	$(".menu-item" ).hover(
		  function() {
				$(this ).addClass( "menuitemhover" );
		  }, function() {
				$(this ).removeClass( "menuitemhover" );
		  }
	);

}

