var apiUrl = '';
var loadUrl = 'http://krishnaconnect.com/talk/subscribe/loading.gif';
var wUrl = 'http://krishnaconnect.com';
var flag = true;

function rotoCop() {
	var box = $('rotoCop');
	var items = $$('#rotoItems li');
	var seq = 0;
	var z = items.length - 1;
	var speed = 3000;
	var period;
	var clicked = false;

	var rotate = function(){
		items[seq].removeClass('on');
		counter[seq].removeClass('on');
		(seq != z) ? seq++ : seq = 0;
		items[seq].addClass('on');
		counter[seq].addClass('on');
	}

	function timer() { period = rotate.periodical(speed);}

	var rotoNav = new Element ('ul', {'id': 'rotoNav'}).injectInside(box);

	items.each(function(el){ 
		el.addEvent('mouseenter', function(e){ $clear(period); })
		el.addEvent('mouseleave', function(e){ timer(); })
		var robLi = new Element ('li');
		var robAnchor = new Element('a', {'href': '#','html': ''});
		var robLia = robLi.grab(robAnchor);
		robLia.inject(rotoNav);
	});

	var counter = $$('#rotoNav li');
	counter[0].addClass('on');
	counter.each(function(el, i){ 
		el.addEvent('click', function(e){
			e = new Event(e).stop();
			$clear(period);
			counter[seq].removeClass('on');
			items[seq].removeClass('on');
			seq = i;
			items[i].addClass('on');
			counter[i].addClass('on');
			clicked = true;
		});
		el.addEvent('mouseenter', function(e){ $clear(period); })
		el.addEvent('mouseleave', function(e){	
			if (!clicked){ timer();} 
		});
	})
	timer();
} //rotoCop

function whirligig(){	 
	var MgrNav = $('MgrLinks');
	var MgrDiv = $('Mgr');
	var MgrUl = '<ul class=\"tabs\">';
	var active = '';
	var currentLink ='';
	var MgrHTML ='';
	var mclicked ='false'
	var req = null; 
	var loadimg = '<center><img src="'+ loadUrl +'" id="loading" width="32" height="32"><\/center>';

if (window.XMLHttpRequest){
	req = new XMLHttpRequest();
	if (req.overrideMimeType) {req.overrideMimeType('text/xml');}
	} 
else if (window.ActiveXObject){
			try { req = new ActiveXObject("Msxml2.XMLHTTP");} catch (e)	{
				try {req = new ActiveXObject("Microsoft.XMLHTTP");} catch (e) {}
				}
		}
req.onreadystatechange = function()	{ 
			MgrDiv.set('html', loadimg);
			if(req.readyState == 4)	{
				if(req.status == 200) { //success
					// process a XML document here
					var doc = req.responseXML;	
					var nElements = doc.getElementsByTagName('POS').length;
					for (i = 1; i <= nElements; i++){
						if(i == 1) {currentLink = '<li class="active"><a href="Mg#'+ i +'">'+ i +'<\/a><\/li>';} 
						else {currentLink = '<li><a href="Mg#'+ i +'">'+ i +'<\/a><\/li>';}
						MgrUl += currentLink;
					}
					MgrUl += '<\/ul>';
					if(nElements != 1) MgrNav.set('html',MgrUl);
					MgrHTML = doc.getElementsByTagName('HTML')[0].firstChild.data;
					MgrDiv.set('html', MgrHTML);		
					
					var mitems = $$('.tabs li');
					var mz = mitems.length - 1;
					var mseq = 0;
					var mspeed = 8000;
					var mperiod;	    					
					if (nElements != 1) {
						var mrotate = function(){
							mitems[mseq].removeClass('active');
							(mseq != mz) ? mseq++ : mseq = 0;
							mitems[mseq].addClass('active');
							MgrHTML = doc.getElementsByTagName('HTML')[mseq].firstChild.data;
							MgrDiv.set('html', MgrHTML);
						}
						function timer() {mperiod = mrotate.periodical(mspeed);}    
						mitems.each(function(el, i){ 
							el.addEvent('click', function(e){
								e = new Event(e).stop();
								$clear(mperiod);
								mitems[mseq].removeClass('active');
								mseq = i;
								mitems[i].addClass('active');
								MgrHTML = doc.getElementsByTagName('HTML')[i].firstChild.data;
								MgrDiv.set('html', MgrHTML);
								mclicked = true;
							})
						el.addEvent('mouseenter', function(e){ $clear(mperiod);})
						el.addEvent('mouseleave', function(e){ if (!mclicked) { timer();}})
						})
						timer(); 
					}
				} else {
					MgrDiv.set('html', '<p class=\'confirm\'>We\'re sorry. Because of a technical error, We\'re unable to share featured content at this time.<\/p>');
				}
			} 
		}; 
		req.open("GET", "5295-1-0.html?type=3&dfType=1&filter=4", true); 
		req.send(null); }

function avatar() {
var tab = $('workspace');
var avatar = $('myAvatar');
var myNav = $$('#myNav li');
var selectBox = $$('#myAvatar .selectBox');
var seq = 0;
var loginText = '<h6><a href="http://techrepublic.com.com/1300-4_11-55.html">Would you like your own dynamic Workspace on TechRepublic?</a></h6> <p>Take two minutes and set up a TechRepublic member profile.</p><div id="signIn"><a href="http://techrepublic.com.com/1300-4_11-55.html"><img src="http://i.bnet.com/images/200807/btn_signIn.gif" width="95" height="23" /></a></div>';

function popRmv() {
	avatar.removeClass('drill');
	tab.removeClass('on');
}

function popAdd() {
	avatar.addClass('drill');
	tab.addClass('on');
}
			function CheckMultiple4(frm, name) {
				for (var i=0; i < frm.length; i++)
				{
					fldObj = frm.elements[i];
					fldId = fldObj.id;
					if (fldId) {
						var fieldnamecheck=fldObj.id.indexOf(name);
						if (fieldnamecheck != -1) {
							if (fldObj.checked) {
								return true;
							}
						}
					}
				}
				return false;
			}
		function CheckForm4(f) {
			if (f.email.value == "") {
				alert("Please enter your email address.");
				f.email.focus();
				return false;
			}
		
				return true;
			}
		

//tab.addEvent('click', function(e) { e = new Event(e).stop(); });
tab.addEvent('mouseenter', function() { 
	popAdd(); 
	if ((!$('contactSelect').hasClass('notLogged') || !$('updateSelect').hasClass('notLogged'))  && flag == true){
		var loadimg = '<p><img src="'+ loadUrl +'" id="loading" width="32" height="32" /></p>';
		$('contactSelect').set('html', loadimg);
		$('updateSelect').set('html', loadimg);
			if (typeof(cid) != 'undefined') { 
				cid = cid.trim(); 
				if (cid.length != 0 || cid != null) { addScript(cid); flag = false;}
			}
	}
});
tab.addEvent('mouseleave', function() { popRmv(); });
avatar.addEvent('mouseenter', function() { popAdd(); });
avatar.addEvent('mouseleave', function() { popRmv(); });

selectBox.each(function(el){
if (el.hasClass('notLogged')) {
	$('contactSelect').set('html', loginText);
	$('updateSelect').set('html', loginText);
}})


myNav.each(function(el,i){
	el.addEvent('click', function(e){ 
	e = new Event(e).stop();
	selectBox[seq].removeClass('on');
	myNav[seq].removeClass('on');
	selectBox[i].addClass('on');
	el.addClass('on');
	seq = i;
}) })
} //avatar


var RegOverlay = {		
	init: function(){
		var regCookie = Cookie.read('trRegOverlay');
		cid = (cid) ? cid : '';
		cid = cid.clean();
		if (regCookie != 'seen' && cid == '') {	
			var path = '?path=' + encodeURI(document.URL);
			var regHtml =	'<div class="inner clear"><h2>Get the Krishna Connect Newsletter</h2>' +
							'<h3>Find spritual sollutions to material problems:</h3>' +
							'<ul class="clear"><li>How to engage your energy for self realization</li>' +
							'<li>Understand the soul, reincarnation and karma scientifically</li>' +
							'<li>Understanding who is a genuine guru</li>' +
							'<li>Finding the original cause of everything</li></ul>' +
							'<p>Develop full knowledge of everything, material and spiritual</p>' +
							'<p id="regSignUp">' +
	'<form method="post" action="http://krishnaconnect.com/talk/form.php?form=4" id="frmSS4" onSubmit="return CheckForm4(this);">' +
	'<table><tr><td>First Name: </td><td><input type="text" name="CustomFields[1]" id="CustomFields_1_4" value="" size="20" maxlength="25"></td>' +
	'<td rowspan="2"><input type="image" src="http://krishnaconnect.com/talk/subscribe/btn_signUp_120x34.jpg" height="34" width="120" value="Submit" alt="Subscribe"></td></tr>' +
	'<tr><td>Email: </td><td><input type="text" name="email" value="" size="20"></td></tr></table>' +
	'<input type="hidden" name="format" value="h">' +
	'<span><a class="close" href="#">No thanks</a> | <a class="close" href="#">I\'ve already subscribed</a></span></p>' +
	'<a id="closeBtn" class="close" href="#"><img src="http://krishnaconnect.com/talk/subscribe/bg_closeWin_16x17.gif" alt="Close" /></a>' +
	'</form>' +
							'</div><!-- /inner -->';
			Cookie.write('trRegOverlay', 'seen', {duration: 1, domain: cdomain, path: '/' });
			var reg = new Element ('div', { 'id': 'regPop' }).set('html',regHtml).inject(document.body);
			var fix = new Element('iframe', {'id': 'regFix'}).inject(reg,'after');
			this.position(reg,fix);
			this.close(reg,fix);
		}
	},
	
	position: function(reg,fix){
		var regSize = reg.getSize();
		var winSize = document.window.getSize();
		var postHorz = (winSize.x / 2) - (regSize.x / 2);
		var postVert = (winSize.y / 2) - (regSize.y / 2);
		reg.set('styles', { 'left': postHorz, 'top': postVert });
		fix.set('styles', { 'left': postHorz, 'top': postVert });
	},

	close: function(reg,fix){	
		reg.getElements('.close').each(function(el,i){ 
			el.addEvent('click', function(e){	
				e = new Event(e).stop();
				Cookie.write('trRegOverlay', 'seen', {duration: 30, domain: cdomain, path: '/' });			
				reg.dispose();
				fix.dispose();
			});
		});
	}
};

var Tags = {
	truncate: function(amount){
		$$('.tags').each(function(el){
			var links = el.get('html').split(',');
			if (links.length > amount) {
				var hiddenLinks = links.splice(amount,links.length - amount);
				hiddenLinks = hiddenLinks.join(', ');
				links = links.join(', ');
				el.set('html',links);
				var ellipsis = new Element ('span', {'class': 'ellipsis'}).set('html','...').inject(el,'bottom');
				var hiddenCont = new Element ('span', {'class': 'more'}).set('html',',' + hiddenLinks).inject(el,'bottom');
				el.addEvents({
					'mouseenter': function(){
						ellipsis.setStyle('display','none');
						hiddenCont.setStyle('display','inline');
					},
					'mouseleave': function(){
						ellipsis.setStyle('display','inline');
						hiddenCont.setStyle('display','none');
					}
				});
			}
		});
	}
};









window.addEvent('domready', function() {		
if ($('rotoCop')) rotoCop();
if ($('myAvatar')) avatar();
if ($$('#col3 .upWebcasts') != '') upWebcasts();
if ($$('.toolbar') && $$('.toolbar') != "") toolbar();
if ($$('.selector') != "") selector();
RegOverlay.init();
if ($$('.tags') != "") Tags.truncate(5);
if ($('Mgr')){if($('Mgr').hasClass('fMgr') && $('MgrLinks').hasClass('fMgrLinks')) whirligig();}
if (typeof(cid) != 'undefined'){
	cid = cid.trim();
		if (cid.length != 0 || cid != null) {
		cookieSetter(); 
		}
	}
})

function addScript(cid){
// Build the Yahoo! web services call
p = ''+ apiUrl +'userupdates/?cid='+ cid +'&viewType=json&jsCallBack=updateView';
// Create a new script object
pObj = new JSONscriptRequest(p);
// Build the script tag
pObj.buildScriptTag();
// Execute (add) the script tag
pObj.addScriptTag();

c = ''+ apiUrl +'onlinecontacts/?cid='+ cid +'&viewType=json&jsCallBack=contactView';
// Create a new script object
cObj = new JSONscriptRequest(c);
// Build the script tag
cObj.buildScriptTag();
// Execute (add) the script tag
cObj.addScriptTag();
}

function cookieSetter () {
	if (cid.length != 0){
		if (Cookie.read('setUser') != "online"){
			var myCookie	= Cookie.write('setUser', 'online', {duration: 0.0138888889, domain: '.com.com', path: '/'});
			checkUser(cid); 
		}
	}
}

function checkUser(cid) {
		if (cid.length != 0){
		onUser= ''+ apiUrl +'setUserOnline/?id='+ cid +'&viewType=json&jsCallBack=setoUser';
		// Create a new script object
		userObj = new JSONscriptRequest(onUser);
		// Build the script tag
		userObj.buildScriptTag();
		// Execute (add) the script tag
		userObj.addScriptTag();
		}
}

function setoUser(data){
	var s = '';
	if (data==null) s='Error';
	else if (data.Community.UserOnline.$ != "OK") s = 'Servers are down';
	else s ='<!--'+data.Community.UserOnline.$+'-->';
	//alert('Status: ' + s);
	userObj.removeScriptTag();
}

function updateView(data){
	var u='';
	if(data==null){ u='<p>We have experienced an error<\/p>';}
	else if (data.Updates){
	if (null != data.Updates.Update[0]) {
		u='<ul>';
		for (var i=0; i < data.Updates.Update.length; i++){
			u+='<li><a href=\"' + wUrl + data.Updates.Update[i].Url.$ + '\">';
			u+=data.Updates.Update[i].Title.$ + '<\/a> ';
			u+='<span>' + data.Updates.Update[i].Date.$ + '<\/span><\/li>';}
			u+='<\/ul>';
		} else {
			u='<p><a href=\"' + wUrl + data.Updates.Update.Url.$ + '\">';
			u+=data.Updates.Update.Title.$ + '<\/a> ';
			u+='<span>' + data.Updates.Update.Date.$ + '<\/span><\/p>';
		} } else {u='<p>You have no Updates<\/p>';}
	$('updateSelect').innerHTML=u;
	// Remove the script tag
	//pObj.removeScriptTag();
}

function contactView(data){
var c='';
if(data==null) { c='<p>We have experienced an error<\/p>';} 
else if (data.Contacts) {
if (null != data.Contacts.Contact[0]) {
c='<ul>'
for (var i=0; i < data.Contacts.Contact.length; i++){
c+='<li><a href=\"' + data.Contacts.Contact[i].ProfileUrl.$ + '\">';
c+=data.Contacts.Contact[i].UserName.$ + '<\/a><\/li>';
} 
c+='<\/ul>';
} else {
c='<p><a href=\"' + data.Contacts.Contact.ProfileUrl.$ + '\">';
c+= data.Contacts.Contact.UserName.$ + '<\/a><\/p>';
} } else { c='<p>You have no contacts online<\/p>'; }
$('contactSelect').innerHTML=c;
// Remove the script tag
//cObj.removeScriptTag();
}

// JSONscriptRequest
// Author: Jason Levitt
function JSONscriptRequest(fullUrl) {
// REST request path
this.fullUrl = fullUrl; 
// Keep IE from caching requests
this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
// Get the DOM location to put the script tag
this.headLoc = document.getElementsByTagName("head").item(0);
// Generate a unique script tag id
this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
JSONscriptRequest.prototype.buildScriptTag = function () {

// Create the script tag
this.scriptObj = document.createElement("script");

// Add script object attributes
this.scriptObj.setAttribute("type", "text/javascript");
this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
this.scriptObj.setAttribute("id", this.scriptId);
}

// removeScriptTag method
JSONscriptRequest.prototype.removeScriptTag = function () {
// Destroy the script tag
this.headLoc.removeChild(this.scriptObj);	 
}

// addScriptTag method
JSONscriptRequest.prototype.addScriptTag = function () {
// Create the script tag
this.headLoc.appendChild(this.scriptObj);
}


