
/******************************************************************************
@owner: SVSi
@author: Arthur Johnson
@functions: PanelBuilder Program Initialization
@setupPB
*******************************************************************************/

if (activepanel){ // index.php---->
	var thispanel;
	var mouse = new MouseControls({responsive: true});
		pb.mouse = mouse;
	window.onWindowResize = function(){
		if (project && project.currentpanel){
			if (project.currentpanel.core.autoresize || qs_is_mobile){
				project.currentpanel.resize(window.innerWidth-2, window.innerHeight-2);
				project.currentpanel.element.style.marginTop = '0px';
				project.currentpanel.element.style.marginLeft = '0px';
			}
			$(project.currentpanel.element).center();
			$('#security-panel').center();
		}
	}
	function update(){
		TWEEN.update();
		if (thispanel){
			for (var i=0; i<thispanel.widgets.length; i++){
				if (thispanel.widgets[i].update) thispanel.widgets[i].update.call(thispanel.widgets[i]);
				for (var j=0; j<thispanel.widgets[i].parts.length; j++){
					if (thispanel.widgets[i].parts[j].update)
						thispanel.widgets[i].parts[j].update.call(thispanel.widgets[i].parts[j]);
				}
			}
		}
		requestAnimationFrame(update);
	}
	window.onresize = function(){window.onWindowResize()};
	window.onWindowResize();
	update();
}
else { // panelbuilder.php---->
	var toolpaneopen = true;
	var toolopener = true;
	var resourcepaneopen = true;
	var resourceopener = true;
	
	window.onWindowResize = function(){
		var winW = $(window).width();
		var tr = document.querySelectorAll('.resize');
		if (tr) for (var i=0; i<tr.length; i++){
			tr[i].style.fontSize = 
				(winW * 0.025 * $(tr[i]).data('size')) + 'px';
			tr[i].style.lineHeight = 
				(winW * 0.025 * $(tr[i]).data('size')) + 'px';
		}
		var tr = document.querySelectorAll('.resize_W');
		if (tr) for (var i=0; i<tr.length; i++){
			if (!tr[i].parentNode) continue;
			tr[i].style.fontSize = 
				($(tr[i].parentNode).width() * 0.025 * $(tr[i]).data('size')) + 'px';
			tr[i].style.lineHeight = 
				($(tr[i].parentNode).width() * 0.025 * $(tr[i]).data('size')) + 'px';
		}
		var tr = document.querySelectorAll('.bradius_W');
		if (tr) for (var i=0; i<tr.length; i++){
			if (!tr[i].parentNode) continue;
			tr[i].style.borderRadius = 
				($(tr[i].parentNode).width() * 0.025 * $(tr[i]).data('brsize')) + 'px';
		}
		$('#wizard_ntouch').center();
		if (parseInt($('#wizard_ntouch').css('left')) < 490) $('#wizard_ntouch').css({left: '490px'});
		if (parseInt($('#wizard_ntouch').css('top')) < 77) $('#wizard_ntouch').css({top: '77px'});	
	}
	window.onresize = function(){ window.onWindowResize(); }
	function update(){//return;
		//console.log('updating', oldIE);
		if (oldIE) return;
		TWEEN.update();
		if (pb.updating){
			if (pb.project && pb.currentpanel){
				var pele = pb.currentpanel.element;
				if (pele) $('#ntouch-bg-image').css({
					position: 'absolute',
					left: (parseFloat($(pele).css('margin-left'))+parseFloat($(pele.parentNode).css('left'))-126) + 'px',
					top: (parseFloat($(pele).css('margin-top'))+parseFloat($(pele.parentNode).css('top'))+13) + 'px'
				});
			}
			$('#project-props-tree').css({height: (window.innerHeight-window.__paneltreeheight)+'px'});
			if (0 && $('#wizard_step2').css('display') != 'none'){
				var s = window.innerWidth/1524;
				var p = 1524/window.innerWidth;
				$('#wizard_ntouch').css({
					'transform': 'scale('+s+','+s+')',
					'-ms-transform': 'scale('+s+','+s+')',
					'-moz-transform': 'scale('+s+','+s+')',
					'-webkit-transform': 'scale('+s+','+s+')',
					'top': (((window.innerWidth/8)*s)-((50)*s))+'px'
				});
				window.__wizardscale = s;
			}
			if (pb.zoomingin){
				pb.currentpanel.core.scale += 0.01;
				$(pb.currentpanel.element).css({
					'-webkit-transform':
						'scale('+pb.currentpanel.core.scale+','
						+pb.currentpanel.core.scale+')',
					'-moz-transform':
						'scale('+pb.currentpanel.core.scale+','
						+pb.currentpanel.core.scale+')',
					'-ms-transform':
						'scale('+pb.currentpanel.core.scale+','
						+pb.currentpanel.core.scale+')',
					'transform':
						'scale('+pb.currentpanel.core.scale+','
						+pb.currentpanel.core.scale+')',
				});
			} 
			if (pb.zoomingout){
				if (pb.currentpanel.core.scale > 0.1) pb.currentpanel.core.scale -= 0.01;
				$(pb.currentpanel.element).css({
					'-webkit-transform':
						'scale('+pb.currentpanel.core.scale+','
						+pb.currentpanel.core.scale+')',
					'-moz-transform':
						'scale('+pb.currentpanel.core.scale+','
						+pb.currentpanel.core.scale+')',
					'-ms-transform':
						'scale('+pb.currentpanel.core.scale+','
						+pb.currentpanel.core.scale+')',
					'transform':
						'scale('+pb.currentpanel.core.scale+','
						+pb.currentpanel.core.scale+')'
				});
			}
			var preview = $('#item-preview-panel');
			var previewele = preview[0];
			var previewwidth = preview.outerWidth();
			var previewheight = preview.outerHeight();
			if (pb.project) if (pb.currentpanel){
				if (pb.currentpanel.buttons)
					for (var i=0; i<pb.currentpanel.buttons.length; i++){
						if (pb.currentpanel.buttons[i].core.update && 
							typeof pb.currentpanel.buttons[i].core.update === 'function')
						pb.currentpanel.buttons[i].core.update();
					}
				if (pb.currentpanel.widgets)
					for (var i=0; i<pb.currentpanel.widgets.length; i++){
						if (pb.currentpanel.widgets[i].core.update && 
							typeof pb.currentpanel.widgets[i].core.update === 'function')
						pb.currentpanel.widgets[i].update();
						for (var j=0; j<pb.currentpanel.widgets[i].parts.length; j++){
							if (pb.currentpanel.widgets[i].parts[j].update && 
								typeof pb.currentpanel.widgets[i].parts[j].update === 'function')
							pb.currentpanel.widgets[i].parts[j].update();
						}
					}
			}
			if (pb.mouse.down && pb.mouse.movingpanel){
				$('#move-button').css('opacity', 1);
				if (pb.mouse.distancefromdown <= 40)
					$('#move-button').css({
						left: pb.mouse.x - $('#move-button').outerWidth()/2 + 'px',
						top: pb.mouse.y - $('#move-button').outerHeight()/2 + 'px'
					});
				else
					$('#move-button').css({
						left: 100 + 40*pb.mouse.vectortodown.x/pb.mouse.distancefromdown - 0*$('#move-button').outerWidth()/2 + 'px',
						top: 34 + -40*pb.mouse.vectortodown.y/pb.mouse.distancefromdown - 0*$('#move-button').outerHeight()/2 + 'px'
					});
				pb.currentpanel.element.style.marginTop =
					(parseInt($(pb.currentpanel.element).css('margin-top')) + pb.mouse.vectortodown.y/10) + 'px';
				pb.currentpanel.element.style.marginLeft =
					(parseInt($(pb.currentpanel.element).css('margin-left')) - pb.mouse.vectortodown.x/10) + 'px';
			}
			if (toolopener){
				if (!toolpaneopen && pb.mouse.x < 90 && pb.mouse.y > 65) {
					$('.tools-panel').stop().animate({left: '0px'}, 480);
					toolpaneopen = true;
				}
				if (toolpaneopen && pb.mouse.x > 320) {
					$('#thebody').focus();
					$('.tools-panel').stop().animate({left: '-290px'}, 680, "easeOutBounce");
					toolpaneopen = false;
				}
			}
			if (resourceopener){	
				if (!resourcepaneopen && pb.mouse.x > $(window).outerWidth() - 90 && pb.mouse.y > 65) {
					$('.resources-panel').stop().animate({right: '0px'}, 480);
					resourcepaneopen = true;
				}
				if (resourcepaneopen && pb.mouse.x < $(window).outerWidth() - 320) {
					$('#panel-bgColorSelector').blur();
					$('.resources-panel').stop().animate({right: '-290px'}, 680, "easeOutBounce");
					resourcepaneopen = false;
				}
			}
		}
		requestAnimationFrame(update);
	}
	
}