tabNav = {
	init: function() {
		$('div.tabSection div.tab').hide();
		$('div.tabSection div.active').show();
		$('div.tabSection ul.tabs a').click(function(e) {
			$('div.tabSection .active').removeClass('active');
			$(this).parent().addClass('active');
			var tab = $(this).attr('href').substr(1);
			$('div.tabSection div#'+tab).addClass('active');
			$('div.tabSection div.tab').hide();
			$('div.tabSection div.active').show();
			return false;
		});
	}
}

$.fn.fillText = function(options) {
	options.maxHeight = (options.maxHeight > 0) ? options.maxHeight : 108;
	options.maxSize = (options.maxSize > 0) ? options.maxSize : 36;
	this.each(function() {
		var $this = $(this);
		$this.wrapInner('<span class="expand" style="line-height: 1em; font-size: 1px;"></span>');
		var span = $this.find('span.expand');
		var size = 0;
		while (($(span).width() < $this.width()) && ($(span).height() < options.maxHeight) && (size <= options.maxSize)) {
			size = $(span).css('font-size').replace('px','')/1;
			$(span).css('font-size',size+1+'px');
		}
		$(span).css('font-size',size-1+'px');
		if ((size-1)*2 >= options.maxHeight) {
			$(span).css('line-height',options.maxHeight+'px');
		}
	});
}

function showDocumentDetail(link) {
	if (!(document.getElementById('docDetailContainer'))) {
		var bh = $(document).height();
		$('body').append('<div id="docDetailWrapper" style="display: none; width: 100%; height: ' + bh + 'px; position: absolute; background: #000; top: 0; left: 0; z-index: 500;">&nbsp;</div><div id="docDetailContainer" style="display: none; position: absolute; z-index: 501;"></div>');
	}
	if (!(animRunning)) {
		animRunning = true;
		$('div#docDetailContainer').empty();
		$('div#docDetailWrapper').css({ opacity: 0, display: "block" }).animate({ opacity: 0.6 }, 500, function() {
			var ww = $(window).width(); var wh = $(window).height(); var scroll = $(document).scrollTop();
			var yOffset = scroll + 100; var xOffset = Math.floor((ww - 550) / 2);
			$('div#docDetailContainer').css({ position: "absolute", top: yOffset + "px", left: xOffset + "px", minHeight: "340px", width: "550px" }).fadeIn("medium", function() {
				animRunning = false;
			}).append('<a href="javascript:hideDocumentDetail();" style="display: block; position: absolute; top: 10px; right: 24px; width: 32px; height: 20px;">&nbsp;</a>');
			$('div#docDetailWrapper').click(function() {
				hideDocumentDetail();
			});
		});
		$('div#docDetailContainer').append("<div></div>");
		if ($(link).parents("div.doc").hasClass('restricted')) {
			var restricted = true;
			$('div#docDetailContainer div').addClass("restricted");
		} else {
			$('div#docDetailContainer div').addClass("details");
		}
		var source = $(link).parents("div.doc").find("div.details");
		var details = '';
		var download = '';
		details = details + '<dl>';
		if ($(source).find('p.ref').text() != '') {
			details += '<dt>Reference:</dt><dd>' + $(source).find('p.ref').text() + '</dd>';
		}
		if ($(source).find('p.version').text() != '') {
			details += '<dt>Version:</dt><dd>' + $(source).find('p.version').text() + '</dd>';
		}
		if ($(source).find('div.abstract').text() != '') {
			details += '<dt>Overview:</dt><dd>' + $(source).find('div.abstract').html() + '</dd>';
		}
		if ($(source).find('p.ext').text() != '') {
			var txt = '';
			switch ($(source).find('p.ext').text().toLowerCase()) {
				case "pdf":
					txt = 'PDF Document';
					break;
				default:
					txt = $(source).find('p.ext').text().toUpperCase();
					break;
			}
			details += '<dt>File Type:</dt><dd>' + txt + '</dd>';
		}
		if ($(source).find('p.size').text() != '') {
			details += '<dt>Size:</dt><dd>' + $(source).find('p.size').text() + '</dd>';
		}
		download = '<a href="javascript:hideDocumentDetail();" class="c">&nbsp;</a>';
		if (restricted) {
			details += '<dt class="restriction">Access:</dt><dd>Authorised Users Only</dd>';
//			download += '<a href="javascript:hideDocumentDetail();showLogin(true);" class="dl">&nbsp;</a></p>';
			download += '<a href="' + $(link).attr("href") + '" class="dl">&nbsp;</a>';
		} else {
			details += '<dt>Access:</dt><dd>You are authorised to download this file</dd>';
			download += '<a href="' + $(link).attr("href") + '" class="dl" onclick="hideDocumentDetail();">&nbsp;</a>';
		}
		details += '</dl>';
		$('div#docDetailContainer div').html('<h3>' + $(source).find("h5").text() + '</h3>'+details).after('<div class="download">'+download+'</div>');
		$('dd p:last').css("margin", "0");
		if (restricted) {
			$('div#docDetailContainer div.download').removeClass('donwload').addClass('noDownload');
		}
	}
}

function hideDocumentDetail() {
	if (!(animRunning)) {
		animRunning = true;
		$('div#docDetailContainer').fadeOut(500, function() {
			$('div#docDetailWrapper').fadeOut(500, function() {
				animRunning = false;
			});
		});
	}
}

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href")) {
			var href = anchor.getAttribute("href");
			if ((href.indexOf('http') != -1) && (href.indexOf('axpactadditions.com') == -1)) {
				anchor.href = '/out?url=' + href;
			}
		}
	}
}

$(document).ready(function() {
	externalLinks();
	animRunning = false;
	if (null != document.getElementById("mainmenu")) {
		$('ul#mainmenu>li').bind('mouseover', function(e) {
			$(this).addClass('over');
		}).bind('mouseout', function(e) {
			$(this).removeClass('over');
		});
	}
	tabNav.init();
	$('div.solutionTitle h2').fillText({maxHeight: 72});
	$('div.vendorLogo h2').fillText({maxHeight: 72});
	$('div.industryImageBrowser div.imageLink').hover(function() {
		$(this).addClass('over');
	}, function() {
		$(this).removeClass('over');
	});
	$('div.screenshots').each(function() {
		$(this).find("a").lightBox();
	});
	$("input[name$='memberOptIn']").attr('checked', true);
	$('div.documents h5 a').click(function(e) {
		showDocumentDetail($(this));
		return false;
	});
});
