// Supports redesigned category pages
var tmr; // global timer var

jQuery(document).ready(function($) { // reverting $ alias
	var ugcCookie = 'brkcat-ugc'; // cookie showing if UGC present
	
	// pass ad tag img src to small thumb HAXzorz
	if ($('.fc-adtag').length) {
		$('.fc-adtag').each(
			function (intIndex) {
				var theId = $(this).attr('id').substring(2);
				var theSrc = $(this).find('img').attr('src');
				$('#t'+theId).find('img').attr('src',theSrc);
			}
		);
	}
	
	// checkbox functionality for user content toggle
	$('.cat-usercnt input').click(function() {
		var options = {
			path : '/',
			expires :  730
		}
		if ( $.cookie(ugcCookie) != null && $.cookie(ugcCookie) != ''  ) { // ugc cookie is set, delete it
			$.cookie(ugcCookie, null, options);
		}
		else { // ugc cookie is not set, set it
			$.cookie(ugcCookie, Math.round(Math.random()*1000000), options);
		}
		location.href = $(this).attr('value');
	});
	
	$('.feat-ico span').css('line-height', '27px'); // fix
	
	// toggle functions for control bar
	$('.clink').click(function(x) {
		
		var which = $(this).attr('id').substring(5); // 'clink' prepended to id in markup
		if($('.thumbs' + which).css('display') != 'block') { //'thumbs' prepended to class in markup
			
			$('.thumbs' + which).children(':first-child').addClass('tc-selected');
		
			$('.tr-selected').fadeOut('slow', function() {
				$(this).removeClass('tr-selected');
			});
			$('.thumbs' + which).fadeIn('slow', function() {
				$(this).addClass('tr-selected');
				$('.tc-selected:not(:visible)').removeClass('tc-selected');
			});
			
			// toggle selected featured content div
			$('.fc-selected').fadeOut('slow', function() {
				$(this).removeClass('fc-selected');
			});
			var mainDiv = (which * 3) -2;
			$('#fc' + mainDiv).fadeIn('slow', function () {
				$(this).addClass('fc-selected');
			});
			
			$('.clink').css('display', 'inline');
			$('.clink-wrap span').css('display', 'none');
			$(this).css('display', 'none').next().css('display', 'inline');
			
		};
		
		x.preventDefault();
		stopChannelTimer();
	});
	
	// toggle function for individual thumbs
	$('.thumb-container a').click(function(x) {
		$('.fc-selected').removeClass('fc-selected').css('display', 'none');
		$('#fc' + $(this).attr('id').substring(1)).addClass('fc-selected').css('display', 'block'); // 't' prepended to id in markup
		
		// set highlighted css for selected thumb
		$('.tc-selected').removeClass('tc-selected');
		$(this).parent().addClass('tc-selected');
		
		x.preventDefault();
		stopChannelTimer();
	});
	
	// gutter height adjustments
	var gutterHeight = $('#body').height();
	gutterHeight -= 30; //offset for bottom padding
	$('.gutter').css('height', gutterHeight)
	
	// tooltips
	tooltip.init();
	// sliding subsort menu position	
	//setSubSortOffset();	
	// start timer
	startChannelTimer();
});
// end ready wrapper

// sliding subsort menu position
function setSubSortOffset() {	
	var a = $('.cat-cnt-subnav');
	var b = $('.ccn-active');	
	var widthDiff = ((b.width() - a.width()) * .5) - 2;	
	var targetPos = b.offset().left - (a.offset().left)
	var oldCss = parseInt(a.css('margin-left'));
	var newCss = (oldCss + targetPos) + widthDiff;	
	a.css('margin-left',newCss+'px');	
}

// timer functions
function startChannelTimer() {
	tmr = setInterval('toggleFeatChannel()', 5000);
}

function stopChannelTimer() {
	clearInterval(tmr);
}

function toggleFeatChannel() {
	var currPos = parseInt($('.tc-selected a').attr('id').substring(1));
	var totalItems = $('.cat-feat-mid').children('div.featured-content:last').attr('id').substring(2);
	var newPos, cbarLink; // thumb and control bar group *destinations*: e.g. cbarLink is *next* control link group id's last char
	if (currPos == totalItems) { // at the end, back to top
		newPos = 1;
		cbarLink = 1;
	}
	else {
		newPos = currPos + 1;
		if (currPos < 4 && totalItems > 3) { // in 1st group, have at least one more to go
			cbarLink = 2;
		}
		else if (currPos > 3 && currPos < 7 && totalItems > 6) { // in 2nd group, have at least one more to go
			cbarLink = 3;
		}
		else { // back to top
			cbarLink = 1;
		}
	}
	
	// move it!
	if ( (currPos == totalItems) ) { // at the end of loop, go back to top
	
		if ($('.thumbs' + cbarLink).css('display') != 'block') { // changing groups
			aniTrans(newPos,cbarLink);
		}
		else { // within current group
			thumbTrans(newPos);
		}
		
	}
	else { // not at end, conditional advance to next
		if (currPos == 3 || currPos == 6) { // changing groups
			aniTrans(newPos,cbarLink);
		}
		else { // within current group
			thumbTrans(newPos);
		}
	}
} 

function thumbTrans(thumbPos) {
	// active thumb
	$('.tc-selected').removeClass('tc-selected'); //remove class from old selected thumb
	$('#t'+thumbPos).parent().addClass('tc-selected'); // add class to new selected thumb
	
	// featured content div
	$('.fc-selected').removeClass('fc-selected').css('display', 'none');
	$('#fc'+thumbPos).addClass('fc-selected').css('display', 'block');
}

function aniTrans(featPos,groupPos) { // for transition from group to group
	// active thumb
	$('#t'+featPos).parent().addClass('tc-selected');
	
	// thumbnail row containers
	$('.tr-selected').fadeOut('slow', function() {
		$(this).removeClass('tr-selected');
	});
	$('.thumbs' + groupPos).fadeIn('slow', function() {
		$(this).addClass('tr-selected');
		$('.tc-selected:not(:visible)').removeClass('tc-selected');
	});
	
	// featured content div
	$('.fc-selected').fadeOut('slow', function() {
		$(this).removeClass('fc-selected');
	});
	$('#fc' + featPos).fadeIn('slow', function () {
		$(this).addClass('fc-selected');
	});
	
	// control bar links
	$('.clink').css('display', 'inline');
	$('.clink-wrap span').css('display', 'none');
	$('#clink'+groupPos).css('display', 'none').next().css('display', 'inline');
} 

// from original in-page JS
function ETSTrackSearchButtonEvent() {
	var oETS = new ETSBase("B_SEARCH_EVNT");
	oETS.addControl("ctl_search_tb");
	oETS.execute();
}

function validateSearch() {
	var searchTxt = document.getElementById('SearchTextbox1');
	var text = '';
	if(searchTxt == null)
	return false;
	text = searchTxt.value;
	text = text.replace(/^\s*|\s*$/g, "");
	return text.length != 0;
}

function hideHPVSDiv() {
	if (document.getElementById) { // DOM3 = IE5, NS6 
		if (document.getElementById('videoplayer')) {
			document.getElementById('videoplayer').style.display = 'none'; 
		}
	} 
	else { 
		if (document.layers) { // Netscape 4 
		document.videoplayer.display = 'none'; 
		} 
		else { // IE 4 
		document.all.videoplayer.style.display = 'none'; 
		}
	}
}

function showHPVSDiv() { 
	if (document.getElementById) { // DOM3 = IE5, NS6 
		if (document.getElementById('videoplayer')) {
			document.getElementById('videoplayer').style.display = 'block'; 
		}
	}
	else { 
		if (document.layers) { // Netscape 4 
		document.videoplayer.display = 'block'; 
		}
		else { // IE 4 
		document.all.videoplayer.style.display = 'block';
		}
	}
}