        var plSlideDelay        = '000'; // Slide out predelay for carousel. was 0
        var plSlideTime         = '400'; // How long it takes to slide out Playlist. was 400
        var mainSlideDelay      = '000'; // Slide out predelay  for main menu
        var mainSlideTime       = '500'; // How long it takes to slide out main Menu
        var voShowTime          = '30'; // Video Overlay show delay
        var adFadeTime          = '1000'; // Time to show AD
        var adDelayTime         = '5000'; // Time to show AD
        var playerTimer         = '800'; // Delay before showing video.

        var adPeriodical        = '5000'; // Time to show AD --- to local. Nolonger in use
        var runBugChain         = $empty; // Nolonger in use
        var bugTimer            = $empty; // Nolonger in use

        // The following variables are for replacing the above 3 variables the nolonger in use.
	/*
        var adPeriodical_siteSpo        = '10000'; // Time to show siteSpo AD --- to local
        var adPeriodical_channelSpo     = '7000'; // Time to show channelSpo AD --- to local
        var adPeriodical_showSpo        = '5000'; // Time to show showSpo AD --- to local
        var adPeriodical_windowBugs     = '2000'; // Time to show windowBugs AD --- to local
        var adPeriodical_trailer        = '3000'; // Time to show trailer AD --- to local

        var siteSpoChain                = $empty;
        var channelSpoChain             = $empty;
        var showSpoChain                = $empty;
        var windowBugsChain             = $empty;
        var trailerChain                = $empty;

        var runsiteSpoChain             = $empty;
        var runchannelSpoChain          = $empty;
        var runshowSpoChain             = $empty;
        var runwindowBugsChain          = $empty;
        var runtrailerChain             = $empty;

        var siteSpoTimer                = $empty;
        var channelSpoTimer             = $empty;
        var showSpoTimer                = $empty;
        var windowBugsTimer             = $empty;
        var trailerTimer                = $empty;
	*/
	
	/*
	var customTips = $$('.tooltip');
	var toolTips = new Tips(customTips, {
		showDelay: 10,
		hideDelay: 10,
		offsets: {'x': 100, 'y': 16},
		fixed: false,
	});
	*/

	/* 
	 * Setup elements with mootools functions 
	*/
	var plMenu = $('plMenu');
	plMenu.set('slide',   {duration: plSlideTime, transition: Fx.Transitions.Pow.easeOut});
	plMenu.setStyle('margin', '0px auto');
	plMenu.slide('hide');
	plMenu.setStyle('visibility', 'visible');

	var mainMenu = $('menuOverlay');
	mainMenu.set('slide', {duration: mainSlideTime, transition: Fx.Transitions.Pow.easeOut});
	mainMenu.setStyle('margin', '0px auto');
	mainMenu.slide('hide');
	mainMenu.setStyle('visibility', 'visible');
	
	// Video Overlay element.
	var vidOverlay = $('vidOverlay'); 
	vidOverlay.set('tween', {duration: 'short', link: 'ignore'});
	vidOverlay.fade('hide');

	var infoMenu = $('rBox');
	var adWindow = $('adArea');
	var uvtvLogo = $('uvtvLogo');

	uvtvLogo.addEvents({
		mouseenter: function() {
			voToggle('0', 'logo', '0');
		},
		mouseleave: function() {
			voHide();
		},
		click: function() {
			document.location.href = "/";
		}
	})
	mainMenu.addEvents({
		mouseenter: function() {
			voHide();
		},
		mouseleave: function() {
			voHide();
		}
	});
	plMenu.addEvents({
		mouseenter: function() {
			voHide();
		},
		mouseleave: function() {
			voHide();
		}
	});
	$('bottomNavBar').addEvents({
		mouseenter: function() {
			voHide();
		},
		mouseleave: function() {
			voHide();
		}
	});

	infoMenu.addEvents({
		mouseenter: function() {
			voHide();
		},
		mouseleave: function() {
			voHide();
		}
	});
	adWindow.addEvents({
		mouseenter: function() {
			voHide();
		},
		mouseleave: function() {
			voHide();
		}
	});

	/* 
	 * This activates the click function for search 
	*/
	var sboxFocus = null;
	$('sButton').addEvent('click', function() {
		plToggle(document.getElementById('sBox').value, 'search');
		$('sBox').value = '';
	});

	var sBox = $('sBox');

	sBox.addEvents({
		click: function() {
			sBox.select();
			sboxFocus = 'true';
		},
		blur: function() {
			sboxFocus = null;
		}

	});

	window.addEvent('keydown', function(event) {
	  if(sboxFocus) { 
		if(event.key == 'enter') {
			plToggle(document.getElementById('sBox').value, 'search');
		}
	  }
	});
	/* This activates the mouseenter functino to hide
	   all menus.
	*/
	var vWrap = $('vidWrap');
/*
	vWrap.addEvents({
	  'mouseenter': function() {
		hideMenus();
		voHide();
	  }
	});
*/
	function popDiv() {
	        var popDiv = new Element('div', {'id':'popDiv'});
	        popDiv.injectInside($('mainWrap'));
	        var popWidth = popDiv.getStyle('width').replace(/px/, '');
	        var popHeight= popDiv.getStyle('height').replace(/px/, '');
	        var wCenter = ((window.getWidth() / 2) - (popWidth / 2));
	        var hCenter = ((window.getHeight() / 2) - (popHeight/ 2));
	        popDiv.setStyle('left', (wCenter - 4));
	        popDiv.setStyle('top', 16);
	        popDiv.setStyle('visibility', 'visible');
	
	        var closeBox = new Element('img', {'id':'closeBox', 'src':'/images/closeButton.png'});
	        closeBox.injectInside(popDiv);
	        closeBox.addEvent('click', function() {
	                popDiv.dispose();
	        });
	        var popDivData = new Element('div', {'id': 'popDivData'});
	        popDivData.injectInside(popDiv);
	        // Now add Shit to box.
	        return popDivData;
	}
	function playLarger(id) {
	var type = '';
	var pData = popDiv();
	setTimeout(function() {
	var req = new Request.HTML({url: '/ajax/vidPlayer.php?id='+id+'&type='+type+'&psudo='+psudoGen(),
			    evalScripts: true,
			    evalResponse: true,
		onSuccess: function(tree, elements, html, rjs) {
			pData.set('text', '');
			setTimeout(function() {
				pData.adopt(tree);
				$('fullPlayer').setStyle('height', '340px');
				$('fullPlayer').setStyle('width', '620px');
			}, playerTimer);
		},
		onFailure: function() {
			pData.set('text', 'Request Failed');
		}
	});
	req.send();
	}, 500);
	

	}

	/* hideMenus() ------------------------------------------------------------------------------------------------
	This is called to  hide menus.
	since I cant detect which is open 
	at any given time, I use this to
	close all of them
	*/
	function hideMenus2() {
		plMenu.slide('out');
		mainMenu.slide('out');
	}
	function hideMenus() {
		plMenu.set('text', '');
		plMenu.slide('out');

		mainMenu.set('text', '');
		mainMenu.slide('out');
	}


	/* openLink() ------------------------------------------------------------------------------------------------
	ARGS: link = URL to open
	*/
	function openLink(type, link) {
		var theLink;
		if(type != 'link') { link = '/rdocs/'+link; }
		window.open(link);
	}

	/* showSiteInfo() ------------------------------------------------------------------------------------------------
	ARGS: el = Element to inject info into
	Create elements for site info and display them.
	*/
	function showSiteInfo(el) {
		el.set('text', '');
		var mailTo = new Element('a', {'href':'mailto:sales@johnsondigitalmedia.com?subject=UVTV Sales Inquery', 
					      'html':'How to Advertise'});
		var mailUs= new Element('a', {'href':'mailto:info@johnsondigitalmedia.com?subject=UVTV Contact', 
					      'html':'Contact Us'});
		var phoneNum = new Element('div', {'html':'Phone: 707 463-1614'});
	
		var emailList = new Element('a', {'href':'#', 
						  'onClick':'javascript: eListForm(); return false;',
						  'html': 'Sign up for Email List'});
		var brEl	 = new Element('br');

		mailTo.injectInside(el);
		brEl.injectInside(el);
		mailUs.injectInside(el);
		brEl.injectInside(el);
		phoneNum.injectInside(el);
		brEl.injectInside(el);
		emailList.injectInside(el);
		
	}

	function eListForm() {
		mainMenu.set('text', '');
		var emailInput = new Element('input', {'type':'text',
						'id':'emailBox',
						'name':'emailAddr',
						'class':'mailInput',
						'onclick':'this.value = \'\'',
						'value':'Email Address'});
		var fNameInput = new Element('input', {'type':'text',
						'id':'fNameBox',
						'name':'fName',
						'class':'mailInput',
						'onclick':'this.value = \'\'',
						'value':'First Name (optional)'});
		var lNameInput = new Element('input', {'type':'text',
						'id':'lNameBox',
						'name':'lName',
						'class':'mailInput',
						'onclick':'this.value = \'\'',
						'value':'Last Name (optional)'});

		var closeButton = new Element('img', {
						'id':'emailClose',
						'src':'/images/closeButton.png',
						'onclick':'javascript: hideMenus();'});

		var mailWrapper = new Element('div', {'class':'mailWrapper'});

		var brEl	 = new Element('br');
		
		var submitButton = new Element('input', {'type':'submit',
						'value':'Sign Up', 
						'class':'mailSubmit'
		});

		var cancelButton = new Element('input', {'type':'submit',
						'value':'cancel',
						'class':'mailSubmit'
		});
		cancelButton.addEvent('click', function() {
			hideMenus();
		});
		submitButton.addEvent('click', function() {
			signUpEmail(document.getElementById('fNameBox').value, document.getElementById('lNameBox').value, document.getElementById('emailBox').value);
		});

		mailWrapper.injectInside(mainMenu);
		fNameInput.injectInside(mailWrapper);
		//brEl.injectInside(infoMenu);
		lNameInput.injectInside(mailWrapper);
		//brEl.injectInside(infoMenu);
		emailInput.injectInside(mailWrapper);
		//brEl.injectInside(infoMenu);
		submitButton.injectInside(mailWrapper);
		cancelButton.injectInside(mailWrapper);
	}

	function signUpEmail(fName, lName, emailAddr) {
	  if(fName.match(/optional/g)) {
		fName = null;
	  }
	  if(lName.match(/optional/g)) {
		lName = null;
	  }
	  if(!emailAddr.match(/@/g)) {
		alert('Please enter Valid Email Address');
		eListForm();
		return;
	  }
	
	  var req = new Request.HTML({url: '/ajax/emailList.php?fName='+fName+'&lName='+lName+'&emailAddr='+emailAddr+'&psudo='+psudoGen(),
			  onSuccess: function(HTML) {
				mainMenu.set('text', '');
				mainMenu.adopt(HTML);
				setTimeout(function() {
					hideMenus();
				}, 1000);
			  },
			  onFailure: function() {
				alert('FAIL');
			  }
			});
		  req.send();
	}

	/* runIntroVid() ------------------------------------------------------------------------------------------------
	ARGS: id = vimeo ID
	      type = show, channel, playlist
	This is the function that fetches the vimeoID 
	from the channel or show table for given 
	channel or show.
	*/
	function runIntroVid(id, type) {
	  var req = new Request({url: '/ajax/getIntro.php?id='+id+'&type='+type+'&psudo='+psudoGen(),
			  onSuccess: function(id) {
				selectVideo(id);
			  },
			  onFailure: function() {
				alert('FAIL');
			  }
			});
		  req.send();


	}

	/* showBuyNow(id) ------------------------------------------------------------------------------------------------
	ARGS: id = playlistID
	*/
	function showBuyNow(id) {

	}

	/* getMainMenu(type) ------------------------------------------------------------------------------------------------
	ARGS: type = channel or show
	Fetches menu for either channel or show.
	*/
	function getMainMenu2(type) {
	  var req = new Request.HTML({url: '/ajax/mainMenu.php?type='+type+'&psudo='+psudoGen(),
			  //onSuccess: function(tree, elements, html, js) {
			  onSuccess: function(html) {
				// Clear DIV
				mainMenu.set('text', '');
				//Inject DOM
				//mainMenu.adopt(html);
				mainMenu.set('html', html.html);
			  },
			  onFailure: function() {
				mainMenu.set('text', 'Request Failed');
			  }
			});
		  req.send();

	}

	function getMainMenu(type) {
	  var req = new Request.HTML({url: '/ajax/mainMenu.php?type='+type+'&psudo='+psudoGen(),
		//method: 'get',
		//update: $(elementID),
		evalScripts: true,
		evalResponse: true,
		onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			mainMenu.set('text', '');
			var closeDiv = new Element('div', {'id':'closeButton'});
			var closeBut = new Element('img', {'src':'/images/closeButton.png'});
			closeDiv.injectInside(mainMenu);
			closeBut.injectInside(closeDiv);
			mainMenu.adopt(responseTree);
			closeBut.addEvent('click', function() {
				hideMenus();
			});
			responseElements.each(function(element) {
			  // Setup CLOSEBUTTON
			  if(element.get('id') === 'closeButton') {
				element.addEvent('click', function() { 
					hideMenus(); 
				});
			  	if(element.get('class').match("menuItem") === null) { return; }
			  } else {

			  // DO PLTOGGLE EVENT
			  if(element.get('id') === 'mailSignup') {
				element.addEvent('click', function() {
					eListForm();
				});
			  } else {
			    if(element.get('class') === 'menuItem') {
			      element.addEvent('click', function() {
				plToggle(element.get('id'), element.get('type'), 0);
			      });
			    }
			  }

			  // VIDEO OVERLAY-----------------------------------------------
			  element.addEvents({
				mouseenter: function() {
				  voToggle(element.get('id'), element.get('type'));
				},
				mouseleave: function() {
				  voHide();
			  	}
			  });
			  }
			});
		},
		onFailure: function() {
			alert('failed');

		}
		});
		req.send();
	}

	/* getPlMenu(id, type, cnt) ------------------------------------------------------------------------------------------------
	ARGS:	id = id of channel or show
		type = channel or show
		cnt = Where to start
	Fetches carousel type widget, starts with count of 0,
	when called recursivly it changes the cnt value respectivly.
	*/
	function getPlMenu(id, type, cnt) {
	  if(cnt < 0) { return; }
	  var req = new Request.HTML({
			  url: '/ajax/plMenu.php?id='+id+'&type='+type+'&cnt='+cnt+'&psudo='+psudoGen(),
			  method: 'get',
			  evalScripts: true,
			  evalResponse: true,
			  urlEncoded: true,
			  onSuccess: function(responseTree, responseElements, responseHTML, responseJavascript) {
				// Clear DIV
				plMenu.set('text', '');
				//Inject DOM
				plMenu.set('html', responseHTML);
			  },
			  onFailure: function() {
				plMenu.set('text', 'Request Failed');
			  }
			});
		  req.send();
	}
	
	
	/* getPLInfo(id, type) ------------------------------------------------------------------------------------------------
	ARGS:	id = id
		type = channel, show, playlist
	Fetches information about the item, then displays it in 
	infoMenu area.
	*/
	function getPLInfo(id, type) {
	  var req = new Request.HTML({url: '/ajax/infoMenu.php?id='+id+'&type='+type+'&psudo='+psudoGen(),
		evalScripts: true,
		onSuccess: function(html) {
			infoMenu.set('text', '');
			infoMenu.adopt(html);
		},
		onFailure: function() {
			infoMenu.set('text', 'getPLInfo Failed');
		}
	  });
	req.send();
	}


	/* plToggle(id, type) ------------------------------------------------------------------------------------------------
	ARGS:	id = id
		type = playlist, or search
	Fetches menu and displays it.
	*/
	function plToggle(id, type) {
	  hideMenus();
	  if(type != 'search') { getPLInfo(id, type); }
	  getPlMenu(id, type, 0);
	  setTimeout(function() {
		 	 plMenu.slide('in');
	  }, plSlideDelay);

	}

	/* menuToggle(type) ------------------------------------------------------------------------------------------------
	ARGS:	type = channel or show
	Fetch main menu, then display it.
	*/
	function menuToggle(type) {
	  hideMenus();
	  getMainMenu(type);
	  var t = setTimeout(function() {
		mainMenu.slide('in');
	  }, mainSlideDelay);

	}

	
	/* voHide() ------------------------------------------------------------------------------------------------
	Hide the Video overlay
	*/
	function voHide() {
		vidOverlay.set('text', '');
		vidOverlay.fade('hide');
	}

	/* injectAd(url, image, adID)------------------------------------------------------------------------------------------------
	ARGS:	url = URI of link
		image = Path to AD Image
		adID = ID of AD for click and view	
	Inject ad div into the right spot, moo will take care of the fade
	and everything else.. 
	*/
	function injectAd(url, image, adID) {
		adWindow.set('text', '');
		var preload = new Image();
		preload.src = '/adImages/'+image;

		//adSpace = new Element('div', {'id':'adSpacer'});
		var adHead = new Element('div', {'id':'adHead',
					'html':'Please visit our sponsors'
		});
		var elink = new Element('span', {'id':'theAd'});
		//var elink = new Element('a', {'href': url, 'onClick': 'javascript: crabClick('+adID+', '+url+'); return false;', 'id':'theAd'});
		elink.addEvent('click', function() {
			crab('adClick', adID);
			window.location.href = url;
		});
		
		adHead.injectInside(adWindow);
		elink.injectInside(adWindow);
		
		var el = new Element('img', {'src': '/adImages/'+image});
		el.injectInside(elink);
		el.set('tween', {duration: adFadeTime, link: 'ignore'});
		el.fade('hide');
		el.fade('in');
		crab('adView', adID);
	}

	/* crabView(adID)------------------------------------------------------------------------------------------------
	ARGS:	adID = id of AD
	Kaleb's view tracking. Ajax database update every time it is displayed.
	*/

	function crab(type, id) {
	  var req = new Request({ url: "/ajax/crab.php?type="+type+"&id="+id+"&psudo="+psudoGen() }).send()
	}
	
	/* doAds(plID)------------------------------------------------------------------------------------------------
	ARGS:	plID = PlaylistID
	Fetches all ad data, creates loops via moo->chain.
	*/
	function doAds(plID) {
	  $clear(bugTimer);
	  var req = new Request.JSON({
		url: "/ajax/adData.php?id="+plID,
		onComplete: function(jsonObj) {
	  		var bugChain = new Chain();
			/* Loop through window bugs and inject them into chain */
			bugChain.chain(function() { injectAd(jsonObj.siteSpo.myurl, jsonObj.siteSpo.myimage, jsonObj.siteSpo.adID); });
			if(jsonObj.channelSpo.myurl != '') { 
			  bugChain.chain(function() { injectAd(jsonObj.channelSpo.myurl, jsonObj.channelSpo.myimage, jsonObj.channelSpo.adID); });
			}
			if(jsonObj.showSpo.myurl != '') { 
			  bugChain.chain(function() { injectAd(jsonObj.showSpo.myurl, jsonObj.showSpo.myimage, jsonObj.showSpo.adID); });
			}
			jsonObj.windowBugs.each(function(bugs) {
				bugChain.chain(function() { injectAd(bugs.myurl, bugs.myimage, bugs.adID); } );
			});
			bugChain.chain(function() { injectAd(jsonObj.trailer.myurl, jsonObj.trailer.myimage, jsonObj.trailer.adID); });
			/* Add other ads here. If there are any new ads, they will have to be selected into JSON
			   via adData.php, then they will come out in jsonObj and can be added linear here. 
			also need to check and see if the show / channel exists. */

			runBugChain = function() {
				bugChain.callChain();
				var cLen = bugChain.$chain.length;
				if(cLen == 0) { 
					/* bugChain.$chain is an array of whats left in the chain to process.
					   When we hit 0, clear the chain and recursive call the function again. */
					runBugChain = $clear(bugTimer);
					doAds(plID);
				}
			}
			/* This is where the loop is, kinda. mootools 
			   periodical runs something in an array every n ms */
			bugTimer = runBugChain.periodical(adPeriodical);
		}
	  }).send();
	}

	/* selectVideo(id, type)------------------------------------------------------------------------------------------------
	ARGS:	id = vimeoID
	This is the function that selects the video!
	Grabs embed code from functions.php:videoPlayer()
	then injects into the right part of the page for displaying.
	*/
	function selectVideo(id) {
	  $('vidWrap').set('text', '');
	  $('vidWrap').set('text', 'Loading Video');
	  setTimeout(function() {
		$('vidWrap').setStyle('overflow', 'hidden');
		vidOverlay.fade('hide');
		getPLInfo(id, 'Playlists');
		crab('plView', id);
	var req = new Request.HTML({url: '/ajax/vidPlayer.php?id='+id+'&psudo='+psudoGen(),
				    evalScripts: true,
				    evalResponse: true,
			onSuccess: function(tree, elements, html, rjs) {
				//Clear Current Text in DIV
				vWrap.set('text', '');
				//Inject New DOM Elementos.
				// Hack to make sure ajax is loaded before 
				// the player starts
				setTimeout(function() {
					//vWrap.set('html', html);
					vWrap.adopt(tree);
				}, playerTimer);
			},
			onFailure: function() {
				vWrap.set('text', 'Request Failed');
			}
		});
		req.send();
		doAds(id);
	  }, 800);
	}

	
	/* getVoData(id, type)------------------------------------------------------------------------------------------------
	ARGS:	id = plID
		type = chan, show, playlist
	Fetches data for the Video Overlay
	*/
	var getVoData = function (id, type) {
	  var req = new Request.HTML({url: '/ajax/voData.php?id='+id+'&type='+type+'&psudo='+psudoGen(),
		onSuccess: function(html) {
			// Clear DIV
			vidOverlay.set('text', '');
			//Inject DOM
			vidOverlay.adopt(html);
		},
		onFailure: function() {
			vidOverlay.set('text', 'Request Failed');
		}
	});
	  req.send();
	}

	/* Dumper(arr)------------------------------------------------------------------------------------------------
	ARGS:	id = plID
	Returns dumped structure
	*/
	/*
	function Dumper(arr,level) {
		var dumped_text = "";
		if(!level) level = 0;
		//The padding given at the beginning of the line.
		var level_padding = "";
		for(var j=0;j<level+1;j++) level_padding += "    ";
		  if(typeof(arr) == 'object') { //Array/Hashes/Objects
		    for(var item in arr) {
  			var value = arr[item];
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		    }
		  } else { //Stings/Chars/Numbers etc.
			dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
			return dumped_text;
		}
	} 
	*/
	/* voToggle(e, id, type)----------------------------------------------------------------
	ARGS:	id = id of channel / show
		type = channel or show
	*/
	function voToggle(id, type, pos) {
		getVoData(id, type);
		if(type === 'Playlists' || type === 'logo') {
			vidOverlay.setStyle('left', pos+'px');
		} else {
			vidOverlay.setStyle('left', '0px');
		}
		setTimeout(function() {
			vidOverlay.fade('show');	
		}, voShowTime);
	}

/*
if ($('blogWrap') != null) {
  if ( ($('blogWrap').getSize().y) > ($('vidWrap').getSize().y) ) {

  // Initiate scroller for our content
  myScrollFx = new Fx.Scroll('vidWrap', {
	wait: false
  });

  // Initiate scroll bar
  mySlide = new Slider($('scroll_area'), $('scroll_knob'), {
	steps: $('scroll_container_content').getSize().y-($('vidWrap').getSize().y),
	mode: 'vertical',
	onChange: function(step){
		$('vidWrap').scrollTo(0,step);
	}
  }).set(0);

  scroller();
  }else {
	$('scroll_area').setStyle('visibility','hidden');
  }
}

function scroller() {
document.addEvent('mousewheel', function(e) {
e = new Event(e).stop();
var step = mySlide.step - e.wheel * 10;
mySlide.set(step);
});
}
*/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

