/******************************************************************************
@owner: SVSi
@author: Arthur Johnson
@functions: User Interface Utilities
@openMenu
@closeMenu
@addPanel
@loadPanel
@rotatePanelsLeft
@rotatePanelsRight
@updatePanelInfo
@setTempButton
*******************************************************************************/

var alreadyAddedPanel = false;

function addPanel(){

	closeMenu(null, ['file','edit','add','view']);
	
	pb.openDialog({
		type: 'confirm',
		content: '\
			<div style="font-size: 28px;">\
				Add a panel to your project:\
			</div>\
			<div style="position: relative; left: 101px; top: 12px;\
				width: 310px;">\
				Name: &nbsp;<input type="text" name="name" id="addpanel-name"\
					style="width: 200px; float: right;">\
				<br clear="all"><br>\
				Width (px): &nbsp;<input type="text" name="width"\
					id="addpanel-width" style="width: 200px; float: right;">\
				<br clear="all"><br>\
				Height (px): &nbsp;<input type="text" name="height" id="addpanel-height"\
					style="width: 200px; float: right;">\
			</div>\
		',
		confirm: 'add',
		init: function(){
			$('#addpanel-name').val('Panel-'+(pb.panels['panel'].length+1));
			if (pb.project.currentpanel){
				$('#addpanel-width').val(parseInt(pb.project.currentpanel.element.style.width));
				$('#addpanel-height').val(parseInt(pb.project.currentpanel.element.style.height));
			}
			else {
				if (wallcon){
					$('#addpanel-width').val('231');
					$('#addpanel-height').val('321');
				}
				else {
					$('#addpanel-width').val('1024');
					$('#addpanel-height').val('768');
				}
			}
		},
		style:  {
			width: '550px', 
			height: '250px', 
			background: 'black',
			color: 'rgb(238, 238, 238)',
			margin: '15px',
			'padding-top': '20px',
			'padding-bottom': '0px',
			'padding-left': '20px',
			'padding-right': '20px',
			'border-radius': '30px'
		},
		onConfirm: function(){
			
			var name = document.getElementById('addpanel-name').value;
			if (name === '') {
				alert('Name required for panel...');
				return;
			}
			var width = document.getElementById('addpanel-width').value;
				if (width === ''){
					if (wallcon) width = 231;
					else  width = 1024;
				}
			var height = document.getElementById('addpanel-height').value;
				if (height === ''){
					if (wallcon) height = 321;
					else  height = 768;
				}
			
			var newpanel = new Panel({
				name: name, 
				style: 'float: left; width: '+width+'px; height: '+height+'px;\
				position: absolute; left: 0px; border: 1px solid black; background: white;',
				className: 'panel selectables'
			});
			
			$('#labels').append('<div style="overflow: hidden; float: left; width: 70px; height: 27px;\
				background: #f5f5f5; text-align: center; margin-top: 1px; -moz-box-shadow: inset 1px 1px 1px 1px #aaa;\
				-webkit-box-shadow: inset 1px 1px 1px 1px #aaa; box-shadow: inset 1px 1px 1px 1px #aaa;\
				border-radius: 4px; margin-left: 4px; margin-right: 4px; line-height: 26px;" id="'+newpanel.core.id
												+'_label" class="panel_label" title="'+name+'">'+name+'</div>');
												
			$('#'+newpanel.core.id+'_label').css('cursor', 'pointer');

			$('#'+newpanel.core.id+'_label')[0].onclick = function(){
				switchToPanel(newpanel);
			};
				
			pb.closeDialog(function(){$('#dialog-mousecatch').fadeOut();});

			switchToPanel(newpanel);
			
		},
		onCancel: function(){
			pb.closeDialog(function(){$('#dialog-mousecatch').fadeOut();});
		}
	});
	
}

function deletePanel(){

	pb.openDialog({
		content: '<div style="margin: 15px;"><span style="font-size: 19px;">Are you sure you want to delete this panel?</span></div>',
		style: {height: '120px', width: '430px', 'border-radius': '15px'},
		confirm: 'yes',
		cancel: 'no',
		onConfirm: function(){
			$(pb.project.currentpanel.element).remove();

			$('#'+pb.project.currentpanel.core.id+'_label').remove();
			var cpanel = pb.project.currentpanel;
			
			removeFromArray(pb.panels['panel'], cpanel);
			if (pb.panels['panel'].length > 0) switchToPanel(pb.panels['panel'][0]);
			//removeFromArray(pb.panels['panel'], cpanel);//pb.project.currentpanel);
			pb.closeDialog();
		},
		onCancel: function(){pb.closeDialog();}
	});
	
}

function loadPanel(panel){

	if (!panel) return;

	$('.outlined').removeClass('outlined');
	$('.first').removeClass('first');

	if (panel.core){
		panel.core.autoinsert = true;
		var thispanel = new Panel({
			core: panel.core,
			copyid: true
		});
	}
	else
		var thispanel = new Panel({
			name: panel.name,
			id: panel.id,
			scale: panel.scale,
			width: panel.width,
			height: panel.height,
			style: panel.style,
			className: panel.className,
			states: panel.states,
			svgstring: panel.svgstring,
			autoresize: panel.autoresize
		});
	
	$('#labels').append('<div style="overflow: hidden; float: left; width: 70px; height: 27px;\
		background: #f5f5f5; text-align: center; margin-top: 1px; -moz-box-shadow: inset 1px 1px 1px 1px #aaa;\
		-webkit-box-shadow: inset 1px 1px 1px 1px #aaa; box-shadow: inset 1px 1px 1px 1px #aaa;\
		border-radius: 4px; margin-left: 4px; margin-right: 4px; line-height: 26px;" id="'+thispanel.core.id
										+'_label" class="panel_label" title="'+thispanel.core.name+'">'+thispanel.core.name+'</div>');
										
	$('#'+thispanel.core.id+'_label').css('cursor', 'pointer');

	$('#'+thispanel.core.id+'_label')[0].onclick = function(){
		switchToPanel(thispanel);
	};
	
	thispanel.element.style.backgroundSize = "100% 100%";
	thispanel.buttons = [];
	for (var j=0;j<panel.buttons.length;j++){
		if (panel.buttons[j].core){
			var thisbutton = new Button({
				core: panel.buttons[j].core,
				copyid : true
			});
		}
		else {
			if (!panel.buttons[j].states[0].images) panel.buttons[j].states[0].images = [];
			if (!panel.buttons[j].states[1].images) panel.buttons[j].states[1].images = [];
			var thisbutton = new Button({
				id: panel.buttons[j].id,
				name: panel.buttons[j].name,
				textnodeHTML: panel.buttons[j].text,
				textoffsetx: panel.buttons[j].textoffsetx,
				textoffsety: panel.buttons[j].textoffsety,
				textstyle: panel.buttons[j].textstyle,
				scale: panel.buttons[j].scale,
				style: panel.buttons[j].style,
				className: panel.buttons[j].className,
				states: panel.buttons[j].states,
				scripts: panel.buttons[j].scripts,
				conditional: panel.buttons[j].conditional,
				touchpanelkey: panel.buttons[j].touchpanelkey
			});
		}
		thisbutton.element.className = 'draggable';
		thisbutton.element.style.backgroundSize = "100% 100%";

		thispanel.addChild(thisbutton);
		thisbutton.container = thispanel.element;
		thisbutton.makeResizable();
	}
	thispanel.widgets = [];
	for (var j=0;j<panel.widgets.length;j++){
	
		var newparts = [];
		for (var k=0;k<panel.widgets[j].parts.length;k++){
			if (panel.widgets[j].parts[k].core)
				var newpart = new WidgetPart({
					copyid: true,
					core: panel.widgets[j].parts[k].core,
					init: panel.widgets[j].parts[k].init,
					update: panel.widgets[j].parts[k].update,
					moving: panel.widgets[j].parts[k].moving,
					drop: panel.widgets[j].parts[k].drop,
					click: panel.widgets[j].parts[k].click,
					clickup: panel.widgets[j].parts[k].clickup,
					hoverin: panel.widgets[j].parts[k].hoverin,
					hoverout: panel.widgets[j].parts[k].hoverout,
					ontopanel: panel.widgets[j].parts[k].ontopanel,
					resize: panel.widgets[j].parts[k].resize
				});
			else
				var newpart = new WidgetPart({
					name: panel.widgets[j].parts[k].name,
					width: panel.widgets[j].parts[k].width,
					height: panel.widgets[j].parts[k].height,
					scale: panel.widgets[j].parts[k].scale,
					style: panel.widgets[j].parts[k].style,
					drop: panel.widgets[j].parts[k].drop,
					script: panel.widgets[j].parts[k].script,
					conditional: panel.widgets[j].parts[k].conditional,
					click: panel.widgets[j].parts[k].click,
					clickup: panel.widgets[j].parts[k].clickup,
					hoverin: panel.widgets[j].parts[k].hoverin,
					hoverout: panel.widgets[j].parts[k].hoverout,
					init: panel.widgets[j].parts[k].init,
					update: panel.widgets[j].parts[k].update,
					moving: panel.widgets[j].parts[k].moving,
					update: panel.widgets[j].parts[k].update,
					className: panel.widgets[j].parts[k].className,
					id: panel.widgets[j].parts[k].id,
					ontopanel: panel.widgets[j].parts[k].ontopanel,
					fn: panel.widgets[j].parts[k].fn,
					innerhtml: panel.widgets[j].parts[k].innerhtml
				});
			newparts.push(newpart);
		}
	
		if (panel.widgets[j].core)
			var thiswidget = new Widget({
				copyid: true,
				core: panel.widgets[j].core,
				init: panel.widgets[j].init,
				update: panel.widgets[j].update,
				hoverin: panel.widgets[j].hoverin,
				hoverout: panel.widgets[j].hoverout,
				ontopanel: panel.widgets[j].ontopanel,
				resize: panel.widgets[j].resize,
				parts: newparts
			});
		else {
			var thiswidget = new Widget({
				name: panel.widgets[j].name,
				id: panel.widgets[j].id,
				type: panel.widgets[j].type,
				width: panel.widgets[j].width,
				height: panel.widgets[j].height,
				scale: panel.widgets[j].scale,
				style: panel.widgets[j].style,
				basewidth: panel.widgets[j].basewidth,
				baseheight: panel.widgets[j].baseheight,
				basetop: panel.widgets[j].basetop,
				baseleft: panel.widgets[j].baseleft,
				init: panel.widgets[j].init,
				update: panel.widgets[j].update,
				className: panel.widgets[j].className,
				states: panel.widgets[j].states,
				innerhtml: panel.widgets[j].innerhtml,
				svgstring: panel.widgets[j].svgstring,
				parts: newparts,
				script: panel.widgets[j].script,
				conditional: panel.widgets[j].conditional,
				resize: panel.widgets[j].resize,
				vars: panel.widgets[j].vars,
				aspectratio: panel.widgets[j].aspectratio
			});
		}
		thiswidget.dropped = true;
		thiswidget.element.className = 'draggable';
		thiswidget.makeResizable();
		thiswidget.container = thispanel.element;
		thispanel.addChild(thiswidget);
		thiswidget.ontopanel();
	}
	//console.log(thispanel);
	switchToPanel(thispanel);
	
}

function searchArrayById(array, id){
	for (var i=0; i<array.length; i++){
		//console.log(array[i].core.id, id);
		if (array[i].core.id == id) return i;
	}
	return -1;
}

function switchToPanel(panel, fit){

	$('.outlined').removeClass('outlined');
	$('.first').removeClass('first');

	if (pb.project.currentpanel == panel) return;
	var oind = 0;
	var nind = 1;
	if (pb.project.currentpanel){
		oind = searchArrayById(pb.panels['panel'], pb.project.currentpanel.element.id);
		nind =  searchArrayById(pb.panels['panel'], panel.element.id);
	}
	//console.log('indexes', nind, oind);
	
	pb.project.currentpanel = panel;
	pb.project.currentpanel.element.style.marginLeft = '0px';
	
	if (pb.project.currentpanel){
		if (pb.panels['panel'][pb.panels['panel'].indexOf(pb.project.currentpanel)]){
			var amount = (pb.panels['panel'].indexOf(pb.project.currentpanel)) * (-78);
			tweencss({object: $( '#labels' )[0], time: 0.5,
				props: {left: parseInt($( '#labels' )[0].style.left)}, to: {left: (234+amount)}});
		} 
	} 
	
	document.getElementById('panel-props-name').value = pb.project.currentpanel.core.name;
	document.getElementById('panel-props-width').value = parseInt(pb.project.currentpanel.element.style.width);
	document.getElementById('panel-props-height').value = parseInt(pb.project.currentpanel.element.style.height);

	if (nind - oind > 0){
	
		var panelspaceR = $('.panel-space-right')[0];
		while (panelspaceR.lastChild) {
			panelspaceR.removeChild(panelspaceR.lastChild);
		}
		$('.panel-space-right').append(panel.element);

		$(".panel_label").css({opacity: 0.3});
		$("#"+pb.project.currentpanel.core.id +"_label").css({opacity: 1.0});

		$('.panel-space-right, .panel-space-center, .panel-space-left' ).css({display: 'block'});
	
		tweencss({object: $('.panel-space-right')[0], time: 1,
				props: {opacity: $('.panel-space-right')[0].style.opacity, left: ($(window).innerWidth()/2)+($(window).innerWidth()/3.0), rotationY: Math.PI/2}, 
				to: {opacity: 1, left: (-$($( '.panel-space-right' )[0].lastChild).outerWidth()/2 + $(window).innerWidth()/2), rotationY: 0},
				transformPerspective: 16500, transformOrigin:"center center -1500", transformStyle: 'preserve-3d'});
		tweencss({object: $('.panel-space-center')[0], time: 1,
				props: {opacity: $('.panel-space-center')[0].style.opacity, left: parseInt($('.panel-space-center')[0].style.left), rotationY: 0}, 
				to: {opacity: 0, left: ($(window).innerWidth()/2)-($(window).innerWidth()/2.7), rotationY: -Math.PI/2},
				transformPerspective: 16500, transformOrigin:"center center -1500", transformStyle: 'preserve-3d'});
		tweencss({object: $('.panel-space-left')[0], time: 1,
				props: {opacity: $('.panel-space-left')[0].style.opacity, left: ($(window).innerWidth()/2)-($(window).innerWidth()/3.0), rotationY: -Math.PI/2}, 
				to: {opacity: 0, left: ($(window).innerWidth()/2)+($(window).innerWidth()/2.7), rotationY: Math.PI/2},
				transformPerspective: 16500, transformOrigin:"center center -1500", transformStyle: 'preserve-3d',
				onComplete: function(){$( '.panel-space-right, .panel-space-left' ).css({display: 'none'});}});
		
		setTimeout(function(){
			$( '.panel-space-right, .panel-space-left' ).css({left: '-90000px', display: 'none'});
		}, 1000);
		
		var right = $('.panel-space-right');
		var center = $('.panel-space-center');
		var left = $('.panel-space-left' );
		right.addClass('panel-space-center').removeClass('panel-space-right');
		center.addClass('panel-space-left').removeClass('panel-space-center');
		left.addClass('panel-space-right').removeClass('panel-space-left');
				
	}
	else {
	
		var panelspaceR = $('.panel-space-left')[0];
		while (panelspaceR.lastChild) {
			panelspaceR.removeChild(panelspaceR.lastChild);
		}
		$('.panel-space-left').append(panel.element);

		$(".panel_label").css({opacity: 0.3});
		$("#"+pb.project.currentpanel.core.id +"_label").css({opacity: 1.0});

		$('.panel-space-right, .panel-space-center, .panel-space-left' ).css({display: 'block'});
	
		tweencss({object: $('.panel-space-right')[0], time: 1,
				props: {opacity: $('.panel-space-right')[0].style.opacity, left: ($(window).innerWidth()/2)+($(window).innerWidth()/3.0), rotationY: Math.PI/2}, 
				to: {opacity: 0, left: ($(window).innerWidth()/2)-($(window).innerWidth()/2.7), rotationY: -Math.PI/2},
				transformPerspective: 16500, transformOrigin:"center center -1500", transformStyle: 'preserve-3d'});
		tweencss({object: $('.panel-space-center')[0], time: 1,
				props: {opacity: $('.panel-space-center')[0].style.opacity, left: parseInt($('.panel-space-center')[0].style.left), rotationY: 0}, 
				to: {opacity: 0, left: ($(window).innerWidth()/2)+($(window).innerWidth()/2.7), rotationY: Math.PI/2},
				transformPerspective: 16500, transformOrigin:"center center -1500", transformStyle: 'preserve-3d'});
		tweencss({object: $('.panel-space-left')[0], time: 1,
				props: {opacity: $('.panel-space-left')[0].style.opacity, left: ($(window).innerWidth()/2)-($(window).innerWidth()/3.0), rotationY: -Math.PI/2}, 
				to: {opacity: 1, left: (-$($( '.panel-space-left' )[0].lastChild).outerWidth()/2 + $(window).innerWidth()/2), rotationY: 0},
				transformPerspective: 16500, transformOrigin:"center center -1500", transformStyle: 'preserve-3d',
				onComplete: function(){$( '.panel-space-right, .panel-space-left' ).css({display: 'none'});}});
				
		setTimeout(function(){
			$( '.panel-space-right, .panel-space-left' ).css({left: '-90000px', display: 'none'});
		}, 1000);
				
		var right = $( '.panel-space-right' );
		var center = $( '.panel-space-center' );
		var left = $( '.panel-space-left' );
		right.addClass('panel-space-left').removeClass('panel-space-right');
		center.addClass('panel-space-right').removeClass('panel-space-center');
		left.addClass('panel-space-center').removeClass('panel-space-left');
		
	}
	 
	if (oldIE) $( '.panel-space-right, .panel-space-left' ).css({display: 'none'});
	
	var drop = pb.project.currentpanel.element;
	  
	addEventHandler(drop, 'dragstart', cancelEvent);
	addEventHandler(drop, 'dragover', cancelEvent);
	addEventHandler(drop, 'dragenter', cancelEvent);
	addEventHandler(drop, 'drop', function (e){
	
		e = e || window.event;   
		if (e.preventDefault) { e.preventDefault(); } 
		
		if (pb.mouse.elementover != pb.project.currentpanel.element) return;
		
		var dt = e.dataTransfer;
		var files = dt.files;
		
		var file = files[0];
		//console.log('fill', file);
		var fd = new FormData();
		fd.append("image", file);
		//console.log('fd', fd);
		
		var opts = {
			command: 'write_image'
		};
		
		if (pb.project.puid) opts.puid = pb.project.puid;
		
		fd.append("opts", JSON.stringify(opts));

		server({formdata: fd, callback: function(data){
			//console.log('fills', data);
			var tstr = data;
			var fstr = tstr.substring(tstr.indexOf('projects/')+9, tstr.indexOf('/images/'));
			pb.project.puid = fstr;
		
			pb.project.currentpanel.element.style.backgroundImage = 'url(' + data + ')';
			pb.project.currentpanel.element.style.backgroundSize = '100% 100%';
			
			var found = false;
			for (var i=0; i<pb.assets['user'].length; i++){
				if (pb.assets['user'][i].core.data == data) found = true;
			}
			
			if (!found){
				var newasset = new Asset({data: data});
				newasset.element.style.backgroundImage = 'url(' + data + ')';
				newasset.element.style.backgroundSize = '100% 100%';
				newasset.element.className += ' cloneable draggable';
				$('#assets-images-pane-list').append(newasset.element);
			}
			
			pb.checkDiskSize();
		}});
		
		return false;
	});
	Function.prototype.bindToEventHandler = function bindToEventHandler() {
	  var handler = this;
	  var boundParameters = Array.prototype.slice.call(arguments);
	  return function(e) {
		  e = e || window.event;   
		  boundParameters.unshift(e);
		  handler.apply(this, boundParameters);
	  }
	};
	
	pb.updateTree();
	
	if (pb.project.currentpanel.core.autoresize) $('#panel-props-autoresize').prop('checked', true);
	else $('#panel-props-autoresize').prop('checked', false);
	
	if (pb.project.currentpanel.core.defaultpanel) $('#panel-props-defaultpanel').prop('checked', true);
	else $('#panel-props-defaultpanel').prop('checked', false);
	
	$('#panel-bgColorSelector div').css({backgroundColor: pb.project.currentpanel.element.style.backgroundColor});
	
	$(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+')'
	});
	
	if (fit) $('#fit-button').click();

}

function rotatePanelsLeft(){

	if (pb.project.currentpanel){
		if (pb.panels['panel'][pb.panels['panel'].indexOf(pb.project.currentpanel)-1]) {
			pb.project.currentpanel = pb.panels['panel'][pb.panels['panel'].indexOf(pb.project.currentpanel)-1];
			pb.project.currentpanel.element.style.marginLeft = '0px';
			var amount = (pb.panels['panel'].indexOf(pb.project.currentpanel)) * (-78);
			TWEEN.removeAll();
			tweencss({object: $( '#labels' )[0], time: 0.5, props: {left: parseInt($( '#labels' )[0].style.left)}, to: {left: (234+amount)}});
		} else return;
	} else return;
	
	$(".panel_label").css({opacity: 0.3});
	$("#"+pb.project.currentpanel.core.id+"_label").css({opacity: 1.0});
	
	document.getElementById('panel-props-name').value = pb.project.currentpanel.core.name;
	document.getElementById('panel-props-width').value = parseInt(pb.project.currentpanel.element.style.width);
	document.getElementById('panel-props-height').value = parseInt(pb.project.currentpanel.element.style.height);
	
	var panelspaceL = $('.panel-space-left')[0];
	while (panelspaceL.lastChild) {
		panelspaceL.removeChild(panelspaceL.lastChild);
	}
	$('.panel-space-left').append(pb.project.currentpanel.element);

	$( '.panel-space-right, .panel-space-center, .panel-space-left' ).css({display: 'block'});
	$('.panel-space-right').css({opacity: 0});
	$('.panel-space-left').css({opacity: 0});
	
	tweencss({object: $('.panel-space-right')[0], time: 1,
				props: {/*scaleY: 1.0, */opacity: $('.panel-space-right')[0].style.opacity, left: ($(window).innerWidth()/2)+($(window).innerWidth()/3.0), rotationY: Math.PI/2}, 
				to: {/*scaleY: 0.3, */opacity: 0, left: ($(window).innerWidth()/2)-($(window).innerWidth()/2.7), rotationY: -Math.PI/2},
				transformPerspective: 16500, transformOrigin:"center center -1500", transformStyle: 'preserve-3d'});
	tweencss({object: $('.panel-space-center')[0], time: 1,
				props: {/*scaleY: 1.0, */opacity: $('.panel-space-center')[0].style.opacity, left: parseInt($('.panel-space-center')[0].style.left), rotationY: 0}, 
				to: {/*scaleY: 0.3, */opacity: 0, left: ($(window).innerWidth()/2)+($(window).innerWidth()/2.7), rotationY: Math.PI/2},
				transformPerspective: 16500, transformOrigin:"center center -1500", transformStyle: 'preserve-3d'});
	tweencss({object: $('.panel-space-left')[0], time: 1,
				props: {/*scaleY: 0.3, */opacity: $('.panel-space-left')[0].style.opacity, left: ($(window).innerWidth()/2)-($(window).innerWidth()/3.0), rotationY: -Math.PI/2}, 
				to: {/*scaleY: 1.0, */opacity: 1, left: (-$($( '.panel-space-left' )[0].lastChild).outerWidth()/2 + $(window).innerWidth()/2), rotationY: 0},
				transformPerspective: 16500, transformOrigin:"center center -1500", transformStyle: 'preserve-3d',
				onComplete: function(){$( '.panel-space-right, .panel-space-left' ).css({display: 'none'});}});
	
	setTimeout(function(){
		$( '.panel-space-right, .panel-space-left' ).css({left: '-90000px', display: 'none'});
	}, 1000);
	
	var right = $( '.panel-space-right' );
	var center = $( '.panel-space-center' );
	var left = $( '.panel-space-left' );
	right.addClass('panel-space-left').removeClass('panel-space-right');
	center.addClass('panel-space-right').removeClass('panel-space-center');
	left.addClass('panel-space-center').removeClass('panel-space-left');
	
	if (oldIE) $( '.panel-space-right, .panel-space-left' ).css({display: 'none'});

	$('.outlined').removeClass('outlined');
	$('.first').removeClass('first');
	
	if (pb.project.currentpanel.core.autoresize) $('#panel-props-autoresize').prop('checked', true);
	else $('#panel-props-autoresize').prop('checked', false);
	
	if (pb.project.currentpanel.core.defaultpanel) $('#panel-props-defaultpanel').prop('checked', true);
	else $('#panel-props-defaultpanel').prop('checked', false);
	
	$('#panel-bgColorSelector div').css({backgroundColor: pb.project.currentpanel.element.style.backgroundColor});
}

function rotatePanelsRight(){
	
	if (pb.project.currentpanel){
		if (pb.panels['panel'][pb.panels['panel'].indexOf(pb.project.currentpanel)+1]){
			pb.project.currentpanel = pb.panels['panel'][pb.panels['panel'].indexOf(pb.project.currentpanel)+1];
			pb.project.currentpanel.element.style.marginLeft = '0px';
			var amount = (pb.panels['panel'].indexOf(pb.project.currentpanel)) * (-78);
			TWEEN.removeAll();
			tweencss({object: $( '#labels' )[0], time: 0.5, props: {left: parseInt($( '#labels' )[0].style.left)}, to: {left: (234+amount)}});
		} else return;
	} else return;
	
	$(".panel_label").css({opacity: 0.3});
	$("#"+pb.project.currentpanel.core.id+"_label").css({opacity: 1.0});
	
	document.getElementById('panel-props-name').value = pb.project.currentpanel.core.name;
	document.getElementById('panel-props-width').value = parseInt(pb.project.currentpanel.element.style.width);
	document.getElementById('panel-props-height').value = parseInt(pb.project.currentpanel.element.style.height);
	
	var panelspaceR = $('.panel-space-right')[0];
	while (panelspaceR.lastChild) {
		panelspaceR.removeChild(panelspaceR.lastChild);
	}
	$('.panel-space-right').append(pb.project.currentpanel.element);

	$( '.panel-space-right, .panel-space-center, .panel-space-left' ).css({display: 'block'});
	$('.panel-space-right').css({opacity: 0});
	$('.panel-space-left').css({opacity: 0});

	tweencss({object: $('.panel-space-right')[0], time: 1,
				props: {/*scaleY: 0.3, */opacity: $('.panel-space-right')[0].style.opacity, left: ($(window).innerWidth()/2)+($(window).innerWidth()/3.0), rotationY: Math.PI/2}, 
				to: {/*scaleY: 1.0, */opacity: 1, left: (-$($( '.panel-space-right' )[0].lastChild).outerWidth()/2 + $(window).innerWidth()/2), rotationY: 0},
				transformPerspective: 16500, transformOrigin:"center center -1500", transformStyle: 'preserve-3d'});
	tweencss({object: $('.panel-space-center')[0], time: 1,
				props: {/*scaleY: 1.0, */opacity: $('.panel-space-center')[0].style.opacity, left: parseInt($('.panel-space-center')[0].style.left), rotationY: 0}, 
				to: {/*scaleY: 0.3, */opacity: 0, left: ($(window).innerWidth()/2)-($(window).innerWidth()/2.7), rotationY: -Math.PI/2},
				transformPerspective: 16500, transformOrigin:"center center -1500", transformStyle: 'preserve-3d'});
	tweencss({object: $('.panel-space-left')[0], time: 1,
				props: {/*scaleY: 1.0, */opacity: $('.panel-space-left')[0].style.opacity, left: ($(window).innerWidth()/2)-($(window).innerWidth()/3.0), rotationY: -Math.PI/2}, 
				to: {/*scaleY: 0.3, */opacity: 0, left: ($(window).innerWidth()/2)+($(window).innerWidth()/2.7), rotationY: Math.PI/2},
				transformPerspective: 16500, transformOrigin:"center center -1500", transformStyle: 'preserve-3d',
				onComplete: function(){$( '.panel-space-right, .panel-space-left' ).css({display: 'none'});}});
				
	setTimeout(function(){
		$( '.panel-space-right, .panel-space-left' ).css({left: '-90000px', display: 'none'});
	}, 1000);
	
	var right = $( '.panel-space-right' );
	var center = $( '.panel-space-center' );
	var left = $( '.panel-space-left' );
		right.addClass('panel-space-center').removeClass('panel-space-right');
		center.addClass('panel-space-left').removeClass('panel-space-center');
		left.addClass('panel-space-right').removeClass('panel-space-left');
	
	if (oldIE) $( '.panel-space-right, .panel-space-left' ).css({display: 'none'});
	
	$('.outlined').removeClass('outlined');
	$('.first').removeClass('first');
	
	if (pb.project.currentpanel.core.autoresize) $('#panel-props-autoresize').prop('checked', true);
	else $('#panel-props-autoresize').prop('checked', false);
	
	if (pb.project.currentpanel.core.defaultpanel) $('#panel-props-defaultpanel').prop('checked', true);
	else $('#panel-props-defaultpanel').prop('checked', false);
	
	$('#panel-bgColorSelector div').css({backgroundColor: pb.project.currentpanel.element.style.backgroundColor});
}

function updatePanelInfo(opts){

	if (pb.project.currentpanel === null) return;
	
	var name = document.getElementById('panel-props-name').value;
	
	var panel_label = document.getElementById(pb.project.currentpanel.core.id+'_label');
	panel_label.innerHTML = name;		
	
	pb.project.currentpanel.core.name = name;
	
}

function promptSave(){
	pb.unselectAll();
	closeMenu('file');
	closeMenu('recent'); 	
	if (undef(pb.project.name) || pb.project.name == 'newproject'){
		pb.openConfirmDialog('Enter a name for your project:',
			'<input id="project_name" size="3" style="width: 145px;" value="" type="text"/>',
			function(){
				pb.project.name = $('#project_name').val();
				$('#project-name-display').css('display', 'block');
				$('#project-name-display')[0].innerHTML = 'Project: ' + pb.project.name;
				pb.saveProject();
			}, true);
	}
	else{
		pb.saveProject();
	}
}

function addNewButton(library){
	closeMenu('add');
	var nbutton = new Button({});
	nbutton.element.style.width = '80px';
	nbutton.element.style.height = '80px';
	openItemEditor({item: nbutton, tolibrary: true});
	pb.openToolsPane();
	$('#button-library-select').val('User Buttons');
	pb.setButtonLibrary();
	var thisid = "button_library_label";
	$(".tools_item").removeClass('layerD').addClass('layerC');
	$('#'+thisid.substring(0,thisid.length-6)).removeClass('layerC').addClass('layerD');
	
}