Community = new function(){}

Community.Page = new function(){	
	this.noHat = false;
	this.hostname = window.location.hostname;
	this.path = window.location.pathname;
	this.qs = window.location.search.substring(1);
	this.vars = this.qs.split("&");
	this.baseUrl = (this.hostname.indexOf("mtv-q.mtvi.com") > 0) ? "http://www.mtv-d.mtvi.com" : "http://www.mtv.com";
	this.headTag = document.getElementsByTagName("head").item(0);
	this.cssOverrideSet = false;
	
	this.isFN = false;
	
	/*
	JS/JSON FOR OLD MTV.COM NAV - EAR 8/17/09
	this.init = function(){
		
		Community.Page.updatePage();
		
		Community.Widgets.createFooter('MTVFooter');
		
		if(Community.Page.isFN){
			Community.Widgets.createNav('MTVNav', Community.Widgets.fnHeaderObj);
		}
		else{
			Community.Widgets.createArtistlist('MTVArtistList');
			Community.Widgets.createSearch('MTVSearch');
			Community.Widgets.createNav('MTVNav');
		}
	}
	THE FOLLOWING FOR THE NEW MTV.COM NAV
	*/
	this.init = function(){
		
		Community.Page.updatePage();
		
		Community.Widgets.createFooter('MTVFooter');
		
		if(Community.Page.isFN){
			Community.Widgets.createNav('MTVNav', Community.Widgets.fnHeaderObj);
		}
		else{
			Community.Widgets.createNav('nav');
		}
	}
	
	this.createScript = function(url){
		var scriptObj = document.createElement("script");
		scriptObj.setAttribute("type", "text/javascript");
		scriptObj.setAttribute("src", Community.Page.baseUrl+url);

		Community.Page.headTag.appendChild(scriptObj);
	}
	
	this.setDiscussionCSS = function(){
		var tcId = "";
		
		for (var i=0; i<Community.Page.vars.length; i++) {
		    var pair = Community.Page.vars[i].split("=");
		    if (pair[0] == "tcid" || pair[0] == "tcId") {
		       tcId = pair[1];
		    } 
		}

		if(tcId!==''){
			var categories = Community.Data.discussionCategories;
	
			for(var i=0; i<categories.length; i++){
				if(categories[i].id==tcId){
					if(typeof categories[i].css == 'string'){
						Community.Page.addCSSLink(categories[i].css);
						Community.Page.cssOverrideSet = true;
					}			
				}
			}
		}	
		
	}
	
	this.addCSSLink = function(url){
		var cssUrl = Community.Page.baseUrl + url;
		var linkObj = document.createElement("link");

		linkObj.setAttribute("rel", "stylesheet");
		linkObj.setAttribute("type", "text/css");
		linkObj.setAttribute("href", cssUrl);

		Community.Page.headTag.appendChild(linkObj);
	}
	
	this.handleFnVideoCategory = function(response){
		var categories = response.categories.category
		var category = "";
	
		for (var i=0; i<Community.Page.vars.length; i++) {
		    var pair = Community.Page.vars[i].split("=");
		    if (pair[0] == "category") {
		       category = pair[1];
		    } 
		}

		for(var i=0; i<categories.length; i++){
			if(categories[i].id==category){
				Community.Page.isFN = true;
				Community.Page.addCSSLink('/ontv/fn-mtv/css/hosted_pages.css');
				Community.Page.cssOverrideSet = true;
				Community.Data.fnVideoCategory = categories[i];
			}
		}

	}
	
	this.updatePage = function(){		
		// fn video category pages
		if(Community.Data.fnVideoCategory!="" && document.getElementById("ctl01_ctl00_ctl00_phBody_phBody_phBody_ctl00_ctl01_divAddModuleArea")==null){	
			var category = Community.Data.fnVideoCategory;
			var fnHeader = document.createElement("div");
			fnHeader.className = "fn-header";
			
			var h1 = document.createElement("h1");
			h1.innerHTML = "FN Uploads <em> | " + category.name + "</em>";
			h1.className = "main-title"

			var img = document.createElement("img");
			img.className = "thumb";
			img.setAttribute("src", Community.Page.baseUrl + category.images.img.src);
			
			var div = document.createElement("div");
			div.className = "deck";
				
				if(category.description!=null){
					var p = document.createElement("p");
					p.appendChild(document.createTextNode(category.description));
					div.appendChild(p);
				}
				
				var action = document.createElement("a");
				action.className = "action";
				action.setAttribute("href", Community.Page.baseUrl + "/ontv/fn-mtv/uploads.jhtml");
				action.appendChild(document.createTextNode("Back to all uploads categories"));
				div.appendChild(action);
				
				if(category.videoId!=null){
					var button = document.createElement("a");
					button.setAttribute("href", Community.Page.baseUrl + "/ontv/fn-mtv/upload.jhtml?categoryId=" + category.id);
					button.innerHTML = "<" + "img src=\"" + Community.Page.baseUrl + "/content/ontv/fn-mtv/images/bttn-upload-to-category.gif\"/>";
					div.appendChild(button);	
				}
				
			fnHeader.appendChild(h1);			
			fnHeader.appendChild(img);
			fnHeader.appendChild(div);

			var zone = document.getElementById("ctl01_ctl00_ctl00_phBody_phBody_phBody_ctl00_zone2_zone2");			
			zone.insertBefore(fnHeader, zone.childNodes[0]);
			
		}// fn video category pages
		
		
		// trim non-community greeting
		if(!document.getElementById('ctl01_ctl00_ctl00_phBody_phBody_quickMenu_pnlCommunityMember')){
			var q = document.getElementById('quickmenu');
			if (q) {
				var spans = q.getElementsByTagName('span');
				var container;

				for (var x=0; x<spans.length; x++){
					if (spans[x].className.indexOf("userGreetingContainer") == 0) container = spans[x];
				}

				if(container){
					for (var i=0; i<container.childNodes.length; i++){
						var element = container.childNodes[i];
						if(element.nodeName!='BR' && (!element.className || element.className.indexOf("userGreeting") < 0)) container.replaceChild(document.createTextNode(''), element);
					}
				}
			}			
		}
		
		// trim friend request text
		var pnlDelimiter = document.getElementById('ctl01_ctl00_ctl00_phBody_phBody_quickMenu1_pnlDelimiter');
		if(pnlDelimiter){
			for (var i=0; i<pnlDelimiter.childNodes.length; i++){
				var element = pnlDelimiter.childNodes[i];
				if(element.nodeName=='#text'){
					if(element.nodeValue.indexOf('||') >= 0){
						pnlDelimiter.replaceChild(document.createTextNode(' | '), element);
					}
					else pnlDelimiter.removeChild(element);
				}
			}
		}
		
		// add User Search link to Friends page
		if(Community.Page.path.indexOf('FriendsManagement.aspx') > 0 || Community.Page.path.indexOf('FriendsManagment.aspx') > 0){
			var inviteLink = document.getElementById('ctl01_ctl00_ctl00_phBody_phBody_phBody_cFriendManagement_btnReorderFriends');
			
			if(inviteLink){
				var tbody = inviteLink.parentNode.parentNode.parentNode;
				var newTR = document.createElement('tr');
				
				var newTD = document.createElement('td');
				newTD.setAttribute('align', 'right');
				
				var newA = document.createElement('a');
				newA.setAttribute('href', '/-/Community/BrowseUsers.aspx');  
				newA.appendChild(document.createTextNode('Browse Members'));
				
				newTD.appendChild(newA);
				newTR.appendChild(document.createElement('td'));
				newTR.appendChild(newTD);
				tbody.appendChild(newTR);
			}
		}// end user search

		// add Upload links to Feed Detail pages
		if(Community.Page.path.indexOf('/Content/Picture/Pictures.aspx') >0 || Community.Page.path.indexOf('/Content/Video/Videos.aspx') > 0){
			var contentType;
			if(Community.Page.path.indexOf('/Content/Picture/Pictures.aspx') >0) contentType = 'photo';
			else if(Community.Page.path.indexOf('/Content/Video/Videos.aspx') >0) contentType = 'video';
			
			var hook = document.getElementById('ctl01_ctl00_ctl00_phBody_phBody_phBody_contentFilterControl_lbPersonal');
			
			if(hook){
				var sidebar = hook.parentNode.parentNode.parentNode.parentNode;
				
				var a = document.createElement('a');
				var text;
				
				switch(contentType){
					case 'video': 
						a.setAttribute('href', '/-/Content/Video/AddVideo.aspx');
						text = 'Upload Videos';
						break;
					case 'photo': 
						a.setAttribute('href', '/-/Content/Picture/AddPhoto.aspx');
						text = 'Upload Photos';
						break;
					default: break;
				}			
				a.appendChild(document.createTextNode(text));
				
				sidebar.appendChild(a);
			}
		}// end upload links
		
		
		// discussion boards breadcrumb
		if(Community.Page.path.indexOf('DiscussionResults.aspx') > 0 && (Community.Page.qs.indexOf('tcid') >= 0 || Community.Page.qs.indexOf('tcId') >= 0)){
			var tcid = "";
		
			for (var i=0; i<Community.Page.vars.length; i++) {
			    var pair = Community.Page.vars[i].split("=");
			    if (pair[0] == "tcid" || pair[0] == "tcId") {
			       tcid = pair[1];
			    } 
			}
			
			var categories = Community.Data.discussionCategories;
	
      
			for(var i=0; i<categories.length; i++){
				if(categories[i].id==tcid){
          var breadcrumb = document.getElementById("breadcrumb");
					
					breadcrumb.href = Community.Page.baseUrl + categories[i].link.args;
	
					var newChild = document.createTextNode(categories[i].link.content);
					var oldChild = breadcrumb.childNodes[0];
					breadcrumb.replaceChild(newChild, oldChild);
				}
			}
      
		}// end discussion boards breadcrumb
		
		if(Community.Page.path.indexOf('Topic') > 0){			
			var breadcrumb = "";
			var catId = "";
			
			var tds = document.getElementsByTagName("td");
			
			for(var i=0; i<tds.length; i++){
				if(tds[i].id.indexOf('fluxBreadCrumbs_rptBreadCrumbs_ctl01_tdBreadCrumbCell') > 0){
					breadcrumb = tds[i].getElementsByTagName("a")[0];
				}
				if(tds[i].id.indexOf('fluxBreadCrumbs_rptBreadCrumbs_ctl03_tdBreadCrumbCell') > 0){
					var categoryLink = tds[i].getElementsByTagName("a")[0].href;
					catId = categoryLink.substring(categoryLink.indexOf("catId=")+6, categoryLink.length);
				}
			}
			
			var categories = Community.Data.discussionCategories;
			for(var i=0; i<categories.length; i++){
				if(categories[i].id==catId){
					breadcrumb.href = Community.Page.baseUrl + categories[i].link.args;
	
					var newChild = document.createTextNode(categories[i].link.content);
					var oldChild = breadcrumb.childNodes[0];
					breadcrumb.replaceChild(newChild, oldChild);
				}
			}
		}
		
		
	}// end this.updatePage
	
}

Community.Data = new function(){
	this.discussionCategories = "";
	this.fnVideoCategory = "";

	this.setDiscussionCategories = function(obj){
		Community.Data.discussionCategories = obj.categories.category;
		Community.Page.setDiscussionCSS();
	}
}