// JavaScript Document
/// <reference path="jquery.min.js" />

function setupNavBar() {
	var speed = 0;
	var content = '#prodServSlider';
	if($('#homepage').length==0) content += ' .container';
	$('#prodServ a,#closeSlider').click(function () {
		if($.browser.msie && $.browser.version < 7)
			return false;
		if ($(content).is(':visible')) {
			$(content).slideUp(speed);
			$('#prodServ .arrow').removeClass('up').addClass('down');
			createCookie('hideNav', true, 14);
		}
		else {
			$(content).slideDown(speed);
			$('#prodServ .arrow').removeClass('down').addClass('up');
			eraseCookie('hideNav');
		}
		return false;
	});
	if (readCookie('hideNav'))
		$('#closeSlider').triggerHandler('click');
	speed = 500;
}

function setupHomeBanner() {
	var items = $('#homeBanner .hbItems');
	var discs = $('#hbNavigation .discNavLayers li');
	var num = items.length;
	var current = 0;
	//setup
	items.parent().css({ position: 'relative', height: items.getMaxHeight() });
	items.css({ position:'absolute', left: 0, top: 0 });
	$('#hbNavigation .dnlDiscIndicator').css({ backgroundPosition: 12 });
	//events
	discs.click(function () {
		current = discs.index(this);
		change();
		return false;
	});
	$('#hbNavigation .aLeft').click(function () {
		current = (current + num - 1) % num;
		change();
		return false;
	});
	$('#hbNavigation .aRight').click(function () {
		current = (current + 1) % num;
		change();
		return false;
	});
	var change = function () {
		var old = items.filter(':visible');
		old.css({ zIndex: 99 }).fadeOut(function () { $(this).css({ zIndex: '' }) });
		var item = items.eq(current);
		item.css({ zIndex: 100 }).fadeIn(function () { $(this).css({ zIndex: '' }) });
		//the little icon
		$('#hbNavigation .dnlDiscIndicator').animate({ backgroundPosition: current * (discs.eq(0).outerWidth(true))+12 }, 200);
	}
}

function setupFaqs() {
	$('.faqlAnswer:gt(0)').hide();
	$('.faqlQuestion').click(function() {
		$('.faqlAnswer:visible').not($(this).siblings('.faqlAnswer')).slideUp();
		$(this).siblings('.faqlAnswer').slideDown();
		$('.showHide').removeClass('hide').addClass('show').text($('.showHide').text().replace('Collapse', 'Expand'));
		return false;
	});
	$('.showHide').click(function () {
		if ($('.faqlAnswer:hidden').length > 0) {
			$('.faqlAnswer').slideDown();
			$('.showHide').removeClass('show').addClass('hide').text($('.showHide').text().replace('Expand', 'Collapse'));
		}
		else {
			$('.faqlAnswer').slideUp();
			$('.showHide').removeClass('hide').addClass('show').text($('.showHide').text().replace('Collapse', 'Expand'));
		}
		return false;
	})
}

function setupTabs() {
	$('.cbtNav a').click(function () {
		$(this).blur().parent().siblings().removeClass('selected').end().addClass('selected');
		var scroller = $(this).closest('.cbTabContainer').find('.tabScroller');
		var tab = $(this.hash);
		scroller.animate({ scrollLeft: scroller.scrollLeft() + tab.position().left }, 300);
		//set hash in url without scrolling
		tab.attr('id','');
		window.location.hash = this.hash;
		tab.attr('id',this.hash.replace('#',''));
		return false;
	});
}

$(function () {
	setupNavBar();
	setupHomeBanner();
	setupFaqs();
	setupTabs();
});

//jQuery extensions
$.fn.extend({
	//gets the max height of a set of elements
	'getMaxHeight': function() {
		var maxHeight = 0;
		this.each(function() {
			maxHeight = Math.max(maxHeight, $(this).height());
		});
		return maxHeight;
	}
});

/* 3rd party helpers */
//cookie functions kindly provided by http://www.quirksmode.org/js/cookies.html
function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}


