var carouselOffset = 0;
var panelSize = 812;

$(document).ready( function() {

	// cached background images - IE6
	if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6)
		document.execCommand("BackgroundImageCache",false,true);
		
	// CAROUSEL
	var win = dynamItWindowSize();
	carouselOffset = parseInt( ( win.w - panelSize ) / 2);
	$('ul#carousel').css('left', carouselOffset);
	$('ul#carousel').dynamItCarousel({
		next: '#next',
		prev: '#prev',
		indexnav: 'ul#nav',
		width: 1062,
		offset: carouselOffset,
		onafter: function(o, index) {
			// denote the active pane
			$("ul#carousel").children("li").removeClass('active').eq(index).addClass('active');
			// set the rocket logo
			var paneId = $("ul#carousel").children("li").eq(index).attr('id');
			$('#logo').children('img').attr('src', '/img/logo_' + paneId + '.png');
		},
		onbefore: function(o, index) {
			// reset this page
			$("ul#carousel").children("li").eq(index).find('ul.tabnav').children().not('.shadow').eq(0).children('a').click();
		},
		updatehash: true
	});

	$('ul.subcarousel').each( function() {
		$(this).dynamItCarousel({
			width: 800,
			onafter: function(o, index) {
				setTitleNav(o, index);
				checkTab(o, index);
			},
			oninit: function(o, index) {
				setTitleNav(o, index);
				checkTab(o, index);
			},
			updatehash: true
		});
	})

	// show and had subsection navigation bars.
	var navBarsTO = null;
	$('div.subsection').hover( function() {
		clearTimeout(navBarsTO);
		$(this).parent().find('div.titlenav').slideDown('slow');
		$(this).parent().find('div.dotnav').slideDown('slow');
	}, function() {
		clearTimeout(navBarsTO);
		var subSec = this;
		navBarsTO = setTimeout( function() {
			$(subSec).parent().find('div.titlenav').slideUp('slow');
			$(subSec).parent().find('div.dotnav').slideUp('slow');
		}, 800 );
	});


	// Forms and Stuff.
	$('input, textarea').focus( function() {
		if( $(this).val() == $(this).attr('title') ) {
			$(this).val('');
		}
	}).blur( function() {
		if( $(this).val() == '' ) {
			$(this).val( $(this).attr('title') );
		}
	});

	$("form").submit( function() {
		$(this).find("input[type='submit']").replaceWith('<img src="/img/ajax-loader.gif" alt="..." />');
		return true;
	});

	// initialize the site.
	$('div#content').width('100%');
	$('div.subsection').hide().css('position', 'absolute');
//	$('div.pagenav').hide();

	$('div.subsection:first-child').addClass('active').show();
	$('div.titlelinks a:first-child').addClass('on');
	$('ul#nav>li:first-child').addClass('on');
	$('ul#carousel>li:first-child').addClass('active');
	$('ul.tabnav>li:first-child').next().addClass('on'); //skip the shadow
	$('ul.dotnavlinks>li:first-child').addClass('on');

	// attach navigation events
	$('ul.tabnav li a').click(clickTab);
	$('a.pagetitle').click(clickTitle);
	$('a.tab').click(nextTab);

	//parse the hash for deep-linking
	if( location.hash.length ) {
		var orighash = location.hash;
		var hash = orighash.split('/');
		var section = hash[0].substring(1);
		var tab = hash[1];
		var page = hash[2];
		var subsectionId = 'section_' + section + '_tab_' + tab;

		if(section && section.length) $('ul#nav').find('.' + section).parent().click();
		if(tab && tab.length) clickTab(subsectionId);
		if(page && page.length) $('#'+subsectionId).find('ul.dotnavlinks').children().eq(page).click();

	}

});

$(window).resize( function(o) {
	var win = dynamItWindowSize();
	carouselOffset = parseInt( ( win.w - panelSize ) / 2);
});


function clickTab(lnk) {
	var o = this;
	if( lnk.length ) {
		if( $("a[href='/#" + lnk + "']").length )
			o = $("a[href='/#" + lnk + "']").get(0);
		else
			return;
	}

	var subsectionId = $(o).attr('href').substring(1);
	$(o).parent().siblings().removeClass('on').end().addClass('on');
	var $current = $(o).closest('li.pane').children('div.section').children('.active');
	$current.siblings('.fader').fadeTo(100, 0.5, function() {
		$current.hide().removeClass('active');
		$(subsectionId).show().addClass('active');
		$(this).fadeOut(100);
		if( $(subsectionId).attr('title').length )
			location.hash = $(subsectionId).attr('title');
	});
	$(subsectionId).find('div.pagenav').show(1, function() { 
		var me = this;
		setTimeout( function() { $(me).slideUp('slow'); }, 800 );
	});
	
	$(subsectionId).find('ul.dotnavlinks').children().eq(0).click();
	return false;
}

function nextTab() {
	var $tabNav = $("ul#carousel").children("li.active").children('ul.tabnav');
	if( $tabNav.children('.on').next().length ) {
		$tabNav.children('.on').next().children('a').click();
	} else {
		$tabNav.children().eq(1).children('a').click();
	}
}

function prevTab() {
	var $tabNav = $("ul#carousel").children("li.active").children('ul.tabnav');
	if( $tabNav.children('.on').prev().length && !$tabNav.children('.on').prev().hasClass('shadow')) {
		$tabNav.children('.on').prev().children('a').click();
	} else {
		$tabNav.children().last().children('a').click();
	}
}

function clickTitle() {
	var href = $(this).attr('href').split('_');
	var index = href[href.length - 1];
	$(this).siblings().removeClass('on').end().addClass('on');
	$(this).closest('div.subsection').find('ul.dotnavlinks').children().eq(index).click();
	return false;
}

function setTitleNav(o, index) {
	var upperBound, lowerBound, href, thisindex;
	var $titleLinks = $(o.indexnav).closest('.subsection').children('.titlenav').find('a');
	if( $titleLinks.length ) {
		upperBound = $(o.indexnav).children().length;
		lowerBound = 0;
		var $cur = $titleLinks.last();
		while( $cur.length ) {
			href = $cur.attr('href').split('_');
			thisindex = href[href.length - 1];
			if( index >= thisindex ) {
				lowerBound = thisindex;
				$cur.siblings().removeClass('on').end().addClass('on');
				break;
			}
			upperBound = thisindex;
			$cur = $cur.prev();
		}

		$(o.indexnav).children().hide();
		for( var i = parseInt(lowerBound); i < parseInt(upperBound); i++ )
			$(o.indexnav).children().eq(i).show();
	}
}

function checkTab(o, index) {
	if( $(o.slider).children().length - 1 == index ) {
		$(o.next).css('visibility', 'hidden');
		$(o.next).siblings('.tab').show();
	} else {
		$(o.next).css('visibility', 'visible');
		$(o.next).siblings('.tab').hide();
	}
}

function formSuccess(box) {
	var msg = '<p><strong>Thanks for reaching out. We have received your message and will give a read shortly.</strong></p>';
	var $frm = $("#" + box).find("form");
	$frm.parent().children('h2').html('thank you');
	$frm.parent().children('p').html('');
	$frm.replaceWith(msg);
}

function formFailure(box) {
	var msg = '<p><strong>There was an error collecting your message. Would you mind trying again?</strong></p>';
	var $frm = $("#" + box).find("form");
	$frm.parent().children('h2').html('sorry');
	$frm.parent().children('p').html('');
	$frm.replaceWith(msg);
}

