$(document).ready(function(){

	// Superfish Navigation
	jQuery('.bulb nav ul').superfish();

	// Cycle
	jQuery('.slides').cycle({
		pause:	1,
		next:   '#ss_next',
		prev:   '#ss_prev',
		pager:	'#ss_pager',
		pagerAnchorBuilder: function(idx, slide) {
			theid = jQuery(slide).attr('id');
			theitem = jQuery(slide).find('img').eq(0).attr('src');
			if( theitem ){
				theitem = theitem.replace('600x370','40x40');
				return '<li><a href="#'+theid+'"><img src="'+theitem+'" /></a></li>'; 
			}
		} 
	});

	// Tabs
	jQuery('.tab_content').hide(); 
	jQuery('ul.tabs li:first').addClass('active').show();
	jQuery('.tab_content:first').show();

	jQuery('ul.tabs li').click(function() {
		jQuery('ul.tabs li').removeClass('active'); 
		jQuery(this).addClass('active');
		jQuery('.tab_content').hide();

		var activeTab = jQuery(this).find('a').attr('href');
		jQuery(activeTab).fadeIn();
		return false;
	});
	
	// Init image swap
	var imgurl = jQuery('.portfolio_thumbs li:first-child a').attr('href');
	jQuery('.portfolio_thumbs li:first-child a').addClass('active');
	jQuery('#portfolio_image').html('<img src="'+imgurl+'"/>');
	
	// Swap images on hover
	jQuery('.portfolio_thumbs li a').live('click', function(){
		imgsrc = jQuery(this).attr('href');
		if( imgsrc != jQuery('#portfolio_image img').attr('src') ){
			img = new Image();
			jQuery(img).load(function () {
				jQuery(this).fadeOut();
				jQuery('#portfolio_image').removeClass('loading').html(this);
				jQuery(this).fadeIn('slow');
			}).attr('src', imgsrc);
			jQuery('.active').each(function(){
				jQuery(this).removeClass('active');
			});
			jQuery(this).addClass('active');
			//jQuery('#imgholder').html('<img src="'+jQuery(this).attr('href')+'"/>');
		}
		return false;
	});
	
	// Force homepage columns to be even
	if( jQuery('#homeleft').length && jQuery('#homeright').length ){
		var left = jQuery('#homeleft').height();
		var right = jQuery('#homeright').height();
		var diff = Math.abs(left-right);
		if( left < right ){
			jQuery('.boxed').height( jQuery('.boxed').height()+diff );
		}else if( right < left ){
			jQuery('.featured_single_details').height( jQuery('.featured_single_details').height()+diff );
		}
		jQuery('.boxed').equalHeight();
	}
	
	// Fix widths in portfolio displays
	if( jQuery('#pfthumbs').length && jQuery('#pfcontractors').length ){
		var thumbs = jQuery('#pfthumbs').width();
		var contractors = jQuery('#pfcontractors').width();
		jQuery('#pfcontractors').width( 635-thumbs );
	}
	
	// Split some lists into two columns
	jQuery('ul.split').easyListSplitter({
		className: 'halves split_list'
	});
	
	// Zebra stripe tables
	jQuery("table tr:even").addClass("alt");
	
	
});

(function($){
    $.fn.equalHeight = function() {
       // find the tallest height in the collection
        tallest = 0;
        this.each(function(){
            thisHeight = $(this).height();
            if( thisHeight > tallest)
                tallest = thisHeight;
        });

        // set each items height to use the tallest value found
        this.each(function(){
            $(this).height(tallest);
        });
    }
})(jQuery);
