var mainBannerHeight, isIE6;
var bannerOffset = 28;
var tid = 0;
var pageName = 'subpage';
var inProgClass = 'processing';

$(document).ready( function() {
	
	$( "body" ).addClass( 'hasJs' );
	
	if( $( '#page' ).hasClass( 'home' ) ) {
		pageName = 'home';
	}
	else if( $('#page').hasClass('events') ) {
		pageName = 'events';
	}
	
	
	/* Set up placeholder text in input and textarea fields */
	insertPlaceholder( 'input:text' );
	insertPlaceholder( 'input:password' );
	insertPlaceholder( 'textarea' );
	
	
	/* Last children */
	$( ".module:last-child" ).addClass( "module-last" );
	
	
	/* N-th children */
	$( "table tr:odd" ).addClass("odd");
	$( ".jumplinks li:nth-child(even)" ).addClass( 'even' );
	
	accordionContent();
	
});

$(window).load(function () {

    if (pageName != 'events') {
        setContentHeight();
    }

    featureItems( '.features .feature', pageName );

    setUpEventsFeature('.event-contents .contents .featured-content');

    setupImgSwitcher('.strategic-partners');

});


/* Expandable content area */
function accordionContent() {
	$( "#accordians .accord-container .h-right a" ).toggle(
		function() {
			$(this).parents('div.accord-container').addClass( 'accord-container-open' );
			$(this).html('&ndash;');
		},
		function() {
			$(this).parents('div.accord-container').removeClass( 'accord-container-open' );
			$(this).html('+');
		}
	);
}


/* Get the full height of an item */
function getHeight( el ) {
	var h = $(el).outerHeight();
	return h;
}


/* Determine the height of the sidebar and make sure the content is at least that tall */
function setContentHeight() {
	
	var $secondary = $( '#content .secondary' );
	var $features = $( '.features' );
	var $memberPromos = $( '.promo-members' );
	var $pageTitle = $( '#page-info' );
	
	var sidebarHeight = getHeight( $secondary );
	
	if( pageName == 'home' ) {
		var featuresHeight = getHeight( $features );
		var promosHeight = getHeight( $memberPromos );
		mainBannerHeight = ( featuresHeight + promosHeight - bannerOffset );
	} else {
		var pageTitleHeight = getHeight( $pageTitle );
		mainBannerHeight = pageTitleHeight - bannerOffset;
	}
	
	sidebarHeight = sidebarHeight - mainBannerHeight;
	
	if( isIE6 == true ) {
		$('#content').css( 'height', sidebarHeight );
	} else {
		$('#content').css( 'min-height', sidebarHeight );
	}
	
	$secondary.removeClass( inProgClass );
}

/* See if the browser understands the placeholder attribute */
function testPlaceholder() {
	var i = document.createElement('input');
	return 'placeholder' in i;
}

/* Insert placeholder text into inputs */
function insertPlaceholder( el ) {
	var hasPlaceholder = testPlaceholder();
	if( hasPlaceholder ) {
		return;
	} else {
		$( el ).each( function() {
			var inputValue = $(this).attr('value');
			var placeholder = $(this).attr('placeholder');
			if(inputValue != placeholder) {
				$(this).val( placeholder );
				inputValue = $(this).val();
			}
			
			$(this).focus( function() {
				if( inputValue == placeholder ) {
					$(this).val( '' );
				}
				inputValue = $(this).val();
			});
			
			$(this).blur( function() {
				inputValue = $(this).val();
				if( inputValue == placeholder || inputValue == '' ) {
					$(this).val( placeholder );
				} else {
					$(this).val( inputValue );
				}
				inputValue = $(this).val();
			});
			
		});
	}
}


/* Controls to change featured events items */
function setUpEventsFeature( el ) {
	
	var nFeatures = $( el ).length;
	
	if( nFeatures == 1 ) {
		return;
	} else {
		var config = {
			current : 1,
			tid : 0,
			period : 7000,
			status : 'rotating',
			nFeatures : nFeatures
		};
		var controllers = '<div class="controls"><a href="#" class="pause">Pause</a><a href="#spk2" class="next" rel="2">Next</a></div>';
		$( '.event-contents .contents' ).append( controllers );
		
		startRotate( config );
		
		// set up click event for pause
		$( '.event-contents .contents .controls a.pause' ).click( function() {
			
			// if rotating, stop rotation. if paused, resume rotation
			if( config.status == 'rotating' ) {
				stopRotate( config );
			} else {
				startRotate( config );
			}
			return false;
		});
		
		// set up click event for next
		$( '.event-contents .contents .controls a.next' ).click( function() {
			
			if( config.status == 'rotating' ) {
				stopRotate( config );
			}
			
			// stop rotation. advanced to next speaker
			changePanel( config, 'spk' );
			
			return false;
		});
		
	}
	
}

function startRotate( config ) {
	config.tid = setInterval( function() {
		changePanel( config, 'spk' )
	}, 7000 );
	config.status = 'rotating';
}
function stopRotate( config ) {
	clearTimeout( config.tid );
	config.status = 'paused';
}

function changePanel( config, prefix ) {
	if( config.current < config.nFeatures ) {
		config.current++;
	} else {
		config.current = 1;
	}
	// get DIV with ID of the current item and show it.
	$( '.controls a.next' ).attr( 'rel', config.current ).attr( 'href', '#' + prefix + config.current );
	
	var panel = $( '.controls a.next' ).attr('rel');
	showPanel( panel, prefix );
	
}

function setupImgSwitcher( container ) {
	var config = {
		current : 0
	}
	var tid = setInterval( function() { imgSwitcher( container, config ) }, 7000 );
}
function imgSwitcher( container, config ) {
	var imgCount = $( container + ' img' ).length;
	
	if( config.current < (imgCount - 1) ) {
		config.current++;
	} else {
		config.current = 0;
	}
	
	$( container + ' img' ).siblings().removeClass( 'current' );
	$( container + ' img' ).eq( config.current ).addClass( 'current' );
	
}



function featureItems( el, pageName ) {
	
	var nFeatures = $( el ).length;
	
	if( nFeatures == 1 ) {
		return;
	} else {
		
		// measure height of all and set each feature to the tallest item
		var maxHeight = 0;
		$(el).each(function () {

            // added by EE to fix height calc error
		    //var element = $(this).clone();
		    //element.css({ visibility: 'hidden', display: 'block' }).insertAfter(this);
		    //element.attr('style', element.attr('style').replace('block', 'block !important'));
		    //var hgt = element.height();
		    //element.remove();
		    var hgt = $(this).height();
            // EE

		    if (hgt > maxHeight) {
		        maxHeight = hgt;
		    }
		});
		
		$( el ).each( function() {
			$(this).height( maxHeight );
		});
		
		
		var config = {
			current : 1,
			tid : 0,
			period : 7000,
			status : 'rotating',
			nFeatures : nFeatures
		};
		
		var selectors = '<div class="controls">';
		for( var i=1; i <= nFeatures; i++ ) {
			selectors += '<a href="#f' + i + '" rel="' + i + '"';
			if( i==1 ) {
				selectors += ' class="active"';
			}
			selectors += '>' + i + '</a>';
		}
		selectors += '</div>';
		if( pageName == 'home' ) {
			$( '.features .wrapper-in' ).prepend( selectors );
		} else {
			$( '.features' ).prepend( selectors );
		}
		
		startFeatureRotate( config, 'f' );
		
		$( '.features .controls a' ).click( function() {
			stopFeatureRotate( config );
			
			config.current = $( this ).attr('rel');
			changeFeatureHighlight( config );
			
			var panel = $(this).attr('rel');
			showPanel( panel, 'f' );
			
			return false;
		});
		
		
	}
	
}

function changeFeaturePanel( config, prefix ) {
	if( config.current < config.nFeatures ) {
		config.current++;
	} else {
		config.current = 1;
	}
	
	var panel = $( ".controls a[rel='" + config.current + "']" ).attr('rel');
	showPanel( panel, prefix );
}

function showPanel( panel, panelPrefix ) {
	$( '#' + panelPrefix + panel ).siblings().removeClass( 'current' );
	$( '#' + panelPrefix + panel ).addClass( 'current' );
}

function changeFeatureHighlight( config ) {
	var currentItem = $( ".controls a[rel='" + config.current + "']" );
	currentItem.siblings().removeClass( 'active' );
	currentItem.addClass( 'active' );
}

function startFeatureRotate( config, prefix ) {
	config.tid = setInterval( function() {
		changeFeaturePanel( config, prefix );
		changeFeatureHighlight( config );
	}, 7000 );
	config.status = 'rotating';
}
function stopFeatureRotate( config ) {
	clearTimeout( config.tid );
	config.status = 'paused';
}

