


// Copyright (c) NimbleCat, 2008

/*
* this object implements a tabview
* 
* */
// make sure that the required includes are there
 if (typeof NC == 'undefined') {
       alert("TabView.js requires the NC JavaScript framework");
 }
 
 if (typeof NC.widget == 'undefined') {
       alert("TabView.js requires the NC JavaScript framework");
 }
 
  NC.widget.ContentView = function( id, contentContainer, tabContent, firstVariableTab) {
		var fnSuperClass = NC.widget.ContentView.superclass.constructor;
		fnSuperClass.call( this, id, contentContainer);
	  	this.content = tabContent;
	  	this.firstVariableTab = firstVariableTab;
		this.startTab = 0;
		this.endTab = -1; // will be set by init()
  }
  
    NC.lang.extend(NC.widget.ContentView, NC.widget.TabView, 
  	{
  		MoreCommand: 1,
  		ContentCommand: 2,

	  	init: function() {
	  		
	  		
            NC.widget.ContentView.superclass.init.call(this);
            var tabDiv = document.getElementById( this.domId);
            var tabRegion = NC.util.Region.getRegion( tabDiv);
           	var navDiv = NC.util.Dom.getElementsByClassName( "nc-nav", "ul", tabDiv, null);
           	this.addTabs( navDiv[0], tabRegion.right);
			NC.widget.ElementManager.register(this.domId, this); 
	  		return "init ContentView function";
	  	},
	  	
	  	addTabs: function( tabList, maxX) {
           	var i = this.startTab;
           	for ( ; i < this.content.length; i ++) {
	           	var tabElement = this.addContentTab( tabList, this.content[i][0], this.content[i][1], this.content[i][2], this.content[i][3]);
           		var elRegion = NC.util.Region.getRegion( tabElement);
           		if (maxX - elRegion.right < 150) {
           			break;
           		}
           	}
           	if (i < this.content.length -1 ||
           	    (i == this.content.length && this.startTab > 0)) {
	        	var tabElement = this.addTab( tabList, "More...", 
	        	{ 
	  				'cmd': this.MoreCommand
	  			}
	        	);
	        	this.endTab = i;
           	}
           	else {
           		this.endTab = -1;
           	}
	  	},

	  	
	  	addContentTab: function( oList, tablabel, nCid, menuDiv, edit) {
	  		return this.addTab( oList, tablabel, 
	  			{ 
	  				'cid': nCid,
	  				'cmd': this.ContentCommand,
	  				'menu': menuDiv,
	  				'edit': edit
	  			}
	  		);
	  	},
	  	
	  	addTab: function( oList, tablabel, attributes) {
				var oListElement = document.createElement('li');
				
				var oTextNode = document.createTextNode(tablabel);
				var oEm = document.createElement( 'em');
				oEm.appendChild(oTextNode);
				var oAnchor = document.createElement('a');
				oAnchor.appendChild( oEm);
				oListElement.appendChild( oAnchor);
				for (var key in attributes) {  
 					NC.widget.Attribute.set( oListElement, key, attributes[key]);
				}	
				
				
				//now add the elemenet to the list
				oList.appendChild(oListElement);
				var r = NC.util.Region.getRegion( oListElement);
	  			return oListElement;
	  	},
	  	
	  	rotateContent: function() {
            var tabDiv = document.getElementById( this.domId);
           	var navDiv = NC.util.Dom.getElementsByClassName( "nc-nav", "ul", tabDiv, null);
           	var tabs = navDiv[0].getElementsByTagName("li");
           	var lastTab = tabs.length;
           	for( var i = this.firstVariableTab; i < lastTab; i++) {
           		navDiv[0].removeChild( tabs[this.firstVariableTab]);
           	}	  	
            var tabRegion = NC.util.Region.getRegion( tabDiv);
            this.startTab = this.endTab + 1;
            if (this.startTab >= this.content.length) {
            	this.startTab = this.firstVariableTab
            }
           	this.addTabs( navDiv[0], tabRegion.right);
        },
	  	
		toggleMenu: function( event, menuId, element)
		{
			var target =  NC.core.Event.getEventTarget(event);
			if (NC.widget.MenuManager.isMenuVisible( menuId)) {
//				NC.widget.MenuManager.hideMenu( menuId);
			}
			else {
				var pos = NC.util.Dom.getAbsoluteXY(target);
				var region = NC.util.Region.getRegion( target);
				var menupos = [region.left + 3, region.bottom+5];
				var oMenu = document.getElementById( menuId);
				var mr = NC.util.Region.getRegion( oMenu);
				NC.widget.MenuManager.showMenu( menuId, menupos, this.domId);
			}
		},
		
	  	onTabMouseOver: function( event, element) {
//			NC.util.Debug.writeToConsole( "contentview:mouseover ");
	  		var contentArea = document.getElementById( this.contentContainer);
	  		var cmd = NC.widget.Attribute.get( element, "cmd");
	  		if (cmd == this.ContentCommand) {
		  		var menuId = NC.widget.Attribute.get( element, "menu");
		  		if (menuId != null && menuId.length > 0) {
		  			/*  */
				  	var menu = document.getElementById( menuId);
				  	var liList = menu.getElementsByTagName('li');
				  	var menuSize = liList.length;
		  			/*  */
		  			if (menuSize > 1) {
		  				this.toggleMenu( event, menuId, element);
		  			}
		  			else {
		  				NC.widget.MenuManager.hideAllMenus();
		  			}
		  		}
		  		else {
		  			NC.widget.MenuManager.hideAllMenus();
		  		}
	  		}
	  	},

	  	onTabMouseOut: function( event, element) {
//			NC.util.Debug.writeToConsole( "contentview:mouseout**** ");
	  		NC.widget.MenuManager.clearShowMenuTimer(); // if it is enabled
	  		var contentArea = document.getElementById( this.contentContainer);
	  		var cmd = NC.widget.Attribute.get( element, "cmd");
	  		if (cmd == this.ContentCommand) {
		  		var cid = NC.widget.Attribute.get( element, "cid");
		  		var menuId = NC.widget.Attribute.get( element, "menu");
		  		if (menuId != null && menuId.length > 0) {
//		  			this.toggleMenu( event, menuId, element);
		  		}
	  		}
	  	},

		getTabForContentType: function( contentTypeId) {
			var tabcontainer = document.getElementById( 'ContentTabList'); // this is bad....
			if (tabcontainer != null) {
		       	var tabs = tabcontainer.getElementsByTagName("li");
		       	for ( var i = 0; i < tabs.length; i++) {
			  		var cid = NC.widget.Attribute.get( tabs[i], "cid");
	  				if (cid > 0) {
	  					if (cid == contentTypeId) {
	  						return tabs[i];
	  					}
	  				}
		       	}
			}
			return null;
		},
	  	
	  	/*
	  	 * this function must be overridden by an subclass
	  	 */
	  	onTabSelected: function( event, selected) {
	  		var contentArea = document.getElementById( this.contentContainer);
	  		var cmd = NC.widget.Attribute.get( selected, "cmd");
	  		var edit = NC.widget.Attribute.get( selected, "edit");
	  		if (cmd == this.ContentCommand) {
			  	var menuId = NC.widget.Attribute.get( selected, "menu");
			  	var menu = document.getElementById( menuId);
			  	var liList = menu.getElementsByTagName('li');
			  	var updateUrl = null;
			  	/*
			  	 * the next few lines work around an observed difference in IE and Firefox
			  	 * its not supposed to be this way, but this is what appears to work
			  	 */
			  	  var litem = liList.item; // works for IE 6
			  	  if (litem.className == undefined) {
			  	  	litem = liList.item(0); // works for Firefox 2.0
			  	  }
		  		if ( litem != null) {
		  			updateUrl = NC.widget.Attribute.get( litem, "url");
		  		}
//			  	var menuSize = liList.length;
	  			if (updateUrl == null || updateUrl.length == 0 || edit == 1) {
	  				// construct a default update url
			  		var cid = NC.widget.Attribute.get( selected, "cid");
			  		if (cid > 0) {
						updateUrl = './sc.php?ct=' + cid;
			  		}
	  			}
	  			if (updateUrl != null) {
	  				if(typeof NC.promo.PromoManager.hidePromo == 'function') {
	  					NC.promo.PromoManager.hidePromo();
	  				}
	  				var funcIndex = updateUrl.indexOf( NC.util.AjaxManager.getNcFuncFlag());
		  			if ( funcIndex > -1) {
		  				var ncFunc = updateUrl.substring( funcIndex + NC.util.AjaxManager.getNcFuncFlag().length);
		  				if (ncFunc == 'showscore') {
		  					NC.widget.ResumeUploadManager.showResumeInfo( 'MainFrame');
							NC.util.AjaxManager.addMainFrameHistory(updateUrl);
		  				}
		  				else if (ncFunc == 'showjshome') {
			  				NC.widget.MemberManager.showMemberHome();
							NC.util.AjaxManager.addMainFrameHistory(updateUrl);
		  				}
		  				else if (ncFunc == 'showemphome') {
			  				NC.widget.MemberManager.showEmployerHome();
							NC.util.AjaxManager.addMainFrameHistory(updateUrl);
		  				}
		  				else if (ncFunc == 'showgetbadge') {
			  				NC.widget.MemberManager.showGetBadge();
							NC.util.AjaxManager.addMainFrameHistory(updateUrl);
		  				}
		  				else if (ncFunc == 'invitemember') {
			  				NC.widget.CandidateManager.showInvitePopup( 'inviteMemberForm.php', 'JsInviteForm', 'do_invite.php','', null, 2, -1);
		  				}
		  				else if (ncFunc == 'newwordad') {
			  				NC.widget.AdManager.showAdEditDialog( 'js_wordad_edit.php?NewAd=1', 'WordAdEditForm', 'do_update_wordad.php', 1);
		  				}
		  				else if (ncFunc == 'showjsquickstart') {
			  				NC.widget.MemberManager.showMemberQuickStart();
							NC.util.AjaxManager.addMainFrameHistory(updateUrl);
		  				}
		  			}
		  			else {
						var updater = new NC.util.AjaxUpdate( 'GET', updateUrl, this.contentContainer,  this.contentContainer, null);
						updater.doUpdate( true);
						NC.util.AjaxManager.addMainFrameHistory(updateUrl);
		  			}
	  			}
	  		}
	  		else if (cmd == this.MoreCommand) {
	  			this.rotateContent();
	  		}
	  	},
	  	
 		onMenuItemSelected: function( event, optionElement, optionLabel, optionValue) {
	  		if(typeof NC.promo.PromoManager.hidePromo == 'function') {
	  			NC.promo.PromoManager.hidePromo();
	  		}
			var url = NC.widget.Attribute.get( optionElement, 'url');
			var target = NC.widget.Attribute.get( optionElement, 'target');
			var func = NC.widget.Attribute.get( optionElement, 'func');
			var ct = -2;
			if (func != null && func.length > 0) {
				if (func == 'copyDivToIFrame') {
					copyDivToIFrame( target, url);
				}
				else if (func == 'showConversationPopupById') {
					var conversationId = NC.widget.Attribute.get( optionElement, 'cid');
					NC.widget.ConversationManager.showConversationPopupById(event, conversationId, "Notes");
					ct = 19;
				}
  				else if (func == 'invitemember') {
	  				NC.widget.CandidateManager.showInvitePopup( 'inviteMemberForm.php', 'JsInviteForm', 'do_invite.php','', null, 2, -1);
	  				ct = 5;
  				}
  				else if (func == 'newwordad') {
	  				NC.widget.AdManager.showAdEditDialog( 'js_wordad_edit.php?NewAd=1', 'WordAdEditForm', 'do_update_wordad.php', 1);
	  				ct=25;
  				}
  				else if (func == 'showgetbadge') {
	  				NC.widget.MemberManager.showGetBadge();
					NC.util.AjaxManager.addMainFrameHistory(url);
					ct = 7;
  				}
  				else if (func == 'uploadresume') {
					showUploadPane('centercontent');
					ct = 7;
  				}
  				else if (func == 'updateprivacy') {
					NC.widget.DashboardManager.showJsSelectPrivacyPopup( event,-1, false);
					ct = 7;
  				}
  				else if (func == 'updateprofile') {
					NC.widget.ResumeUploadManager.editResumeInfo(event,'./getResumeInfo.php', 'centercontent');
					ct = 7;
  				}
  				else if (func == 'showscore') {
  					NC.widget.ResumeUploadManager.showResumeInfo( 'MainFrame');
					NC.util.AjaxManager.addMainFrameHistory(url);
					ct = 7;
  				}
  				else if (func == 'showjsquickstart') {
	  				NC.widget.MemberManager.showMemberQuickStart();
					NC.util.AjaxManager.addMainFrameHistory(url);
					ct = -1;
  				}
			}
			else {
				if (target == '_blank') {
					window.location = url;
				}
				else {
	 				xmlHttpGet( url, true);
				}
			}
			if (ct == -2) {
				ct = NC.util.AjaxManager.findContentType( url);
			}
			if (ct > -2 && window.oContentTabContainer != null) { // find the correct tab and highlight  it
  				var tab = window.oContentTabContainer.getTabForContentType(ct);
  				if (tab != null) {
					NC.widget.TabView.selectTab( 'ContentTabList', tab); // ContentTabList must exist on the home page... bad?
  				}
			}
 		}
	  	
  	}
);  
  
 