// corrective styles

$(function () {
	$("#infoNav li:gt(0)").prepend("|");
	$('input[type=text]').each(function () {
		$(this).data('original-text', $(this).val());
		$(this).focus(function () {
			if ($(this).val() == $(this).data('original-text')) {
				$(this).val('');
			}
		}).blur(function () {
			if ($(this).val() == '') {
				$(this).val($(this).data('original-text'));
			}
		});
	});
});
$(window).load(function () {
	if ($('#sectionsLeftColumnLeft').height() < $('#sectionsLeftColumnRight').height()) {
		$('#sectionsLeftColumnLeft').height($('#sectionsLeftColumnRight').height());
	}
	var diff = $('#sectionsLeftColumn').height()-$('#sectionsLeftColumnLeft').height();
	if (($('#sectionsLeftColumnLeft').height()+diff) < $('#sectionsRightColumn').height()) {
		$('#sectionsLeftColumnLeft').height($('#sectionsRightColumn').height()-diff);
	}
});

// navigation menu
$(function () {
	$("#mainNav li.current").prev().css("border", "none");
});

// suckerfish hover
sfHover = function() {
	var sfEls = document.getElementById("mainNav").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

if (window.attachEvent) window.attachEvent("onload", sfHover);

// sections carousel
$(function () {
	$('.articleCarousel').each(function () {
		var that = $(this);
		$(".contentStrip", that).css("width", ($(".contentStrip div", that).size()*148)+"px");
		var pageWidth = 593;
		var pages = (($(".contentStrip", that).width()/pageWidth) | 0)-1;
		if(($(".contentStrip", that).width()%pageWidth)>0) pages++;
		var currentPage=0;
		var transitionSpeed=500;
		
		$(".carrouselNext", that).click(function (event) {
			event.preventDefault();
			currentPage++;
			if(currentPage>pages) currentPage=0;
			var offsetPx=currentPage*pageWidth;
			var offset='';
			if(offsetPx>0) offset='-'+offsetPx+"px";
			else offset="0px"
			$(".contentStrip", that).animate({
				left: offset
			},transitionSpeed);
		});
		$(".carrouselPrev", that).click(function (event) {
			event.preventDefault();
			currentPage--;
			if(currentPage<0) currentPage=pages;
			var offsetPx=currentPage*pageWidth;
			var offset='';
			if(offsetPx>0) offset='-'+offsetPx+"px";
			else offset="0px"
			$(".contentStrip", that).animate({
				left: offset
			},transitionSpeed);
		});
	});
});

// article carousel
$(function () {
	$('.articleCarousel').each(function () {
		var that = $(this);
		var contentWidth = 284;
		var content = ($(this).find(".mainContainer").size() * contentWidth);
		$(".contentStrip", that).css("width", content + "px");
		var pageWidth = 568;
		var pages = ((content/pageWidth) | 0)-1;
		if(($(".contentStrip", that).width()%pageWidth)>0) pages++;
		var currentPage=0;
		var transitionSpeed=500;
		
		$(".artCarouselNext", that).click(function (event) {
			event.preventDefault();
			currentPage++;
			if(currentPage>pages) currentPage=0;
			var offsetPx=currentPage*pageWidth;
			var offset='';
			if(offsetPx>0) offset='-'+offsetPx+"px";
			else offset="0px"
			$(".contentStrip", that).animate({
				left: offset
			},transitionSpeed);
		});
		$(".artCarouselPrev", that).click(function (event) {
			event.preventDefault();
			currentPage--;
			if(currentPage<0) currentPage=pages;
			var offsetPx=currentPage*pageWidth;
			var offset='';
			if(offsetPx>0) offset='-'+offsetPx+"px";
			else offset="0px"
			$(".contentStrip", that).animate({
				left: offset
			},transitionSpeed);
		});
	});
});

// interactive data table

$(function () {
	$("#interactiveDataTable").tablesorter({
		widgets: ['zebra', 'columnHighlight']
	});
});

// switcher animation

$(function () {
	$('.switcherNav>li>a').click(function (e) {
		e.preventDefault();
		var that = $(this);
		
		// decorate nav
		
		var nav = that.closest('.switcherNav');
		nav.children('li').removeClass('active').find('a').trigger('focus').trigger('blur');
		that.closest('li').addClass('active');
		
		// animate content
		
		var content = $('#'+that.attr('href').split('#').pop());
		var contentContainer = nav.next('.switcherContentContainer');
		contentContainer.queue(function () {
			contentContainer.animate({
				'height': content.outerHeight()
			}, 750);
			$('.switcherContent', contentContainer).fadeOut(1000);
			$(this).dequeue();
		});
		contentContainer.queue(function () {
			content.fadeIn('slow');
			$(this).dequeue();
		});
	});
});

// masonry for multimedia page

//$(function () {
//	$('#webcastsContainer').masonry();
//});

// comparison table animation

$(function () {
	$('#comparisonTable thead .headerButtonClose').click(function (e) {
		e.preventDefault();
		var that = $(this);
		var cell = that.closest('th');
		var row = cell.closest('tr');
		var index = row.find('th').index(cell)
		var headerRows = $('#comparisonTable thead tr');
		var dataRows = $('#comparisonTable tbody tr td').parent();
		
		// hide cells with this index
		
		$('#comparisonTable thead tr').addClass('single');
		headerRows.each(function () {
			var cells = $(this).find('th');
			cells.each(function () {
				if (cells.index(this) == index) {
					$(this).attr('colspan', '0').hide();
				} else {
					$(this).attr('colspan', '2').show();
					$('.headerButtonOpen', this).show();
					$('.headerButtonClose', this).hide();
				}
			});
		});
		dataRows.each(function () {
			var cells = $(this).find('td');
			cells.each(function () {
				if (cells.index(this) == index) {
					$(this).attr('colspan', '0').hide();
				} else {
					$(this).attr('colspan', '2').show();
				}
			});
			$(this).addClass('single');
		});
	});
	$('#comparisonTable thead .headerButtonOpen').click(function (e) {
		e.preventDefault();
		var that = $(this);
		var cell = that.closest('th');
		var row = cell.closest('tr');
		var index = row.find('th').index(cell)
		var headerRows = $('#comparisonTable thead tr');
		var dataRows = $('#comparisonTable tbody tr td').parent();
		
		// show all cells
		
		$('#comparisonTable thead tr').removeClass('single');
		headerRows.each(function () {
			var cells = $(this).find('th');
			cells.each(function () {
				$(this).attr('colspan', '1').show();
				$('.headerButtonOpen', this).hide();
				$('.headerButtonClose', this).show();
			});
		});
		dataRows.each(function () {
			var cells = $(this).find('td');
			cells.each(function () {
				$(this).attr('colspan', '1').show();
			});
			$(this).removeClass('single');
		});
	});
});
