jQuery.noConflict();

jQuery(document).ready(function(){
	
	initLayout();

	if (jQuery("body.homepage").length ) initRotator();
	
	if (jQuery("body.websites-list").length ) initWebsitesList();
	
	if (jQuery("#catblogoutput div.the-post").length ) initPostLayout();
});

jQuery(window).load(
    function() {
        initLayout();
		jQuery(window).resize(function() {
			initLayout();
		});
    }
);

function initPostLayout()
{
	jQuery("#pagecontent div.blog-container div.post-body img").appendTo('#aside');
	jQuery("#pagecontent div.blog-container div.post-body div.feedback").appendTo('#aside');
	jQuery("#pagecontent div.blog-container div.post-body a").wrap('<span class="view-site"></span>');
}

function initWebsitesList()
{
	jQuery('#pagecontent div.blog-post').each(function(index) {
		var postHref = jQuery(this).find('h2 a').attr('href');
		var postTitle = jQuery(this).find('h2').html();
		var postImageSrc = jQuery(this).find('img:eq(0)').attr('src');
		jQuery(this).find('h1,h2,img,br').remove();
		var postTxt = jQuery(this).find('div.post-body').html().substr(0,50) + "...";
		
		jQuery(this).html(
			'<h2>' + postTitle + '</h2>' +
			'<a href="' + postHref + '"><img src="' + postImageSrc + '" /></a>' +
			'<h3>project summary:</h3>' + 
			postTxt +
			' <a href="' + postHref + '" class="more">read more</a>' 
		).css('visibility', 'visible'); 
	});
}

function initLayout()
{
	contentHeight = jQuery(window).height() - jQuery('#header').outerHeight() - jQuery('#footer').outerHeight();
	jQuery('#content-area').height('auto'); 
	if (jQuery('#content-area').height() < contentHeight) jQuery('#content-area').height(contentHeight);
}

var slideImages;

function initRotator()
{
	jQuery('#content-area').append('<div id="rotator"></div>');
	
	jQuery("#rotator").append('<ul></ul><div id="rotator-footer"><h3>recent work</h3><div id="rotator-nav"></div><div id="rotator-title"></div></div>');

	slideImages = jQuery("#pagecontent table.photogalleryTable img");
	slideImages.each(function(index) {
		var src = jQuery(this).attr('src');
		src = src.slice(0,src.indexOf("?"));
		src = src + '?Action=thumbnail&Width=611&Height=402&Algorithm=proportional&USM=1';

		var linkData = jQuery(this).parent().attr('title').split(" {url=}");
		if(linkData[1])
			jQuery("#rotator ul").append('<li class="fcSlide-' + index + '"><a href="'+linkData[1]+'"><img src="' + src + '" /></a></li>');
		else 
			jQuery("#rotator ul").append('<li class="fcSlide-' + index + '"><img src="' + src + '" /></li>');

		jQuery(document.createElement('a'))
		.attr("href", "#").html('<span>'+ (index + 1) +'</span>') 
		.appendTo('#rotator-nav')
		.click(function() {
				if (jQuery(this).hasClass("selected")) return false;
				clearTimeout(runSlider);
				slideSwitch(index);
				return false;
			});
	});

	jQuery('#rotator li:first').addClass('active');
	jQuery("#rotator-nav a").eq(0).addClass("selected");

	var linkData = slideImages.eq(0).parent().attr('title').split(" {url=}");
	if (linkData[1])
		jQuery('#rotator-title').html('<a href="' + linkData[1] + '">' + linkData[0] + '</a>');
	else
		jQuery('#rotator-title').html(linkData[0]);
	//jQuery('#rotator-title').html(slideImages.eq(0).parent().attr('title'));
	runSlider = setTimeout('slideSwitch()', 4000);

}
var currentSlideIndex = 0;
function slideSwitch(i) {

	clearTimeout(runSlider);
	
	var $active = jQuery('#rotator li.active');

    if ( $active.length == 0 ) $active = jQuery('#rotator li:last');

    if (i != undefined)
	{
		var $next =  jQuery('#rotator li.fcSlide-'+i);
		var nextSlideIndex = i;
	}
	else {
		if ($active.next().length) {
			var $next =  $active.next();
			var nextSlideIndex = currentSlideIndex + 1;
		} else {
			var $next = jQuery('#rotator li:first');
			var nextSlideIndex = 0;
		}
	}
    $active.addClass('last-active');

	jQuery("#rotator-nav a").removeClass("selected");
	jQuery("#rotator-nav a").eq( $next.index() ).addClass("selected");
	
    $next.css({left: "-611px"})
        .addClass('active')
        .animate({left: "0px"}, 400, function() {
			var linkData = slideImages.eq(nextSlideIndex).parent().attr('title').split(" {url=}");
			if (linkData[1])
				jQuery('#rotator-title').html('<a href="' + linkData[1] + '">' + linkData[0] + '</a>');
			else
				jQuery('#rotator-title').html(linkData[0]);
			currentSlideIndex = nextSlideIndex;
            $active.removeClass('active last-active');
			runSlider = setTimeout('slideSwitch()', 4000); 
       });
}

