var shoot = false;

function getviewport(type)
{
	var viewportHeight;
	var viewportWidth;
	if (typeof window.innerHeight != 'undefined')
	{
	      viewportHeight = window.innerHeight
	      viewportWidth = window.innerWidth
	}
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined'
	   && typeof document.documentElement.clientHeight !=
	  'undefined' && document.documentElement.clientHeight != 0)
	{
		viewportHeight = document.documentElement.clientHeight
		viewportWidth = document.documentElement.clientWidth
	}
	switch(type)
	{
		case 'height':
			return(viewportHeight);
		break;
		case 'width':
			return(viewportWidth);
		break;
	}
	
}


$(window).load(function(){
	
	if($("body").hasClass('home'))
	{
		if($.browser.safari){ bodyelem = $("body") } else{ bodyelem = $("html") } 
		bodyelem.animate({scrollTop: (($(document).height()-getviewport('height'))/2)}, 500);		
	}
	else{
		/*var imageHeight = getviewport('height')-$(".scroll .fixedwrapper").height(); // SET SCROLLER HEIGHT
		var imagewrapperWidth = 0;
		
		imageHeight = imageHeight-20; // PORTFOLIO HEIGHT
		$(".shoot .image img.pic").height(imageHeight);
		$(".shoot .image").css({'height':imageHeight});	
		
		$(".donotstealcopycat").css({'height':imageHeight, 'top': (0-imageHeight)});
		$(".donotstealcopycat img").height(imageHeight);
		*/
		var startitemPos = parseInt($(".scroll .fixedwrapper").css('bottom').replace('px',''));
		var wrapperPadding = parseInt($(".scroll .wrapper").css('padding-bottom').replace('px',''));
		var closed = true;
		
		checkscrollPresence(); // CHECK IF THERE IS A SCROLLBAR ON LOAD
		$(window).resize(function(){checkscrollPresence();}); // ON RESIZE	
	}
		
	
	function checkscrollPresence()
	{
		var viewportHeight = getviewport('height');
		var documentHeight = $(document).height();
		
		if($.browser.msie){ documentHeight = documentHeight-50; }
		
		if (documentHeight <= viewportHeight && shoot !=true)
		{
			$(".scroll .fixedwrapper").css({'bottom': 0});
		}
		else{
       		$(".scroll .fixedwrapper").css({'bottom': parseInt($(".scroll .fixedwrapper").css('bottom').replace('px',''))});
			$(window).scroll(function()
			{
				
				scrollFunc($(window));
			});
		}
	}	
		
	function scrollFunc(e)
	{
   		if ( typeof scrollFunc.y == 'undefined' )
    	{
        	scrollFunc.y=e.scrollTop();
    	}
    	var diffY=scrollFunc.y-e.scrollTop();

    	if( diffY!=0 ) 
    	{
 			updateBottom();
    	} 
    	scrollFunc.y=e.scrollTop();
	}


	function updateBottom()
	{
		var viewportHeight = getviewport('height');
		var documentHeight = $(document).height();	
		var itemHeight = $(".scroll .fixedwrapper").height();
		var allowtoscrollPos = viewportHeight-(itemHeight);//-startitemPos SMOOTHING IT
		var currentScrollPos = (documentHeight - $(window).scrollTop())-wrapperPadding;
		
		//$('pre').append($('html').position().top+" == "+$(window).scrollTop()+"<br/>");	
		$('pre').append(allowtoscrollPos+" >= "+currentScrollPos + "<br/>");
		if(allowtoscrollPos >= currentScrollPos)
		{
			//var newBottom = (allowtoscrollPos-currentScrollPos)+startitemPos;			
			if(closed)
			{	
				//alert('scroll to top');
				$('.scroll .fixedwrapper').animate({'bottom':0},500);
				closed = false;
				return false;
			}
		}
		else if($('.scroll .fixedwrapper').css('bottom') == '0px')
		{
			if(!closed)
			{
				$('.scroll .fixedwrapper').animate({'bottom':startitemPos},500);
				closed = true;
				return false;
			}
		}
	}	
});



$('document').ready(function(){
	
	$('.popup').fancybox();
	
	// SET BOXSHADOW
	var newHeight= $(".news.detail .pictures .thumb .boxshadow").parent().height();
	if(newHeight <=170)
	{
		newHeight = newHeight-10;
	}
	$(".news.detail .pictures .thumb .boxshadow").css({'height':newHeight});
	$(".news.detail .pictures .thumb a").css({'margin-top':-newHeight});
	
	
	// PORTFOLIO
	$(".portfolio .item .thumb").css('cursor','pointer');
		
	$(".portfolio .item .thumb").mouseover(function(){
		if($.browser.msie){
			$(this).find(".hover").show();
		}else{
			$(this).find(".hover").fadeIn('slow');
		}
		//effect("pulsate", { times:1 }, 200);
	});
	
	$(".portfolio .item .thumb .hover").mouseout(function(){
		$(".portfolio .item .thumb .hover").each(function(){
			if($.browser.msie){
				$(this).hide();
			}else{
				$(this).fadeOut('fast');
			}
		});
			
	});
	
	$(".portfolio .item .thumb .hover").click(function()
	{
		window.location = $(this).parent().parent().find("h3 a").attr("href");
	});
	
	// AWARDS
	$(".awards .item .thumb").css('cursor','pointer');
		
	$(".awards .item .thumb").mouseover(function(){
		if($.browser.msie){
			$(this).find(".hover").show();
		}else{
			$(this).find(".hover").fadeIn('slow');
		}
		//effect("pulsate", { times:1 }, 200);
	});
	
	$(".awards .item .thumb .hover").mouseout(function(){
		$(".awards .item .thumb .hover").each(function(){
			if($.browser.msie){
				$(this).hide();
			}else{
				$(this).fadeOut('fast');
			}
		});
			
	});
	
	$(".awards .item .thumb .hover").click(function()
	{
		window.location = $(this).parent().parent().find("h3 a").attr("href");
	});			
});
