/******************************************************************************
@owner: SVSi
@author: Arthur Johnson
@function: Asset Class
@Asset
*******************************************************************************/

//*************  ASSET CLASS  ***************//

	function Asset(opts) {
		
		if (undef(opts)) opts = {};
		
		if (undef(pb.assetindex)) pb.assetindex = 0;
		
	//**************************** CORE ******************************//
		if (opts.hasOwnProperty('core')){
		
			this.core = opts.core;
			
			if (undef(opts.copyid)) this.core.id = 'Asset-'+(pb.assetindex++);
			
			if (!undef(opts.type)) this.core.type = opts.type;
			
		}
		else {
		
			this.core = {};
			
			this.core.id = opts.hasOwnProperty('id') ? opts.id : 'Asset-'+(pb.assetindex++);	
			this.core.type = opts.hasOwnProperty('type') ? opts.type : 'image';			
			this.core.scale = opts.hasOwnProperty('scale') ? opts.scale : 1;
			this.core.elementHTML = opts.hasOwnProperty('elementHTML') ? opts.elementHTML : '';
			this.core.style = opts.hasOwnProperty('style') ? opts.style : 
				'width: 80px; height: 80px; margin: 4px; float: left;';	
			this.core.basewidth = opts.hasOwnProperty('basewidth') ? opts.basewidth : 80;
			this.core.baseheight = opts.hasOwnProperty('baseheight') ? opts.baseheight : 80;
			this.core.basetop = opts.hasOwnProperty('basetop') ? opts.basetop : 0;
			this.core.baseleft = opts.hasOwnProperty('baseleft') ? opts.baseleft : 0;			
			this.core.draggedoffsetx = opts.hasOwnProperty('draggedoffsetx') ? opts.draggedoffsetx : 0;
			this.core.draggedoffsety = opts.hasOwnProperty('draggedoffsety') ? opts.draggedoffsety : 0;
			this.core.updating = opts.hasOwnProperty('updating') ? opts.updating : false;
			this.core.update = opts.hasOwnProperty('update') ? opts.update : noop;
			this.core.responsive = opts.hasOwnProperty('responsive') ? opts.responsive : false;
			this.core.keepaspect = opts.hasOwnProperty('keepaspect') ? opts.keepaspect : false;
			this.core.draggable = opts.hasOwnProperty('draggable') ? opts.draggable : false;
			this.core.zvalue = opts.hasOwnProperty('zvalue') ? opts.zvalue : 2000;
			
			this.core.library = opts.hasOwnProperty('library') ? opts.library : 'image';
			
			this.core.data = opts.hasOwnProperty('data') ? opts.data : false;
			
		}
	
	//*************************** ELEMENT ****************************//
		this.element = document.createElement("div");	
		this.element.style.cssText += this.core.style;
		this.element.id = this.core.id;

	//**************************** INIT ******************************//
		pb.user.assets[this.core.type+'s'].push(this);
		
		this.core.previouslocation = {top: this.element.style.top, left: this.element.style.left};
		
	}
	//************************** PROTOTYPE **************************//
	Asset.prototype = {
		scaleTo: function(s){
			this.scale = s;
			$(this.element).css('transform', 'scale('+s+','+s+')');
		},
		clone: function(opts){
		
			if (undef(opts)) opts = {};
			
			var extrastyle = opts.hasOwnProperty('extrastyle') ? opts.extrastyle : '';
		
			var newasset = new Asset({	
				core: deepCopy(this.core)
			});
			
			newasset.element.style.cssText += this.element.style.cssText
				+ ' ' + extrastyle + ' background-size: 100% 100%; margin: 0px;';
			
			if (!undef(opts.library)) newasset.library = opts.library;
			
			return newasset;
		},
		click: function(){
			
			$('.'+this.core.library+'libraryselected').removeClass(this.core.library+'libraryselected');
			$(this.element).addClass(this.core.library+'libraryselected');

			if (pb.project.textEditing){
			
				if (pb.project.textEditing.core.text)
					pb.project.textEditing.core.text = 
					$(pb.project.textEditing.textnode)[0].innerHTML;
				
				for (k in CKEDITOR.instances){
					var instance = CKEDITOR.instances[k];
					instance.destroy();
				}
				
				if (pb.project.textEditing.textnode) {
					$(pb.project.textEditing.textnode).attr('contenteditable', 'false');
					$(pb.project.textEditing.textnode).blur();
					$(pb.project.textEditing.textnode).addClass('item_child');
					allDescendantsAddClass(pb.project.textEditing.textnode, 'ck_edit');
					$(pb.project.textEditing.textnode).children().addClass( 'ck_edit' );
				}
				else {
					$(pb.project.textEditing.element).attr('contenteditable', 'false');
					$(pb.project.textEditing.element).blur();
					$(pb.project.textEditing.element).addClass('item_child');
					if (pb.project.textEditing.element) 
						allDescendantsAddClass(pb.project.textEditing.element, 'ck_edit');
					$(pb.project.textEditing.element).children().addClass( 'ck_edit' );
				}
				pb.project.textEditing = false;
				
			}
			
		},
		doubleclick: function(){
		
			if (pb.project.textEditing) {
			
				if (pb.project.textEditing.text) pb.project.textEditing.text = $(pb.project.textEditing.textnode)[0].innerHTML;
				
				for (k in CKEDITOR.instances){
					var instance = CKEDITOR.instances[k];
					instance.destroy();
				}
				
				if (pb.project.textEditing.textnode){
					$(pb.project.textEditing.textnode).attr('contenteditable', 'false');
					$(pb.project.textEditing.textnode).blur();
					$(pb.project.textEditing.textnode).addClass('item_child');
					allDescendantsAddClass(pb.project.textEditing.textnode, 'ck_edit');
					$(pb.project.textEditing.textnode).children().addClass( 'ck_edit' );
				}
				else {
					$(pb.project.textEditing.element).attr('contenteditable', 'false');
					$(pb.project.textEditing.element).blur();
					$(pb.project.textEditing.element).addClass('item_child');
					if (pb.project.textEditing.element) 
						allDescendantsAddClass(pb.project.textEditing.element, 'ck_edit');
					$(pb.project.textEditing.element).children().addClass( 'ck_edit' );
				}
				
				pb.project.textEditing = false;
				
			}
			
		},
		dragging: function(offx, offy){
		
			this.element.style.zIndex = 24000;
			this.element.style.position = 'absolute';

			var elebounds = this.element.getBoundingClientRect();
			var s = pb.currentpanel.core.scale;
			var doffsetx = -this.core.draggedoffsetx;
			var doffsety = -this.core.draggedoffsety;
			var oy = 0;
			var ox = 0;
			
			if (!this.panel){
				doffsetx = doffsetx*s;
				doffsety = doffsety*s;
			}
			
			this.element.style.left = ox + (doffsetx + pb.mouse.x) - ($(this.element).outerWidth()*(1-s)/2) + 'px';			
			this.element.style.top = oy + (doffsety + pb.mouse.y) - ($(this.element).outerHeight()*(1-s)/2) + 'px';
			
			$(this.element).css('-webkit-transform', 'scale('+pb.currentpanel.core.scale+','+pb.currentpanel.core.scale+')');

		},
		drop: function(){
			var eles = pb.mouse.itemsOver();
			
			if (eles){
				if (eles.length != 0){
					var lastele = eles[eles.length-1];
					var item = pb.getItemById(lastele.id);
					
					if (item) if (item instanceof Panel || item instanceof Button){
						if (item instanceof Button){
							var str = this.element.style.backgroundImage; //console.log('src', str);
							str = str.replace(/url\("/g, '').replace(/"\)/g, '');
							str = str.replace(/url\(/g, '').replace(/\)/g, '');
							item.getState(item.core.currentstate+1).bgimage = str;
							item.setState();
						}
						else {
							item.element.style.backgroundImage = this.element.style.backgroundImage;
							item.element.style.backgroundSize = '100% 100%';
						}
					}
				}
			}
			
			$(this.element).remove();
		},
		clickup: noop,
		hoverin: noop,
		hoverout: noop,
		constrain: function(){
		
			var bbox = pb.currentpanel.element.getBoundingClientRect();
			var elebox = this.element.getBoundingClientRect();
			var ox = 0; var oy = 0;
		
			if (elebox.left < bbox.left) this.element.style.left = '0px';
			if (elebox.top < bbox.top) this.element.style.top = '0px';
			if (elebox.right > bbox.right) this.element.style.left =
				$(pb.currentpanel.element).outerWidth()
				- $(this.element).outerWidth() + ox + 'px';
			if (elebox.bottom > bbox.bottom) this.element.style.top =
				$(pb.currentpanel.element).outerHeight()
				- $(this.element).outerHeight() + oy + 'px';
				
		},
		setStyle: function(s){
			if (this.element) this.element.style.cssText = s;
			this.states[this.currentstate].style = s;
		},
		setLeft: function(l){
			if (this.element) this.element.style.left = l + 'px';
			this.left = l;
		},
		setTop: function(t){
			if (this.element) this.element.style.top = t + 'px';
			this.left = t;
		},
		setPosition: function(l,t){
			if (this.element) this.element.style.left = l + 'px';
			if (this.element) this.element.style.top = t + 'px';
			this.left = l;
			this.top = t;
		},
		setWidth: function(w){
			if (this.element) this.element.style.width = w + 'px';
			this.actualwidth = w;
		},
		setHeight: function(h){
			if (this.element) this.element.style.height = h + 'px';
			this.actualheight = h;
		},
		setSize: function(w, h){
			if (this.element) this.element.style.width = w + 'px';
			if (this.element) this.element.style.height = h + 'px';
			this.actualwidth = w;
			this.actualheight = h;
		},
		setPositionCenter: function(x,y){
			if (this.element){
				this.element.style.left = (x - $(this.element).outerWidth()/2) + 'px';
				this.element.style.top = (y - $(this.element).outerHeight()/2) + 'px';
			}
			this.left = parseFloat(this.element.style.left);
			this.top = parseFloat(this.element.style.top);
		},
		getPositionCenter: function(){
			var result = {};
			result.x = parseFloat(this.element.style.left) + ($(this.element).outerWidth()/2);
			result.y = parseFloat(this.element.style.top) + ($(this.element).outerHeight()/2);
			return result;
		},
		getWidthToHeight: function(){
			return this.width/this.height;
		},
		getHeightToWidth: function(){
			return this.height/this.width;
		},
		getActualWidth: function(){
			return parseFloat(this.element.style.width);
		},
		getActualHeight: function(){
			return parseFloat(this.element.style.width);
		},
		makeDraggable: function(opts){
		
			if (undef(opts)) opts = {};
		
			var appendTo = opts.hasOwnProperty('appendTo') ? opts.appendTo : 'body';
			var helper = opts.hasOwnProperty('helper') ? opts.helper : 'clone';
			var start = opts.hasOwnProperty('start') ? opts.start : noop;
			var drag = opts.hasOwnProperty('drag') ? opts.drag : noop;
			var stop = opts.hasOwnProperty('stop') ? opts.stop : noop;
			var grid = opts.hasOwnProperty('grid') ? opts.grid : [1, 1];
			var containment = opts.hasOwnProperty('containment') ? opts.containment : 'document';
		
			$(this.element).draggable({
				appendTo: appendTo,
				helper: helper,
				start: start,
				drag: drag,
				stop: stop,
				grid: grid,
				containment: containment
			});
			
		},
		makeResizable: function(){
			
			$(this.element).resizable({
				autoHide: true,
				containment: 'parent', 
				handles: 'n, e, s, w, se, sw, nw, ne',
				resize: function (e, ui) {	
					//console.log(ui);
					var s = pb.currentpanel.core.scale;
					var bbox = pb.currentpanel.element.getBoundingClientRect();
					var elebox = this.element.getBoundingClientRect();
					
					var changeWidth = ui.size.width - ui.originalSize.width;
					
					var newWidth = ui.originalSize.width + changeWidth/s;
				 
					var changeHeight = ui.size.height - ui.originalSize.height;
					var newHeight = ui.originalSize.height + changeHeight/s; 
					
					this.element.style.width = newWidth+'px';
					this.element.style.height = newHeight+'px';
					
					var oldtop = this.element.style.top;
	
					if (ui.handle == 'w' || ui.handle == 'nw' || ui.handle == 'sw'){
						console.log('nw');
						this.element.style.left = ((pb.mouse.x - bbox.left)/s) + 'px';
					}
					if (ui.handle == 'n' || ui.handle == 'nw' || ui.handle == 'ne'){
						console.log('ne');
						this.element.style.top = ((pb.mouse.y - bbox.top)/s) + 'px';
					}
					
					var elebox = this.element.getBoundingClientRect();
					if (elebox.right >= bbox.right)
						this.element.style.width = newWidth - ((elebox.right - bbox.right)/s) + 'px';
					if (elebox.bottom >= bbox.bottom)
						this.element.style.height = newHeight - ((elebox.bottom - bbox.bottom)/s) + 'px';
					if (elebox.top <= bbox.top){
						if (this._lastsizey) this.element.style.height = this._lastsizey;
						this.element.style.top = '0px';
					}
					if (elebox.left <= bbox.left){
						if (this._lastsizex) this.element.style.width = this._lastsizex;
						this.element.style.left = '0px';
					}
				
					var theight = ($(this.element).outerHeight()/2)
						- ($(this.textnode).outerHeight()/2);
					$(this.textnode).css('top', theight + 'px');
					
					this._lastsizex = this.element.style.width;
					this._lastsizey = this.element.style.height;
					this._lastleft = this.element.style.left;
					this._lasttop = this.element.style.top;
					
				}.bind(this),
				start: function(e, ui) {
					this.action = new Action({
						type: 'size',
						items: [this],
						oldvalues: [{width: this.element.style.width, height: this.element.style.height}]
					});
				}.bind(this),
				stop: function(e, ui) {
					this.action.newvalues = [{width: this.element.style.width, height: this.element.style.height}];
					pb.saveaction(this.action);
				}.bind(this)
			}); 
			
		}
	}
//**************  END ASSET  ****************//