var showcontentId;
// ISF1.11 :: Image swap-fade 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
//******************************************************
//global object
var isf = { 'clock' : null, 'fade' : true, 'count' : 1 }
/*******************************************************



/*****************************************************************************
 List the images that need to be cached
*****************************************************************************/

isf.imgs = ['rotator/pict1.jpg',
			'rotator/pict2.jpg',
			'rotator/pict3.jpg',
			'rotator/pict4.jpg',
			'rotator/pict5.jpg',
			'rotator/pict6.jpg',
			'rotator/pict7.jpg',
			'rotator/pict8.jpg',
			'rotator/pict9.jpg',
			'rotator/pict10.jpg',
			'rotator/pict11.jpg',
			'rotator/pict12.jpg',
			'rotator/pict13.jpg',
			'rotator/pict14.jpg',
			'rotator/pict15.jpg',
			'rotator/pict16.jpg',
			'rotator/pict17.jpg',
			'rotator/pict18.jpg',
			'rotator/pict19.jpg',
			'rotator/pict20.jpg',
			'rotator/pict21.jpg',
			'rotator/pict22.jpg',
			'rotator/pict23.jpg',
			'rotator/pict24.jpg',
			'rotator/pict25.jpg',
			'rotator/pict26.jpg',
			'rotator/pict27.jpg',
			'rotator/pict28.jpg',
			'rotator/pict29.jpg'
	];

/*****************************************************************************
*****************************************************************************/

if(!xmlHttp) {
	if(window.ActiveXObject) {
		var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		var xmlHttp = new XMLHttpRequest();
	}
}

//cache the images
isf.imgsLen = isf.imgs.length;
isf.cache = [];
for(var i=0; i<isf.imgsLen; i++)
{
	isf.cache[i] = new Image;
	isf.cache[i].src = isf.imgs[i];
}


//swapfade setup function
function swapfade(objId, imageIndex, transition, imgalt )
{
	//if the timer is not already going
	if(isf.clock == null)
	{
		//copy the image object 
		isf.obj = objId;

		//copy the image src argument 
		isf.src = isf.imgs[imageIndex];
		
		//store the supported form of opacity
		if(typeof isf.obj.style.opacity != 'undefined')
		{
			isf.type = 'w3c';
		}
		else if(typeof isf.obj.style.MozOpacity != 'undefined')
		{
			isf.type = 'moz';
		}
		else if(typeof isf.obj.style.KhtmlOpacity != 'undefined')
		{
			isf.type = 'khtml';
		}
		else if(typeof isf.obj.filters == 'object')
		{
			//weed out win/ie5.0 by testing the length of the filters collection (where filters is an object with no data)
			//then weed out mac/ie5 by testing first the existence of the alpha object (to prevent errors in win/ie5.0)
			//then the returned value type, which should be a number, but in mac/ie5 is an empty string
			isf.type = (isf.obj.filters.length > 0 && typeof isf.obj.filters.alpha == 'object' && typeof isf.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			isf.type = 'none';
		}
		
		//change the image alt text if defined
		if(typeof imgalt != 'undefined' && imgalt != '')
		{
			isf.obj.alt = imgalt;
		}
		
		//if any kind of opacity is supported
		if(isf.type != 'none')
		{
			//copy and convert fade duration argument 
			//the duration specifies the whole transition
			//but the swapfade is two distinct transitions
			isf.length = parseInt(transition, 10) * 500;
			
			//create fade resolution argument as 20 steps per transition
			//again, split for the two distrinct transitions
			isf.resolution = parseInt(transition, 10) * 10;
			
			//start the timer
			isf.clock = setInterval('isf.swapfade()', isf.length/isf.resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{
			//just do the image swap
			isf.obj.src = isf.src;
		}
		
	}
};


//swapfade timer function
isf.swapfade = function()
{
	//increase or reduce the counter on an exponential scale
	isf.count = (isf.fade) ? isf.count * 0.9 : (isf.count * (1/0.9)); 
	
	//if the counter has reached the bottom
	if(isf.count < (1 / isf.resolution))
	{
		//clear the timer
		clearInterval(isf.clock);
		isf.clock = null;

		//do the image swap
		isf.obj.src = isf.src;

		//reverse the fade direction flag
		isf.fade = false;
		
		//restart the timer
		isf.clock = setInterval('isf.swapfade()', isf.length/isf.resolution);

	}
	
	//if the counter has reached the top
	if(isf.count > (1 - (1 / isf.resolution)))
	{
		//clear the timer
		clearInterval(isf.clock);
		isf.clock = null;

		//reset the fade direction flag
		isf.fade = true;
		
		//reset the counter
		isf.count = 1;
	}

	//set new opacity value on element
	//using whatever method is supported
	switch(isf.type)
	{
		case 'ie' :
			isf.obj.filters.alpha.opacity = isf.count * 100;
			break;
			
		case 'khtml' :
			isf.obj.style.KhtmlOpacity = isf.count;
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			isf.obj.style.MozOpacity = (isf.count == 1 ? 0.9999999 : isf.count);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			isf.obj.style.opacity = (isf.count == 1 ? 0.9999999 : isf.count);
	}
};

function feedOnline() {
	var url = "getcontent.php?do=whoslive";
	showcontentId = 'home_chatbox';
	getContent(url);	
}

function feedChat() {
	var url = "getcontent.php?do=chat";
	showcontentId = 'chatContent';
	getContent(url);	
}

function getMenu() {
	var strHref = window.location.href;
  	if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?"));
	}
	else{
		var strQueryString ='?';
	}
	var url = 'http://www.musclepowerbeauty.com/members/gallery.php'+strQueryString;
	xmlHttp.open("GET",url);
	xmlHttp.onreadystatechange = displayMenu;
	xmlHttp.send(null);		
}

function displayMenu() {
	if(xmlHttp.readyState == 4){		  
		document.getElementById('members_gall_navigation').innerHTML =  xmlHttp.responseText;		
	}	
}

function getBreadCrumb() {
	var strHref = window.location.href;
  	if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?"));
	}
	else{
		var strQueryString ='?';
	}
	var url = 'http://www.musclepowerbeauty.com/members/breadcrumb.php'+strQueryString;
	xmlHttp.open("GET",url);
	xmlHttp.onreadystatechange = displayBreadCrumb;
	xmlHttp.send(null);	
}

function displayBreadCrumb() {
	if(xmlHttp.readyState == 4){		  
		document.getElementById('preview_toptxt').innerHTML =  xmlHttp.responseText;		
	}	
}

function rotate() {
	/*
	var url = "rotator/rotator.php?getbyclient=yes";
	showcontentId = 'pictureRotate';
	getMedia(url);
	*/
	var key =( Math.floor ( Math.random ( ) * 29) ); 
	swapfade(document.getElementById('pictureRotate'), key, '5','') ;	
}

function displayContent() {
	if(xmlHttp.readyState == 4){		  
		document.getElementById(showcontentId).innerHTML =  xmlHttp.responseText;
		//clearInterval(showtimeonline);
		if(showcontentId =='home_chatbox') {
			if(xmlHttp.responseText.indexOf("launchPrivate.m") > -1 || xmlHttp.responseText.indexOf("members.php") > -1 || xmlHttp.responseText.indexOf("whoslive.php") > -1) {
				document.getElementById(showcontentId).className = 'chat_online';
			}
			else {
				document.getElementById(showcontentId).className = 'chat_offline';
			}
		}
	}	
}
function displayMedia() {
	if(xmlHttp.readyState == 4){	
		if(xmlHttp.responseText!='') {	
			swapfade(document.getElementById('pictureRotate'), xmlHttp.responseText, '5','') ;	
			document.getElementById(showcontentId).src = xmlHttp.responseText;			
		}
	}
	
}
function getContent(url) {
	xmlHttp.open("GET",url);
	xmlHttp.onreadystatechange = displayContent;
	xmlHttp.send(null);		
}

function getMedia(url) {
	xmlHttp.open("GET",url);
	xmlHttp.onreadystatechange = displayMedia;
	xmlHttp.send(null);		
}
