var Portal1Widget = (function(window, undefined) {                                                                                      
                                                                                                                                    
	var Portal1Widget = Portal1Widget || {};                                                                                                
	function extractHostPortApp(src) {	                                                                                            
		
		Portal1Widget.microserviceId = 1;                                                                                 
		Portal1Widget.pathArray = src.split( '/' );                                                                                     
			
		Portal1Widget.widgetName = 1;
		Portal1Widget.serviceSeperator = Portal1Widget.pathArray[Portal1Widget.pathArray.length - 4];                                           
		Portal1Widget.commonUrl = src.substring(0, src.lastIndexOf("/" + Portal1Widget.pathArray[Portal1Widget.pathArray.length - 2]));       
		
		Portal1Widget.recipientDivDataAttrib = 'data-' + Portal1Widget.widgetName;                                                          
		Portal1Widget.controllerName = 'Portal1Ctrl';                                                                                     
		Portal1Widget.readyCssFlag = 'portal1-css-ready';                                                                                       
		Portal1Widget.readyCssFlagExpectedValue = '#bada55';                                                                            
		Portal1Widget.serviceURL = src.substring(0, src.lastIndexOf("/" + Portal1Widget.serviceSeperator)) + '/portalApi/microservice/proxy/parameter/' + 1;  
	}                                  
	
	extractHostPortApp(document.getElementsByTagName('script')[0].src);
	
	function loadStylesheet(url) {                                                                                                 
		var link = document.createElement('link');                                                                                 
		link.rel = 'stylesheet';                                                                                                   
		link.type = 'text/css';                                                                                                    
		link.href = url;                                                                                                           
		var entry = document.getElementsByTagName('script')[0];                                                                    
		entry.parentNode.insertBefore(link, entry);                                                                                
	}                                                                                                                              
                                                                                                                                   
	function isCssReady(callback) {                                                                                                
		var testElem = document.createElement('span');                                                                             
		testElem.id = Portal1Widget.readyCssFlag;	                                                                                   
		testElem.style = 'color: #fff';                                                                       
		var entry = document.getElementsByTagName('script')[0];                                                                    
		entry.parentNode.insertBefore(testElem, entry);                                                                            
	                                                                                                                               
		(function poll() {                                                                                                         
			var node = document.getElementById('css-ready');                                                                       
			var value;                                                                                                             
			if (window.getComputedStyle) {                                                                                         
				value = document.defaultView.getComputedStyle(testElem, null)                                                      
						.getPropertyValue('color');                                                                                
			}                                                                                                                      
			else if (node.currentStyle) {                                                                                          
				value = node.currentStyle.color;                                                                                   
			}                                                                                                                      
			if (value && value === 'rgb(186, 218, 85)' || value.toLowerCase() === Portal1Widget.readyCssFlagExpectedValue) {           
				callback();                                                                                                        
			} else {                                                                                                               
				setTimeout(poll, 500);                                                                                             
			}                                                                                                                      
		})();                                                                                                                      
	}                                                                                                                              
	                                                                                                                               
	function injectCss(css) {                                                                                                      
		var style = document.createElement('style');                                                                               
		style.type = 'text/css';                                                                                                   
 		css = css.replace(/\}/g, "}\n");                                                                                           
	                                                                                                                               
		if (style.styleSheet) {                                                                                                    
			style.styleSheet.cssText = css;                                                                                        
		} else {                                                                                                                   
			style.appendChild(document.createTextNode(css));                                                                       
		}                                                                                                                          
		var entry = document.getElementsByTagName('script')[0];                                                                    
		entry.parentNode.insertBefore(style, entry);                                                                               
	}                                                                                                                              
	                                                                                                                               
	function loadScript(url, callback) {                                                                                           
		var script = document.createElement('script');                                                                             
		script.src = url;                                                                                                          
		
		var entry = document.getElementsByTagName('script')[0];                                                                    
		entry.parentNode.insertBefore(script, entry);                                                                              
		
		script.onload = script.onreadystatechange = function() {   
			var rdyState = script.readyState;                                                                                      
			if (!rdyState || /complete|loaded/.test(script.readyState)) {                                                          
				callback();                                                                                                        
				script.onload = null;                                                                                              
				script.onreadystatechange = null;                                                                                  
			}                                                                                                                      
		};                                                                                                                         
	}                                                                                                                              
                                                                                                                                   
	function loadSupportingFiles(callback) {                                                                                       
		callback();                                                                                                                
	}                                                                                                                              
	                                                                                                                               
	function getWidgetParams() {                                                                                                   
		                                                                                                                           
	}                                                                                                                              
	                                                                                                                               
	function getWidgetData(widgetUrl, callback) {   	                                                                           
	var responseData;                      																						   
	try{                                   																						   
		jQuery.ajax({                       																					   
			url: widgetUrl,               																						   
			method: "GET",           																							   
			xhrFields: {              																							   
				withCredentials: true       																					   
			},    
			crossDomain: true,			
			success: function (result) {    																				 	   
				if (result.isOk == false){  																					   
			                     																							   
				}else{                      																					   
					callback(result);		  																					   
				}                           																					   
			}                             																					   
		});                                 																				   
	}                                      																						   
	catch(e){                              																						   
	
	}                                      																						   
																							   
	}                                                                                                   
	
	function getMarkupContent(markupLocation, callback){                                                                             
		                                                                                                                           
		try{
			jQuery.ajax({                                                                                                              
		        url: markupLocation,
		        method: "GET",   
		        xhrFields: {              																							   
					withCredentials: true       																					   
				},   
				crossDomain: true,		
		        success: function (result) {                                                                                           
		            if (result.isOk == false){                                                                                         
		            	                                                                                                               
		            }else{                                                                                                             
		            	callback(result);                                                                                   
					}                                                                                                                  
		        }                                                                                                        
			});       
		}
		catch(e){
			
		}
	}                                                                                                                              
	                                                                                                                               
	function renderWidget(data, location, $controllerProvider) {                                                                   
		var div = document.createElement('div');                                                                                   
		getMarkupContent(Portal1Widget.commonUrl + "/markup/" + Portal1Widget.widgetName, function(div){
			location.append(div);                                                                                                      
			Portal1Widget.widgetData = data;
			app.controllerProvider.register(Portal1Widget.controllerName, Portal1Widget.controller);                                           
			var mController = angular.element(document.getElementById("widgets"));                                                   
			mController.scope().activateThis(location);   
		});
		
	}                                                                                                                              
	                                                                                                                               
	function printAllArtifacts(moduleName, controllerName) {                                                                       
	    var queue = angular.module(moduleName)._invokeQueue;                                                                       
	    for(var i=0;i<queue.length;i++) {                                                                                          
	        var call = queue[i];                                                                                                   
	        console.log(i + '. ' + call[0] + ' - ' + call[1] + ' - ' + call[2][0]);                                                
	    }                                                                                                                          
	}                                                                                                                              
	                                                                                                                               
	function get(name){                                                                                                            
	   if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))                                     
	      return decodeURIComponent(name[1]);                                                                                      
	}                                                                                                                              
                                                                                                                                   
	loadSupportingFiles(function() {                                                                                               
		loadStylesheet(Portal1Widget.commonUrl + '/' + Portal1Widget.widgetName + '/style.css');                                           
		loadScript(Portal1Widget.commonUrl + '/' + Portal1Widget.widgetName + '/controller.js',                                            
			function() {                                                                                                           
				$('['+ Portal1Widget.recipientDivDataAttrib + ']').each(function() {                                                   
					var location = jQuery(this);                                                                                   
					location.removeAttr(Portal1Widget.recipientDivDataAttrib);                                                         
					var id = location.attr(Portal1Widget.recipientDivDataAttrib);                                                      
					getWidgetData(Portal1Widget.serviceURL, function(data) { 
						isCssReady(function(){                                                                                     
							renderWidget(data, location);                                                                          
						});								                                                                           
					});                                                                                                            
				});                                                                                                                
			}                                                                                                                      
		);                                                                                                                         
	});                                                                                                                            
	                                                                                                                               
	return Portal1Widget;	                                                                                                           
})(window);                                                                                                                        
				