var done = false;

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

jQuery(window).bind("unload", function() {
		init();
});

function init() {
		done = true;

		$('.case').mosaic();
		$('.case:nth-child(4n)').addClass('last');
		//$('#wrapper').hide();
		$('#wrapper').fadeIn('medium');
		//$('#case-listing').hide();
		$('#case-listing').delay('500').fadeIn('medium');
		$("#case-content").overscroll({showThumbs: false});

		var _caseInfoHeight = $('#selected-case #case-information .col2-4').height() + 0;
		$('#selected-case #case-information').css('height', _caseInfoHeight);
		var _aboutInfoHeight = $('#about-information .col2-4').height() + 50;
		$('#about-information').css('height', _aboutInfoHeight);
		var _caseImagesWidth = 0;
		$("#case-images img").each(function() {
		    			_caseImagesWidth += $(this).width();
		  				$('#case-images').css({width: _caseImagesWidth});
		});
		
		if(_caseImagesWidth < 1032) {
			$("#scroll-left").hide();
			$("#scroll-right").hide();
		}

		$("#selected-case").hide();
		$("#selected-case").delay(500).fadeIn('medium');
		
		$('#case-content').serialScroll({
						items:'img',
						prev:'#scroll-left a',
						next:'#scroll-right a',
						duration:400,
						force:true,
						stop:true,
						lock:false,
						cycle:false, //don't pull back once you reach the end
						easing:'easeOutQuart', //use this easing equation for a funny effect
						jump: false //click on the images to scroll to them
		});
		
		$('a').click(function(ev){
			
			var $self=$(this);
			
			if($self.hasClass('local'))
			{
				ev.preventDefault();

				//prevent scroll-left/right to fade
				if($(this).parent().attr('id') == "scroll-left" || $(this).parent().attr('id') == "scroll-right" ) {
					return false;
				}

				$('#wrapper').fadeOut('fast', function() {
					document.location = $self.attr('href');
				});
			}else{
			
			}
		});
		
		var comp = new RegExp(location.host);
		$('a').each(function(){
		   if(comp.test($(this).attr('href'))){
		       // a link that contains the current host           
		       $(this).addClass('local');
		   }
		   else{
		       // a link that does not contain the current host
		       $(this).addClass('external');
		   }
		});
		
		$('#selected-case a').attr('target','_blank');
		$('#about-page a').attr('target','_blank');
		
		$('#navigation .col5').click(function(ev){
			if($(this).hasClass('collapsed')){
				$('#contact').animate({
				    height: "130px"
				  }, 500 );
				$(this).removeClass('collapsed');
				$('#contact-info').fadeIn('fast');
			}else{
				$('#contact').animate({
				    height: "5px"
				  }, 500 );
				$(this).addClass('collapsed');
				$('#contact-info').fadeOut('fast');
			}
			
		})
}
