(function(jQuery) {
jQuery.fn.fadeInSequence = function(fadeInTime, timeBetween)
{
    //Default Values
    timeBetween = typeof(timeBetween) == 'undefined' ? 0 : timeBetween;
     fadeInTime = typeof(fadeInTime) == 'undefined' ? 500 : fadeInTime;
 
    //The amount of remaining time until the animation is complete.
    //Initially set to the value of the entire animation duration.
    var remainingTime = jQuery(this).size() * (fadeInTime+timeBetween);
 
    var i=0; //Counter
    return jQuery(this).each(function()
    {
        //Wait until previous element has finished fading and timeBetween has elapsed
        jQuery(this).delay(i++*(fadeInTime+timeBetween));
 
        //Decrement remainingTime
        remainingTime -= (fadeInTime+timeBetween);
 
        if(jQuery(this).css('display') == 'none')
        {
            jQuery(this).fadeIn(fadeInTime);
        }
        else //If hidden by other means such as opacity: 0
        {
            jQuery(this).animate({'opacity' : 1}, fadeInTime);
        }
 
        //Delay until the animation is over to fill up the queue.
        jQuery(this).delay(remainingTime+timeBetween);
 
    }); 
 
};
 
})(jQuery);

function fixImageBaselines(selector) {
    $(document).ready(function() {
        var sbl = $.browser.msie ? document.body.currentStyle.lineHeight : $(document.body).css('line-height');
        var bl = parseInt(sbl);
        if (sbl.indexOf('pt') != -1) bl = parseFloat(sbl) * (20 / 14.94); // IE!
        $(selector).each(function() {
            var $item = $(this);
            var $d = $('<div />');
            $d.css({
                'padding-bottom': (bl * (Math.ceil($item.height() / bl)) - $item.height()) + 'px',
                'background': 'transparent url(' + $item.attr('src') + ') no-repeat 0 0',
                'width': $item.width(),
                'height': $item.height()
            });
            $d.id = $item.id;
            $d.className = $item.className;
            $item.replaceWith($d);
        });
    });
}

function thumbnail_inverse(filepath) {
	var new_filepath = filepath.replace('clients/default','imagecache/inverse/clients/default');
	return unescape(new_filepath);
}

function thumbnail_default(filepath) {
	var old_filepath = filepath.replace('imagecache/inverse/clients/default','clients/default');
	return unescape(old_filepath);
}

$(document).ready(function(){
	
	$('.backtotop').click(function () {
		$('body,html').animate({
			scrollTop: 0
		}, 800);
		return false;
	});	
		
	if(window.location.hash) {
		var urlproject = window.location.hash.substr(1).replace("&","");
		$('.projectthumb').hide();
		$('.projectdetail').hide();
		$('.videoplayer').hide();		
		$('.projectimage').hide();
		$('.footerspacer').hide();
		
		// this bit is for the related projects...
		var c = ".related"+$('.projectdetail').attr('class').split(" ")[2].slice(7);
		$(c).show();
		$('.videoplayer').show();
		
		$('.'+urlproject).show();
		$('.'+urlproject+' > .projectimage').fadeInSequence();
		
		// $('.backlink').css('color', 'green'); // from work

	} else {

		$('.projectthumb').show();
		// $('.backlink').css('color', 'yellow'); // client's project thumbs
		// $('a.backlink').attr('href','/clients');
		$('a.backlink').attr('href','javascript:history.go(-1)');
		
	}
	
	$('.showproject').click(function() {
		
		//$('.backlink').css('color', 'red'); // project thumbs via client
		// if hash in url -1, else 0
		
		if(window.location.hash) {
			$('a.backlink').attr('href','javascript:history.go(-1)');
		} else {
			$('a.backlink').attr('href','javascript:location.reload(true)');
		}
		
		$('.footerspacer').hide();
		$('.projectthumb').hide();
		$('.spacer').hide();
		
		var project = $(this).attr('id');
		
		if(project.indexOf("show") != -1) {
			
			$('.projectdetail').hide();
			$('.projectimage').hide();			

			
			// $('.'+project.substr(4)).show(); // iterate over p tag in this and show sequentially
			// jquery 1.3.2
			// $('.'+project.substr(4)+' p').fadeInSequence();

			$('.'+project.substr(4)).show();
			$('.'+project.substr(4)+' > .projectimage').fadeInSequence();
			
		} else {

			$('.'+project).show();
			$('.'+project+' > .projectimage').fadeInSequence();
		}
		
	});
	
		
	$('.projectlink').click(function() {
		
		// $('.backlink').css('color', 'black');
		
		$('.projectthumb').hide();
		$('.spacer').hide();
		
		var project = $(this).parent().prev('img').attr('id');
		
		if(project.indexOf("show") != -1) {
			
			$('.projectdetail').hide();
			$('.projectimage').hide();			

			
			// $('.'+project.substr(4)).show(); // iterate over p tag in this and show sequentially
			// jquery 1.3.2
			// $('.'+project.substr(4)+' p').fadeInSequence();

			$('.'+project.substr(4)).show();
			$('.'+project.substr(4)+' > .projectimage').fadeInSequence();
			
		} else {

			$('.'+project).show();
			$('.'+project+' > .projectimage').fadeInSequence();
		}
		
	});	
		
	$('.close').click(function() {
		$('.projectdetail').hide();
		$('.projectthumb').show();
		$('.spacer').show();
	});
	
  	$("img.imagefield-field_thumbnail").hover(
		function () {
			var thumbnail = $(this).attr('src');
			$(this).attr('src',thumbnail_inverse(thumbnail));
		},
		function () {
			var thumbnail = $(this).attr('src');		
			$(this).attr('src',thumbnail_default(thumbnail));
		}
	)
	
  	$("img.imagefield-field_thumbnail").click(
		function() {
			var thumbnail = $(this).attr('src');		
			$(this).attr('src',thumbnail_default(thumbnail));			
		}
	);	
	
});

