// -------------------------------------------------------------------------------------------------
// Set variables
// -------------------------------------------------------------------------------------------------

var isiPad = navigator.userAgent.match(/iPad/i) != null;
if ( $(window).width() >= 768 ) {
	var isLarge = true;
} else {
	var isSmall = true;
}


// -------------------------------------------------------------------------------------------------
// Execute on page load
// -------------------------------------------------------------------------------------------------

$(document).ready(function(){

	// Execute for all browsers
	hideBios();
	resizeTube();
	trackOutbound();

	// Execute for iPad browser
	if (isiPad) {
		careersLinks();
	}

	// Execute for larger browser windows
	if (isLarge) {
		closeTrees();
		fadeTestimonials();
	}

	// Execute for smaller browser windows
	if (isSmall) {
		openTrees();
		careersTable();
		hideTestimonials();
	}

});


// -------------------------------------------------------------------------------------------------
// Execute on window resize
// -------------------------------------------------------------------------------------------------

$(window).resize(function () {

	// Execute for all browsers
	resizeTube();

	// Execute for larger browser windows
	if (isLarge) {
		closeTrees();
	}

	// Execute for smaller browser windows
	if (isSmall) {
		openTrees();
		careersTable();
	}

});


// -------------------------------------------------------------------------------------------------
// Functions
// -------------------------------------------------------------------------------------------------

// Track Outbound Links with GA
function trackOutbound() {
	$("#menu-marketplaces li a").click(function() {
		_gaq.push(['_trackEvent', 'Marketplace Bar', 'Links', "'" + this.href + "'"]);
	});
	$("a[href^='http://investor.liquidityservicesinc.com']").click(function() {
		_gaq.push(['_trackEvent', 'Investor Site', 'Links', "'" + this.href + "'"]);
	});
	$("a[href^='http']").not("a[href*='liquidityservicesinc.com']").not("#menu-marketplaces li a").click(function() {
		_gaq.push(['_trackEvent', 'Outbound', 'Links', "'" + this.href + "'"]);
	});
};

// Hide / show biographies
function hideBios() {
	$("#post-190 div, #post-192 div").hide();
	$("#post-190 hgroup, #post-192 hgroup").click(function() {
		$(this).next("div").toggle();
	});
};

// Change Careers page links to full frame instead of lightbox
function careersLinks() {
	if ($("#opportunities").length > 0) {
		$("#opportunities td a").attr('href', function() {
			return $(this).attr('id');
		});
		$("a[rel^='adp_job']").removeAttr('rel');
	}
};

// Close all tree menus
function closeTrees() {
	if ($(".dtree_pge").length > 0) {
		pge1.closeAll();
	}
};

// Open all tree menus
function openTrees() {
	if ($(".dtree_pge").length > 0) {
		pge1.openAll();
	}
};

// Reformat Careers page table
function careersTable() {
	if ($("#opportunities").length > 0) {
		$("#opportunities thead .dept, #opportunities thead .loc, #opportunities tfoot .dept, #opportunities tfoot .loc").hide();
		$("#opportunities td").unwrap();
		$("#opportunities td").wrap("<tr></tr>");
	}
};

// Resize YouTube videos
function resizeTube() {
	if ($(".vvqyoutube").length > 0) {
		$(".vvqyoutube").css('height','inherit');
		var $origVideo = $(".vvqyoutube object");
		var aspectRatio = $origVideo.attr("height") / $origVideo.attr("width");
		var wrapWidth = $("#main").width();
		$origVideo.width(wrapWidth).height(wrapWidth * aspectRatio);
		$origVideo.next('embed').width(wrapWidth).height(wrapWidth * aspectRatio);
	}
};

// Hide all testimonials except the first
function hideTestimonials() {
	if ($("#home_testimonials").length > 0) {
		$("#home_testimonials ul li").show();
		$("#home_testimonials ul li:not(:first-child)").hide();
	}
}

// Fade through testimonials
function fadeTestimonials() {
	if ($("#home_testimonials").length > 0) {
		$("#home_testimonials ul li").show();
		$("#home_testimonials ul").fadeTransition({
			pauseTime: 10000,
			transitionTime: 1000,
			ignore: "#introslide",
			manualNavigation: false,
			pauseOnMouseOver: true,
			createNavButtons: true
		});
	}
}
