var paper;
var resizeTimer = null;
var windowSize = $(window).width();

var lastMovement = 0;

var totalServices;
var currentServicesScroll = 0;

var totalHero;
var currentHeroScroll = 0;

var animatingTweet = false;

var autoSlideHeroTimer;
var slidingHeroIndex = 0;

function drawLine(){
	
	var baseHeight;
	
	if($('.container').height() > $('.wrapper').height()){
		baseHeight = $('.container').height();
	} else { 
		baseHeight = $('.wrapper').height();
	}
	paper = Raphael("lowestLine",$('.wrapper').width(),baseHeight);
	midpaper = Raphael("middleLine",$('.wrapper').width(),baseHeight);
	
	var strokeOne_pointOne_X = $('.wrapper').width() * 0.4;
	var strokeOne_pointOne_Y = baseHeight;
	var strokeOne_pointTwo_X = $('.wrapper').width() * 0.97;
	var strokeOne_pointTwo_Y = 0;
	
	var strokeTwo_pointOne_X = $('.wrapper').width() * 0.65;
	var strokeTwo_pointOne_Y = baseHeight;
	var strokeTwo_pointTwo_X = $('.wrapper').width() * 0.85;
	var strokeTwo_pointTwo_Y = 0;
	
	var c = paper.path("M"+strokeOne_pointOne_X+" "+strokeOne_pointOne_Y+"L"+strokeOne_pointTwo_X+" "+strokeOne_pointTwo_Y).attr({stroke: "#500211"});
	var d = paper.path("M"+strokeTwo_pointOne_X+" "+strokeTwo_pointOne_Y+"L"+strokeTwo_pointTwo_X+" "+strokeTwo_pointTwo_Y).attr({stroke: "#ba0628"});
	$('#middleLine').css('z-index',11);
}

function animateSubnav(subnav,displaySubnav){
	$(subnav).children('li').stop(true,true);
	var topPos = 0;
	if(displaySubnav){
		$(subnav).show();
	} else {
		topPos = $(subnav).children('li').height()*-1;
	}
	var counterMax = $(subnav).children('li').length;
	for(var i=1; i<=counterMax; i++){
		$(subnav).children('li').eq(counterMax-i).animate(
			{ top: topPos }, {
				duration: 1300/i,
				easing: 'easeOutExpo',
				complete:function(){
					if(!displaySubnav){
						$(subnav).hide();
					}
				}
			}
		);
	}
}

function animateHeroModule(){
	/*
	var mousePosX;	
	var movementSpeed = 12;
	$(document).mousemove(function(e){
		mousePosX = e.pageX;
		mousePosY = e.pageY;
		if(Math.abs(mousePosX - lastMovement) > 30){
			lastMovement = mousePosX;
			if(mousePosY > $('#header').height()+20){
				$('.activeSlide .secondLayer').stop(true,true);
				$('.activeSlide .thirdLayer').stop(true,true);
				var movement = (mousePosX - windowSize/2)/windowSize*movementSpeed;
				var secondLayerMovement = -1*movement;
				var thirdLayerMovement = movement;
				$('.activeSlide .secondLayer').animate({
					left: secondLayerMovement
				},200);
				$('.activeSlide .thirdLayer').animate({
					left: thirdLayerMovement
				},200);
			}
		}
	});
	*/
	
}

function autoSlide(){
	totalHero = $('.heroSlide .heroFeed').length;
	autoSlideHeroTimer = setTimeout(function(){
		if(slidingHeroIndex < totalHero-1){
			slidingHeroIndex++;
		} else {
			slidingHeroIndex = 0;
		}
		slideHero($('#heroPagination li a').eq(slidingHeroIndex));
	},4500);
}

function activeHeroSlider(){
	$('#heroPagination li a').click(function(){
		$('.heroSlide').stop(false,false);
		window.clearTimeout(autoSlideHeroTimer);
		autoSlide();
		slideHero($(this));
	});
}

function slideHero(btnPressed){
	var page = $(btnPressed).html()-1;
	slidingHeroIndex = page;
	if(!$(btnPressed).hasClass('on')){
		$('.heroSlide').animate({
			left: page*-1*$('.heroSlide .heroFeed').width()
		},600, function(){
			$('#heroPagination li a').removeClass('on');
			$(btnPressed).addClass('on');
			$('.activeSlide').removeClass('activeSlide');
			$('.heroSlide .heroFeed').eq(page).addClass('activeSlide');
			//animateHeroModule();
			window.clearTimeout(autoSlideHeroTimer);
			autoSlide();
		});
	}
}

function autoScrollServices(){
	totalServices = $('.scrollingServices ul li').length;
	currentServicesScroll++
	if(currentServicesScroll < totalServices){
		$('.scrollingServices ul').animate({
			top: currentServicesScroll*-1*$('.scrollingServices ul li').height()
		}, {
			duration: 800,
			complete:function(){
				if(currentServicesScroll == totalServices-1){
					currentServicesScroll = 0;
					$('.scrollingServices ul').css("top",0);
				}
			}
		});
	}
}

function initiateTweetScroll() {
	var maxTweet = $('.tweetCollection .tweet').size();
	//$('.tweetCollection .tweet').eq(0).clone().appendTo('.tweetCollection');
	var currentTweet = 0;
	$('.twitterNav a').click(function(){
		if(!animatingTweet) {
			animatingTweet = true;
			var getID = $(this).attr('id');
			var direction = -1;
			if(getID == "twitterNext"){
				currentTweet++;
			} else {
				currentTweet--;
				/*
				if(currentTweet > 0){
					currentTweet--;
				} else {
					currentTweet = maxTweet-1;
					$('.tweetCollection').css('left',-1*(maxTweet)*$('.tweetMask').width());
				}
				*/
			}
			$('.tweetCollection').animate({
				left: direction*currentTweet*$('.tweetMask').width()
			},300,function(){
				if(currentTweet == maxTweet-1){
					/*
					$('.tweetCollection').css('left',0);
					currentTweet = 0;
					*/
					$('#twitterNext').hide();
					$('#twitterPrev').show();
				} else if(currentTweet == 0){
					$('#twitterPrev').hide();
					$('#twitterNext').show();
				} else {
					$('#twitterNext').show();
					$('#twitterPrev').show();
				}
				animatingTweet = false;
				$('.tweetCollection .on').removeClass('on');
				$('.tweetCollection .tweet').eq(currentTweet).addClass('on');;
				$('.twitterModule span.date').html($('.twitterModule div.tweet.on').attr('data-day'));		
				$('.twitterModule span.month').html($('.twitterModule div.tweet.on').attr('data-month'));
				Cufon.refresh();
			});
		}
	});
}

$(window).bind('resize', function() {
	paper.remove();
	midpaper.remove();
	if (resizeTimer) {
		clearTimeout(resizeTimer);
	}
	resizeTimer = setTimeout(drawLine, 100);
});

$(window).load(function(){
	$('.footer').css('bottom',-1*$('.footer').height()+$('.footer-navigation').height());
	$('.footer').css('visibility','visible');
	drawLine();
	
	$('.lastInRow').each(function(){
		if($(this).outerHeight(true) >= $(this).prev().outerHeight(true)){
			$(this).prev().height($(this).height());
		} else {
			$(this).height($(this).prev().height());
		}
	});
	$('.serviceFeed').hide();
	$('.serviceFeed').css('visibility','visible');
	$('.serviceFeed').fadeIn(200);
});

$(document).ready(function(){
	//animateHeroModule();
	autoSlide();
	activeHeroSlider();
	var teamEntryHeight;
	
	if($('.twitterModule div.tweet').length > 0){
		$('.twitterModule span.date').html($('.twitterModule div.tweet:first').attr('data-day'));		
		$('.twitterModule span.month').html($('.twitterModule div.tweet:first').attr('data-month'));
		initiateTweetScroll();
	}
	
	$('#nav li').hover(function(){
		if($(this).children('.subnav').length > 0){
			if($(this).hasClass('opensubnav') == false){
				animateSubnav($(this).children('.subnav'),true);
			}
		}
	},function(){
		if($(this).children('.subnav').length > 0){
			animateSubnav($(this).children('.subnav'),false);
		}
	});
	$('.subnav').hover(function(){
		$(this).siblings('a').addClass('on');
	},function(){
		$(this).siblings('a').removeClass('on');
	});
	if((".scrollingBranding").length > 0){
		$('.scrollingServices ul li').eq(0).clone().appendTo($('.scrollingServices ul'));
		setInterval(autoScrollServices,4000);
	}
	$('.newsFeed .readMore').click(function(){
		if(!$(this).hasClass('on')){
			$('.readmoreContent').slideDown(200,function(){
				$('.newsFeed .readMore').html("Show less...");
			});
			$(this).addClass('on');
		} else {
			$('.readmoreContent').slideUp(200,function(){
				$('.newsFeed .readMore').html("Read more...");
			});
			$(this).removeClass('on');
		}
		return false;
	});
	$('.teamFeed').hover(function(){
		$(this).children('.serviceDetail').stop(true,true);
		$(this).find('.profileDetail').stop(true,true);
		teamEntryHeight = $(this).children('.profilePhoto').height();
		$(this).children('.profilePhoto').slideUp(100);
		$(this).children('.serviceDetail').animate({
			height: $(this).children('.serviceDetail').height()+teamEntryHeight
		}, 100);
		$(this).find('.profileDetail').fadeIn(100);
	},function(){
		$(this).children('.serviceDetail').stop(true,true);
		$(this).find('.profileDetail').stop(true,true);
		$(this).children('.profilePhoto').slideDown(100);
		$(this).children('.serviceDetail').animate({
			height: $(this).children('.serviceDetail').height()-teamEntryHeight
		}, 100);
		$(this).find('.profileDetail').fadeOut(100);
	});

	$('.sitemap').click(function(){
		if(!$(this).parent().hasClass('on')){
			$(this).parent().addClass('on');
			Cufon.refresh('.cufonme');
			var footerHeight = $('.sitemap-content').outerHeight(true);
			$('.footer').animate({
				bottom: 0
			},250);
		} else {
			$(this).parent().removeClass('on');
			Cufon.refresh('.cufonme');
			$('.footer').animate({
				bottom: -1*$('.sitemap-content').outerHeight(true)
			},250);
		}
		return false;
	});
	
	$('.activeProjectSlideshow').cycle({  
		speed:  1000, 
		timeout: 3500, 
		pager:  '#pagination' 
	});
	
	$('.jcycleme01').cycle({  
		speed:  1000, 
		timeout: 4500,
		delay: 0
	});
	$('.jcycleme02').cycle({  
		speed:  1000, 
		timeout: 4500,
		delay: 1000
	});
	$('.jcycleme03').cycle({  
		speed:  1000, 
		timeout: 4500,
		delay: 2000
	});
	
	$('#em_subscribe_form').submit(function() {
		if ($('#em_wfs_formfield_2494383').val()=="Email address" || $('#em_wfs_formfield_2494383').val()=="" || $('#em_wfs_formfield_2494383').val()==" "){
			alert("Please fill in your email address");
			return false;
		} else {
			/*
			if(!checkForm(this)){
				return false;	
			}
			*/
			if(!validateEmail($('#em_wfs_formfield_2494383').val())){
				alert("Invalid email address");
				return false;	
			}
			
			window.open('', 'newsletterpopup', 'scrollbars=no,menubar=no,height=200,width=500,resizable=yes,toolbar=no,status=no');
			this.target = 'newsletterpopup';
			
		}
	});
	
	Cufon.refresh();
});
