
VH1 = new function(){
	this.version = 'charlie';
	
	this.init = function(){
		document.body.onmousedown = mouseDown;
		VH1.Events.addLinkEvents();
		return true;
	}
	
	mouseDown = function(e){
		if (VH1.UI.ModalWindow.isOpen) VH1.UI.ModalWindow.mouseDownBody(e);
		return true;
	}
}

VH1.UI = new function(){}

VH1.UI.ModalWindow = new function(){
	var modalWindowId = "modalWindow";
	var currentLocationId = "";
	this.ignoreMouseDownBody = false;
	this.isOpen = false;
	
	this.open = function(requestUrl, locationId){
		currentLocationId = locationId;
		VH1.UI.ModalWindow.updateLocation();
		VH1.Utils.ahah('/rss/home/xml/details.jhtml?rssId='+locationId,'dialogContent',' ');
	}
	
	this.updateLocation = function(){		
		var mwElement = document.getElementById(modalWindowId);
		var locElement = document.getElementById(currentLocationId);
		
		var locLeft = VH1.Utils.findPosX(locElement) - VH1.Utils.findPosX(document.getElementById("rss_template_columns"));	//left position relative to wrap instead of the window left
		var locTop = VH1.Utils.findPosY(locElement);
				
		var mwLeft = 0; 		
		var mwTop = 0;		

		if(locLeft < 320){ mwLeft = 25; }
		else if(locLeft > 320 && locLeft < 640){ mwLeft = 355; }
		else{ mwLeft = 535; }
		
		mwTop = locTop - (mwElement.offsetHeight + 1);
		mwTop = mwTop + 20;
		//mwTop = locTop + 20;

		leftPos = 0
		topPos = 0
		if (screen) {
			leftPos = (screen.width / 2) - 251
			topPos = (screen.height / 2) - 162
		}

		mwElement.style.left = mwLeft + "px";
		mwElement.style.top = topPos + "px";
		
		VH1.Utils.show(modalWindowId);
		VH1.UI.ModalWindow.isOpen = true;
	}
	
	this.close = function(){
		VH1.Utils.hide(modalWindowId);
		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;
	}
}

VH1.Content = new function(){}

VH1.Content.RSS = new function(){
	this.open = function(obj){
		var dataUrl = "/rss/home/xml/details.jhtml?rssId=" + obj.id;		
		VH1.UI.ModalWindow.open(dataUrl, obj.id);
	}
}

VH1.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(){
						VH1.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);
	}
}

VH1.Community = new function(){}

VH1.Community.Widgets = function(){
	this.errorLocation = '';
}
		
VH1.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.ahah = function(url, target, waitingMsg, delay){
		var req;
		document.getElementById(target).innerHTML = 'waiting...';
		if(waitingMsg.length > 0)
		document.getElementById(target).innerHTML = waitingMsg;
		
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (req != undefined) {
			req.onreadystatechange = function() {VH1.Utils.ahahDone(req, url, target, delay);};
			req.open("GET", url, true);
			req.send("");
		}
	}

	this.ahahDone = function(req, url, target, delay){
		if (req.readyState == 4) { // only if req is "loaded"
			if (req.status == 200) { // only if "OK"
				document.getElementById(target).innerHTML = req.responseText;

				//hack to get the input field "feed-url-input" the correct rss feed url.
				document.getElementById("feed-url-input").value = document.getElementById("rsslinkvalue").value;
			} else {
				document.getElementById(target).innerHTML="ahah error:\n"+req.statusText;
			}
			if (delay != undefined) {
				setTimeout("ahah(url,target,delay)", delay); // resubmit after delay
				//server should ALSO delay before responding
			}
		}
	}

	this.setClipBoard = function(obj){
		var isIE=(document.all)?true:false;
		if(isIE){
			ieClipBoard(obj);
		} else {
			c.setClipboard(obj.value);
		}							
	}

	this.ieClipBoard = function(tdObj){
		var holdtext = document.all['feed-url-input'];
		holdtext.innerText = tdObj.innerText;
		Copied = holdtext.createTextRange();
		Copied.execCommand("Copy");
	}
	
}

VH1.Utils.Clipboard = new function(){
	this.clipObj = '';
	this.Proxy = '';

	this.init = function(){
		//Input text field objects
		this.clipObj = document.getElementById("feed-url-input");

		//SWF Object copy proxy.
		this.Proxy = document.getElementById("copyProxy");
		return true;
	}

	this.setClipBoard = function(){
		var isIE=(document.all)?true:false;
		if(isIE){
			//this.ieClipBoard(clipObj);
			var holdtext = document.all['feed-url-input'];
			holdtext.innerText = this.clipObj.innerText;
			Copied = holdtext.createTextRange();
			Copied.execCommand("Copy");
		} else {
			VH1.Utils.Clipboard.Proxy.setClipboard(VH1.Utils.Clipboard.clipObj.value);
		}							
	}

	this.ieClipBoard = function(tdObj){
		var holdtext = document.all['feed-url-input'];
		holdtext.innerText = tdObj.innerText;
		Copied = holdtext.createTextRange();
		Copied.execCommand("Copy");
	}	
}