/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

	WebCore: Content switch scripts
	Abyss Studios Ltd. (c) 2004-2006

	history:
		061201 - creation -=AGi=-

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/

// --- content switch - base ---

function contentSwitch_EventStopBubble(e)
{
	if (e==null) e = window.event;
	if (e!=null) {
		if (e.cancelBubble!=null) e.cancelBubble = true;
		if (e.returnValue!=null) e.returnValue = false;
		/*if (e.preventDefault!=null) e.preventDefault(); *//* firefox 1.x.x <-egor-> */ 
		if (e.stopPropagation!=null) e.stopPropagation();
	}
	return false;
}

function contentSwitch_IsValidContent(content)
{
	if (content==null) return false;
	if (content.className==null) return false;
	if (content.className.indexOf('Separator')>=0) return false;
	if (content.className.indexOf('Next')>=0) return false;
	if (content.className.indexOf('Prev')>=0) return false;
	return true;
}

function contentSwitch_SetInactive(content)
{
	if (content==null) return false;
	if (content.className==null) return false;
	j = content.className.indexOf('Active');
	if (j>=0) {
		content.className = content.className.substring(0, j);
	}
	return true;
}

function contentSwitch_SetActive(content)
{	
	if (content==null) return false;
	if (content.className==null) return false;
	j = content.className.indexOf('Active');
	if (j < 0) {
		content.className = content.className + 'Active';
	}
	return true;
}

function contentSwitch_SetInactiveAll(container)
{
	if (typeof(container)=='string') container = document.getElementById(container);
	if (container==null) return null;
	for(i = 0; i < container.childNodes.length; i++) {
		e = container.childNodes[i];
		contentSwitch_SetInactive(e);
	}
}

function contentSwitch_SetActiveAll(container)
{
	if (typeof(container)=='string') container = document.getElementById(container);
	if (container==null) return null;
	for(i = 0; i < container.childNodes.length; i++) {
		e = container.childNodes[i];
		contentSwitch_SetActive(e);
	}
}

function contentSwitch_SetActiveSingle(container, content)
{	
	if (typeof(container)=='string') container = document.getElementById(container);
	if (container==null) return null;
	for(i = 0; i < container.childNodes.length; i++) {
		e = container.childNodes[i];
		if (!contentSwitch_IsValidContent(e)) continue;
		if (e==content) {
			contentSwitch_SetActive(e);
		}
		else {
			contentSwitch_SetInactive(e);
		}
	}
}

function contentSwitch_GetActive(container)
{	
	//alert(container);
	//alert(container.id);
	if (typeof(container)=='string') container = document.getElementById(container);
	if (container==null) return null;
	for(i = 0; i < container.childNodes.length; i++) {
		e = container.childNodes[i];
		//alert(e);		
		if (!contentSwitch_IsValidContent(e)) continue;
		j = e.className.indexOf('Active');
		if (j>=0) return e;
	}
}


function contentSwitch_GetSuccessor(container, content)
{
	if (container==null) return null;
	if (container.childNodes.length <= 0) return null;
	
	e = (content!=null) ? content.nextSibling : null;
	while(e!=null) {
		if (contentSwitch_IsValidContent(e)) return e;
		e = e.nextSibling;
	}
			
	e = container.childNodes[0];
	while(e!=null && e!=content) {
		if (contentSwitch_IsValidContent(e)) return e;
		e = e.nextSibling;
	}	
	
	return null;
}

function contentSwitch_GetPredecessor(container, content)
{
	if (!contentSwitch_IsValidContent(content)) content = contentSwitch_GetSuccessor(content);
	var pe = content;
	while(pe!=null) {
		succ = contentSwitch_GetSuccessor(container, pe);
		if (succ==null) return content;
		if (succ==content) {
			return pe;
		};
		pe = succ;
	}
	return null;
}

function contentSwitch_ExecuteSelectScript(content)
{
	if (content==null) return;
	if (typeof(content.onselect)=='string') {
		content.onselect = new Function(content.onselect);
	}
	if (content.onselect!=null) content.onselect();
}

function contentSwitch_SelectNext(container)
{
	if (typeof(container)=='string') container = document.getElementById(container);
	content = contentSwitch_GetActive(container);
	content = contentSwitch_GetSuccessor(container, content);
	contentSwitch_SetActiveSingle(container, content);
	contentSwitch_ExecuteSelectScript(content);
}

function contentSwitch_SelectPrev(container)
{
	if (typeof(container)=='string') container = document.getElementById(container);
	content = contentSwitch_GetActive(container);
	content = contentSwitch_GetPredecessor(container, content);
	contentSwitch_SetActiveSingle(container, content);
	contentSwitch_ExecuteSelectScript(content);
}

function contentSwitch_AutoSelectNext(container, timeout, skipCount)
{
	if (typeof(container)=='string') container = document.getElementById(container);
	if (container==null) return false;
	if (skipCount==null) skipCount = 1;
	
	if (typeof(container)=='string') container = document.getElementById(container);
	content = contentSwitch_GetActive(container);

	for(skipIndex = 0; skipIndex < skipCount; skipIndex++) {
		content = contentSwitch_GetSuccessor(container, content);
	};
	
	contentSwitch_SetActiveSingle(container, content);
	contentSwitch_ExecuteSelectScript(content);	
	
	container['timeoutHandle'] = setTimeout("contentSwitch_AutoSelectNext('" + container.id + "', " + timeout + ")", timeout);
	return true;
}


function contentSwitch_AutoSelectNext_Default(container, skipCount)
{
	contentSwitch_AutoSelectNext(container, 5000, skipCount);
}


function contentSwitch_PauseTimer(container)
{
	if (typeof(container)=='string') container = document.getElementById(container);
	if (container==null) return false;
	handle = container['timeoutHandle'];
	clearTimeout(handle);	
}

function contentSwitch_ContinueTimer(container)
{
	if (typeof(container)=='string') container = document.getElementById(container);
	if (container==null) return false;
	container['timeoutHandle'] = setTimeout("contentSwitch_AutoSelectNext_Default('" + container.id + "')", 15000);	
}

function contentSwitch_UserSelect(container, content)
{
	contentSwitch_PauseTimer(container);
	contentSwitch_SetActiveSingle(container, content);
	contentSwitch_ExecuteSelectScript(content);
	contentSwitch_ContinueTimer(container);
}

function contentSwitch_UserSelectPrev(container)
{
	contentSwitch_PauseTimer(container);
	contentSwitch_SelectPrev(container)
	contentSwitch_ContinueTimer(container);
}

function contentSwitch_UserSelectNext(container)
{
	contentSwitch_PauseTimer(container);
	contentSwitch_SelectNext(container)
	contentSwitch_ContinueTimer(container);
}

// --- ad switch ---

var adSwitch_Crossfade_timeout
function adSwitch_SetClassNameUrl(adMainControl, adLinkControl, className, url)
{
	if (adMainControl==null) return;
	if (adLinkControl==null) return;
	adMainControl.className = className;
	adLinkControl.onselect = "window.location.href='" + url + "'";
}

function adSwitch_SetClassNameUrl_Parented(baseControl, adMainParentLevel, adLinkParentLevel, className, url)
{
	adMainControl = baseControl;
	for(i = 0; adMainControl!=null && i < adMainParentLevel; i++) adMainControl = adMainControl.parentNode;

	adLinkControl = baseControl;
	for(i = 0; adLinkControl!=null && i < adLinkParentLevel; i++) adLinkControl = adLinkControl.parentNode;
	
	adSwitch_SetClassNameUrl(adMainControl, adLinkControl, className, url);
}

function adSwitch_SetImageFade_Parented(baseControl, adMainParentLevel, adLinkParentLevel, imageIndex, url)
{
	adMainControl = baseControl;
	for(i = 0; adMainControl!=null && i < adMainParentLevel; i++) adMainControl = adMainControl.parentNode;

	adLinkControl = baseControl;
	for(i = 0; adLinkControl!=null && i < adLinkParentLevel; i++) adLinkControl = adLinkControl.parentNode;	
	
	adLinkControl.onselect = "window.location.href='" + url + "'";


	backImageIndex = 0;
	eCurrentImage = null;
	ePreviousImage = null;
	for(i = 0; i < adMainControl.childNodes.length; i++) {
		e = adMainControl.childNodes[i];
		if (e.className!='adBackgroundImage') continue;
		
		if (e['timeoutHandle']!=null) {
			clearTimeout(e['timeoutHandle']);
			e['timeoutHandle'] = null;
		}
		
		if (imageIndex==backImageIndex) {
			e.style.visibility = 'visible';
			e.style.zIndex = 2;
			eCurrentImage = e;
		}
		else {
			if (e.style.zIndex==2) {
				ePreviousImage = e;
				e.style.zIndex = 1;
			}
			else {
				e.style.zIndex = 0;
				e.style.visibility = 'hidden'
			};
			e.style.opacity = 1;
			e.style.filter = "";
		}
		backImageIndex++;
	}

	

	/*

	// set the current image to front
	backImageIndex = 0;
	eCurrentImage = null;	
	for(i = 0; i < adMainControl.childNodes.length; i++) {
		e = adMainControl.childNodes[i];
		if (e.className!='adBackgroundImage') continue;
		
		if (e['timeoutHandle']!=null) {
			clearTimeout(e['timeoutHandle']);
			e['timeoutHandle'] = null;
		}
		
		if (imageIndex==backImageIndex) {
			e.style.visibility = 'visible';
			e.style.zIndex = 3;
			eCurrentImage = e;
		}
		else {
			//e.style.opacity = 1;
			//e.style.filter = "";
			e.style.visibility = 'hidden'		
		}
		
		backImageIndex++;
	}
	
	// manage all other images
	backImageIndex = 0;
	ePreviousImage = null;		
	for(i = 0; i < adMainControl.childNodes.length; i++) {
		e = adMainControl.childNodes[i];
		if (e.className!='adBackgroundImage') continue;
		
		if (imageIndex!=backImageIndex) {
			if (e.style.zIndex==2) {
				ePreviousImage = e;
				e.style.zIndex = 1;
			}
			else {
				e.style.zIndex = 0;
				e.style.visibility = 'hidden'
			};
			e.style.opacity = 1;
			e.style.filter = "";					
		}
	
		backImageIndex++;
	}
	eCurrentImage.style.zIndex = 2;
	*/
	
	if (eCurrentImage!=null && ePreviousImage!=null) {
		adSwitch_FadeImage(eCurrentImage.id, null, 600);
	}
}

function adSwitch_FadeImage(eImage, timeStart, timeTotal)
{	
	now = new Date();
	if (timeStart==null || timeStart==0)
		timeStart = now.getTime();
	timeCurrent = now.getTime() - timeStart;
	fadePosition = timeCurrent / timeTotal;
	
	if (timeCurrent > timeTotal) timeCurrent = timeTotal;
	
	if (typeof(eImage)=='string') eImage = document.getElementById(eImage);
	
	if (eImage!=null) {
		opacity = fadePosition;
		if (document.all) {
			eImage.style.filter = "alpha(opacity=" + Math.round(opacity*100) + ")";
		}
		else {
			eImage.style.opacity = opacity;
		}
		if (timeCurrent < timeTotal) {
			eImage['timeoutHandle'] = setTimeout("adSwitch_FadeImage('" + eImage.id + "', " + timeStart + ", " + timeTotal + ")", 30);
		}
	};
}

