function onBefore(current,next,c,d){
	//console.log('before');
	$(next).find('h2').css('margin-left', '-620px');
	$(current).find('h2').css('margin-left', '0px').animate({'margin-left':'-620px'}, 300);	
}
var gwidth = 0;
var stops = [0];
var currentstop = -1;
function onAfter(current,next,c,d){
	//console.log('after');
	$(next).find('h2').css('margin-left', '-620px').animate({'margin-left':'0px'}, 300);
}

var timer;
function goto(i){
	clearInterval(timer);
	currentstop = i;
	$('.gallery.nodes p').html($($('#gallery .slide')[i]).find('.title').text());
	$('.gallery.nodes a').removeClass('active');
	$($('.gallery.nodes a')[i]).addClass('active');
	$('#gallery .mask').animate({
		'margin-left': stops[i]+'px'}
	, 600);
	$('#gallery .slide').each(function(index,value){
			(currentstop)==index?
				$(value).animate({'opacity':1}, 300):
				$(value).animate({'opacity':.2}, 300);
	});
	if (currentstop>stops.length-3) currentstop=-1;
	//console.log(currentstop);
	timer = setInterval(function(){
		currentstop++;
		goto(currentstop);
	}, 2000);
}
$(document).ready(function(){
	if ($.browser.msie) {
		$('#nav a:not(.children a)').corner();
		$('.projects li a').corner();
    	$('#nav ul a').corner('top, cc:#5e5e5e');
    	$('.gallery.nodes').corner('top');
    	$('#alt-nav').corner('bl').corner('br cc:#e2e2e2');
    	$('#right').wrapInner('<div>').children('div').css('background','url(/wp-content/themes/contata/style/images/sidebar-top.png) no-repeat center top').wrapInner('<div>').children('div').css({'background': 'url(/wp-content/themes/contata/style/images/sidebar-bottom.png) no-repeat center bottom', 'padding':' 0 21px 0 20px', 'width':'319px'});
		$('#nav li:not(#nav li li)').hover(function(){
			$(this).find('a:not(li li a)').toggleClass('hover');
		});
		Cufon.replace('#slides h2');
		$('#nav li:not(#nav li li)').hover(
		function(){
			$(this).toggleClass('hover');
		});
	}
	$('.taxonomylist a:contains("Services")').parent('li').remove();
	var timeout = autoplay ? 4000:0;
	$('#slides:not(.page-template-campaign-php #slides)').cycle({ 
	   	fx:     'fade',
	   	speed:  150,
		startingSlide: 0,
		pager: '#nodes',
		before: onBefore,
		timeout: timeout,
		after: onAfter,
		pagerAnchorBuilder: function(idx, slide) {
      	  return '<a href="#" class="'+$(slide).attr('class')+'">'+$(slide).find('.title').text()+'</a>'; 
    	}
	});
	$('.page-template-campaign-php #slides').cycle({ 
	   	fx:     'scrollHorz',
	   	speed:  300,
		startingSlide: 0,
		pager: '#nodes',
		timeout: 7000,
		before: onBefore,
		after: onAfter,
		pagerAnchorBuilder: function(idx, slide) {
      	  return '<a href="#" class="'+$(slide).attr('class')+'">'+$(slide).find('.title').text()+'</a>'; 
    	}
	});
	$('#gallery .slide').wrapAll('<div class="mask" />');
	$('#gallery .slide').each(function(i,v){
		gwidth += parseInt($(v).width());
		stops.push(-gwidth);
	});
	$('#gallery .mask').width(gwidth);
	$('#gallery .slide').each(function(index,value){
			(currentstop)==index?
			$(value).animate({'opacity':1}, 300):
			$(value).animate({'opacity':.2}, 300);
			$('.nodes').append('<a class="pager" href="javascript:goto('+index+')">'+index+'</a>');
	});
	currentstop++;
	goto(currentstop);
	$('#nodes a').removeClass('slide');
	if($('#slides .slide').length>1){
	$('#nodes').append('<a class="pause" href="javascript:void(0);"><strong>&#9658;</strong></a><br class="clear"/>').find('a.pause').hover(
		function(){
			if($(this).hasClass('paused')){
				$(this).html("<strong>&#9658;</strong>");
			}else{
				$(this).html('<strong>||</strong>');
			}
		},function(){
			if($(this).hasClass('paused')){
				$(this).html('<strong>||</strong>');
			}else{
				$(this).html("<strong>&#9658;</strong>");
			}
		}
		).click(function(){
			$(this).toggleClass('paused');
			$('#slides').cycle('toggle');
			//console.log($(this).attr('class'));
		
	});
	if(!autoplay) $('#nodes a.pause').addClass('paused').html('<strong>||</strong>');
	}else{
		$('#nodes').hide();
	}
	// Start Cycle
	$('.captions ul').cycle({ 
	   	fx:     'fade',
		timeout: 0,
	   	speed:  1,
		startingSlide: 0,
		pagerEvent: 'mouseover',
		pauseOnPagerHover: true
	});
	
	$('.recent_with_excerpt ul').cycle({ 
	   	fx:     'scrollUp',
		timeout: 8000,
	   	speed:  1000,
		startingSlide: 0
	});
	
	// Go To Slide functions
	$('a#one').hover(function(){
		$('.captions ul').cycle(0);
		return false;
	});
	$('a#two').hover(function(){
		$('.captions ul').cycle(1);
		return false;
	});
	$('a#three').hover(function(){
		$('.captions ul').cycle(2);
		return false;
	});
	$('a#four').hover(function(){
		$('.captions ul').cycle(3);
		return false;
	});
	
	// Hover function
		$('.icons ul li a').fadeTo(1, 0.7);
		
		$('.icons ul li a').hover(function() {
			$(this).stop().fadeTo(150, 1);
		},function() {
			$(this).stop().fadeTo(150, 0.7);
		});
		
	// Featured Tab Items		

	var tabItems = [];
	i = 0;
	tabItems[i] = {};
	tabItems[i].description = [];
	$('.page-template-campaign-php #qa-tabs *:not(h3)').each(function(){
		if($(this).next().is('h3') || $(this).is('#qa-tabs p:last-child')){
			//console.log($(this).is('#qa-tabs p:last'));
			tabItems[i].title = $(this).parent().find('h3')[i];
			tabItems[i].description.unshift($(this).parent().find('h3')[i]);
			tabItems[i].description.push($(this)[0]);
			i++;
			if(!$(this).is('p:last-child')){
			tabItems[i] = {};
			tabItems[i].description = [];
			}
		}else{
			tabItems[i].description.push($(this)[0]);
		}
	});
	//console.log(tabItems);
	var list = $('<dl>');
	$(tabItems).each(function(){
		list.append('<dt>'+$(this.title).text()+'</dt>');
		var desc = $('<dd>');
		$(this.description).each(function(){
			desc.append(this);
		});
		list.append(desc);
	});
	var tallest = 0;
	$('.page-template-campaign-php #qa-tabs').html('').prepend(list).find('dd').each(function(){
		if($(this).height()>tallest){tallest = $(this).height()};
		//console.log(tallest);
	});
	list.height(tallest);
	list.find('dd').height(tallest);
	
	$('dt').mouseover(function(){
		$('dd.hover, dt.hover').removeClass();
		$(this).addClass('hover');
		$(this).next('dd').addClass('hover');
	});
	$('dt:first, dd:first').addClass('hover');
	$('#overview h3').each(function(){ 
    	$(this).add( $(this).next() ).wrapAll('<div class="feature"></div>');
	});
	$('<div class="column"></div><div class="column second"></div>').prependTo('#overview');
	$('#overview .feature').each(function(i, val){
		if((i+1) <= Math.ceil($('#overview .feature').length/2)){
			$(this).appendTo('#overview .column:first');
		}else{
			$(this).appendTo('#overview .column:last');
		}
	});
	//$('.page-template-campaign-php .feature').height(tallest);
	
	//split case studies
	var descriptions = [];
	i = 0;
	descriptions[i] = {};
	descriptions[i].description = [];
	$('.page-template-campaign-php #cases p:not(p.description)').each(function(){
		if($(this).next().is('h4') || $(this).is('#cases p:last-child')){
			descriptions[i].description.push($(this)[0]);
			i++;
			if(!$(this).is('p:last-child')){
			descriptions[i] = {};
			descriptions[i].description = [];
			}
		}else{
			descriptions[i].description.push($(this)[0]);
		}
	});
	
	$('#cases h3').after('<table></table>');
	$('#cases h4').each(function(i, val){
		$('#cases table').append('<tr><td class="title"></td></tr>');
		var split = $(val).text().split(':');
		$(this).text(split[0]).prependTo($($('#cases table tr')[i]).find('td'));
		//console.log(split.slice(0,1));
		$('<p class="sub">'+$.trim((split.slice(1)).join(':'))+'</p>').appendTo($($('#cases table tr')[i]).find('td'));
	});
	$(descriptions).each(function(i, val){
		$($('#cases table tr')[i]).append('<td></td>').find('td:last').append(val.description);
	});
});
$(window).load(function(){
	$('.page-template-campaign-php .column:first .feature').each(function(i,el){
		var el = $(el);
		var height = ($('.page-template-campaign-php .column:last .feature').eq(i).height()>el.height())?$('.page-template-campaign-php .column:last .feature').eq(i).height():el.height();
		//console.log($('.page-template-campaign-php .column:last .feature').eq(i).height()+' '+el.height());
		el.height(height);
		//console.log(height);
		$('.page-template-campaign-php .column:last .feature').eq(i).height(height);
	});
	var tallest = 0;
	$('.column').each(function(){
		if($(this).height()>tallest)tallest = $(this).height();
	}).height(tallest);

});
