


MTV = new function(){
	this.version = 'charlie';
	
	this.init = function(){
		document.body.onmousedown = mouseDown;
		MTV.Events.addLinkEvents();
		MTV.Voting.init();
		return true;
	}
	
	mouseDown = function(e){
		if (MTV.UI.ModalWindow.isOpen) MTV.UI.ModalWindow.mouseDownBody(e);
		return true;
	}
}

MTV.UI = new function(){}

MTV.UI.ModalWindow = new function(){
	var modalWindowId = "modalWindow";
	var currentLocationId = "";
	this.ignoreMouseDownBody = false;
	this.isOpen = false;
	
	this.open = function(requestUrl, locationId){
		currentLocationId = locationId;
		MTV.Utils.requestTaconite(requestUrl, '','','', MTV.UI.ModalWindow.updateLocation);
		//this.isOpen = true;
	}
	
	this.updateLocation = function(){		
		var mwElement = document.getElementById(modalWindowId);
		var mwLeft = 0; 		
		var mwTop = 0;
	
		//MTV.Utils.makeVisible(modalWindowId);
		MTV.Utils.show(modalWindowId);
		
		if(!currentLocationId){
			// defaults to centered position
			mwTop = Math.round(((document.documentElement.clientHeight - mwElement.offsetHeight)/2) + document.documentElement.scrollTop) - 30; // 30 pixel cheat
			mwLeft = 257; // centered for 446px-wide modal window			
		}
		else{
			//positioning relative to an element
			var locElement = document.getElementById(currentLocationId);
			
			var locLeft = MTV.Utils.findPosX(locElement) - MTV.Utils.findPosX(document.getElementById("wrap"));	//left position relative to wrap instead of the window left
			var locTop = MTV.Utils.findPosY(locElement);
					
			if(locLeft < 320){ mwLeft = -20; }
			else if(locLeft > 320 && locLeft < 640){ mwLeft = 257; }
			else{ mwLeft = 533; }
			
			//mwTop = locTop - (mwElement.offsetHeight + 1);
			mwTop = locTop + 20;
		}
		
		mwElement.style.left = mwLeft + "px";
		mwElement.style.top = mwTop + "px";
		
		MTV.UI.ModalWindow.isOpen = true;
	}
	
	this.close = function(){
		//MTV.Utils.makeInvisible(modalWindowId);
		//MTV.Utils.hide(modalWindowId);
		var mwElement = document.getElementById(modalWindowId);
		mwElement.style.left = "-9999px";
		currentLocationId="";
		this.isOpen = false;
	}

	this.mouseDown = function(e){
		this.ignoreMouseDownBody = true;
		return true;
	}
	
	this.mouseDownBody = function(e){
		if (!this.ignoreMouseDownBody){
			this.close();
		}
		this.ignoreMouseDownBody = false;
		return true;
	}
}

MTV.UI.Tab = new function(){
	this.open = function(obj){
		var newActiveTabId = obj.parentNode.id;
		var newActivePageId = newActiveTabId.substring(0, newActiveTabId.indexOf("Tab"));

		var tab = document.getElementById(newActiveTabId);
		var tabList = tab.parentNode;
		var tabs = tabList.getElementsByTagName("li");

		// set new active tab
		for(var i=0; i < tabs.length; i++){
			var t = tabs[i];
			
			if(t.id == newActiveTabId){
				if(t.className.indexOf('active') < 0){
					t.className = t.className + " active";
				}
			}
			else{
				if(t.className.indexOf('active') >= 0){
					t.className = t.className.substring(0, t.className.indexOf("active"));
				}
			}
		}

		// set new active page
		var page = document.getElementById(newActivePageId);
		var pageList = page.parentNode;
		var pages = pageList.childNodes;
		
		for(var i=0; i < pages.length; i++){
			var p = pages[i];

			if(p.nodeName=="DIV"){
				if(p.id == newActivePageId){
					MTV.Utils.show(p.id);
				}
				else{
					MTV.Utils.hide(p.id);
				}
			}
		}
		
	}
}

MTV.Content = new function(){}

MTV.Content.RSS = new function(){
	this.open = function(obj){
		var dataUrl = "/rss/xml/detail.jhtml?rssId=" + obj.id;		
		MTV.UI.ModalWindow.open(dataUrl, obj.id);
	}
}

MTV.Voting = new function(){
	this.pc = "";
	
	this.init = function(){
		MTV.Voting.pc = new PlayerController("MTV.Voting.pc");
		MTV.Voting.pc.configUrl = MTV.Voting.Player.configUrl;
		
		var allDivs = document.getElementsByTagName("div");
		
		for (var i=0; i< allDivs.length; i++){
			switch(allDivs[i].className){
				case 'voting-player': 
					if (MTV.Voting.Player.active){
						var playerUrl = "";
						var targetId = allDivs[i].id;
						var embedId = targetId + "Player";
	
						var vars = targetId.split(".");
						for (var j=0; j<vars.length; j++) {
						    var pair = vars[j].split(":");
							playerUrl = playerUrl + pair[0] + "=" + pair[1];
						    if(vars[j+1]) playerUrl += "&";
						}
						
						playerUrl += "&o=1";
						
						MTV.Voting.pc.write2(MTV.Voting.Player.configUrl, playerUrl, targetId, embedId, MTV.Voting.Player.width, MTV.Voting.Player.height, []);
					}
					break;
				case 'vote-btn': 
					if (MTV.Voting.Button.active){
						var flashVars = "";
						var targetId = allDivs[i].id;
						var embedId = targetId + "Button";
						
						var vars = targetId.split(":"); //get pollId and value
						var pollId = vars[0];
						var value = vars[1];
						
						flashVars += "cogix=" + MTV.Voting.Button.cogix + "&amp;";
						flashVars += "pollid=" + pollId + "&amp;";
						flashVars += "answer" + "=" + value + "&amp;"; 
						flashVars += "validator=" + MTV.Voting.Button.validator + "&amp;"; 
						flashVars += "authClass=" + MTV.Voting.Button.authClass; 
						
						var so = new SWFObject(MTV.Voting.Button.src, embedId, MTV.Voting.Button.width, MTV.Voting.Button.height, "9", "#000000");
						so.addParam("flashVars", flashVars);
						so.addParam("AllowScriptAccess", "sameDomain");
						so.addParam("wmode", "transparent");
						so.useExpressInstall('/sitewide/components/expressInstall/adobeExpressInstall.swf');
						so.write(targetId);
					}
					break;
				default: break;
			}
		}
	}
}

MTV.Voting.Player = new function(){
	this.active = false;
	this.configUrl = "";
	this.width = "240";
	this.height = "180";
}

MTV.Voting.Button = new function(){
	this.active = false;
	this.src = "/sitewide/components/buttons/vote/voteButton.swf";
	this.width = "75";
	this.height = "30";
	this.cogix = "polling-onair";
	this.validator= "timestamp|votes";
	this.authClass = "com.mtvnet.auth.dis.HashDigestInputStrategy1";
}

MTV.Events = new function(){
	this.addLinkEvents = function(){
		var allLinks = document.getElementsByTagName("a");
		
		for (var i=0; i< allLinks.length; i++){
			switch(allLinks[i].className){
				case 'rssLink': 
					allLinks[i].onclick = function(){
						MTV.Content.RSS.open(this); 
						return false;
					}
					break;
				default: break;
			}
		}		
	}	
	
	// addEventSimple and removeEventSimple courtesy of quirksmode.org
	this.addEventSimple = function(obj,evt,fn) {
		if (obj.addEventListener)
			obj.addEventListener(evt,fn,false);
		else if (obj.attachEvent)
			obj.attachEvent('on'+evt,fn);
	}
	
	this.removeEventSimple = function(obj,evt,fn) {
		if (obj.removeEventListener)
			obj.removeEventListener(evt,fn,false);
		else if (obj.detachEvent)
			obj.detachEvent('on'+evt,fn);
	}
}

MTV.Community = new function(){}

MTV.Community.Widgets = function(){
	this.errorLocation = '';
}
		
MTV.Utils = new function(){
	this.show = function(id){
		var element = document.getElementById(id);
		if (element!=null && element.className.indexOf('hide') >= 0){
			element.className = element.className.substring(0, element.className.indexOf('hide'));
		}
	}
	
	this.hide = function(id){
		var element = document.getElementById(id);
		if (element!=null){
			element.className = element.className + " hide";
		}
	}
	
	this.makeVisible = function(id){
		var element = document.getElementById(id);
		if (element!=null && element.className.indexOf('hidden') >= 0){
			element.className = element.className.substring(0, element.className.indexOf('hidden'));
		}
	}
	
	this.makeInvisible = function(id){
		var element = document.getElementById(id);
		if (element!=null){
			element.className = element.className + " hidden";
		}
	}
	
	this.findPosX = function(obj){
		var curleft = 0;
		if(obj.offsetParent)
			while(1){
				curleft += obj.offsetLeft;
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
		else if(obj.x)
			curleft += obj.x;
		return curleft;
	}
	
	this.findPosY = function(obj){
		var curtop = 0;
		if(obj.offsetParent)
			while(1){
				curtop += obj.offsetTop;
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
		else if(obj.y)
			curtop += obj.y;
		return curtop;
	}
	
	this.requestTaconite = function(url, formElements, async, preRequest, postRequest){
		var ar = new AjaxRequest(url);
		if (formElements != undefined){
			for (var i=0; i < formElements.length; i++){
				ar.addNamedFormElements(formElements[i]); 
			}
		}
		
		if (async!=null && async!=''){ ar.setAsync(async); }
		if (preRequest!=null && preRequest!=''){ ar.setPreRequest(preRequest); }
		if (postRequest!=null && postRequest!=''){ ar.setPostRequest(postRequest); }
		
		ar.sendRequest();
	}
	
}

