var currentTab = 0;
var isClicked = false;
var firstrun = true;
var isiPad = navigator.userAgent.match(/iPad/i) != null;
var isiPhone = navigator.userAgent.match(/iPhone/i) != null;
var isAndroid = navigator.userAgent.match(/Android/i) != null;
var snav = false;
$(function(){
	
	$('html').removeClass('no-js');	
	
	if($('.case-study').length) snav = true;
	
	$(window).bind('load', function(){
		// Load content
		if($('.case-study #ajax').length){
			loadContent($('.slider-nav li').eq(0));
		}
		
		if($('.home').length){
			var fheight;
			$('#footer li').each(function(){
				fheight = $(this).height();
			});
			$('#footer li').height(fheight);
		}
		
		$('#loading').hide();
	})
	
	$(window).resize(function(){
		coffset = $('.center').offset();
		wh = $(window);
		f = $('#footer');
		if($('.center').length){
			if(wh.height() >= ($('.center').outerHeight() + coffset.top + f.outerHeight())){
				f.css({ bottom:'20px', top:'auto' })
			}
		}
	});
	
	$('div#loading').append('<div></div>');
	
	$.fn.fixedCenter = function(){
		return this.each(function(){
			var element = $(this), win = $(window);
			centerElement();
			
			$(window).bind('resize',function(){
				centerElement();
			});

			function centerElement(){
				var elementWidth, elementHeight, windowWidth, windowHeight, X2, Y2;
				elementWidth = element.outerWidth();
				elementHeight = element.outerHeight();
				windowWidth = win.width();
				windowHeight = win.height();	
				X2 = (windowWidth/2 - elementWidth/2) + "px";
				Y2 = (windowHeight/2 - elementHeight/2) + "px";
				$(element).css({
					'left':X2,
					'top':Y2,
					'position':'fixed'
				});						
			} //centerElement function
		});
	}
	
	if(isiPhone || isAndroid){
		
	} else {
		$('.center').fixedCenter();
	}

	
	// homepage controls
	$('#leftControl, #rightControl').click(function(){
		if($(this).attr('id') == 'rightControl') { 
			dir = 'right';
		} else {
			dir = 'left';
		}
		clickTab(dir);
	});
	
	if(isiPad || isiPhone || isAndroid){
		if($('#leftControl, #rightControl').length){
			$('.center').touchwipe({
				wipeLeft: function(){
						$('#rightControl').trigger('click');
						if($('#prev-slide').length){
							$(this).trigger('click');
						}
					},
				wipeRight: function() {
						$('#leftControl').trigger('click');
					},
				preventDefaultEvents: true
			});
			$('.slider-nav').touchwipe({
				wipeLeft: function(){
						$('.next').trigger('click');
					},
				wipeRight: function() {
						$('.prev').trigger('click');
					},
				preventDefaultEvents: true
			});
		}
	}
	
	
	// Scrolling
	if($('.listing #careers-content').length){
		$('.listing .content').jScrollPane({
			animateScroll: true
		});
	}
	
	if($('#awards').length){
		$('#awards .recent').jScrollPane({
			animateScroll: true
		});
		
		$('#awards-nav a').click(function(){
			if($(this).hasClass('cur-tab')){
				return false;	
			} else {
				$('#awards-nav a').removeClass('cur-tab');
				$(this).addClass('cur-tab');		
			}
			
			if($('#awards-nav .recent-nav').hasClass('cur-tab')){
				$('#awards .awards-slide').removeClass('cur-awards');
				$('#awards .recent').addClass('cur-awards');
			} else {
				$('#awards .awards-slide').removeClass('cur-awards');
				$('#awards .past').addClass('cur-awards');
				$('#awards .past').jScrollPane({
					animateScroll: true
				});
			}
		});
	}
	
	// Open/close footer
	if($('#footerSlideContent').length){
		$('#footerSlideContent').show();
		loadCarousel();
		$('#caption').append($('.slider-nav .cur-tab .caption'));	
		/*setTimeout(function(){
			if(!isClicked){
				$('#footerSlideContent').slideToggle();
			}
		}, 3000);*/
	}
	$('#footerSlideButton').click(function () {
		isClicked = true;
		$('#footerSlideContent').slideToggle();
		$('#open').toggle();
		$('#close').toggle();
	});	
	
	$('.slider-nav li').click(function(){
		if(snav == true) {
			loadContent(this);
			return false;
		} else {
			window.location = $(this).find('a').attr('href');
		}
	});
	
	$('.careers #left-nav .active').click(function(){
		return false;
	});
	
	// About
	if($('#what-we-do').length){
		var swidth;
		$('#what-we-do-acc > li:first-child').addClass('slide-open');
		$('li.head').each(function(){
			if($(this).hasClass('slide-open')) return;
			$(this).width($(this).find('a').width());
			swidth = $(this).find('a').width();
		});
		$('li.head a').click(function(){
			if($(this).parent('li').hasClass('slide-open')) return;
			$('li.slide-open').animate({ width:swidth }, { duration: 300, specialEasing: { width: 'easeInOutExpo' } });
			$('.slide-open').removeClass('slide-open');
			$(this).parent('li').addClass('slide-open');
			$('.slide-open').animate({ width:$('.slide-open').find('ul').width()+swidth }, { duration: 300, specialEasing: { width: 'easeInOutExpo' }});
			
		});
	}
	// Leadership
	if($('#leadership-carousel').length){
		var leadLength = $('#leadership-carousel li').length;
		var leadershipNav = $('#leadership-nav li');
			
		$('#leadership-carousel').jCarouselLite({
			visible:1,
			speed:0,
			auto:false,
			btnGo:leadershipNav,
			beforeStart: function(el){
				$(el).parent().fadeTo(150, 0);
			},
			afterEnd: function(el){
				$(el).parent().fadeTo(150,1);
				var index = $('#leadership-carousel li').index(el);
				$('#leadership-nav li').removeClass('cur-tab');
				$('#leadership-nav li').eq(index-1).addClass('cur-tab');
				if(index == leadershipNav+1){
					$('#leadership-nav li').eq(0).addClass('cur-tab');
				}
			}
		});
	}
	/* Awards
	if($('#awards-carousel').length){
		var awardsNav = $('#awards-nav li');
		var last = $('.awards-slide').length-1;
		$('#awards-carousel').jCarouselLite({
			visible:1,
			speed:0,
			auto:false,
			btnGo:awardsNav,
			beforeStart: function(el){
				$(el).parent().fadeTo(150, 0);
			},
			afterEnd: function(el){
				$(el).parent().fadeTo(150,1);
				$('.awards-slide').jScrollPane({
					animateScroll: true
				  });
				var index = $('#awards-carousel .awards-slide').index(el);
				$('#awards-nav li').removeClass('cur-tab');
				$('#awards-nav li').eq(index-1).addClass('cur-tab');
				if(index == awardsNav+1){
					$('#awards-nav li').eq(0).addClass('cur-tab');
				}
			}
		})
	}*/
	
	
		
	// Open links in new windows
	$('.social a[href^="http://"], a.external').attr('target', '_blank');
	
	$('.case-study .next, .case-study .prev').click(function(){ isClicked = true });
	
	if($('#newsletter').length){
		$('#submit').click(function(){
			isValid = $('#newsletter').validate().element('#email');
			if(isValid) submitEmail();
		});
	}
	
	$('#btn-all-clients').click(function(){
		$('.btn-clients-view').removeClass('cur-client-view');
		$(this).addClass('cur-client-view');
		$('#featured-clients').fadeOut(200, function(){
			$('#all-clients').fadeIn(200);
		});
	});
	
	$('#btn-featured-clients').click(function(){
		$('.btn-clients-view').removeClass('cur-client-view');
		$(this).addClass('cur-client-view');
		$('#all-clients').fadeOut(200, function(){
			$('#featured-clients').fadeIn(200);
		});
	});
	
	if($('#featured-clients').length){
		$('#featured-clients li a').hover(function(){
			$(this).find('p').css({
				'marginTop': (76-$(this).find('p').height())/2
			});
			$(this).find('img').css({ opacity:.05});
		}, function(){
			$(this).find('img').css({ opacity:1 });
		});
	}
	
});

	

function updateOrientation(){
	if(isiPad || isiPhone || isAndroid){
		var orientation = window.orientation;
		var fheight;
		$('#footer li').each(function(){
			$(this).height('auto');
			fheight = $(this).height();
		});
		$('#footer li').height(fheight);
		
		$('#viewport').attr('content', 'width=device-width, maximum-scale=1.0');
	}
}

function loadCarousel(){
	var numThumb = $('.thumbs li').size();
	var visibleItems = 7;
	if(numThumb <= 7){
			visibleItems = numThumb;
			$('.next, .prev').remove();
	} else {
		$(".carousel").jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev",
			visible: visibleItems,
			scroll: 1,
			circular: false
		});
	}
}
function loadContent(ele){
	if(snav == false){
		return false;
	}
	
	if(firstrun) {
		isClicked = false;
	} else {
		isClicked = true;
	}
	
	var pathname = window.location.pathname;
	var url = $(ele).find('a').attr('href');
	/*
	$('#ajax').fadeTo(150,0, function(){
	$('#ajax').animate({opacity:0, filter: 'alpha(opacity=0)'}, 150, function(){		
		$('#ajax').load(pathname + url + ' .frame', function(){
			$('.center').css({
				position:'absolute',
				left: ($(window).width() - $('.center').outerWidth())/2,
				top: ($(window).height() - $('.center').outerHeight())/2
			});
			if($('.frame img').length){
				$('.frame img').load(function(){
					if($('.case-study').length) $('#ajax').css({'margin-left':-($('#ajax').find('.frame').width()/2), 'margin-top':-($('#ajax').find('.frame').height()/2) });
					$('#case-content').append('<div id="loader"></div>');
					$('#ajax').animate({opacity: 1, filter: ''}, 150);
					$('#case-content').find('#loader').remove();
				});
			} else {
				if($('.case-study').length) $('#ajax').css({'margin-left':-($('#ajax').find('.frame').width()/2), 'margin-top':-($('#ajax').find('.frame').height()/2) });
				$('#case-content').append('<div id="loader"></div>');
				$('#ajax').animate({opacity: 1, filter: ''}, 150);
				$('#case-content').find('#loader').remove();
			}
		});
	})
	*/
	$('#ajax').animate({opacity:0, filter: 'alpha(opacity=0)'}, 150, function(){		
		$('#ajax').load(pathname + url + ' .frame', function(){
			$('#case-content').delay(100).append('<div id="loader"></div>');
			$('.center').css({
				position:'absolute',
				left: ($(window).width() - $('.center').outerWidth())/2,
				top: ($(window).height() - $('.center').outerHeight())/2
			});
			if($('.frame img').length){
				$('.frame img').load(function(){
					if($('.case-study').length) $('#ajax').css({'margin-left':-($('#ajax').find('.frame').width()/2), 'margin-top':-($('#ajax').find('.frame').height()/2) });
					$('#ajax').animate({opacity: 1, filter: ' !important'}, 150, function(){
						$('#ajax').attr("filter", 'none', function(){console.log('filter removed')});
						$('#case-content').find('#loader').remove();
					});
				});
			} else {
				if($('.case-study').length) $('#ajax').css({'margin-left':-($('#ajax').find('.frame').width()/2), 'margin-top':-($('#ajax').find('.frame').height()/2) });
				$('#ajax').animate({opacity: 1, filter: ' !important'}, 150, function(){
					$('#ajax').attr("filter", 'none', function(){console.log('filter removed')});
					$('#case-content').find('#loader').remove();
				});
			}
		});
	});
	
	// Update bottom nav on About page
	if($('.slider-nav').length){
		$('.slider-nav .cur-tab').append($('#caption .caption'));
		$('.slider-nav li').removeClass('cur-tab');
	}
	
	$(ele).addClass('cur-tab');
	currentTab = $(ele).index();
	
	// Load captions in work pages
	if($('#caption').length) $('#caption').append($('.slider-nav .cur-tab .caption'));
	
	firstrun = false;
}

function clickTab(dir){
	isClicked = true;
	l = $('.slider-nav li').length;

	if($('.case-study').length){
		if(dir == 'right'){
			if(currentTab == l-1){
				$('.slider-nav li').eq(0).trigger('click');
			} else {
				$('.slider-nav li').eq(currentTab+1).trigger('click');
				$('.next').trigger('click');
			}
		} else {
			if(currentTab == 0){
				$('.slider-nav li').eq(l-1).trigger('click');
			} else {
				$('.slider-nav li').eq(currentTab-1).trigger('click');
				$('.prev').trigger('click');
			}
		}
		firstrun = false;
	} else {
		var cur = $('.slider-nav .cur-tab').index();
		if(dir == 'right'){
			if(cur == l-1) $('.slider-nav li').eq(0).find('a').trigger('click');
			$('.slider-nav li').eq(cur+1).find('a').trigger('click');
		} else {
			$('.slider-nav li').eq(cur-1).find('a').trigger('click');
		}
	}
}

function submitEmail(){
	$('#newsletter').submit(function(){
		$('#news-letter .error').remove();
		var email = $('#email').val();
		$.ajax({
			   url: 'http://www.pappasgroup.com/_process/newsletter.php',
			   type: 'GET',
			   data: 'email=' + email,
			   success: function(result) {
				   if (result=='TRUE') {
						$('#news-letter p').text('Thank you for your interest in the Pappas Group.');
						$('#newsletter').hide();
				   } else {
						$('#news-letter p').text('An error has occured with your submission.');
						$('#newsletter').hide();
				   }
			   }
		});
		return false;
	});
}
