/* Scripts for ACC */


/*
 * Tabs
 * Controls the display and switching of the tabbed content
 * on the home page and landing pages.
 */
function Tabs() {
	
	var tabContentBox = document.getElementById( 'tab-content' );
	if ( !tabContentBox ) { return; }
	
	var tabContentHref;
	var tabContentBoxLinks = jQuery( '.tab-box .tabs h3 a' );
	var initTabLoad = jQuery( '.tab-box .tabs h3 a.selected' );
	
	tabContentHref = getTabHref( initTabLoad[ 0 ] );
	updateContent( tabContentHref );
	
	// set up click events on tabs
	clickTab( tabContentBoxLinks );
	
	function resetSelected() {
		tabContentBoxLinks.removeClass( 'selected' );
	}
	
	function clickTab( tabContentBoxLinks ) {
		tabContentBoxLinks.click( function() {
			resetSelected();
			jQuery(this).addClass( 'selected' );
			tabContentHref = getTabHref( this );
			updateContent( tabContentHref );
			return false;
		} );
	}
	
	function getTabHref( el ) {
		var tabContentHref = el.href.split( '#' )[1];
		return tabContentHref;
	}
	
	function updateContent( el ) {
		// find el and get its content
		var tabContent = document.getElementById( el );
		if( jQuery( tabContent ).hasClass( 'flush' ) ) {
			jQuery( tabContentBox ).addClass( 'flush' );
		} else {
			jQuery( tabContentBox ).removeClass( 'flush' );
		}
		// put content inside the container boxcar
		tabContentBox.innerHTML = tabContent.innerHTML;
	}

}


/* Check All - Uncheck All */
function checkAll() {
	jQuery( 'fieldset .check-all' ).click( function() {
		console.log( jQuery(this).parents( 'fieldset:eq(0)' ).find( ':checkbox') );
		jQuery( this ).parents( 'fieldset:eq(0)' ).find( ':checkbox' ).attr( 'checked', 'checked' );
	} );
	jQuery( 'fieldset .uncheck-all' ).click( function() {
		jQuery( this ).parents( 'fieldset:eq(0)' ).find( ':checkbox' ).attr( 'checked', '' );
	} );
}


/*
 * Navigation Hovers
 * Handles rules to handle flyouts in the navigtion elements.
 * mainNavHover is only for IE6 since it doesn't under :hover
 * sideNavHover is for all since the flyout display is more complex.
 */
function mainNavHover() {
	jQuery( 'ul.main-nav li' ).hover(
		function() { jQuery(this).addClass( 'hover' ); },
		function() { jQuery(this).removeClass( 'hover' ); }
	);
}

function sideNavHover() {
	jQuery( '.sidenav li.flyout' ).hover(
		function() { jQuery(this).addClass( 'flyout-hover' ); },
		function() { jQuery(this).removeClass( 'flyout-hover' ); }
	);
}


/* Form Setup
 * Add classes to form controls for styling in IE6
 */
function formSetup() {
	jQuery( ':text' ).addClass( 'text' );
	jQuery( ':password' ).addClass( 'password' );
	jQuery( ':radio' ).addClass( 'radio' );
	jQuery( ':checkbox' ).addClass( 'checkbox' );
	jQuery( ':submit' ).addClass( 'submit' );
	jQuery( ':image' ).addClass( 'image' );
	jQuery( ':reset' ).addClass( 'reset' );
	jQuery( ':button' ).addClass( 'button' );
	jQuery( ':file' ).addClass( 'file' );
	jQuery( ':enabled' ).addClass( 'enabled' );
	jQuery( ':disabled' ).addClass( 'disabled' );
	jQuery( ':checked' ).addClass( 'checked' );
	jQuery( ':selected' ).addClass( 'selected' );
}


/* Login fields focus/blur */
function searchFields() {
	
	/* For login fields in the header */
	jQuery( '.login :text, .login :password' ).each ( function() {
	
		if( jQuery(this).val() != '' ) {
			jQuery(this).addClass( 'entered' );
		}
		
		jQuery(this).focus( function() {
			jQuery(this).addClass( 'entered' );
		} );
		
		jQuery(this).blur( function() {
			if( jQuery(this).val() == '' ) {
				jQuery(this).removeClass( 'entered' );
			}
		} );
		
	
	} );
	
	
	/* For search field on the video page */
	jQuery( '.video-page .search-videos input:text' ).focus( function() {
		if ( jQuery(this).val() == 'Search Video' ) {
			jQuery(this).val( '' );
		}
	})
	.blur( function() {
		if ( jQuery(this).val() == '' || jQuery(this).val() == 'Search Video' ) {
			jQuery(this).val( 'Search Video' );
		}
	});

}



/* Rotating Feature Slideshow */
jQuery.fn.slideShow = function( timeOut ) {
	var $elem = this;
	if( this.children().length == 1 ) {
		// no need to rotate if only 1 item
		return;
	}
	this.children( ':gt(0)' ).hide();
	setInterval( function() {
		$elem.children().eq( 0 ).fadeOut( 'slow' ).next().fadeIn( 'slow' ).end().appendTo( $elem );
	}, timeOut || 3000 );
};


/* Popup Generator for External Sites */
function popupGen() {
	jQuery( '.external' ).click( function() {
		
		var href = jQuery( this ).attr( 'href' );
		
		window.open( href, 'popup', "width=586,height=265,status=yes");
		
		return false;
		
	} );
}

/* Scripts to handle leave cardiosource popup */
function popupController( queryKey ) {
	var urlFull, url, windowParent, isPopup;
	urlFull = window.location.href;
	url = urlFull.split( queryKey )[1];
	
	windowParent = window.opener;
	if( windowParent != window.top ) { isPopup = true; }
	
	jQuery( ".return" ).click( function() {
		window.close();
		return false;
	} );
	
	jQuery( ".leave" ).each( function() { this.href = url; } );
	jQuery( ".leave" ).click( function() {
		// change parent window's URL to button's href
		var href = this.href;
		windowParent.location.href = href;
		
		// close this window
		window.close();
		return false;
	} );

}

jQuery( document ).ready( function() {
	
	// Check which version of IE we have here
	var isIE, ieVersion;
	if ( jQuery.browser.msie ) {
		isIE = true;
		ieVersion = '5.5';
		
		switch ( jQuery.browser.version ) {
			case '6.0':
				ieVersion = 6;
				break;
			
			case '7.0':
				ieVersion = 7;
				break;
			
			case '8.0':
				ieVersion = 8;
				break;
				
			default:
				break;
		}
	}
	
	if( ieVersion == 6 ) {
		mainNavHover();
		formSetup();
	}
	
	sideNavHover();
	
	searchFields();
	
	var tabs = new Tabs();
	jQuery('.rotating').slideShow( 6000 );
	
	checkAll();
	
	popupGen();
	
} );



function openLGWinBars(link) {
	linkWindow = window.open(link, "pop", "width=730,height=550,innerWidth=730,innerHeight=550,top=0,left=0,screenX=0,screenY=0,resizable=yes,scrollbars=yes,dependent=yes,menubar=yes,toolbar=yes,location=yes");
	linkWindow.focus();
}