/******************************************************************************
@owner: SVSi
@author: Arthur Johnson
@functions: Widget Parts are widget children objects
@WidgetPart
*******************************************************************************/

//*************  WIDGETPART CLASS  ***************//
	function WidgetPart(opts){
	
		if (undef(opts)) opts = {};
		
		if (undef(pb.widgetpartindex)) pb.widgetpartindex = 0;
		
	//**************************** CORE ******************************//
		if (opts.hasOwnProperty('core')){
		
			this.core = opts.core;
			
			if (undef(opts.copyid)) this.core.id = pb.uniqueId('WidgetPart-'+(pb.widgetpartindex++));
			//else this.core.id = pb.uniqueId(this.core.id);
			
			if (!undef(opts.type)) this.core.type = opts.type;
			
		}
		else {
		
			this.core = {};
			
			this.core.id = opts.hasOwnProperty('id') ? opts.id : pb.uniqueId('WidgetPart-'+(pb.widgetpartindex++));	
			this.core.set = opts.hasOwnProperty('set') ? opts.set : 'default';
			this.core.type = opts.hasOwnProperty('type') ? opts.type : 'default';
			this.core.scale = opts.hasOwnProperty('scale') ? opts.scale : 1;			
			this.core.scripts = opts.hasOwnProperty('scripts') ? opts.scripts : ['', ''];
			this.core.conditional = opts.hasOwnProperty('conditional') ? opts.conditional : false;
			this.core.elementHTML = opts.hasOwnProperty('elementHTML') ? opts.elementHTML : '';
			this.core.className = opts.hasOwnProperty('className') ? opts.className : '';
			this.core.style = opts.hasOwnProperty('style') ? opts.style : '';	
			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.touchpanelkeys = opts.hasOwnProperty('touchpanelkeys') ? opts.touchpanelkeys : [];
			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;

		}
	
	//*************************** WIDGET *****************************//
		this.widget = opts.hasOwnProperty('widget') ? opts.widget : false;
		
	//**************************** PANEL *****************************//
		this.panel = opts.hasOwnProperty('panel') ? opts.panel : false;
	
	//*************************** ELEMENT ****************************//
		this.element = document.createElement("div");	
		this.element.style.cssText += this.core.style;
		this.element.id = this.core.id;
		this.element.className = this.core.className;
		this.element.innerHTML = this.core.elementHTML;
	
	//**************************** INIT ******************************//
		pb[this.core.type].widgetparts.push(this);
		
		this.init = opts.hasOwnProperty('init') ? opts.init : noop;
		this.drop = opts.hasOwnProperty('drop') ? opts.drop : noop;
		this.moving = opts.hasOwnProperty('moving') ? opts.moving : noop;
		this.update = opts.hasOwnProperty('update') ? opts.update : noop;
		this.click = opts.hasOwnProperty('click') ? opts.click : noop;
		this.clickup = opts.hasOwnProperty('clickup') ? opts.clickup : noop;
		this.hoverin = opts.hasOwnProperty('hoverin') ? opts.hoverin : noop;
		this.hoverout = opts.hasOwnProperty('hoverout') ? opts.hoverout : noop;
		this.resize = opts.hasOwnProperty('resize') ? opts.resize : noop;
		this.ontopanel = opts.hasOwnProperty('ontopanel') ? opts.ontopanel : noop;
		this.doubleclick = opts.hasOwnProperty('doubleclick') ? opts.doubleclick : noop;
		
		this.core.previouslocation = {top: this.element.style.top, left: this.element.style.left};

		if (this.init) this.init();
		
	}
	WidgetPart.prototype = {
		clone: function(opts){
		
			if (undef(opts)) opts = {};
			
			var extrastyle = opts.hasOwnProperty('extrastyle') ? opts.extrastyle : '';
			var className = opts.hasOwnProperty('className') ? opts.className : this.element.className;
			
			this.core.style = this.element.style.cssText + extrastyle;
			this.core.elementHTML = this.element.innerHTML;
			this.core.className = className;
			
			var newcore = deepCopy(this.core);
			if (!opts.copyid) newcore.id = 'WidgetPart-'+(pb.widgetpartindex++);
		
			var newpart = new WidgetPart({
				core: newcore,	
				widget: this.widget
			});
			
			for (var property in this) {
				if (this.hasOwnProperty(property) && property != 'panel' &&
					property != 'core' && property != 'element' && property != 'parts' &&
					property != 'container' && property != 'library' && property != 'action'){
					newpart[property] = this[property];
				}
			}
			
			return newpart;
			
		},
		destroy: function(){
			$(this.element).remove();
			removeFromArray(this.widget.parts, this);
		}
	}
//**************  END WIDGETPART  ****************//