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('#sidebar').css('position','absolute');
	//setTimeout("jQuery('#sidebar').css('position','absolute');", 3000);
	//fill in the blog background
	
	//jQuery('.posts-container').height(newheight);
	//jQuery('#sidebar').height(newheight+10);
}

jQuery(document).ready( function() {
	if (setupComplete) return;
	setupComplete=true;
	
		jQuery('.cform ol').masonry({ columnWidth: 318, itemSelector: 'li:not(.cf_hidden)' });
	jQuery('.testimonials-surround').masonry({ columnWidth: 340, itemSelector: 'div.testimonial'});
	
	
	equalizeHeights();
	//jQuery('.comments-surround').resize( function() { alert('whoah'); equalizeHeights();});
	
	//var sheight = jQuery('.blog-surround').offset().top + jQuery('.blog-surround').height();
	//var pheight = jQuery('.posts-container').offset().top + jQuery('.posts-container').height();
	//jQuery('.blog-surround').css('padding-bottom', (pheight-sheight-9)+'px');
	//alert(pheight-sheight-9);
	
	

	
	var source = readCookie('mm_source');
	if (source != null)	jQuery('.cform #source').val(source);
	
	jQuery('a[rel*=lightbox]').lightBox();
	
	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;
}


/* Temp HP Carousel */
/* Homepage Carousel */
var c_curFrame = 0;
var c_maxFrames = 0;
var c_panelWidth = 0;
var c_panelHeight = 0;
var c_duration = 750;
var c_isTransitioning = false;

function setupCarousel() {
	if (jQuery('#homeslide_previewbox').length > 0) {
		//preview window
		var child = jQuery('#homeslide_previewbox').children('.panel')[0];
		c_maxFrames = jQuery('#homeslide_previewbox').children('.panel').length;
		
		c_panelWidth = parseInt(jQuery(child).css('marginRight')) +  parseInt(jQuery(child).get(0).offsetWidth || jQuery(child).css('width'));
		var panelOffset = 0;
		
		jQuery("#homeslide_previewbox").children(".panel").each(function() {
			jQuery(this).css({left: panelOffset + 'px', top: 0});
			panelOffset += c_panelWidth;
		});
		jQuery('.home .panel').css('visibility', 'visible');
		
		//excerpt window
		child = jQuery('#homeslide_excerptbox').children('.panel')[0];		
		c_panelHeight = parseInt(jQuery(child).css('marginBottom')) +  parseInt(jQuery(child).get(0).offsetHeight || jQuery(child).css('height'));
		panelOffset = 0;
		
		jQuery("#homeslide_excerptbox").children(".panel").each(function() {
			jQuery(this).css({top: panelOffset + 'px', left: 0});
			panelOffset += c_panelHeight;
		});
		
		for(var i=0;i<c_maxFrames;i++) {
			createPip(i, (i==0));
		}
		
		jQuery('#nextproject').click(function() { carouselNextFrame(); return false; });
				for(var i=0;i>c_maxFrames;i++) {
			createPip(i, (i==0));
		}
		jQuery('#prevproject').click(function() { carouselNextFrame(); return false; });
	}
}

function createPip(num, active) {
	var img = jQuery(document.createElement("a")).addClass('pip').attr('href', '#').text('');
	if (active) jQuery(img).addClass('pipon');
	var container = jQuery('#pipcontainer');
	jQuery(container).append(img);
	
	jQuery(img).click( function() { setCarouselFrame(num); pageTracker._trackEvent('Featured', 'Clicked Next');return false; });
	
	return img;
}

function setCarouselFrame(frame) {
	
	if (frame != c_curFrame && !c_isTransitioning) {
		//setCarouselTransitioning(true);
		
		//preview
		var panelOffset = -1 * (c_panelWidth * frame);	
		jQuery('#homeslide_previewbox .panel').each(function() {
			  jQuery(this).animate({left: panelOffset + 'px', top: 0}, c_duration);
			  panelOffset += c_panelWidth;
  	    });
		
		//excerpt
		panelOffset = -1 * (c_panelHeight * frame);	
		jQuery('#homeslide_excerptbox .panel').each(function() {
			  jQuery(this).animate({top: panelOffset + 'px', left: 0}, c_duration);
			  panelOffset += c_panelHeight;
  	    });
		
		jQuery('#pipcontainer .pipon').removeClass('pipon');
		var pip = jQuery('#pipcontainer .pip')[frame];
		jQuery(pip).addClass('pipon');
		
		c_curFrame = frame;	
	}
}

function carouselNextFrame() {
	var f = c_curFrame + 1;
	if (f>(c_maxFrames-1)) f = 0;
	
	setCarouselFrame(f);
	return false;
}

