var firstFeedId, currentWorldFeedId, currentMiscFeedId;
var stocksChartIndex = 1;
var upArrowFaded = "/web/pics/misc/arrowUpFaded.gif";
var upArrow = "/web/pics/misc/arrowUp.gif";
var currentPOTDIndex = 0;
var photosOfTheDay;
var photoIndex;
var lastTopFeedClick;
var lastBotFeedClick;
var ytScrollPos=0;
var lastVideoPlayedUrl;
var lastVideoPlayedTitle;
var lastVideoPlayedImage;

function PhotoOfTheDay() {return this;}

var serviceClickedLink, so, scrollObject = new ScrollObj('scrollObject');

function newsGroup_oc(anchor, index, suffix) {
	toggle(anchor);
	var t=0;
	while (e('t' + suffix + t)) {
		hide(e('t' + suffix + t));
		t++;
	}
	if (index=="Daily") {
		var newsDiv = e('nd'+suffix);
		newsDiv.innerHTML = getWordOfTheDayHTML() + "<br><br>" + getQuoteHTML() + '<br><br><a style="margin-left:0" href="' + spanishWordOfTheDay[0] + '" target="_blank" onmousemove="toolTip(\'' + spanishWordOfTheDay[1] + '\', this, event)">' + spanishWordOfTheDay[3].substring(spanishWordOfTheDay[3].indexOf("[")) + '</a>';
		newsDiv.scrollTop = 0;
		ajax.get("/ajax/feed.do?feedId=" + firstFeedId + "&title=Daily&suffix=misc&defaultFeed=false");
	} else {
		show(e('t' + suffix + index));
		showImages(anchor.parentNode);
		e('nTab'+suffix+index+'0').onclick(null);
	}
	return false;
}

function getQuoteHTML() {
	var idx = Math.floor(Math.random() * quotes.length);
	return '<img src="/web/pics/todays/quoteLeft.jpg" height="7"/><a href="' + quotes[idx][0] + '" id="quote" target="_blank" onmousemove="toolTip(\'' + quoteGeneralDescription + '\', this, event)">' + quotes[idx][1] + '</a><img src="/web/pics/todays/quoteRight.jpg" height="7"/>&nbsp;&nbsp;<span class="miniText">' + quotes[idx][2] + '</span>';
}

function getWordOfTheDayHTML() {
	return '<span id="wordOfTheDay"><a href="' + wordOfTheDay[0] + '" target="_blank" onmousemove="toolTip(\'' + wordOfTheDay[1] + '\', this, event)">' + wordOfTheDay[2] + '</a></span>&nbsp;-&nbsp;' + wordOfTheDay[3];
}

function news_oc(o, s, i, id, suffix, title, msg) {
	var params = s+i + "[SEPERATOR]" + suffix;
	firstFeedId = id;
	toggle(o);
	if (!lastTopFeedClick || !lastBotFeedClick) {
		feedCB(e('firstNewsFeed'+suffix).innerHTML, null, params);
		if (!lastTopFeedClick) {
			lastTopFeedClick = s+i;
		} else if (!lastBotFeedClick) {
			lastBotFeedClick = s+i;
		}
	} else {
		var newsDiv = e('nd'+suffix);
		newsDiv.innerHTML = '<br><br><img align="absmiddle" src="/web/pics/common/ajaxLoader.gif" width="32" height="32"/>&nbsp;&nbsp;' + msg + ' <b>' + title + '</b>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br><br>';
		if (suffix=="world") {
			newsDiv.innerHTML += getQuoteHTML();
			currentWorldFeedId=id;
			lastTopFeedClick = s+i;
		} else {
			newsDiv.innerHTML += getWordOfTheDayHTML();
			currentMiscFeedId=id;
			lastBotFeedClick = s+i;
		}
		newsDiv.scrollTop = 0;
		var defaultFeedStatus = s == "0" && i == "0";
		bodyVars = {
			sectionIndex:s,
			feedIndex:i,
			feedId:id,
			title:e('tt'+suffix+s).innerHTML + "_" + title,
			suffix:suffix,
			defaultFeed:defaultFeedStatus
		}
		ajax.get("/ajax/feed.do", bodyVars, feedCB, false, params);
	}
	return false;
}

function feedCB(str, o2, params) {
	var ary = new Array();	
	ary = str.split('[SEPERATOR]');
	var ary2 = new Array();
	ary2 = params.split('[SEPERATOR]');
	var sectionIndex = ary2[0];
	var suffix = ary2[1];
	if ((suffix=="world" && (!lastTopFeedClick || lastTopFeedClick == sectionIndex)) || (suffix!="world" && (!lastBotFeedClick || lastBotFeedClick == sectionIndex))) {
		var newsDiv = e('nd'+suffix);
		newsDiv.innerHTML = ary[0];
		var yBuffer = 15;
		if (newsDiv.scrollHeight - yBuffer > e('news'+suffix).clientHeight) {
			e('nd'+suffix+'UpImg').src = upArrowFaded;
			show('nd'+suffix+'Up');
			show('nd'+suffix+'Down');
		} else {
			hide('nd'+suffix+'Up');
			hide('nd'+suffix+'Down');
		}
		if (e('ntWrapper'+suffix)) {
			e('ntWrapper'+suffix).innerHTML = "<span id='a"+suffix+sectionIndex+"'>"+ary[1]+"<\/span>";
		}
		if (sectionIndex == "00") {
			hide(e('setAsDefault' + suffix));
		} else {
			showi(e('setAsDefault' + suffix));
		}
	}
}

function getArticleDiv(o) {
	if (o && o.id && o.id.indexOf("nl") == 0) {
		return e("nt" + o.id.substring(2));
	}
	return null;
}

function docMouseMove(evt) {
	var article = getArticleDiv(getElement(evt));
	if (article) {
		alignHoverObj(evt, article, 15, 30);
		showi(article);
		article.style.visibility = 'visible';
	}
}

function docMouseOver(evt) {
	var article = getArticleDiv(getElement(evt));
	if (article) {
		showImages(article);
	}
}

function docMouseOut(evt) {
	var article = getArticleDiv(getElement(evt));
	if (article) {
		hide(article);
	}
}

/* POSTIT Begin */
function onClickPostIt(o, msg) {
	if (!logged) {
		o.value = msg;
	}
}

function savePostIt(textAreaId) {
	var o = e(textAreaId);
	var cloneId = "logoClone";
	var co = getClone(textAreaId, cloneId);
	co.value = o.value;
	co.style.overflow = "hidden";
	show(co);
	if (logged) {
		//d.frmAddPostIt.submit();
		bodyVars = {
			quickAdd:"true",
			noteTextArea:escape(e(textAreaId).value)
		}
		ajax.postVars("/user/savePostIt.do", bodyVars, null, empty, true);
	}
	o.value = "";
	o.style.visibility="hidden";
	shrink(o, cloneId, "postItImg");
}

function getClone(id, cloneId) {
	var o = e(id);
	var pos = o.style.position;
	o.style.position = "relative";
	var c = o.cloneNode(true);
	c.id = cloneId;
	c.style.position = "absolute";
	c.style.left = o.offsetLeft;
	c.style.top = o.offsetTop;
	c.style.width = o.clientWidth;
	o.parentNode.appendChild(c);
	o.style.position = pos;
	return c;
}

function ShrinkObj() {return this;}

function shrink(o, sourceId, destId) {
	var speed = 5;
	var destO = e(destId);
	destO.style.position = "relative";

	so = new ShrinkObj();
	so.ro = o;
	so.o = e(sourceId);
	so.diffX = (destO.offsetLeft + destO.clientWidth/2) - so.o.offsetLeft;
	so.diffY = (destO.offsetTop + destO.clientHeight/2) - so.o.offsetTop;
	var h = Math.sqrt(so.diffX * so.diffX + so.diffY * so.diffY);
	so.x = so.o.offsetLeft;
	so.y = so.o.offsetTop;
	so.w = so.o.clientWidth;
	so.h = so.o.clientHeight;
	so.per = speed / h;
	so.cper = 0;
	shrinkInterval = window.setInterval('shrinkIntervalMethod()', 1);
}

function shrinkIntervalMethod() {
	so.cper += so.per;
	if (so.cper < 1) {
		so.o.style.left = so.x + so.cper * so.diffX;
		so.o.style.top = so.y + so.cper * so.diffY;
		so.o.style.width = so.w * (1-so.cper);
		var newHeight = so.h * (1-so.cper);
		if (newHeight >= 1) {
			so.o.style.height = newHeight;
		}
	} else {
		so.o.parentNode.removeChild(so.o);
		// for post it
		so.ro.style.visibility="";
		window.clearInterval(shrinkInterval);
	}
}

/* SEARCH Begin */

function toggleMap(index){
	var a = e('gMap');
	var b = e('mapquest');
	if (index == 1) {
		hide(b);
		show(a);
	} else {
		hide(a);
		show(b);
	}
}

function toggleSearch(pNode, pSearch) {
	var searchLI = e('searchUL').childNodes;
	toggle(pNode);
	hideNodes(e("searchForm").childNodes[bIndex].childNodes)

	showImages(pSearch);
	e(pSearch).className = "on";
	if (pSearch == "map" || pSearch == "weather") {
		hide(e("generalInput"));
		if (pSearch == "map") {
			try	{
				d.frmSearch.gMapSearch.focus();
			} catch (ex) {}
		} else if (pSearch == "weather") {
			d.frmSearch.cityOrZip.focus();
		}
	} else {
		/*
		var label;
		if (pSearch == "images") {
			label = "Image:";
		} else if (pSearch == "videos") {
			label = "Video:";
		} else if (pSearch == "groups") {
			label = "Groups:";
		} else {
			label = "&nbsp;&nbsp;";
		}
		e('searchLabel').innerHTML = label;
		*/
		showi(e("generalInput"));
		d.frmSearch.sField.focus();
	}
}

function writeSearchTabs() {
	for (a=0; a<searchTabs.length; a++) {
		var firstLI = a==0 ? " class='on' id='first'" : "";
		document.write("<li" + firstLI + "><a href=# id=X" + searchTabs[a] + " onfocus=this.blur() onclick=toggleSearch(this,'" + searchTabs[a] + "') onmouseover=toggleTab(this,true) onmouseout=toggleTab(this,false)>" + searchTabsLabels[a].substring(0,1).toUpperCase() + searchTabsLabels[a].substring(1) + "</a></li>");
	}
}

function search(evt, o, id, inputId) {
	var input = inputId ? e(inputId) : e('sField');
	if (id == "alluc") {
		postClick(evt, o, id, SEARCH_ALLUC + escape(input.value), true);
	} else {
		for (a=0; a<searchURLs.length; a++) {
			if (id == searchURLs[a][0]) {
				postClick(evt, o, id, searchURLs[a][1] + escape(input.value), true);
				break;
			}
		}
	}
}

function resizeService(o, height, width) {
	var s = o.childNodes[0];
	s.height=height;
	if (!width) {
		width=height;
	}
	s.width=width;
}

function clickButtonOnEnterForSearch(pEvent) {
	if (pEvent.keyCode == 13 || pEvent.which == 13) {
		var buttonObj;
		var listNodes = e("search").childNodes[bIndex].childNodes;
		var liOn;
		for (i=0; i<listNodes.length; i++) {
			if (listNodes[i].className == "on") {
				liOn = listNodes[i];
				break;
			}
		}
		buttonObj = e(liOn.childNodes[0].id.substring(1,liOn.childNodes[0].id.length)).childNodes[0];
		return clickButtonOnEnter(pEvent, buttonObj);
	}
}

function setUrlParam(url, param, value) {
	if (url.indexOf(param + "=") != -1) {
		var regex = new RegExp(param + "=.*", "ig");
		return url.replace(regex, param + "=" + value);
	} else {
		if (url.indexOf("?") == -1) {
			url += "?";
		} else {
			url += "&";
		}
		return url + param + "=" + value;
	}
}

function showYouTubeVideo(url, index, title, image) {
	addDarkBackground();
	lastVideoPlayedUrl = url;
	lastVideoPlayedImage = image;
	var w = 480, h = 395;
	var urlLower = url.toLowerCase();
	if (urlLower.indexOf("video.google") != -1) {
		w = 600;
		h = 400;
		if (urlLower.indexOf("autoplay") == -1) {
			url = setUrlParam(url, "autoPlay", "true");
		}
	} else if (urlLower.indexOf("spikedhumor") != -1) {
		w = 424;
		h = 336;
	} else if (urlLower.indexOf("youtube") != -1) {
		if (urlLower.indexOf("autoplay") == -1) {
			url += "&autoplay=1";
		}
	}	
	var so = new SWFObject(url, "youTubeVideoObject", w, h, "7", "#ffffff");
	var youTubeWrapperId = "youTubeVideoWrapper";
	var youTubeVideoFrameTitleId = "youTubeVideoFrameTitle";
	var youTubeVideoFrameId = "youTubeVideoFrame";
	if (so.write(youTubeWrapperId)) {
		if (title) {
			e(youTubeVideoFrameTitleId).innerHTML = title;
		} else {
			e(youTubeVideoFrameTitleId).innerHTML = e('youTubeVideoTitle'+index).innerHTML;		
		}
		e('videoLink').value = generatePlayVideoURL(url, e(youTubeVideoFrameTitleId).innerHTML, image);
		lastVideoPlayedTitle = e(youTubeVideoFrameTitleId).innerHTML;
		e(youTubeVideoFrameId).style.marginLeft = 300 - (w / 2) + "px";
		e(youTubeVideoFrameId).style.width = w + "px";
	} else {
		e(youTubeVideoFrameTitleId).innerHTML = 'Flash Player';	
		e(youTubeWrapperId).innerHTML = "<br><br>You need to enable or upgrade your Flash Player for you browser. <a href='http://www.macromedia.com/go/getflashplayer'>Get the lastest flash flayer.</a>";
		e(youTubeVideoFrameId).style.height = "100px"
	}
	show(e(youTubeVideoFrameId));
}

function generatePlayVideoURL(url, title, image) {
	var link = getHost() + "?playVideo=" + encodeURIComponent(url) + "&title=" + encodeURIComponent(title);
	if (image) {
		link += "&image=" + encodeURIComponent(image);
	}
	return link;
}

function hideYouTubeVideo() {
	hide(e('youTubeVideoFrame'));
	e('youTubeVideoWrapper').innerHTML = "";
	removeDarkBackground();
}

function changeSign(o) {
	hide('changeSign');
	show('horoscopeSelection');
}

function ScrollObj(id) {
	var yBuffer = 10;
	this.id = id;
	this.scroll = function(id, height) {
		if (this.scrolling) return;
		this.o = e(id);
		this.height = height;
		this.YSpeed = 0;
		this.YOffset = 0;
		this.intervalObj = setInterval(this.id + ".doSlide()", 10);
		this.scrolling = true;
	};
	this.doSlide = function() {
		//document.title = new Date();
		if (this.YOffset >= Math.abs(this.height)) {
			clearInterval(this.intervalObj);
			var upImgId = e(this.o.id + "UpImg");
			var downImgId = e(this.o.id + "DownImg");
			if (this.o.scrollTop < yBuffer) {
				upImgId.src= upArrowFaded;
			} else {
				upImgId.src= upArrow;
			}
			if (this.o.scrollTop + this.height > this.o.scrollHeight - yBuffer) {
				hide(this.o.id + "Down");
			} else {
				show(this.o.id + "Down");
			}
			this.scrolling = false;
		}
		if (this.YOffset < Math.abs(this.height) / 2) {
			this.YSpeed++;
		} else {
			if (this.YSpeed > 2) {
				this.YSpeed--;
			}
		}
		var direction = this.height > 0 ? 1 : -1;
		this.YOffset += this.YSpeed;
		this.o.scrollTop += this.YSpeed * direction;
	};
	return this;
}

function showStocksChart(index, clicked) {
	var stocksHREF;
	var stocksImgSrc;
	if (index==1) {
		stocksHREF = "http://finance.yahoo.com/q/bc?s=%5eIXIC&t=1d&c=";
		stocksImgSrc = "http://ichart.finance.yahoo.com/t?s=%5EIXIC";
	} else if (index==2) {
		stocksHREF = "http://finance.yahoo.com/q/bc?s=%5EDJI&t=1d&c=";
		stocksImgSrc = "http://ichart.finance.yahoo.com/t?s=%5EDJI";
	} else if (index==3) {
		stocksHREF = "http://finance.yahoo.com/q/bc?s=%5EGSPC&t=1d&c=";
		stocksImgSrc = "http://ichart.finance.yahoo.com/t?s=%5EGSPC";
	} else if (index==4) {
		stocksHREF = "http://finance.yahoo.com/q/bc?s=%5ETNX&t=1d&c=";
		stocksImgSrc = "http://ichart.finance.yahoo.com/t?s=%5ETNX";
	}
	if (index != stocksChartIndex) {
		stocksChartIndex = index;
		e('stocksHREF').href = stocksHREF;
		e('stocksImgSrc').src = stocksImgSrc;
	}
	if (clicked) {
		location.href = stocksHREF;
	}
	return false;
}

function feedTitleOnError (o, title) {
	o.src='/web/pics/i.gif';
	o.parentNode.innerHTML = title;
}

function videoCategory(o, categoryId) {
	ytScrollPos=0;
	var bodyVars = initializeTabSelection(o, categoryId, 'youTubeAjaxWrapper');
	ajax.get("/ajax/videoFeed.do", bodyVars, videoFeedCB, false);
	return false;
}

function videoFeedCB(str, o2, params) {
	var videosDiv = e('youTubeAjaxWrapper');
	videosDiv.innerHTML = str;
}

function loadVideoImages() {
	showImages("ytDiv"+(ytScrollPos*2+2));
	showImages("ytDiv"+(ytScrollPos*2+3));
}

function showImages(o) {
	o = getObj(o);
	if (o) {
		o.innerHTML = o.innerHTML.replace(/imghidden/gi,"img");
	}	
}

function photoCategory(o, categoryId) {
	var bodyVars = initializeTabSelection(o, categoryId, 'photosOfTheDay');
	ajax.get("/ajax/photoFeed.do", bodyVars, photoFeedCB, false);
	return false;
}

function photoFeedCB(str, o2, params) {
	eval(str);
}

function initializeTabSelection(o, categoryId, loadingDiv) {
	if (o) {
		toggle(o);
		o.blur();
	}
	var div = e(loadingDiv);
	div.innerHTML="<div style='padding:0'></div>&nbsp;&nbsp;<img src='/web/pics/common/ajaxLoader.gif'/>";
	var bodyVars = {
		feedId:categoryId
	}
	return bodyVars;
}

function decodeHtmlEntity(str) {
	var ta=document.createElement("textarea");
	ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
	return ta.value;
}

function showPhotoOfTheDay(index) {
	if (index<0) {
		currentPOTDIndex = photoIndex-1;
	} else if (index>=photoIndex) {
		currentPOTDIndex = 0;
	} else {
		currentPOTDIndex = index;
	}
	e('photosOfTheDay').innerHTML = "<a href='" + photosOfTheDay[currentPOTDIndex].link + "' target='_blank'><img src='" + photosOfTheDay[currentPOTDIndex].img + "' alt='" + photosOfTheDay[currentPOTDIndex].title + "' onmousemove=\"toolTip(\'" + photosOfTheDay[currentPOTDIndex].description + "\', this, event)\"/><br>" + decodeHtmlEntity(photosOfTheDay[currentPOTDIndex].title) + "</a>";
}

function movieObj_OnMouseDown(o, url, index, width, height) {
	postClick(null, o, 'youTubeEmbedClick', null, false);
	var movieParent = o.parentNode;
	hide(o);
	movieParent.innerHTML = "<div style='text-align:left;position:relative;height:" + (height-13) + "px;display:block'><div style='position:absolute;top:30%;width:" + width + "px;text-align:center'>Loading...</div></div>" + movieParent.innerHTML;
	showYouTubeVideo(url, index);
}

function musicGenre(o, idx) {
	if (o) {
		toggle(o);
		o.blur();
	}
	e('currentStations').innerHTML = e('stations'+idx).innerHTML;
}

function mPlayer(id) {
	var w = window.open("http://playlist.icebergradio.com/player.php?station=" + id + "&theme=icebergradio", "player", "width=420, height=337, scrollbars=no, resizable=no, status=no");
	// &bitrate=32
	if (w) {
		w.focus();
	}
}

function showVideoDetails(o, evt, videoDetailsId) {
	var keyPressed;
	if (isIE()) {
		keyPressed = event.ctrlKey;
	} else {
		keyPressed = evt.ctrlKey;
	}
	if (keyPressed) {
		hide(o);
		show(videoDetailsId);
		selectText(e(videoDetailsId));
		e(videoDetailsId).focus();
	}
	return false;
}

function openPopup(url,name,height,width,opt_show_scrollbars) {
	var scrollbar_param=opt_show_scrollbars?",scrollbars=1":"";
	var newwindow=window.open(url,name,'height='+height+',width='+width+scrollbar_param);
	if(newwindow&&!newwindow.opener){newwindow.opener=window;}
	if(window.focus){newwindow.focus()}
	return false;
}

d.onmousemove=docMouseMove;
d.onmouseover=docMouseOver;
d.onmouseout=docMouseOut;