//* Preload Images
var i1 = new Image(); i1.src = 'images/banner-adaptive-systems.jpg';
var i2 = new Image(); i2.src = 'images/header-consulting-solutions.gif';
var i3 = new Image(); i3.src = 'images/consulting-solutions.gif';
var i4 = new Image(); i4.src = 'images/panelstmp.jpg';

//* Set Page Vars
//var page = (location.href.indexOf('?p=') > -1) ? location.href.split('?p=')[1] : '';
var page = (location.href.indexOf('#') > -1) ? location.href.split('#')[1] : '';
var subnav = {isOpen:false};

//* Set Flash Vars
var fv='6';
var flashvars={}, params={allowScriptAccess:"always",wmode:"opaque",scaleMode:"noScale"};

//* Define Functions
function animation(n) {
	switch (n) {
		case 0 :
			$('#header').animate({opacity:0},500,function() { n++; animation(n); });
			break;
		case 1 :
			$('#right').animate({opacity:0},500,function() { n++; animation(n); });
			break;
		case 2 :
			$('#left').animate({opacity:0},500,function() { n++; animation(n); });
			break;
		case 3 :
			ajax();
			break;
		case 4 :
			$('#header').animate({opacity:1},700,function() { n++; animation(n); });
			break;
		case 5 :
			if (!swfobject.hasFlashPlayerVersion(fv)) $('#panelstmp').css({visibility: 'visible'});
			$('#left').animate({opacity:1},700,function() { n++; animation(n); });
			break;
		case 6 : // collage SWF
			if (page != '' || page != 'home') swfobject.embedSWF("swf/collage.swf", "flashContent", 280, 208, fv, null, null, null);
			n++; animation(n);
			break;
		case 7 :
			$('#right').animate({opacity:1},700,function() { n++; animation(n); });
			break;
		case 8 : // sub-nav
			if ($('#nav-adaptive').hasClass('active')) {
				$('#subnav-services').hide();
				$('#subnav-adaptive').slideDown(400);
			} 
			else if ($('#nav-services').hasClass('active')) {
				$('#subnav-adaptive').hide();
				$('#subnav-services').slideDown(400);
			}
			break;
	}
}
function getContentHeight() {
	var lh = $('#left').height();
	var rh = $('#right').height();
	var hh = ($('#header').length > 0) ? $('#header').height() : 0;
	var h = (lh > rh) ? hh + lh + 'px' : hh + rh + 'px';
	return h;
}
function ajax() {
	$.ajax({
		
		method:		'get',
		url:		'index.php',
		data:		'ajax=true&p='+ page,
		cache:		false,
		
		success: function(html){
			
			var h = getContentHeight();
			$('#content').css('height',h);
			
			$('.active').removeClass('active');
			$('#content').html(html);
			
			h = getContentHeight();
			$('#content').animate({height:h},500);
			
			var n = ($('#header').length > 0) ? 4 : 5;
			
			animation(n);
			
			switch (page) {
				case 'adaptive' : case 'skills' :
					$('#nav-adaptive').addClass('active');
					break;
				case 'services' : case 'outsourcing' : case 'offshore' : case 'supplementation' : case 'management' :
					$('#nav-services').addClass('active');
					break;
				case 'industry' :
					$('#nav-industry').addClass('active');
					break;
				case 'contact' :
					$('#nav-contact').addClass('active');
					break;
				case 'home' : case '' : default :
					$('#nav-home').addClass('active');
					break;
			}
			active = false;
		}
	});
}
$(document).ready(function() {
	
	//* Initialize Page
		// Set all links to javascript calls
		$('.local').each(function() {
			$(this).attr('href','#'+ $(this).attr('href').split('?p=')[1]);
		});
		if (page != '') { ajax(); } else { var n = ($('#header').length > 0) ? 4 : 5; animation(n); }
	//
	
	
	
	//* Navigation Control
	
		// Mouse Enter
		$('a.nav').mouseenter(function() {
			
			// Allow action only if ready
			if (!active) { 
				
				var obj = $(this);
				
				// Set page to (this) ID
				page = obj.attr('id').split('-')[1];
				var sub = '#subnav-' + page;
				/*
				// Close sub-nav if open
				if (subnav.isOpen) {
					// and only if you enter a new primary item
					if ('#' + $('.subnav:visible').attr('id') != sub) {
						$('#nav .over').removeClass('over');
						$('.subnav:visible').hide();
						subnav.isOpen = false;
					}
				}
				
				// Fade in new sub-nav and set open status
				$(sub).fadeIn(400);
				obj.addClass('over');
				subnav.isOpen = true;
				*/
			}
		});
		// On Click
		$('#nav a').click(function() {
			$('#subnav > ul').slideUp(400);3
			var obj = $(this);
			active = true;
			page = obj.attr('id').split('-')[1];
			page = (page == undefined) ? obj.parents('ul.subnav').attr('id').split('-')[1] : page;
			if ($('#header').length > 0) {
				var n = 0;
			} else {
				var n = 1;
			}
			animation(n);
		});
		// Mouse Leave
		$('#nav').mouseleave(function() {
			if (subnav.isOpen) {
				$('.subnav:visible').fadeOut(100,function() {
					subnav.isOpen = false;
				});
			}
			$('#nav .over').removeClass('over');
		});
	//
});
