﻿var DropDwn = window.DropDwn || {};

DropDwn.namespace = function(ns){
  if(!ns || !ns.length){
      return null;
  }

  var nw_2 = ns.split(".");
  var nw_3 = DropDwn;

   for(var i=(nw_2[0]=="DropDwn")?1:0;i<nw_2.length;++i){
       nw_3[nw_2[i]] = nw_3[nw_2[i]] || {};
       nw_3 = nw_3[nw_2[i]];
   }
   return nw_3;
};

DropDwn.namespace("Component");
DropDwn.namespace("Widget");
DropDwn.namespace("Util");


var Paths = {
	
	Current : null,

	InferRootPath : function() {
		var subdirectory = window.location.pathname;
		subdirectory = subdirectory.substring(0, subdirectory.lastIndexOf("/"));
		if (subdirectory.indexOf("/id") > -1) {
			subdirectory = subdirectory.substring(0, subdirectory.indexOf("/id"));
		}
		if (subdirectory.indexOf("/profile") > -1) {
			subdirectory = subdirectory.substring(0, subdirectory.indexOf("/profile"));
		}
		return window.location.protocol + "//" + window.location.host + subdirectory;
	},
	
	Init : function() {
		var defaultPath = Paths.InferRootPath();
		this.RootPath = defaultPath;
		this.FilePath = defaultPath;
		this.StaticContentRoot = defaultPath;
		this.RegistrationHost = defaultPath;
		this.SearchHost = defaultPath;
		
		if (typeof(rootpath) != 'undefined'){
			this.RootPath = rootpath;
		}
		if (typeof(filepath) != 'undefined'){
			this.FilePath = filepath;
		}
		if (typeof(staticContentRoot) != 'undefined'){
			this.StaticContentRoot = staticContentRoot;
		}
		if (typeof(registrationHost) != 'undefined'){
			this.RegistrationHost = registrationHost;
		}
		if (typeof(searchHost) != 'undefined'){
			this.SearchHost = searchHost;
		}
	}
};

Paths.Current = new Paths.Init();



DropDwn.Widget.Expander = {

	boxHt : null,
	isActive : false,

	init : function(){
	
		$('div.Expander').each(function(i){
            var Cloud = this;
            Cloud = {
              
                isActive     : false,
                isOpen       : false,
                source       : $('div.content div.column div.nav a', this),
                content      : $('div.content', this),
                contentHt    : $('div.content', this).css('height'),
                maxColumnHt  : null
            };   
            
			DropDwn.Widget.Expander.boxHt = parseInt($('div.content', this).height());
            
            $(Cloud.source).click(function(e){
				if(DropDwn.Widget.Expander.isActive) {return;}
				DropDwn.Widget.Expander.isActive = true;
            
				if(this.className == 'seeAll'){
					DropDwn.Widget.Expander.expand(Cloud);                         				
				} else if (this.className == 'collapseAll') {
					DropDwn.Widget.Expander.collapse(Cloud); 		
				}
            });
        });  
	},
	
	expand : function(Cloud){
			
		$(Cloud.source).removeClass('seeAll').addClass('collapseAll').text('Collapse All');
		$('div.column ul', Cloud.content).removeClass('collapsed');
		var colHeight = 0;
		
		if(Cloud.maxColumnHt === null){
			$('div.column ul', Cloud.content).each(function(i){
				if($(this).height() > colHeight){
					colHeight = $(this).height();
				}
			});
			Cloud.maxColumnHt = colHeight;			
		} else {
			colHeight = Cloud.maxColumnHt;
		}
						
		$('div.column ul', Cloud.content).height(colHeight);
								
		$(Cloud.content).animate({
			height : (colHeight + 26) + 'px'
		}, 250, function(){
			DropDwn.Widget.Expander.isActive = false;
		});
	},
	
	collapse : function(Cloud){
						
		$(Cloud.content).animate({
			height : DropDwn.Widget.Expander.boxHt + 1 + 'px'
		}, 250, function(){
			$(Cloud.source).removeClass('collapseAll').addClass('seeAll').text('See All');
			$('div.column ul', Cloud.content).css('height', '65px');
			$('div.column ul', Cloud.content).addClass('collapsed');			
			DropDwn.Widget.Expander.isActive = false;				
		});
	}
};

