var setupComplete=false;

function equalizeHeights()
{
	var pheight = jQuery('.posts-container').height();
	var sheight = jQuery('#sidebar').height();
	var newheight = Math.max(pheight, sheight);
	//alert(sheight + '  ' + pheight);
	
	if (sheight > pheight) {
		jQuery('.posts-container').css('min-height', sheight-10);
	}
} 

jQuery(document).ready( function() {
	if (setupComplete) return;
	setupComplete=true;
	
	jQuery('.posts-container  .cform ol').masonry({ columnWidth: 220, itemSelector: 'li:not(.cf_hidden)' });
	jQuery('.testimonials-surround').masonry({ columnWidth: 340, itemSelector: 'div.testimonial'});
	
	equalizeHeights();
	
	jQuery('.cf-box-b').parent().addClass('cb-field');
	
	jQuery('.menu .search input').focus( function() { jQuery(this).animate({'width': '160px'})});
	jQuery('.menu .search input').blur( function() { jQuery(this).animate({'width': '61px'})});
	
	
	var source = readCookie('mm_source');
	if (source != null)	jQuery('.cform #source').val(source);
	
	//jQuery('a[rel*=lightbox]').lightBox();
	
	if (jQuery('#homeslide_previewbox').length > 0) setupCarousel();
});



function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


/* Homepage Carousel */
var c_duration = 750;
var c_pipoffset = 0;

function setupCarousel() {
	if (jQuery('#homeslide_previewbox').length > 0) {
		jQuery('#homeslide_previewbox .panel').each( function() {
			var pip = jQuery(document.createElement("li"));
			var thelink = jQuery(document.createElement('a'));
			jQuery(thelink).attr('href', '#'+jQuery(this).attr('id')).text(jQuery(this).attr('rel'));
			jQuery(pip).append(thelink);
			jQuery('#pipcontainer ul').append(pip);
		});
		
		jQuery('.pipwrap .links ul').width(jQuery('.pipwrap .links li').length * jQuery('.pipwrap .links li').width());
		
		jQuery('#pipcontainer ul li:nth-child(1) a').addClass('current');
		updateIndicator();
	}
	
	jQuery('#pipcontainer .links a').click( function() {
		jQuery('#homeslide_previewbox .panel.current').fadeOut(750).removeClass('current');
		jQuery(jQuery(this).attr('href')).fadeIn(750).addClass('current');
		jQuery('#pipcontainer a.current').removeClass('current');
		jQuery(this).addClass('current');
		updateIndicator();
		checkEdges();
		clearInterval(iTimer);
		return false;
	});
	
	jQuery('.pipright a').click( function() {	
		if (jQuery('.links a.current').parent().next().length==0) {
			window.location = jQuery(this).attr('href');
		} else {
			jQuery('.links a.current').parent().next().find('a').click();
			checkEdges();
			clearInterval(iTimer);
			return false;
		}
	});
	
	
	jQuery('.pipleft a').click( function() {
		jQuery('.links a.current').parent().prev().find('a').click();
		checkEdges();
		clearInterval(iTimer);
		return false;
	});
	
	iTimer = setInterval('nextSlide();', 4000);
	jQuery('.frontpage-slideshow-container').hover(function(){isHovering=true;}, function(){isHovering=false;});
}
var iTimer = false;
var isHovering = false;
function nextSlide()
{
	if (!isHovering)
	{
		var nextLink = jQuery('.links a.current').parent().next();
		if (jQuery(nextLink).length==0) nextLink = jQuery('.links ul li:nth-child(1)');
		

		jQuery('#homeslide_previewbox .panel.current').fadeOut(750).removeClass('current');
		jQuery(jQuery(nextLink).find('a').attr('href')).fadeIn(750).addClass('current');
		jQuery('#pipcontainer a.current').removeClass('current');
		jQuery(nextLink).find('a').addClass('current');
		
		jQuery('#header').css('display', 'none').css('display', 'block');
		
		updateIndicator();
		checkEdges();

	}
}


function updateIndicator()
{
	var center = 1425;
	var currentLeft = parseInt(jQuery('.pipwrap ul').css('background-position'));
	
	var duration = c_duration;
	if (currentLeft==0) duration = 0;
	
	var idealLeft = -center;
	idealLeft += jQuery('#pipcontainer a.current').parent().position().left+(-100/30);

	var currentIndex = jQuery('.links a.current').parent().index();
	if (currentIndex > (c_pipoffset+4)) {
		params = {'background-position': idealLeft+'px 0',
					'left':-((currentIndex-4)*140)+'px'};
		c_pipoffset++;
	} else if (currentIndex < (c_pipoffset)) {
		params = {'background-position': idealLeft+'px 0',
					'left':-((currentIndex)*140)+'px'};
		c_pipoffset--;
	} else {
		params = {'background-position': idealLeft+'px 0'};
	}
	
	jQuery('.pipwrap ul').animate(params, duration);
}

function checkEdges()
{
	var curSlide = jQuery('#pipcontainer a.current').parent().index();
	var slideCount = jQuery('#homeslide_previewbox .panel').length;
	if (curSlide == 0) {
		jQuery('.pipleft img').css('visibility', 'visible');
	} else {
		jQuery('.pipleft img').css('visibility', 'visible');
	}	
	
	//no longer hide the arrow, make it link to clients page
	if (curSlide == slideCount-1) {
		//jQuery('.pipright img').css('visibility', 'hidden');
	} else {
		//jQuery('.pipright img').css('visibility', 'visible');
	}
}

