// Basic properties
LayersOSApplicationBar.prototype._referal;
LayersOSApplicationBar.prototype._displayable;

// Applications Buttons
LayersOSApplicationBar.prototype._applicationsButtons = new Array();

// Properties for the applications
LayersOSApplicationBar.prototype.applicationSeeds = new Array();

//Cosntructor
function LayersOSApplicationBar(referal,displayable){
	var me = this;
	this._referal = referal;
	this._displayable = displayable;

	this._displayable.append(this._referal._templates.getApplicationBarHTML());

	var apps = os.getApplicationList();
	
	for(var i=0;i<apps.length;i++){
	//	console.info(apps[i]);
		var tmp = $('#layersOS_toolbar_content');
		me._applicationsButtons = new LayersOSApplicationButton(referal,tmp,apps[i]);
	}	
	
	//Make draggable and resizeable
    var disp = this._displayable;
    
    disp.draggable({
        start: function(){
            //Put a transparent div over the whole page to avoid Iframe event robbery
            $('#layersOS_transparentHelper').attr('style', 'display: block');
        },
        stop: function(event, ui){
            //Hide the transparent div
            $('#layersOS_transparentHelper').attr('style', 'display: block');
        }
    });
    
    /*
     .resizable({
        minHeight: 70,
        minWidth: 61
    });
    */
    

}

LayersOSApplicationBar.prototype.show = function(speed){
	this._displayable.show(speed);
}

/*
LayersOSApplicationBar.prototype.objectCreate = function(index){
	var me = this;
	if (os.activeApp != undefined && os.activeApp != '') {
		os.openedApps[os.activeApp].end();
	}
	os.activeApp = index;
	var init_seed = null;
	if(this.applicationSeeds[index]) init_seed = this.applicationSeeds[index]; 
	
	
	//get application seed
	if(init_seed==null) this.applicationSeeds[index] = os.openedApps[index].init();
	else os.openedApps[index].init(init_seed);
	
	// bind layerModified event to object created
	//$('#' + init_seed).unbind('layerModified').bind('layerModified', function(){
	//	//zme._referal.dockItemEnable('layersOS_dock_save');
	//});
} 
*/


LayersOSApplicationBar.prototype.activeAllButtons = function(){
	$('#layersOS_toolbar_content .layersOS_tool').addClass('layersOS_tool_enabled').removeClass('layersOS_tool_disabled');
}

LayersOSApplicationBar.prototype.getLayerDescriptionFromApplication = function(){
	var content = '';
	// First look for the notes
	
	
	$(".seed_annotate").each(
		function(){
			if(content == ''){
				var semilla = $(this).find('.layerOS_semilla')[0].value;
				var noteText = $('#container_' + semilla  + ' .layersOS_annotate_content_text:first').text();
				content = noteText.substring(0,280);
				console.info(content);
				
				if (noteText.length > 280){
					// Convert the result into an array using space as separator, to find the last word if content is greater than 280 chars.
					var contentArray = content.split(" ");
					// Delete the last word (which may be truncated) from the array
					contentArray.splice(contentArray.length -1, 1, '...');
					content = contentArray.join(" ");	
				}				
		
				if(content.length > 0) {
					console.info("lenght > 0");
					return true;
				}
			}
		}
	);
	
	if(content.length <= 0){
		// Look in the highlight class=layersOS_highlight
		$(os.getBackgroundPageObject()).find('.layersOS_highlight').each(function(){
			
			if(content == ''){
				content = $(this).text().substring(0,280);			
			
				if ($(this).text().length > 280){
					// Convert the result into an array using space as separator, to find the last word if content is greater than 280 chars.
					var contentArray = content.split(" ");
					// Delete the last word (which may be truncated) from the array
					contentArray.splice(contentArray.length -1, 1, '...');
					content = contentArray.join(" ");	
				}
			
				if(content.length > 0) return true;
			}
		});		
	}
	return content;
}
