function popup( url, name, xsize, ysize, features ) {
    if( !url )
        return true;
    if( !name )
        name = "popupwindow"
    if( xsize == undefined || xsize <= 0 || ysize == undefined || ysize <= 0 )
        window.open( url, name, features )
    else {
        if( !features )
            features = ""
        else
            features = "," + features
        window.open( url, name, "height="+ysize+",width="+xsize+features )
    }
    return false;
}

function getImage( img ) {
	if( typeof(img) == "string" )
		img = document.getElementById( img );
	if(img.src) {
		return img.src;
	} else {
		if( !img.style.filter )
			return null;
		return ( /src='(.*?)'/i ).exec( img.style.filter )[1];
	}
}

function setImage( img, url ) {
	if( typeof(img) == "string" )
		img = document.getElementById( img );
	if(img.src) 
		img.src = url;
	else
		img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader"
			+ "(src=\'" + url + "\', sizingMethod=\'crop\')";
	return false;
}

function flashRun( source, width, height, wmode, noflash, node ) {
	document.write( '<a href="http://www.adobe.com/go/getflash/" target="_blank"><img alt="no flash" src="'+noflash+'" /></a>' );
	var so = new SWFObject( source, "banner", width, height, "7" )
	so.addParam( "wmode", wmode );
	so.write( node );
}

function getStyle(oElm, strCssRule){
    var strValue = "";
    if(document.defaultView && document.defaultView.getComputedStyle){
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if(oElm.currentStyle){
        strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function _hasAttribute( node, attr ) {
	return (node.hasAttribute && node.hasAttribute( attr )) ||
		eval( 'node.'+attr+';' );
}
function _getAttribute( node, attr ) {
	if( node.getAttribute )
		return node.getAttribute( attr );
	return eval( 'node.'+attr+';' );
}
function _setAttribute( node, attr, value ) {
	if( node.setAttribute )
		return node.setAttribute( attr, value );
	return eval( 'node.'+attr+'= value;' );
}
function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}
function createAJAXRequest() {
	if(window.XMLHttpRequest)
		return new XMLHttpRequest();
	else if(window.ActiveXObject)
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
			}
		}
	return null;	
}

function doSearch( node ) {
	if( typeof(node) == 'string' )
		node = document.getElementById(node);
	if(!node)
		return true;
	if( _getAttribute(node,'default') == 'true') {
		_setAttribute(node,'value','')
		_setAttribute(node,'default','false');
	}
	return true;
}
function searchFocus( node ) {
	if( typeof(node) == 'string' )
		node = document.getElementById(node);
	if(!node)
		return true;
	if( _getAttribute(node,'default') == 'true' ) {
		_setAttribute(node,'value','')
		_setAttribute(node,'default','false');
	}
	return true;
}

function initDebug() {
	var node = document.getElementById( "debug" );
	var open = readCookie( "debug_open" ) == 'true' ? true : false;
	setDebug( open );
}

function setDebug( open ) {
	var node = document.getElementById( 'debug' );
	if( !node )
		return;
	if( open )
		node.className = node.className.replace( / ?close/, ' open' );
	else
		node.className = node.className.replace( / ?open/, ' close' );
}

function debug( key, value ) {
	var childArea = document.getElementById( 'debug_children' );
	if( !childArea )
		return;
	var child = document.getElementById( 'debug_'+key );
	if( !child ) {
		child = document.createElement( 'tr' );
		child.setAttribute( 'id', 'debug_'+key );
		child.setAttribute( 'class', childArea.childNodes.length % 2 == 0 ? 'gray' : 'white' );
		
		var keyTd = document.createElement( 'td' );
		keyTd.setAttribute( 'class', 'key' );
		keyTd.innerHTML = key;
		child.appendChild( keyTd );
		var valueTd = document.createElement( 'td' );
		valueTd.setAttribute( 'id', 'debug_value_'+key );
		valueTd.setAttribute( 'class', 'value' );
		valueTd.innerHTML = value;
		child.appendChild( valueTd );
		childArea.appendChild( child );
	} else {
		var valueTd = document.getElementById( 'debug_value_'+key );
		valueTd.innerHTML = value;
	}
}

function debug_closebutton() {
	var node = document.getElementById( 'debug' );
	if( !node )
		return false;
	var oldOpen = node.className.indexOf( "close" ) == -1;
	var newOpen = !oldOpen;
	setDebug( newOpen );
	eraseCookie( "debug_open" );
	createCookie( "debug_open", newOpen, 1 );
	return false;
}

function position( node ) {
	if( !node )
		return [ 0, 0 ];
	return position_recurse( node, [ node.offsetLeft, node.offsetTop ] );
}
function position_recurse( node, pos ) {
	while( node = node.offsetParent ) {
		pos[0] += node.offsetLeft;
		pos[1] += node.offsetTop;
	}
	return pos;
}
function add( a1, a2 ) {
	return [ a1[0] + a2[0], a1[1] + a2[1] ];
}
function diff( a1, a2 ) {
	return [ a1[0] - a2[0], a1[1] - a2[1] ];
}
function contains( parent, child ) {
	if( !parent || !child )
		return false;
	if( parent == child )
		return true;
	while( child = child.parentNode ) {
		if( parent == child )
			return true;
	}
	return false;
}
function mousePosition( e ) {
	if( !e )
		return [0, 0];
	if( e.pageX != undefined )
		return [e.pageX, e.pageY];
	if( document.documentElement && document.documentElement.scrollLeft != undefined )
		return [e.clientX + document.documentElement.scrollLeft, e.clientY + document.documentElement.scrollTop ];
	if( document.body && document.body.scrollLeft != undefined )
		return [e.clientX + document.body.scrollLeft, e.clientY + document.body.scrollTop ];
	return [0, 0];
}
function getScroll() {
	if( window.scrollX != undefined )
		return [ window.scrollX, window.scrollY ];
	if( document.documentElement && document.documentElement.scrollLeft != undefined )
		return [ document.documentElement.scrollLeft, document.documentElement.scrollTop ];
	return [ 0, 0 ];
}
function getViewport() {
	if( document.documentElement && document.documentElement.clientWidth != undefined )
		return [ document.documentElement.clientWidth, document.documentElement.clientHeight ];
	if( window.innerWidth != undefined )
		return [ window.innerWidth, window.innerHeight ];
	return undefined;
}
function clipAmount( pos, size ) {
	var currentPos = diff( add( pos, size ), getScroll() );
	var result = [ 0, 0 ];
	var viewport = getViewport();
	if( !viewport )
		return result;
	if( currentPos[0] > viewport[0]  ) {
		result[0] = currentPos[0] - viewport[0];
	}
	if( currentPos[1] > viewport[1] ) {
		result[1] = currentPos[1] - viewport[1];
	}
	return result;		
}
function hoverBox( e, node, show ) {
	if( typeof(node) == 'string' )
		node = document.getElementById( node );
	if( !node )
		return true;

	if( show ) {
		if ( typeof(node.hideMe) == "number" ) {
			window.clearTimeout( node.hideMe );
			node.hideMe = undefined;
		} else {
			var doPos = true;
			if( e.target )
				doPos = !contains( node, e.target);
			else if( e.srcElement )
				doPos = !contains( node, e.srcElement );
			
			if( doPos ) {
				var parentPos = position( node.offsetParent );
				var mousePos = mousePosition( e );
				var pos = diff( mousePos, parentPos );
				var size = [ node.offsetWidth, node.offsetHeight ];
				pos = diff( pos, clipAmount( mousePos, size ) );
				node.style.left = (pos[0])+"px";
				node.style.top = (pos[1])+"px";
			}
			// Safari needs this to be before the positioning code,
			// but Firefox 1.5 needs it to be after.
			// Can move off screen, and support both, but for now,
			// Firefox 1.5 trumps Safari.
			node.style.visibility = "visible";	
		}
	} else {
		node.hideMe = window.setTimeout(
			function() {
				node.hideMe = undefined;
				node.style.visibility = "hidden";
			}
			, 200 );
	}
	if( e.stopPropagation )
		e.stopPropagation();
		
	return false;
}

/* top login and login controls */
function initTopLogin() {
	var topLogin = document.getElementById( "topLogin" );
	if( !topLogin )
		return;
	var open = readCookie( "open" ) == 'true' ? true : false;
	topLogin.className = open ? "open" : "close";
	if( open ) {
		topLogin.style.top = "0px";
		topLogin.style.marginBottom = "0px";
	}
}
function topLoginOpenClose( open ) {
	var topLogin = document.getElementById( "topLogin" );

	var startPosition = 60; // in css above too
	var endPosition = 0;
	var totalSteps = 10;
	var interval = 50;
	var power = 2;
	var curOpen = isOpen( topLogin.className );
	var newOpen = open ? true : false;
	
	topLogin.className = open ? "open" : "close";
	eraseCookie( "open" );
	createCookie( "open", newOpen, 1 );

	if( curOpen != newOpen ) {
		if( !newOpen ) {
			// swap
			var tmp = endPosition;
			endPosition = startPosition;
			startPosition = tmp;
		}
		if ( topLogin.sizeChanger ) window.clearInterval( topLogin.sizeChanger );
		var curStep = 0;
		topLogin.sizeChanger = window.setInterval(
			function() {
				topLogin.currentPosition = easeInOut( startPosition, endPosition, totalSteps, curStep, power );
				topLogin.style.top = "-" + topLogin.currentPosition + "px";
				topLogin.style.marginBottom = "-" + topLogin.currentPosition + "px";
				curStep++;
				if (curStep > totalSteps) {
					window.clearInterval( topLogin.sizeChanger );
				}
			}
			,interval );
	}
	
	return true;
}
function isOpen( state ) {
	return state.indexOf( "open" ) != -1;
}
function easeInOut(minValue,maxValue,totalSteps,curStep,power){
	return Math.ceil( minValue + (maxValue - minValue) * Math.pow( curStep/totalSteps, power ) )
}
function login() {
}
function logout() {
	topLoginOpenClose( false );
}
	
function toggleHomeLeft(showPanel)
{
	var loginPanel = document.getElementById( "homeLogin" );
	var finderPanel = document.getElementById( "homeFinder" );
	
	if(showPanel == 0)
	{
		loginPanel.style.display = "none";
		finderPanel.style.display = "block";
		
		setImage("imgFind", "/static/images/login/findafitter_on.png");
		setImage("imgSignIn", "/static/images/login/sign_off.png");
	}
	else
	{
		loginPanel.style.display = "block";
		finderPanel.style.display = "none";

		setImage("imgFind", "/static/images/login/findafitter_off.png");
		setImage("imgSignIn", "/static/images/login/sign_on.png");
	}
}

addEvent( window, 'load', function() {
	initTopLogin();
	 } );

/* club detail (and find a fitter) */
var totalTabs = -1;

function setBorder( node ) {
	// do different things based on if this is club detail or find a fitter
	// on clubdetail we set the parent node
	// on find a fitter we set any child nodes
	
	if( document.getElementById( 'clubdetail' ) ) {
		node.parentNode.style.borderColor = getStyle(node, 'border-top-color');
	} else {
		var children = node.childNodes;
		var color = getStyle(node, 'border-top-color');
		for( var x = 0; x < children.length; x++ ) {
			if( children[x] && children[x].style )
				children[x].style.borderColor = color;
		}
	}
}

function initTabs() {
	var active_tab_node = document.getElementById( 'active_tab' );
	var active_tab;
	if( active_tab_node && active_tab_node.value )
		active_tab = active_tab_node.value;

	totalTabs = 0;
	for ( var i = 1; ; i++ ) {
		var tab = document.getElementById( 'tab' + i );
		var content = document.getElementById( 'content' + i );
		if( !tab || !content )
			break;
		totalTabs = i;
		if( !active_tab && tab.className == 'active' )
			active_tab = i;

		if( active_tab == i ) {
			tab.className = 'active';
			content.className = 'active';
			setBorder( content );
		} else {
			tab.className = '';
			content.className = 'inactive';			
		}
	}
	for ( var i = 1; i <= totalTabs ; i++ ) {
		var tab = document.getElementById( 'tab' + i );
		tab.z = totalTabs - i + 1;
	}
	//	alert("active tab: " + active_tab);	
	if( active_tab )
		clubdetailSelect( active_tab );
}
function findafitterSelect( which ) {
	clubdetailSelect( which );
}
function clubdetailSelect( which ) {
	var active_tab_node = document.getElementById( 'active_tab' );
	var newTab = document.getElementById( 'tab' + which );
	if( !newTab )
		return;
	if( !newTab.z )
		initTabs(totalTabs);
	var oldz = newTab.z;
	for ( var i = 1; i <= totalTabs; i++ ) {
		var tab = document.getElementById( 'tab' + i );
		var content = document.getElementById( 'content' + i );

		var imageId = 'tabImage' + i;
		var imagePath = getImage(imageId);

		if( i == which ) {
			if( active_tab_node )
				active_tab_node.value = i;
			tab.className = 'active';
			content.className = 'active';
			tab.z = totalTabs;
			setBorder( content );

			// Make selected tab be highlighted in White
			if(imagePath.indexOf("_wht") == -1)
				imagePath = imagePath.replace( "_tan.gif", "_wht.gif" );
			setImage( imageId, imagePath );

		} else {
			tab.className = '';
			content.className = 'inactive';

			if( tab.z > oldz )
			tab.z--;

			// Make non-selected tabs grey
			imagePath = imagePath.replace( "_wht.gif", "_tan.gif" );
			setImage( imageId, imagePath );
		}
		var children = tab.childNodes;
		for( var j = 0; j < children.length; j++ ) {
			if( children[j] && children[j].style && children[j].style.zIndex != undefined ) {
				children[j].style.zIndex = tab.z;
				break;
			}
		}
	}
	
	return false;
}

function filter( node, list ) {
	var list = document.getElementById( list );
	if( !list )
		return;
	var manufacturer = node[node.selectedIndex].value.toLowerCase();
	var children = list.childNodes;
	for( var i = 0; i < children.length; i++ ) {
		if( !_hasAttribute( children[i], 'manufacturer' ) )
			continue;
		if( manufacturer == 'all' || manufacturer == _getAttribute( children[i], 'manufacturer' ).toLowerCase() ) {
			children[i].style.display = 'block';
		} else {
			children[i].style.display = 'none';
		}
	}
}
addEvent( window, 'load', function() {
	 initTabs();
	 filter( document.getElementById('content1select'), 'content1data' );
	 filter( document.getElementById('content2select'), 'content2data' );
	 filter( document.getElementById('content3select'), 'content3data' );
	 filter( document.getElementById('content4select'), 'content4data' );
	 filter( document.getElementById('content5select'), 'content5data' );
	 } );

/* findafitter (and above) */
function toggleDemoInfo( node ) {
	if( typeof( node ) == 'string' )
		node = document.getElementById( node );
	
	if( node.isHidden == undefined )
		node.isHidden = false;
	
	node.isHidden = !node.isHidden;
	
	if( node.isHidden )
		try {
			node.style.display = "table-row";
		} catch(e) {
			node.style.display = "block";
		}
	else
		node.style.display = "none";
}


/* faqs */
function highlightAnswer( id ) {
	var node = document.getElementById( id )
	if( !node )
		return;
	var siblings = node.parentNode.childNodes
	for( var i = 0; i < siblings.length; i++ ) {
		if( siblings[i] == node ) {
			siblings[i].className = "highlight"
		} else {
			siblings[i].className = ""
		}
	}								
}
function initFaqs() {
	if( window.location && window.location.hash && unescape )
		highlightAnswer( unescape( window.location.hash.substr( 1 ) ) );
}
addEvent( window, 'load', function() {
	initFaqs();
} );

/* testimonials */
function setActive( node, state ) {
	var className = new String( node.className );
	if( state ) {
		if( className.indexOf( ' active' ) == -1 )
			node.className += ' active';
	} else {
		node.className = className.replace( ' active', '' );
	}
}
function setButtonActiveDirect( node, state ) {
	if( !node )
		return;
	var src = getImage( node );
	if( state ) {
		setImage( node, src.replace( '_inactive.png', '.png' ) );
		node.style.cursor = 'pointer';
	} else {
		src = src.replace( '_hover.png', '.png' );
		setImage( node, src.replace( '.png', '_inactive.png' ) );
		node.style.cursor = 'default';
	}			
}
function setButtonActive( which, state ) {
	if( which == 'left' ) {
		setButtonActiveDirect( document.getElementById( 'topLeftButton' ), state );
		setButtonActiveDirect( document.getElementById( 'bottomLeftButton' ), state );
	} else if( which == 'right' ) {
		setButtonActiveDirect( document.getElementById( 'topRightButton' ), state );
		setButtonActiveDirect( document.getElementById( 'bottomRightButton' ), state );
	}
}
function hoverButton( img, state ) {
	var src = getImage( img );
	if( state ) {
		if( src.indexOf( '_inactive.png' ) == -1 )
			setImage( img, src.replace( '.png', '_hover.png' ) );
	} else {
		setImage( img, src.replace( '_hover.png', '.png' ) );
	}
}

function updateStatus( position, number, total ) {
	var end = (position + number);
	if( end > total )
		end = total;
	var newStatus = (position+1) + '-' + end + ' of ' + total;

	var status = document.getElementById( 'topStatus' );
	if( status )
		status.innerHTML = newStatus;
	status = document.getElementById( 'bottomStatus' );
	if( status )
		status.innerHTML = newStatus;
}
function getList( node ) {
	if( typeof( node ) == 'string' )
		node = document.getElementById( node );
	if( !node )
		return new Array();
	
	var rawList = node.childNodes;
	var list = new Array();
	for( var i = 0; i < rawList.length; i++ ) {
		if( rawList[i].nodeName.toLowerCase() == 'div' && rawList[i].className.indexOf('blurb') != -1 ) {
			list.push( rawList[i] );
		}
	}
	return list;
}

var curPosition;
function moveBlurbs( direction, number ) {
	var list = getList( 'testimonialsBlurbsWrapper' );
		
	var curPositionNode = document.getElementById( 'testimonialsCurPosition' );

	if( curPositionNode )
		curPosition = 1*curPositionNode.value;

	if( !curPositionNode && curPosition == undefined )
		curPosition = 0;
		
	if( curPosition + direction >= list.length )
		return false;
	if( curPosition + direction < 0 )
		return false;
		
	curPosition += direction;
	if( curPositionNode )
		curPositionNode.value = curPosition;
	
	for( var i = 0; i < list.length; i++ ) {
		if( i >= curPosition && i - curPosition < number ) {
			setActive( list[i], true );
		} else {
			setActive( list[i], false );
		}
	}
	
	setButtonActive( 'left', curPosition > 0 );
	setButtonActive( 'right', curPosition + number < list.length );
	
	updateStatus( curPosition, number, list.length );
	
	return false;
}
addEvent( window, 'load', function() { moveBlurbs( 0, 4 ); } );

