jQuery(document).ready(function() {
	//Top deals ticker scroller
	jQuery('.scroller').bxSlider({
		ticker:true,
		tickerSpeed: 4200,
		tickerHover: true,
		mode: 'vertical'
	});
	
	//fix container height on short pages
	MakeElementFillVisibleSpace('#container');
	
	//remove empty menu elements
	jQuery('#topnav ul').each(function() {
		if (jQuery(this).children().length == 0) {
			jQuery(this).remove();
		}
	});
});

jQuery(window).load(function() {
	//fix container height on short pages
	MakeElementFillVisibleSpace('#container');
});
jQuery(window).resize(function() {
	//fix container height on short pages
	if ( ! jQuery.browser.msie ) {
		MakeElementFillVisibleSpace('#container');
	}
});

//add to google tracking to PDF and EXE links and other file types
jQuery('a[href$=".pdf"], a[href$=".exe"], a[href$=".mp4"], a[href$=".flv"], a[href$=".dmg"], a[href$=".zip"], a[href$=".ppt"], a[href$=".eps"], a[href$=".mpeg"], a[href$=".doc"], a[href$=".txt"], a[href$=".mp3"], a[href$=".avi"], a[href$=".pps"], a[href$=".xls"], a[href$=".wmv"]').click(function() {
    pageTracker._trackPageview(this.href);
});

//add tracking to the more_material selectors
jQuery('select[name="more_material"]').parents('form').submit(function() {
    var selected_link = jQuery('select[name="more_material"]').val();
    pageTracker._trackPageview(selected_link);
});

/* Nav site */
var topnav = document.getElementById("topnav");
if (topnav != null) {
	topnav.style.position = "relative";
	topnav.style.left = "50%";
	topnav.style.marginLeft = "-" + (topnav.clientWidth / 2) + "px";
}

var footernav = document.getElementById("footernav");
if (footernav != null) {
	footernav.style.position = "relative";
	footernav.style.left = "50%";
	footernav.style.marginLeft = "-" + (footernav.clientWidth / 2) + "px";
}

//Remove empty nav <ul> items so they don't glitch
jQuery('ul.nav').each(function() {
    if (jQuery(this).children().length == 0) {
        jQuery(this).remove();
    }
});

//fit IE footer issue
function fixIEfooter() {
	if (document.getElementById("footer-wrapper") != null && document.getElementById("footer") != null) {
		document.getElementById("footer-wrapper").style.position = "static";
		document.getElementById("footer").style.position = "static";
		document.getElementById("footer-wrapper").style.position = "relative";
		document.getElementById("footer").style.position = "relative";
		jQuery('#left-bg-bottom').hide();
		jQuery('#left-bg-bottom').show();
		jQuery('#left-bg-bottom').css('position', 'absolute');
		jQuery('#right-bg-bottom').css('bottom', '0');
		jQuery('#right-bg-bottom').hide();
		jQuery('#right-bg-bottom').show();
		jQuery('#right-bg-bottom').css('position', 'absolute');
		jQuery('#right-bg-bottom').css('bottom', '0');
	}
}

//attach function to onload
if (window.attachEvent) {window.attachEvent('onload', fixIEfooter);}
else if (window.addEventListener) {window.addEventListener('load', fixIEfooter, false);}
else {document.addEventListener('load', fixIEfooter, false);} 

//Function to make an element, e.g. content area, fill the visible space so a footer appears at the bottom. Wrapper can be a container or body element.
function MakeElementFillVisibleSpace(element_to_adjust, wrapper_element) {
	if (wrapper_element == undefined) { wrapper_element = 'body'; }
	$(element_to_adjust).height('auto');
	if ( $(wrapper_element).height() < $(window).height() ) {
		height_difference = $(window).height() - $(wrapper_element).height();
		new_height = $(element_to_adjust).height() + height_difference;
		$(element_to_adjust).height(new_height);
	}
}

//for the forum
function VerifyCaptchaAjax(thisform, textinput) {
	var http = new Array();
	if (http[0] != null) {
		http[0].abort();
	}
	if(navigator.appName == "Microsoft Internet Explorer") {
	  http[0] = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
	  http[0] = new XMLHttpRequest();
	}
	http[0].open("GET", "testcaptcha.asp?q=" + textinput, true);
	http[0].onreadystatechange=function() {
		if(http[0].readyState == 4) {
			if (http[0].responseText == "1") {
				thisform.submit();
			} else {
				//reload image
				document.getElementById("imgCaptcha").src = "images/blank.gif";
				alert("Verification code is incorrect. Please try again.");
				setTimeout(" document.getElementById('imgCaptcha').src = 'captcha.asp?id=' + uniqid(); ", 500);
				thisform.captchacode.focus();
			}
		}
	}
	http[0].send(null);
	return false;
}

//unique number generator
function uniqid()
{
	var newDate = new Date;
	return newDate.getTime();
}


//scrolling for the video playlists
var scrollInterval = null;
var scrollAmount = null;
var scrollFactor = 5;
function smoothScrollDiv(elementid, amount) {
	if (!scrollInterval) {
		scrollInterval = setInterval( "scrollDiv('" + elementid + "', " + amount + ")", 30 );
	}
}
function scrollDiv(elementid, amount) {
	if (scrollAmount == null) {
		scrollAmount = amount;
		scrollThisTime = scrollAmount/scrollFactor;
		scrollAmount = scrollAmount - Math.round(scrollThisTime);
	} else if (scrollAmount < 1 && scrollAmount > -1) {
		scrollAmount = null;
		scrollThisTime = 0;
		clearInterval(scrollInterval);
		scrollInterval = null;
		checkScrollButtonStatus(elementid,"videoplaylistup","videoplaylistdown");
	} else {
		scrollThisTime = scrollAmount/scrollFactor;
		if (scrollThisTime < 1 && scrollThisTime > 0) {
			scrollThisTime = 1;
		} else if (scrollThisTime > -1 && scrollThisTime < 0) {
			scrollThisTime = -1;
		}
			scrollAmount = scrollAmount - Math.round(scrollThisTime);
	}
	element = document.getElementById(elementid);
	element.scrollTop += Math.round(scrollThisTime);
}
function checkScrollButtonStatus(elementid, upelementid, downelementid) {
	element = document.getElementById(elementid);
	upelement = document.getElementById(upelementid);
	downelement = document.getElementById(downelementid);
	
	if ( element.clientHeight + element.scrollTop >= element.scrollHeight ) { //at the bottom
		downelement.style.display = "none";
	} else if ( element.clientHeight + element.scrollTop < element.scrollHeight ) {
		downelement.style.display = "block";
	}
	
	if ( element.scrollTop == 0 ) { //at the top
		upelement.style.display = "none";
	} else if ( element.scrollTop > 0) {
		upelement.style.display = "block";
	}
}
if ( document.getElementById("videoplaylist") ) {	//if playlist exits, set the buttons
	checkScrollButtonStatus("videoplaylist","videoplaylistup","videoplaylistdown");
}


//scrolling for the horizontal video playlists
var scrollIntervalH = null;
var scrollAmountH = null;
var scrollFactorH = 5;
function smoothScrollDivH(elementid, amount) {
	if (!scrollIntervalH) {
		scrollIntervalH = setInterval( "scrollDivH('" + elementid + "', " + amount + ")", 30 );
	}
}
function scrollDivH(elementid, amount) {
	if (scrollAmountH == null) {
		scrollAmountH = amount;
		scrollThisTime = scrollAmountH/scrollFactorH;
		scrollAmountH = scrollAmountH - Math.round(scrollThisTime);
	} else if (scrollAmountH < 1 && scrollAmountH > -1) {
		scrollAmountH = null;
		scrollThisTime = 0;
		clearInterval(scrollIntervalH);
		scrollIntervalH = null;
		checkScrollButtonStatusH(elementid,"videoplaylistleft","videoplaylistright");
	} else {
		scrollThisTime = scrollAmountH/scrollFactorH;
		if (scrollThisTime < 1 && scrollThisTime > 0) {
			scrollThisTime = 1;
		} else if (scrollThisTime > -1 && scrollThisTime < 0) {
			scrollThisTime = -1;
		}
			scrollAmountH = scrollAmountH - Math.round(scrollThisTime);
	}
	element = document.getElementById(elementid);
	element.scrollLeft += Math.round(scrollThisTime);
}
function checkScrollButtonStatusH(elementid, upelementid, downelementid) {
	element = document.getElementById(elementid);
	upelement = document.getElementById(upelementid);
	downelement = document.getElementById(downelementid);
	
	if ( element.clientWidth + element.scrollLeft >= element.scrollWidth ) { //at the bottom
		downelement.style.display = "none";
	} else if ( element.clientWidth + element.scrollLeft < element.scrollWidth ) {
		downelement.style.display = "block";
	}
	
	if ( element.scrollLeft == 0 ) { //at the top
		upelement.style.display = "none";
	} else if ( element.scrollLeft > 0) {
		upelement.style.display = "block";
	}
}
if ( document.getElementById("innervideoplaylisthorizontal") ) {	//if playlist exits, force the UL list to be the right width
	ulelement = document.getElementById("innervideoplaylisthorizontal");
	listlength = 0;
	for (i=0; i < ulelement.childNodes.length; i++) {
		 listlength += 165; //width of each element with padding
	}
	
	if (ulelement.childNodes.length == 1) {	//center the li if there's only one
		ulelement.childNodes[0].style.marginLeft = "165px";
		 listlength += 165;
	} else if (ulelement.childNodes.length == 2) { // center if there's only two
		ulelement.childNodes[0].style.marginLeft = "82px";
		 listlength += 82;
	}
	
	//set the UL to be the right length
	ulelement.style.width = listlength + "px";
}
if ( document.getElementById("videoplaylisthorizontal") ) {	//if playlist exits, set the buttons
	checkScrollButtonStatusH("videoplaylisthorizontal","videoplaylistleft","videoplaylistright");
}

function ToggleVideoTranscription() {
	var new_height = $('#video_transcription').outerHeight() + $('#video_transcription_title').outerHeight();
	if (new_height < 96) { new_height = 96; }
	if ($('#video_transcription_container').height() >= new_height ) {
		$('#video_transcription_container').stop().animate({height: '96px'}, 300);
		$('#video_show_more').text('Show more');
	} else {
		$('#video_transcription_container').stop().animate({height: new_height}, 300);
		$('#video_show_more').text('Show less');
	}
}

function ResetVideoTranscriptionHeight() {
	$('#video_transcription_container').css('height', '');
}

function ToggleHideShowMore() {
	var new_height = $('#video_transcription').outerHeight() + $('#video_transcription_title').outerHeight();
	if (new_height <= 96) {
		$('#video_show_more').hide();
	} else {
		$('#video_show_more').show();
	}
}
