/* Code for the portfolio upper section */

var p_curFrame = -1;
var p_panelWidth = 0;
var p_duration = 1000;
var p_maxFrames = 0;
var p_isTransitioning = false;

function setupPortfolio() {
	if (jQuery('#portfolio').length > 0) {
		var child = jQuery('#portfolio').children('.panel')[0];
		p_maxFrames = jQuery('#portfolio').children('.panel').length;
		
		p_panelWidth = parseInt(jQuery(child).css('marginRight')) +  parseInt(jQuery(child).get(0).offsetWidth || jQuery(child).css('width'));
		var panelOffset = p_panelWidth;
		
		var i = 0;
		jQuery("#portfolio").children(".panel").each(function() {
			jQuery(this).css({left: panelOffset + 'px', top: 0});
			panelOffset += p_panelWidth;
		});
		
		jQuery('.home .panel').css('visibility', 'visible');
		
		jQuery("#portfolio_next").click(function() { portfolioNextFrame(); return false; });
		jQuery("#portfolio_prev").click(function() { portfolioPrevFrame(); return false; });
		
		jQuery('.portfolio_screenshots').each( function() {
			jQuery(this).children('a').lightBox();
		});
		
		//determine starting point
		var hash = window.location.hash;

		//load specific project
		var id=0;
		var i=0;
		var found=false;
		jQuery('#portfolio .panel').each(function() {
			if ("#"+jQuery(this).attr('rel')==hash) {
				found=true;
			} else if (!found) {
				id++;
			}
			
			jQuery(this).attr('onclick', 'setPortfolioFrame('+i+');');
			i++;
		});
		if (found) {
			setPortfolioFrame(id);
			return false;
		}
		
		
		if (p_maxFrames>2) {
			//start on 2nd one so 3 are shown by default
			setPortfolioFrame(1);
		} else {
			setPortfolioFrame(0);
		}
	}
	
	return false;
}

function setPortfolioFrame(frame) {
	
	if (frame != p_curFrame && !p_isTransitioning) {
		setPortfolioTransitioning(true);
		
		jQuery('#portfolio .current').removeClass('current', p_duration);
		
		var newFrame = jQuery('#portfolio').children('.panel')[frame];
		jQuery(newFrame).children().addClass('current', p_duration, function() { setPortfolioTransitioning(false); });
		
		jQuery("#portfolio_descriptions > .portfolio_entry").fadeOut();
		var desc = jQuery("#portfolio_descriptions > .portfolio_entry")[frame];
		jQuery(desc).fadeIn(p_duration);
		
		//shift all the frames
		var panelOffset = p_panelWidth - (p_panelWidth * frame);	
		jQuery('#portfolio .panel').each(function() {
			  jQuery(this).animate({left: panelOffset + 'px', top: 0}, p_duration);
			  panelOffset += p_panelWidth;
  	    });
		
		p_curFrame = frame;
		window.location.hash = jQuery(newFrame).attr('rel');
	}
	
	return false;
}

function setPortfolioTransitioning(isTransitioning) {
	p_isTransitioning = isTransitioning;
	
	//enable or disable the buttons
	if (p_isTransitioning) {
		jQuery('#pcallout .portfolio_nav').addClass('disable');
	} else {
		jQuery('#pcallout .portfolio_nav.disable').removeClass('disable');
	}
}

function portfolioNextFrame() {
	var f = p_curFrame + 1;
	if (f>(p_maxFrames-1)) f = 0;
	
	setPortfolioFrame(f);
}

function portfolioPrevFrame() {
	var f = p_curFrame - 1;
	if (f < 0) f = (p_maxFrames-1);
	
	setPortfolioFrame(f);
}

//portfolio events
jQuery(document).ready( function() {
    setupPortfolio();
});


/* 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; });
	}
}

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;
}


jQuery(document).ready( function() {
	setupCarousel();
});

jQuery(document).ready( function() {
	var sizeSidebar = function() {
		if (jQuery('#sidebar').length > 0) {
			var height = jQuery('#posts').height();
			var height2 = jQuery('#sidebar').height();
			
			if (height > height2) {
				jQuery('#sidebar').css('height', height+"px" );
			} else {
				jQuery('#posts').css('min-height', height2+"px");
			}
		}
	}
	

	sizeSidebar();
	
	jQuery(document).bind('postsContentChanged', function() {
		sizeSidebar();
	});
});

jQuery(document).ready( function() {
	if (jQuery('.serviceslist').length > 0) {
		var tallest = 0;
		jQuery('.serviceslist > ul').each( function() {
			if (jQuery(this).height()>tallest) tallest = jQuery(this).height();
	   });
		jQuery('.serviceslist > ul').css('height', tallest+'px');
	}
});

jQuery(document).ready(function() {
	jQuery('.post_content a').each( function() {
		var href = jQuery(this).attr('href');
		if (linksToImage(href)) {
			jQuery(this).lightBox();
		}
   	});
});

jQuery(document).ready(function() {
	jQuery('#content .container span.extlink').each( function() {
		jQuery(this).parent().attr('target', '_blank');
  });
});
	
String.prototype.endsWith = function(str)
{return (this.match(str+"$")==str)}

function linksToImage(href) {
	href = href.toLowerCase();
	if (href.endsWith('png') ||
		href.endsWith('jpg') ||
		href.endsWith('jpeg') ||
		href.endsWith('gif'))
			return true;
			
	return false;
}


jQuery(document).ready( function() {
	jQuery('.delayed_load').each( function() { 
		jQuery(this).attr('src', jQuery(this).attr('alt'));
		jQuery(this).attr('alt', '');
	});
});
