var fadingIn;
var fadingOut;

jQuery.fn.togglePane = function(speed) {
	if(fadingIn) {
		fadingIn.stop(false,true);
	}
	if(fadingOut) {
		fadingOut.stop(false,true);
	}
	fadingIn = $('.carousel .current_pane');
	fadingOut = $(this);
	$('.carousel .current_pane').stop(false,true);
	var self = $(this);
	$('.carousel .current_pane').fadeOut(250, function() {
	$('.carousel .current_pane').removeClass('current_pane');
	self.fadeIn(250);
	self.addClass('current_pane'); });
}


$(document).ready(function() {
	//for home
	var play_interval = null,
		current_button = 1,
		time_till_next = 8; // time between the switches in the carousel (in seconds)
	//news and tweets
	$('.news_tweets .news_link').live('click', function() {
		$('.news_tweets > .news_content').show('fast');
		$('.news_tweets > .tweets_content').hide('fast');
		return false;
	});
	$('.news_tweets .tweets_link').live('click', function() {
		$('.news_tweets > .news_content').hide('fast');
		$('.news_tweets > .tweets_content').show('fast');
		return false;
	});
	//carousel menu links
	$('.carousel_menu .who_we_are_link').live('click', function() {
		$('.carousel .who_we_are').togglePane('slow');
		/*$('.carousel .who_we_are').show('fast');
		$('.carousel .what_we_do').hide('fast');
		$('.carousel .how_we_do_it').hide('fast');
		$('.carousel .why_different').hide('fast');
		$('.carousel .our_clients').hide('fast');*/
		$('.carousel_menu > li > a.buttons').removeClass('current');
		$('.carousel_menu > li > .who_we_are_link').addClass('current');
		current_button = 1;
		return false;
	});
	$('.carousel_menu .what_we_do_link').live('click', function() {
		$('.carousel .what_we_do').togglePane('slow');
		/*$('.carousel .who_we_are').hide('fast');
		$('.carousel .what_we_do').show('fast');
		$('.carousel .how_we_do_it').hide('fast');
		$('.carousel .why_different').hide('fast');
		$('.carousel .our_clients').hide('fast');*/
		$('.carousel_menu > li > a.buttons').removeClass('current');
		$('.carousel_menu > li > .what_we_do_link').addClass('current');
		current_button = 2;
		return false;
	});
	$('.carousel_menu .how_we_do_it_link').live('click', function() {
		$('.carousel .how_we_do_it').togglePane('slow');
		/*$('.carousel .who_we_are').hide('fast');
		$('.carousel .what_we_do').hide('fast');
		$('.carousel .how_we_do_it').show('fast');
		$('.carousel .why_different').hide('fast');
		$('.carousel .our_clients').hide('fast');*/
		$('.carousel_menu > li > a.buttons').removeClass('current');
		$('.carousel_menu > li > .how_we_do_it_link').addClass('current');
		current_button = 3;
		return false;
	});
	$('.carousel_menu .why_different_link').live('click', function() {
		$('.carousel .why_different').togglePane('slow');
		/*$('.carousel .who_we_are').hide('fast');
		$('.carousel .what_we_do').hide('fast');
		$('.carousel .how_we_do_it').hide('fast');
		$('.carousel .why_different').show('fast');
		$('.carousel .our_clients').hide('fast');*/
		$('.carousel_menu > li > a.buttons').removeClass('current');
		$('.carousel_menu > li > .why_different_link').addClass('current');
		current_button = 4;
		return false;
	});
	$('.carousel_menu .our_clients_link').live('click', function() {
		$('.carousel .our_clients').togglePane('slow');
		/*$('.carousel .who_we_are').hide('fast');
		$('.carousel .what_we_do').hide('fast');
		$('.carousel .how_we_do_it').hide('fast');
		$('.carousel .why_different').hide('fast');
		$('.carousel .our_clients').show('fast');*/
		$('.carousel_menu > li > a.buttons').removeClass('current');
		$('.carousel_menu > li > .our_clients_link').addClass('current');
		current_button = 0;
		return false;
	});
	function play() {
		$($('.carousel_menu > li > a.buttons')[current_button]).click();
		//if( current_button == 4 ) { current_button = 0; }
	}
	//$('.carousel_menu .play').live('click', function() {
	$('#play_button').live('click', function() {
		if( !$(this).hasClass("inactive") ) {
			play_interval = setInterval( function(){ play(); }, time_till_next*1000 );
			//$(this).addClass('inactive');
			//$('.carousel_menu .pause').removeClass('inactive');
			$(this).addClass('inactive');
		}
		//return false;
	//});
	//$('.carousel_menu .pause').live('click', function() {
		//if( !$(this).hasClass("inactive") ) {
		 else {
			clearInterval(play_interval);
			play_interval = null;
			//$(this).addClass('inactive');
			//$('.carousel_menu .play').removeClass('inactive');
			//$(this).addClass('play');			
			$(this).removeClass('inactive');
		}
		return false;
	});
	play_interval = setInterval( function(){ play(); }, time_till_next*1000 );
	
	// for careers and services
	//left menu
	$('aside').delegate('a', 'click', function(){
		if( !$(this).hasClass("current") ) {
			$('aside > a').removeClass('current');
			$(this).addClass('current');
			var height = 0,
				index = $('aside a').index(this);
			for( var i = 0; i < index; i++ ) {
				height += $($('.content > div:not(.hr)')[i]).outerHeight(true);
			}
			height += index * 33; // adding the height of the .hr element
			var val = $('.content_wrapper > .content').css('margin-top').replace('px',''),
				diff = -height - 1 - val;
			$('.content_wrapper > .content').animate({'margin-top': '+=' + diff}, 1000);
		}
		return false;
	});
	// "more" link -  expand/collapse
	$('.module_1 > h2 > a').live('click', function() {
		if( $('span', this).hasClass('close_button') ) {
			var that = $(this).parent();
			that.siblings('.left').animate({'height': '145px'}, 600);
			that.parent().animate({'height': '250px'}, 600);
			$('span', this).removeClass('close_button');
			$('.more', that.parent()).show(600);
		}
		return false;
	});
	$('.module_2 > h2 > a').live('click', function() {
		if( $('span', this).hasClass('close_button') ) {
			var that = $(this).parent();
			that.siblings('.left').animate({'height': '195px'}, 600);
			that.parent().animate({'height': '310px'}, 600);
			$('span', this).removeClass('close_button');
			$('.more', that.parent()).show(600);
		}
		return false;
	});
	$('.module_1 > .more, .module_2 > .more').live('click', function() {
		var that = $(this);
		that.parent().css('height', 'auto');
		that.siblings('.left').css('height', '100%');
		that.siblings('.right').css('height', '100%');
		that.hide();
		$('a > span', that.siblings('h2')).addClass('close_button');
		return false;
	});	
	// "case study" link - expand/collapse
	$('.module_2 > .right .case_study').live('click', function() {
		var that = $(this).parent();
		that.parent().animate({'width': '434px'}, 700);
		that.siblings('.expanded').show(700);
		that.hide(700);
		that.parent().siblings('.left').css('height', 'auto');
		that.parent().siblings('.left').animate({'width': '145px'}, 600);
		that.parent().siblings('.left').children('.default').hide(600);
		that.parent().siblings('.left').children('.minimized').show(600);
		that.parent().siblings('.more').hide();
		that.parent().parent().css('height', '100%');
		return false;
	});
	$('.module_2 > .left > .minimized > .description').live('click', function() {
		var that = $(this).parent();
		that.parent().animate({'width': '434px'}, 700);
		that.siblings('.default').show(700);
		that.hide(700);		
		that.parent().css('height', '195px');
		that.parent().siblings('.right').animate({'width': '145px'}, 600);
		that.parent().siblings('.right').children('.default').show(600);
		that.parent().siblings('.right').children('.expanded').hide(600);
		that.parent().siblings('.more').show();
		that.parent().parent().css('height', '310px');
		return false;
	});
	$('.module_2 > .right > .expanded > h4 > .minimize').live('click', function() {
		var that = $(this).parent().parent();
		that.parent().animate({'width': '145px'}, 600);
		that.siblings('.default').show(600);
		that.hide(600);
		that.parent().siblings('.left').css('height', '195px');
		that.parent().siblings('.left').animate({'width': '434px'}, 700);
		that.parent().siblings('.left').children('.default').show(700);
		that.parent().siblings('.left').children('.minimized').hide(700);
		that.parent().siblings('.more').show();
		that.parent().parent().css('height', '310px');
		return false;
	});
});

