if ( typeof(BFGUI) == 'undefined' ) {
	var BFGUI = function() {
		function AddPipesToList( rootElement ) {
			var list = $( rootElement );
			if ( list ) {
				$( 'li', list ).each( function( index, item ) {
					if ( !$(item).is( ':nth-child(1)' ) ) {
						$(item).before('<li class="sep">|</li>');
					}
				});
			}
		}
		
		function AddContentToElement( element, content ) {
			var elm = $( element );
			if ( elm ) {
				elm.append( content );
			}
		}
		
//		function AddButtonGraphics() {
//			$('.gbtn').each(function(){
//				var b = $(this);
//				var tt = b.text() || b.val();
//				if ( b.is( ':submit,:button' ) ) {
//					b = $('<a>').insertAfter(this).addClass(this.className).attr('id',this.id);
//					$(this).remove();
//				}
//				b.text('').css({cursor:'pointer'}).prepend('<i></i>').append( $('<span></span>').text(tt).append('<i></i><span></span>') );
//			});
//		}

		function AddButtonGraphics() {
			$('.gbtnh').each(function(){
				var b = $(this);
				var tt = b.text() || b.val();
				b.removeClass('gbtnh').addClass('gbtn');
				if ( b.is( ':submit,:button' ) ) {
					b = $('<a>').insertAfter(this).addClass(this.className).attr('id',this.id);
					$(this).remove();
				}
				b.text('').css({cursor:'pointer'}).append( $('<span></span>').text(tt) );
			});
		}
		
		function addScreenFrame( listName ) {
			var container = null;
			if ( arguments[1] ) {
				container = $( listName + ' .screenframe', arguments[1] );
			} else {
				container = $( listName + ' .screenframe');
			}
			
			container.each( function() {
				var sfobj = null;
				if ( $(this).parent( '.selected' ).length > 0 ) {
					sfobj = $(this).wrap( '<span class="sfwp"></span>' ).parent( '.sfwp' ).append( '<span class="sel"></span>' );
				} else {
					sfobj = $(this).wrap( '<span class="sfwp"></span>' );
				}
				if ( $.browser.msie6 ) { /* fix for issue in ie6 where hyperlinks rendered on elements with 'AlphaImageLoader'-loaded backgrounds do not work. */
					$( this ).parent( '.sfwp' ).css({ cursor: 'pointer' }).click( function() { $( 'a', this ).trigger( 'click' ); });
				}
			});
		}
		
		function fixSoldierBoxLink() {
			$( '.soldierItem' ).each( function() {
				var item = $(this);
				item.click( function() { location.href = $( '.view', this ).get(0).href; } );
				item.css( { cursor: 'pointer' } );
			});
		}
		
		return {
			AddPipesToList : AddPipesToList,
			AddContent : AddContentToElement,
			AddButtonGraphics : AddButtonGraphics,
			AddScreenShotFrame : addScreenFrame,
			AddSoliderBoxLink : fixSoldierBoxLink
		}
	}();
}

if ( typeof(BFSLIDER) == 'undefined' ) {
	var BFSLIDER = function() {
		function init_sliders() {
			$( '.jcarousel-skin-bf' ).each( function() {
				var item = $(this);
				item.css({visability: 'hidden'});
				if ( item.is( 'ul' ) ) { init_slider( item ); }
			});	
		}
		
		function init_slider( element ) {
			var jcstart = 1;
			var selItem = $( 'li[class*=jcstart]', element );
			if ( selItem.length>0 ) {
				var pattern = /jcstart([0-9]{1,3})/ig;
				var matches = pattern.exec( selItem.attr( 'class' ) );
				jcstart = parseInt(matches[1]-2);
			}
			if ( element.attr( 'id' ) === 'lstMovieList' ) {
				jQuery( '#'+element.attr( 'id' ) ).jcarousel({
					vertical: element.is( '.vert' ) || element.is('.vertsmall'),
					scroll: 2,
					start: jcstart,
					visible: 3,
					buttonNextCallback : BFSLIDER.navCallback,
					buttonPrevCallback : BFSLIDER.navCallback
				});
			} else {
				jQuery( '#'+element.attr( 'id' ) ).jcarousel({
					vertical: element.is( '.vert' ) || element.is('.vertsmall'),
					start: jcstart,
					setHeight: false
				});			
			}
			element.css({visability: 'visible'});
		}
		
		function checkNavButtons( carousel, button, enabled ) {
			$(button).css( { display: (carousel.size()>3)?'block':'none' } );
		}
		
		return {
			init: init_sliders,
			navCallback : checkNavButtons
		}
	}();
}

$(document).ready(
	function() {
		BFGUI.AddPipesToList( '#globalMenu' );
		BFGUI.AddPipesToList( '#mainMenu' );
		BFGUI.AddPipesToList( '.ftrlegals' );
		BFGUI.AddButtonGraphics();
		BFGUI.AddScreenShotFrame( '#screenshotList' );
		BFGUI.AddScreenShotFrame( '#movieList' );
		BFSLIDER.init();
		
		/* init lightbox */
		var screens = $('a[@rel*=lightbox]');
		if ( screens.length > 0 ) {
			screens.lightImgBox({
				imageLoading: '/images/lightbox-ico-loading.gif',
				imageBtnClose: '/images/lightbox-btn-close.gif',
				imageBtnPrev: '/images/screennav_prev.gif',
				imageBtnNext: '/images/screennav_next.gif'	
			});
		}
		$( /* ((partialupdate)?'.partialUpdate ':'') +*/ "a.modal" ).modalBox();

		$('#lstLatestNews li').each( function() {
			var item = $( this );
			item.click( function() { location.href = $( '.gbtn', this ).get(0).href; } );
			item.css("cursor", "pointer");
		});		
		
		if ( $.browser.msie6 ) {
			$( '#userMenu > li' ).hover(
				function() { $(this).addClass('hover'); },
				function() { $(this).removeClass('hover'); }
			);
		}
		
		$('.castVote').click( function(e) {
			var link = $(this);
			window.open( link.attr('href'), '', 'width=400,height=300,statusbar=1,toolbar=0,resizable=0' );
			return false;
		});
	}
);