var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0;
var tempY = 0;

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.documentElement.scrollTop;
    tempY = event.clientY + document.documentElement.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX;
    tempY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  return true;
}

function dovote(sid,vote)
{
	
	votePad = document.getElementById('votePad_' + sid.substr(1));
	upFinger = document.getElementById('voteUp_' + sid.substr(1));
	downFinger = document.getElementById('voteDown_' + sid.substr(1));
	changeClass('voteUp_' + sid.substr(1), 'voteUpOff');
	changeClass('voteDown_' + sid.substr(1), 'voteDownOff');

	votescore = document.getElementById('voteScore_' + sid.substr(1));

	new Ajax.Request('/ajax_vote.php',{
		method: 'get',
		parameters: {sid:sid,vote:vote},
		onSuccess: function(transport)
		{
			if (transport.responseText != "x") {
				votescore = document.getElementById('voteScore_' + sid.substr(1));
				votescore.innerHTML = transport.responseText + ' votes';
				//$(sid).update(transport.responseText);
			}
		}
	});
}

voteIds = '';

function checkVotes() {
	var myAjax = new Ajax.Request('ajax_did_i_vote.php?sid=s&sids=' + voteIds,
		{method: 'get', parameters: "", onComplete: setVotePads} );	
}

function setVotePads(originalRequest) {
	items = Array();
	items = originalRequest.responseText.split('~');
	for(i=0;i<items.length;i++) {
		fields = items[i].split(':');
		sid = fields[0];
		curscore = fields[1];
		myvote = fields[2];
		votePad = document.getElementById('votePad_' + sid);
		upFinger = document.getElementById('voteUp_' + sid);
		downFinger = document.getElementById('voteDown_' + sid);
		if (myvote != '') {
			changeClass('voteUp_' + sid, 'voteUpOff');
			changeClass('voteDown_' + sid, 'voteDownOff');
			//votePad.style.backgroundImage = 'url(/simg/voting_bg_inactive.gif)';
		}
		votescore = document.getElementById('voteScore_' + sid);
		votescore.innerHTML = curscore + ' votes';
	}
}

function didIVote(sid)
{

	if (voteIds.length > 0) {
		voteIds = voteIds + ',' + sid.substr(1);
	}
	else {
		voteIds = sid.substr(1);
	} 
/*
	new Ajax.Request('/ajax_did_i_vote.php',{
		method: 'get',
		parameters: {sid:sid},
		onSuccess: function(transport)
		{
			votePad = document.getElementById('votePad_' + sid.substr(1));
			upFinger = document.getElementById('voteUp_' + sid.substr(1));
			downFinger = document.getElementById('voteDown_' + sid.substr(1));
			result =  transport.responseText.split("~");
			myvote = result[0];
			curscore = result[1];
			if (myvote != 'x') {
				changeClass('voteUp_' + sid.substr(1), 'voteUpOff');
				changeClass('voteDown_' + sid.substr(1), 'voteDownOff');
				//votePad.style.backgroundImage = 'url(/simg/voting_bg_inactive.gif)';
			}
			votescore = document.getElementById('voteScore_' + sid.substr(1));
			votescore.innerHTML = curscore + ' votes';
		}
	});
	*/
}


var hoverTimer;
var onScreenTimer;
var activePopup;
var hoverPause = 500;
var currentObj;

function showMenu(menuId) {
	newLink = 7;
	menuCount = 7;
	for (i=1;i<=menuCount;i++) {
		changeClass('subMenu' + i, 'hiddenSub');
		if (i == newLink) {
			changeClass('menuLink' + i, 'newLink');	
		}
		else {
			changeClass('menuLink' + i, '');	
		}
	}	
	changeClass('subMenu' + menuId, 'normalSub');
	if (menuId == newLink) {	
		changeClass('menuLink' + menuId, 'newActive');
	}
	else {
		changeClass('menuLink' + menuId, 'active');
	}
}

function hideMenu() {
	menuCount = 5;
	for (i=1;i<=menuCount;i++) {
		changeClass('subMenu' + i, 'hiddenSub');
		changeClass('menuLink' + i, '');	
	}	
}

function changeClass(id, newClass) {
	identity=document.getElementById(id);
    if (identity != null) {   
	    identity.className=newClass;
    }
}

function clearPopupTimer() {
	clearTimeout(hoverTimer);
}

function clearActive() {
	callerX = 0;
	callerY = 0;
	changeClass(activePopup, 'hiddenDiv');
	clearTimeout(hoverTimer);
	bigBio = document.getElementById('bioInfo');
	if (bigBio != null) {
		bigBio.style.overflow = 'auto';
	}
}

callerX = 0;
callerY = 0;
mouseX = 0;
mouseY = 0;
function startPopupTimerWithCoords(obj, func, coords_x, coords_y) {
	if (BrowserDetect.browser == 'Explorer') {
	    mouseX = window.event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
	  	mouseY = window.event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	callerX = coords_x + 1;
	callerY = coords_y + 26;
	startPopupTimer(obj, func);	
}

function startPopupTimer(obj, func) {
	currentObj = obj;
	hoverTimer = setTimeout(func + '()', hoverPause);
}

function findPosX(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;}
function findPosY(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;}

//picks functions
var activeTeaserThumb = 0;
var teaserList = Array();
var teaserTimer;

function showPick(activeThumb) {
	clearTimeout(teaserTimer);
	if (activeThumb != activeTeaserThumb) {
		changeClass('thumbDiv' + activeTeaserThumb, 'teaserThumb');
		changeClass('thumbDiv' + activeThumb, 'teaserThumbSelected');
		changeClass('mainImage' + activeThumb, "fred");
		changeClass('mainImage' + activeTeaserThumb, "hidden");
	}
	activeTeaserThumb = activeThumb;
//	var myAjax = new Ajax.Request('ajax/onePick.php?sub_id=' + activeThumb,
//		{method: 'get', parameters: "", onComplete: loadNewTeaser} );
	
}

function loadNewTeaser(originalRequest) {
	teaserDiv = document.getElementById('teaserBlock');
	teaserDiv.innerHTML = originalRequest.responseText;	
}

function nextTeaser() {
	if (activeTeaserThumb >= 6) {
		showPick(1);
	}
	else {
		showPick(activeTeaserThumb + 1);
	}
	teaserTimer = setTimeout('nextTeaser()', 4000);
}

//individual functions
function miniBio() {
	urlparts = currentObj.href.split('?');
	if (urlparts.length == 1 && urlparts[0].indexOf('celebrities/') > 0) {
		urlparts = currentObj.href.split('celebrities/');
	}
	else if (urlparts.length == 1 && urlparts[0].indexOf('twitter/') > 0) {
		urlparts = currentObj.href.split('twitter/');
	}
	else if (urlparts.length == 1 && urlparts[0].indexOf('twitpics/') > 0) {
		urlparts = currentObj.href.split('twitpics/');
	}
	var myAjax = new Ajax.Request('/ajax/miniBio.php?' + urlparts[1],
 		{method: 'get', parameters: "", onComplete: showMiniBio} );
}

function miniBioLeftCol() {
	urlparts = currentObj.href.split('?');
	if (urlparts.length == 1) {
		urlparts = currentObj.href.split('celebrities/');
	}
	var myAjax = new Ajax.Request('/ajax/miniBio.php?' + urlparts[1],
 		{method: 'get', parameters: "", onComplete: showMiniBioLeftCol} );
}

function miniBioBday() {
	urlparts = currentObj.href.split('?');
	if (urlparts.length == 1) {
		urlparts = currentObj.href.split('celebrities/');
	}
	var myAjax = new Ajax.Request('/ajax/miniBio.php?' + urlparts[1],
 		{method: 'get', parameters: "", onComplete: showMiniBioBday} );
}

function showMiniBio(originalRequest) {
	bigBio = document.getElementById('bioInfo');
	if (bigBio != null) {
		bigBio.style.overflow = 'hidden';
	}
	activePopup = 'popupDiv1';
	showDiv = document.getElementById('popupDiv1');
	showDiv.innerHTML = originalRequest.responseText;
	    IEPos = (parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop) < mouseY + 230 && mouseY > 0);
	 //   alert(IEPos);
	//	alert(mouseY + 230);
//		alert(parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop));
	if (parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop) - callerY - 230 > parseInt(findPosY(currentObj))  - 15 && IEPos == false) {
		changeClass('popupDiv1', 'miniBio');
		if (BrowserDetect.browser == 'Explorer') {
			if (findPosX(currentObj) >  300) {
				//if it's less than 300, javascrip is behaving irrationally
				showDiv.style.top = parseInt(findPosY(currentObj)) + callerY + 10 + 'px';
				showDiv.style.left = parseInt(findPosX(currentObj)) + callerX - 230 + 'px';
			}
			else {
				showDiv.style.top = parseInt(mouseY) + 'px';
				showDiv.style.left = parseInt(mouseX) - 300 + 'px';		
			}
		}
		else {
			showDiv.style.top = parseInt(findPosY(currentObj)) + callerY - 35 + 'px';
			showDiv.style.left = parseInt(findPosX(currentObj)) + callerX - 230 + 'px';
		}
	}
	else {
		changeClass('popupDiv1', 'miniBioInverse');
			
		if (BrowserDetect.browser == 'Explorer') {
			if (findPosX(currentObj) >  300) {
				//if it's less than 300, javascrip is behaving irrationally
				showDiv.style.top = parseInt(findPosY(currentObj)) + callerY - 230 + 40 + 'px';
				showDiv.style.left = parseInt(findPosX(currentObj)) + callerX - 270 + 'px';
			}
			else {
				showDiv.style.top = parseInt(mouseY) - 230 +'px';
				showDiv.style.left = parseInt(mouseX) - 300 + 'px';		
			}
		}
		else {
			showDiv.style.top = parseInt(findPosY(currentObj)) + callerY - 15 - 230 + 'px';
			showDiv.style.left = parseInt(findPosX(currentObj)) + callerX - 270 + 'px';
		}
	}
	if (parseInt(showDiv.style.left) < 0) {
		showDiv.style.left = "200px";
		showDiv.style.top = tempY + "px";
	}
}

function showMiniBioLeftCol(originalRequest) {
	bigBio = document.getElementById('bioInfo');
	if (bigBio != null) {
		bigBio.style.overflow = 'hidden';
	}
	activePopup = 'popupDiv1';
	showDiv = document.getElementById('popupDiv1');
	showDiv.innerHTML = originalRequest.responseText;
	    IEPos = (parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop) < mouseY + 230 && mouseY > 0);
	 //   alert(IEPos);
	//	alert(mouseY + 230);
//		alert(parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop));
	if (parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop) - callerY - 230 > parseInt(findPosY(currentObj))  - 15 && IEPos == false) {
		changeClass('popupDiv1', 'miniBioLeft');
		if (BrowserDetect.browser == 'Explorer') {
			if (findPosX(currentObj) >  300) {
				//if it's less than 300, javascrip is behaving irrationally
				showDiv.style.top = parseInt(findPosY(currentObj)) + callerY + 40 + 'px';
				showDiv.style.left = parseInt(findPosX(currentObj)) + callerX + 60 + 'px';
			}
			else {
				showDiv.style.top = parseInt(mouseY) + 'px';
				showDiv.style.left = parseInt(mouseX) + 60 + 'px';		
			}
		}
		else {
			showDiv.style.top = parseInt(findPosY(currentObj)) + callerY - 15 + 'px';
			showDiv.style.left = parseInt(findPosX(currentObj)) + callerX + 60 + 'px';
		}
	}
	else {
		changeClass('popupDiv1', 'miniBioInverseLeft');
			
		if (BrowserDetect.browser == 'Explorer') {
			if (findPosX(currentObj) >  300) {
				//if it's less than 300, javascrip is behaving irrationally
				showDiv.style.top = parseInt(findPosY(currentObj)) + callerY - 230 + 40 + 'px';
				showDiv.style.left = parseInt(findPosX(currentObj)) + callerX + 60 + 'px';
			}
			else {
				showDiv.style.top = parseInt(mouseY) - 230 +'px';
				showDiv.style.left = parseInt(mouseX) + 60 + 'px';		
			}
		}
		else {
			showDiv.style.top = parseInt(findPosY(currentObj)) + callerY - 15 - 230 + 'px';
			showDiv.style.left = parseInt(findPosX(currentObj)) + callerX + 60 + 'px';
		}
	}
}


function showMiniBioBday(originalRequest) {
	bigBio = document.getElementById('bioInfo');
	if (bigBio != null) {
		bigBio.style.overflow = 'hidden';
	}
	activePopup = 'popupDiv1';
	showDiv = document.getElementById('popupDiv1');
	showDiv.innerHTML = originalRequest.responseText;
	if (parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop) - 230 > parseInt(findPosY(currentObj)) - 15) {
		changeClass('popupDiv1', 'miniBio');
		if (BrowserDetect.browser == 'Explorer') {
			showDiv.style.top = parseInt(findPosY(currentObj)) + 40 + 'px';
		}
		else {
			showDiv.style.top = parseInt(findPosY(currentObj)) + 15 + 'px';
		}
		showDiv.style.left = parseInt(findPosX(currentObj)) - 270 + 'px';
	}
	else {
		changeClass('popupDiv1', 'miniBioInverse');
			
		if (BrowserDetect.browser == 'Explorer') {
			showDiv.style.top = parseInt(findPosY(currentObj)) - 230 + 40 + 'px';
		}
		else {
			showDiv.style.top = parseInt(findPosY(currentObj)) + 15 - 230 + 'px';
		}
		showDiv.style.left = parseInt(findPosX(currentObj)) - 270 + 'px';
	}
}

var pageCelebsClearTimer;
function pageCelebs(obj) {
	urlparts = currentObj.href.split('?');
	var myAjax = new Ajax.Request('/ajax/pageCelebs.php?' + urlparts[1],
		{method: 'get', parameters: "", onComplete: showPageCelebs} );
}

function pageCelebsNewFormat(obj) {
	var myAjax = new Ajax.Request('/ajax/pageCelebs.php?pageUrl=' + escape(currentObj.href),
		{method: 'get', parameters: "", onComplete: showPageCelebs} );
}


function showPageCelebs(originalRequest) {
	clearTimeout(pageCelebsClearTimer);
	activePopup = 'popupDiv1';
	showDiv = document.getElementById('popupDiv1');
	showDiv.innerHTML = originalRequest.responseText;
	changeClass('popupDiv1', 'pageCelebs');
	showDiv.style.top = parseInt(findPosY(currentObj)) - 192 + 'px';
	showDiv.style.left = parseInt(findPosX(currentObj)) + 'px';
}

function startPageCelebsClearTimer() {
	pageCelebsClearTimer = setTimeout('clearActive()', 1000);
}



var BrowserDetect = {	init: function () {		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";		this.version = this.searchVersion(navigator.userAgent)			|| this.searchVersion(navigator.appVersion)			|| "an unknown version";		this.OS = this.searchString(this.dataOS) || "an unknown OS";	},	searchString: function (data) {		for (var i=0;i<data.length;i++)	{			var dataString = data[i].string;			var dataProp = data[i].prop;			this.versionSearchString = data[i].versionSearch || data[i].identity;			if (dataString) {				if (dataString.indexOf(data[i].subString) != -1)					return data[i].identity;			}			else if (dataProp)				return data[i].identity;		}	},	searchVersion: function (dataString) {		var index = dataString.indexOf(this.versionSearchString);		if (index == -1) return;		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));	},	dataBrowser: [		{ 	string: navigator.userAgent,			subString: "OmniWeb",			versionSearch: "OmniWeb/",			identity: "OmniWeb"		},		{			string: navigator.vendor,			subString: "Apple",			identity: "Safari"		},		{			prop: window.opera,			identity: "Opera"		},		{			string: navigator.vendor,			subString: "iCab",			identity: "iCab"		},		{			string: navigator.vendor,			subString: "KDE",			identity: "Konqueror"		},		{			string: navigator.userAgent,			subString: "Firefox",			identity: "Firefox"		},		{			string: navigator.vendor,			subString: "Camino",			identity: "Camino"		},		{		// for newer Netscapes (6+)			string: navigator.userAgent,			subString: "Netscape",			identity: "Netscape"		},		{			string: navigator.userAgent,			subString: "MSIE",			identity: "Explorer",			versionSearch: "MSIE"		},		{			string: navigator.userAgent,			subString: "Gecko",			identity: "Mozilla",			versionSearch: "rv"		},		{ 		// for older Netscapes (4-)			string: navigator.userAgent,			subString: "Mozilla",			identity: "Netscape",			versionSearch: "Mozilla"		}	],	dataOS : [		{			string: navigator.platform,			subString: "Win",			identity: "Windows"		},		{			string: navigator.platform,			subString: "Mac",			identity: "Mac"		},		{			string: navigator.platform,			subString: "Linux",			identity: "Linux"		}	]};BrowserDetect.init();

function add2fav(cid) {
		var myAjax = new Ajax.Request('/ajax/toggleFavorites.php?cid=' + cid,
		{method: 'get', parameters: "", onComplete: toggleFavorites} );
}

function remfromfav(cid) {
		var myAjax = new Ajax.Request('/ajax/toggleFavorites.php?cid=' + cid,
		{method: 'get', parameters: "", onComplete: toggleFavorites} );
}

function toggleFavorites(originalRequest) {
	favsDiv = document.getElementById('favsCell');
	favsDiv.innerHTML = originalRequest.responseText;	
}
//alert(BrowserDetect.browser);


function getCheckedValue(radioObj) {	if(!radioObj)		return "";	var radioLength = radioObj.length;	if(radioLength == undefined)		if(radioObj.checked)			return radioObj.value;		else			return "";	for(var i = 0; i < radioLength; i++) {		if(radioObj[i].checked) {			return radioObj[i].value;		}	}	return "";}

var currentImageWidgetId = 0;

function updateDaterange(widgetId) {

	currentImageWidgetId = widgetId;
	
	daterange = getCheckedValue(eval('document.daterangeForm_' + widgetId + '.daterange'));
	var myAjax = new Ajax.Request('imgDaterange.php?daterange=' + daterange,
		{method: 'get', parameters: "", onComplete: newImagePicker} );
}

function newImagePicker(originalRequest) {
	wrapper = document.getElementById('pickerWrapper_' + currentImageWidgetId);
	
	htmlBlock = originalRequest.responseText;	

	//replace this text: daterangeForm_{cb: widgetId}" onchange="updateDaterange('{cb: widgetId}'
	htmlBlock = htmlBlock.replace(/daterangeForm_\d+/, 'daterangeForm_' + currentImageWidgetId);
	htmlBlock = htmlBlock.replace(/updateDaterange\('\d+/g, "updateDaterange('" + currentImageWidgetId);
	wrapper.innerHTML = htmlBlock;

		
	currentImageWidgetId = 0;
}



/*function hotCelebsFilter() {
	
	len = document.topCelebPicker.dateRange.length;

	for (i = 0; i <len; i++) {
		if (document.topCelebPicker.dateRange[i].checked) {
			range = document.topCelebPicker.dateRange[i].value
		}
	}
	var myAjax = new Ajax.Request('/ajax_topCelebs.php?range=' + range,
		{method: 'get', parameters: "", onComplete: refreshHotCelebs} );
}
*/

function refreshHotCelebs(originalRequest) {
	hcdiv = document.getElementById('topCelebs');
	hcdiv.innerHTML = originalRequest.responseText;	
}

//code to manage "in this story" popups



function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}
